-
-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
167 changed files
with
4,538 additions
and
3,410 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { Camera, Entity, Logger, Script, Vector2, Vector3 } from "oasis-engine"; | ||
|
||
/** | ||
* The camera's 2D controller, can zoom and pan. | ||
*/ | ||
export class OrthoControl extends Script { | ||
cameraEntity: Entity; | ||
camera: Camera; | ||
|
||
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; | ||
private _panStartPos: Vector3 = new Vector3(); | ||
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.cameraEntity = entity; | ||
this.camera = entity.getComponent(Camera); | ||
} | ||
|
||
onUpdate(dt: number): void { | ||
if (this._zoomScale !== 1) { | ||
const { camera } = this; | ||
const sizeDiff = this._zoomScaleUnit * (this._zoomScale - 1); | ||
const size = camera.orthographicSize + sizeDiff; | ||
camera.orthographicSize = Math.max(this._zoomMinSize, Math.min(this._zoomMaxSize, size)); | ||
this._zoomScale = 1; | ||
} | ||
|
||
if (this._isPanStart) { | ||
const { _panStart: panStart, _panEnd: panEnd } = this; | ||
const panDelta = this._panDelta; | ||
Vector2.subtract(panEnd, panStart, panDelta); | ||
if (panDelta.x === 0 && panDelta.y === 0) { | ||
return ; | ||
} | ||
this._handlePan(); | ||
panEnd.cloneTo(panStart); | ||
} | ||
} | ||
|
||
/** | ||
* 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) return; | ||
|
||
this.cameraEntity.transform.position.cloneTo(this._panStartPos); | ||
this._panStart.setValue(x, y); | ||
this._panEnd.setValue(x, y); | ||
this._isPanStart = true; | ||
} | ||
|
||
/** | ||
* Panning. | ||
* @param x - The x-axis coordinate (unit: pixel) | ||
* @param y - The y-axis coordinate (unit: pixel) | ||
* | ||
* @remarks Make sure to call panStart before calling panMove. | ||
*/ | ||
panMove(x: number, y: number): void { | ||
if (!this.enabled) return; | ||
if (!this._isPanStart) { | ||
Logger.warn("Make sure to call panStart before calling panMove"); | ||
} | ||
this._panEnd.setValue(x, y); | ||
} | ||
|
||
/** | ||
* End pan. | ||
*/ | ||
panEnd(): void { | ||
if (!this.enabled) return; | ||
this._isPanStart = false; | ||
} | ||
|
||
private _getZoomScale(): number { | ||
return Math.pow(0.95, this.zoomSpeed); | ||
} | ||
|
||
private _handlePan(): void { | ||
const { width, height } = this.engine.canvas; | ||
const { x, y } = this._panDelta; | ||
const { camera } = this; | ||
const doubleOrthographicSize = camera.orthographicSize * 4; | ||
const width3D = doubleOrthographicSize * camera.aspectRatio; | ||
const height3D = doubleOrthographicSize; | ||
const pos = this._panStartPos; | ||
pos.x -= (x * width3D) / width; | ||
pos.y += (y * height3D) / height; | ||
this.cameraEntity.transform.position = pos; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { Camera, Entity, WebGLEngine } from "oasis-engine"; | ||
import { OrthoControl } from "../src/OrthoControl"; | ||
|
||
const canvasDOM = document.createElement("canvas"); | ||
canvasDOM.width = 1024; | ||
canvasDOM.height = 1024; | ||
|
||
describe(" Ortho Control", () => { | ||
let entity: Entity; | ||
let camera: Camera; | ||
let cameraControl: OrthoControl; | ||
|
||
beforeAll(() => { | ||
const engine = new WebGLEngine(canvasDOM); | ||
entity = engine.sceneManager.activeScene.createRootEntity(); | ||
camera = entity.addComponent(Camera); | ||
cameraControl = entity.addComponent(OrthoControl); | ||
}); | ||
|
||
it("test zoom", () => { | ||
cameraControl.zoomIn(); | ||
cameraControl.onUpdate(0); | ||
expect(camera.orthographicSize).toEqual(8.749999999999998); | ||
cameraControl.zoomOut(); | ||
cameraControl.onUpdate(0); | ||
expect(camera.orthographicSize).toEqual(10.065789473684207); | ||
}); | ||
|
||
it("test pan", () => { | ||
cameraControl.panStart(0, 0); | ||
cameraControl.panMove(2, 0); | ||
cameraControl.onUpdate(0); | ||
cameraControl.panEnd(); | ||
|
||
const pos = entity.transform.position; | ||
expect(pos.x).toEqual(-0.07863898026315787); | ||
expect(pos.y).toEqual(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/** | ||
* Sprite mask interaction. | ||
*/ | ||
export enum SpriteMaskInteraction { | ||
/** The sprite will not interact with the masking system. */ | ||
None, | ||
/** The sprite will be visible only in areas where a mask is present. */ | ||
VisibleInsideMask, | ||
/** The sprite will be visible only in areas where no mask is present. */ | ||
VisibleOutsideMask | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
/** | ||
* Sprite mask layer. | ||
*/ | ||
export enum SpriteMaskLayer { | ||
/** Mask layer 0. */ | ||
Layer0 = 0x1, | ||
/** Mask layer 1. */ | ||
Layer1 = 0x2, | ||
/** Mask layer 2. */ | ||
Layer2 = 0x4, | ||
/** Mask layer 3. */ | ||
Layer3 = 0x8, | ||
/** Mask layer 4. */ | ||
Layer4 = 0x10, | ||
/** Mask layer 5. */ | ||
Layer5 = 0x20, | ||
/** Mask layer 6. */ | ||
Layer6 = 0x40, | ||
/** Mask layer 7. */ | ||
Layer7 = 0x80, | ||
/** Mask layer 8. */ | ||
Layer8 = 0x100, | ||
/** Mask layer 9. */ | ||
Layer9 = 0x200, | ||
/** Mask layer 10. */ | ||
Layer10 = 0x400, | ||
/** Mask layer 11. */ | ||
Layer11 = 0x800, | ||
/** Mask layer 12. */ | ||
Layer12 = 0x1000, | ||
/** Mask layer 13. */ | ||
Layer13 = 0x2000, | ||
/** Mask layer 14. */ | ||
Layer14 = 0x4000, | ||
/** Mask layer 15. */ | ||
Layer15 = 0x8000, | ||
/** Mask layer 16. */ | ||
Layer16 = 0x10000, | ||
/** Mask layer 17. */ | ||
Layer17 = 0x20000, | ||
/** Mask layer 18. */ | ||
Layer18 = 0x40000, | ||
/** Mask layer 19. */ | ||
Layer19 = 0x80000, | ||
/** Mask layer 20. */ | ||
Layer20 = 0x100000, | ||
/** Mask layer 21. */ | ||
Layer21 = 0x200000, | ||
/** Mask layer 22. */ | ||
Layer22 = 0x400000, | ||
/** Mask layer 23. */ | ||
Layer23 = 0x800000, | ||
/** Mask layer 24. */ | ||
Layer24 = 0x1000000, | ||
/** Mask layer 25. */ | ||
Layer25 = 0x2000000, | ||
/** Mask layer 26. */ | ||
Layer26 = 0x4000000, | ||
/** Mask layer 27. */ | ||
Layer27 = 0x8000000, | ||
/** Mask layer 28. */ | ||
Layer28 = 0x10000000, | ||
/** Mask layer 29. */ | ||
Layer29 = 0x20000000, | ||
/** Mask layer 30. */ | ||
Layer30 = 0x40000000, | ||
/** Mask layer 31. */ | ||
Layer31 = 0x80000000, | ||
/** All mask layers. */ | ||
Everything = 0xffffffff | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
export { SpriteMaskInteraction } from "./enums/SpriteMaskInteraction"; | ||
export { SpriteMaskLayer } from "./enums/SpriteMaskLayer"; | ||
export * from "./sprite/index"; |
Oops, something went wrong.