Skip to content

Instantly share code, notes, and snippets.

@buhichan
Last active October 21, 2020 12:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save buhichan/218fc558b916fcc9c1cbe0e2e3a0b6ae to your computer and use it in GitHub Desktop.
Save buhichan/218fc558b916fcc9c1cbe0e2e3a0b6ae to your computer and use it in GitHub Desktop.
babylonjs-builder-pattern.ts
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