Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ortho control for camera #278

Merged
merged 19 commits into from
Jun 8, 2021
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
9e9d6d9
refactor(2d): opt pixels per unit value
singlecoder Apr 12, 2021
53fe76a
refactor(2d): change rect to region in Sprite
singlecoder Apr 12, 2021
fea984a
refactor(2d): modify recomment
singlecoder Apr 12, 2021
ba38c08
fix: fix conflicts
singlecoder Apr 22, 2021
6795be8
perf(renderqueue): opt the problem of unstable sorting in the renderi…
singlecoder Apr 22, 2021
ce6999f
Merge branch 'dev/0.4' of https://github.com/oasis-engine/engine into…
singlecoder Apr 27, 2021
0f6145a
Merge branch 'dev/0.4' of https://github.com/oasis-engine/engine into…
singlecoder May 7, 2021
dd4d0ed
Merge branch 'dev/0.4' of https://github.com/oasis-engine/engine into…
singlecoder May 10, 2021
2eda587
Merge branch 'dev/0.4' of https://github.com/oasis-engine/engine into…
singlecoder May 10, 2021
153259e
feat(control): init
singlecoder May 10, 2021
d70d076
feat(control): ortho control version 0.1
singlecoder May 13, 2021
3a0f84f
Merge branch 'dev/0.4' of https://github.com/oasis-engine/engine into…
singlecoder May 14, 2021
2c8e72b
Merge branch 'dev/0.4' of https://github.com/oasis-engine/engine into…
singlecoder Jun 4, 2021
9545492
feat(control): add transform update
singlecoder Jun 4, 2021
bb8e393
Merge branch 'dev/0.4' of https://github.com/oasis-engine/engine into…
singlecoder Jun 8, 2021
557ee76
feat(control): opt code
singlecoder Jun 8, 2021
73f558e
feat(constrol): opt code
singlecoder Jun 8, 2021
47c8267
feat(control): add ortho control test
singlecoder Jun 8, 2021
6f11a70
feat(control): opt code
singlecoder Jun 8, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
136 changes: 136 additions & 0 deletions packages/controls/src/OrthoControl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
import { Camera, Entity, Script, Vector2, Vector3 } from "oasis-engine";

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need comment for class

export class OrthoControl extends Script {
camera: Entity;
cameraComp: Camera;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless for the class


private _position: Vector3 = new Vector3();
private _zoomSpeed: number = 1.0;
private _zoomScale: number = 1.0;
private _zoomScaleUnit: number = 25.0;
private _zoomMinSize: number = 0.0;
private _zoomMaxSize: number = Infinity;
private _isPanStart: boolean = false;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_isPanMove

private _panStart: Vector2 = new Vector2();
private _panEnd: Vector2 = new Vector2();
private _panDelta: Vector2 = new Vector2();

/**
* The zoom speed.
*/
get zoomSpeed(): number {
return this._zoomSpeed;
}

set zoomSpeed(value: number) {
this._zoomSpeed = value;
}

constructor(entity: Entity) {
super(entity);

this.camera = entity;
this.cameraComp = entity.getComponent(Camera);

const position = this.camera.transform.position;
position.cloneTo(this._position);

// // @ts-ignore
// this.mainElement = this.engine.canvas._webCanvas;
// this.mainElement.addEventListener("wheel", (e) => {
// if (e.deltaY < 0) {
// this.zoomIn();
// } else {
// this.zoomOut();
// }
// }, false);
// this.mainElement.addEventListener("mousedown", (e) => {
// this.panStart(e.clientX, e.clientY);
// });
// this.mainElement.addEventListener("mousemove", (e) => {
// this.panMove(e.clientX, e.clientY);
// });
// this.mainElement.addEventListener("mouseup", (e) => {
// this.panEnd();
// });
}

onUpdate(dt: number): void {
if (!this.enabled) return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for enabled is false


if (this._zoomScale !== 1) {
const { cameraComp } = this;
const sizeDiff = this._zoomScaleUnit * (this._zoomScale - 1);
const size = cameraComp.orthographicSize + sizeDiff;
cameraComp.orthographicSize = Math.max(this._zoomMinSize, Math.min(this._zoomMaxSize, size));
}

this._zoomScale = 1;
}

/**
* Zoom in.
*/
zoomIn(): void {
this._zoomScale *= this._getZoomScale();
}

/**
* Zoom out.
*/
zoomOut(): void {
this._zoomScale /= this._getZoomScale();
}

/**
* Start pan.
* @param x - The x-axis coordinate (unit: pixel)
* @param y - The y-axis coordinate (unit: pixel)
*/
panStart(x: number, y: number): void {
if (this.enabled === false) return;

this._isPanStart = true;
this._panStart.setValue(x, y);
}

/**
* Panning.
* @param x - The x-axis coordinate (unit: pixel)
* @param y - The y-axis coordinate (unit: pixel)
*/
panMove(x: number, y: number): void {
if (this.enabled === false || this._isPanStart === false) return;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isPanMove = true

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!this.enabled


const { _panStart: panStart, _panEnd: panEnd } = this;
panEnd.setValue(x, y);
Vector2.subtract(panEnd, panStart, this._panDelta);
this._handlePan();
panEnd.cloneTo(panStart);
}

/**
* End pan.
*/
panEnd(): void {
if (this.enabled === false) return;
this._isPanStart = false;
}

private _getZoomScale(): number {
return Math.pow(0.95, this.zoomSpeed);
}

private _handlePan(): void {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should update camera transform in onUpdate function to avoid unnecessary calculation. use a flag to mark whether to update transform.

const { width, height } = this.engine.canvas;
const { x, y } = this._panDelta;
const { cameraComp } = this;
const doubleOrthographicSize = cameraComp.orthographicSize * 2;
const width3D = doubleOrthographicSize * cameraComp.aspectRatio;
const height3D = doubleOrthographicSize;
const pos = this._position;
pos.x -= (x * width3D) / width;
pos.y += (y * height3D) / height;
this.camera.transform.position = pos;
}
}
1 change: 1 addition & 0 deletions packages/controls/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { FreeControl } from "./FreeControl";
export { OrbitControl } from "./OrbitControl";
export { OrthoControl } from "./OrthoControl";