Last active
October 21, 2020 12:07
-
-
Save buhichan/218fc558b916fcc9c1cbe0e2e3a0b6ae to your computer and use it in GitHub Desktop.
babylonjs-builder-pattern.ts
This file contains hidden or 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
import { Animation, CubicEase, EasingFunction, Mesh, MeshBuilder, MultiMaterial, Scene, Vector3, ActionManager, InterpolateValueAction } from "@babylonjs/core" | |
import { RACK_HEIGHT, RACK_WIDTH, UNIT_HEIGHT } from "config/canvas-constants" | |
import { genMultiMaterialMesh } from "utils/canvas-utils" | |
export class DoorBuilder { | |
constructor(private scene: Scene, material: MultiMaterial) { | |
const doorCube = MeshBuilder.CreateBox("door", { width: RACK_WIDTH, height: RACK_HEIGHT, depth: UNIT_HEIGHT }) | |
genMultiMaterialMesh(doorCube, material) | |
doorCube.setPivotPoint(new Vector3(RACK_WIDTH / 2, 0, 0)) | |
doorCube.metadata = { | |
type: "door", | |
} | |
bindAnimation: { | |
const animationDoor = new Animation("animationDoor", "rotation.y", 30, Animation.ANIMATIONTYPE_FLOAT, Animation.ANIMATIONLOOPMODE_CYCLE) | |
const keys = [] | |
keys.push({ | |
frame: 0, | |
value: 0, | |
}) | |
keys.push({ | |
frame: 50, | |
value: -0.6 * Math.PI, | |
}) | |
keys.push({ | |
frame: 100, | |
value: 0, | |
}) | |
animationDoor.setKeys(keys) | |
const easingFunction = new CubicEase() | |
easingFunction.setEasingMode(EasingFunction.EASINGMODE_EASEINOUT) | |
animationDoor.setEasingFunction(easingFunction) | |
doorCube.animations && doorCube.animations.push(animationDoor) | |
} | |
this.proto = doorCube | |
doorCube.position.set(-20000, 0, 0) | |
} | |
private proto: Mesh | |
private id = 1 | |
create() { | |
const inst = this.proto.createInstance("door" + this.id++) | |
{ | |
const actionManager = new ActionManager(this.scene) | |
actionManager.registerAction( | |
new InterpolateValueAction( | |
ActionManager.OnRightPickTrigger, | |
0, | |
"visibility", | |
1, | |
1000 | |
) | |
) | |
inst.actionManager = actionManager | |
} | |
return inst | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment