the code I write is very bad, if you take a look you will probably ban me from this forum
class BabylonInstanceEngine {
reactCanvas = null;
engine;
scene;
utilLayer;
canvas;
initialize(_canva) {
if (!_canva) return;
this.canvas = _canva;
this.engine = new Engine(this.canvas);
this.scene = new Scene(this.engine);
this.run();
if (this.scene.isReady()) {
// console.log(this.scene);
this.camera = new ArcRotateCamera(
"camera1",
(3 * Math.PI) / 2,
-Math.PI / 2,
20,
Vector3.Zero(),
this.scene
);
this.camera.setTarget(Vector3.Zero());
this.camera.attachControl(this.canvas, true);
const light = new HemisphericLight(
"light",
new Vector3(0, 1, 0),
this.scene
);
light.intensity = 0.7;
console.log("Scene is working");
this.run();
} else {
console.log("Scene is not working");
}
}
run() {
if (this.engine && this.scene) {
this.engine.runRenderLoop(() => {
this.scene.render();
});
}
}
debug() {
new DebugLayer(this.scene).show({
embedMode: true,
gizmoCamera: this.camera,
});
}
resize() {
if (this.engine) {
this.engine.resize();
}
}
gizmo() {
var utilLayer = new UtilityLayerRenderer(this.scene);
var translationGizmo = new BABYLON.PositionGizmo(utilLayer);
translationGizmo.updateGizmoRotationToMatchAttachedMesh = false;
var rotationGizmo = new BABYLON.RotationGizmo(utilLayer);
// rotationGizmo.updateGizmoRotationToMatchAttachedMesh = false;
var objClick = [];
// event on position gizmo x drag
translationGizmo.xGizmo.dragBehavior.onDragObservable.add((event) => {
objClick
.filter((mesh) => mesh != translationGizmo.attachedMesh)
.forEach((mesh) => {
mesh.position.x += event.delta.x;
});
});
// event on position gizmo y drag
translationGizmo.yGizmo.dragBehavior.onDragObservable.add((event) => {
objClick
.filter((mesh) => mesh != translationGizmo.attachedMesh)
.forEach((mesh) => {
mesh.position.y += event.delta.y;
});
});
// event on position gizmo z drag
translationGizmo.zGizmo.dragBehavior.onDragObservable.add((event) => {
objClick
.filter((mesh) => mesh != translationGizmo.attachedMesh)
.forEach((mesh) => {
mesh.position.z += event.delta.z;
});
});
// event on rotation gizmo x drag
rotationGizmo.xGizmo.dragBehavior.onDragObservable.add(() => {
objClick
.filter((mesh) => mesh != rotationGizmo.attachedMesh)
.forEach((mesh) => {
// mesh.rotation.x += rotationGizmo.xGizmo.angle;
mesh.rotate(
BABYLON.Axis.X,
rotationGizmo.xGizmo.angle,
BABYLON.Space.LOCAL
);
});
rotationGizmo.xGizmo.angle = 0;
});
// event on rotation gizmo y drag
rotationGizmo.yGizmo.dragBehavior.onDragObservable.add(() => {
objClick
.filter((mesh) => mesh != rotationGizmo.attachedMesh)
.forEach((mesh) => {
// mesh.rotation.y += rotationGizmo.yGizmo.angle;
mesh.rotate(
BABYLON.Axis.Y,
rotationGizmo.yGizmo.angle,
BABYLON.Space.LOCAL
);
});
rotationGizmo.yGizmo.angle = 0;
});
// event on rotation gizmo z drag
rotationGizmo.zGizmo.dragBehavior.onDragObservable.add(() => {
objClick
.filter((mesh) => mesh != rotationGizmo.attachedMesh)
.forEach((mesh) => {
// mesh.rotation.z += rotationGizmo.zGizmo.angle;
mesh.rotate(
BABYLON.Axis.Z,
rotationGizmo.zGizmo.angle,
BABYLON.Space.LOCAL
);
});
rotationGizmo.zGizmo.angle = 0;
});
// click event
this.scene.onPointerObservable.add((pointerInfo) => {
switch (pointerInfo.type) {
case BABYLON.PointerEventTypes.POINTERTAP: {
var pickResult = this.scene.pick(
this.scene.pointerX,
this.scene.pointerY
);
if (pickResult.hit) {
pickResult.pickedMesh.showBoundingBox = true;
translationGizmo.attachedMesh = pickResult.pickedMesh;
rotationGizmo.attachedMesh = pickResult.pickedMesh;
objClick.push(pickResult.pickedMesh);
} else {
objClick.forEach((mesh) => (mesh.showBoundingBox = false));
translationGizmo.attachedMesh = undefined;
rotationGizmo.attachedMesh = undefined;
objClick = [];
}
break;
}
}
});
}
box(y_pos) {
console.log("Starting box function with y position: " + y_pos);
try {
this.boxs = MeshBuilder.CreateBox(
"box",
{ size: 2, updatable: true },
this.scene
);
console.log("Box created, setting position...");
this.boxs.position.y = y_pos;
this.scene.render();
console.log("Position set to y_pos: " + this.boxs.position.y);
console.log("Box : " + this.boxs);
} catch (error) {
console.error("An error occurred: ", error);
}
}
ground() {
const gd = MeshBuilder.CreateGround(
"ground",
{ width: 6, height: 6 },
this.scene
);
console.log("ground : " + gd);
}
sphere() {
const keyPress = {
w: false,
a: false,
s: false,
d: false,
};
const initialSpeed = 0;
const desiredSpeed = 0.01;
const acceleration = 0.0001;
const deceleration = 0.001;
let currentSpeed = initialSpeed;
// Set up easing function
const lerp = (start, end, t) => {
return start * (1 - t) + end * t;
};
var sphere = MeshBuilder.CreateSphere(
"sphere",
{ diameter: 2, segments: 32 },
this.scene
);
// Move the sphere upward 1/2 its height
sphere.position.y = 1;
this.scene.onKeyboardObservable.add((kbInfo) => {
switch (kbInfo.type) {
case KeyboardEventTypes.KEYDOWN:
switch (kbInfo.event.key) {
case "a":
case "A":
keyPress["a"] = true;
break;
case "d":
case "D":
keyPress["d"] = true;
break;
case "w":
case "W":
keyPress["w"] = true;
break;
case "s":
case "S":
keyPress["s"] = true;
break;
}
break;
case KeyboardEventTypes.KEYUP:
switch (kbInfo.event.key) {
case "a":
case "A":
keyPress["a"] = false;
break;
case "d":
case "D":
keyPress["d"] = false;
break;
case "w":
case "W":
keyPress["w"] = false;
break;
case "s":
case "S":
keyPress["s"] = false;
break;
}
break;
}
});
this.scene.registerBeforeRender(() => {
if (keyPress["w"] || keyPress["a"] || keyPress["s"] || keyPress["d"]) {
if (currentSpeed < desiredSpeed) {
currentSpeed = Math.min(currentSpeed + acceleration, desiredSpeed);
}
} else {
if (currentSpeed > 0) {
currentSpeed = Math.max(currentSpeed - deceleration, 0);
}
}
const sign = Math.sign(this.camera.alpha);
let movement = new Vector3(0, 0, 0);
if (keyPress["w"]) {
const angle =
-this.camera.alpha + (sign === -1 ? Math.PI / 2 : -Math.PI / 2);
movement = new Vector3(Math.sin(angle), 0, Math.cos(angle));
}
if (keyPress["a"]) {
const angle = this.camera.alpha + (sign === 1 ? -Math.PI : Math.PI);
movement = new Vector3(-Math.sin(angle), 0, -Math.cos(angle));
}
if (keyPress["d"]) {
const angle = this.camera.alpha + (sign === 1 ? -Math.PI : Math.PI);
movement = new Vector3(Math.sin(angle), 0, Math.cos(angle));
}
if (keyPress["s"]) {
const angle =
-this.camera.alpha + (sign === -1 ? Math.PI / 2 : -Math.PI / 2);
movement = new Vector3(-Math.sin(angle), 0, -Math.cos(angle));
}
movement.normalize();
const scaledSpeed =
currentSpeed * this.scene.getEngine().getDeltaTime();
movement.scaleInPlace(scaledSpeed);
sphere.position.addInPlace(movement);
});
alert("Use W,A,S,D to move the ball");
this.run();
}
}
I am using this code in my react frontend
- Initialize : This function is responsible for the creating new engine and the scene and for camera (ArcRotateCamera) and light (Hemispherical)
- run : This render the scene
- resize : To resize the engine
- gizmo : This function is for attaching the gizmo on the current selected mesh. It also helping in attaching gizmo on muti-selected meshes to move and rotate them at once.
- box : This function is simply creating the box
- ground : This function is simply creating the ground
- sphere : this function is used to create sphere but also add the movement ability to it like a character movement with the W,A,S,D button.