From 4da92509cb13288fadacc2d9a02a5561a65a18c7 Mon Sep 17 00:00:00 2001 From: syuilo <4439005+syuilo@users.noreply.github.com> Date: Mon, 16 Feb 2026 10:27:13 +0900 Subject: [PATCH] wip --- packages/frontend/src/utility/room/engine.ts | 126 +----------------- packages/frontend/src/utility/room/utility.ts | 117 ++++++++++++++++ 2 files changed, 120 insertions(+), 123 deletions(-) diff --git a/packages/frontend/src/utility/room/engine.ts b/packages/frontend/src/utility/room/engine.ts index 04bb445112..f13d1a93ca 100644 --- a/packages/frontend/src/utility/room/engine.ts +++ b/packages/frontend/src/utility/room/engine.ts @@ -23,6 +23,7 @@ import { GridMaterial } from '@babylonjs/materials'; import { ShowInspector } from '@babylonjs/inspector'; import { ref, watch } from 'vue'; import { getObjectDef, OBJECT_DEFS } from './object-defs.js'; +import { HorizontalCameraKeyboardMoveInput } from './utility.js'; import * as sound from '@/utility/sound.js'; type RoomSettingObject = { @@ -74,123 +75,6 @@ export function defineObject>(def: ObjectDef return def; } -const _assumedFramesPerSecond = 60; - -class HorizontalCameraKeyboardMoveInput extends BABYLON.BaseCameraPointersInput { - public camera: BABYLON.FreeCamera; - private engine: BABYLON.AbstractEngine; - private scene: BABYLON.Scene; - moveSpeed = 6 / _assumedFramesPerSecond; - preShift = false; - codes = []; - codesUp = ['KeyW']; - codesDown = ['KeyS']; - codesLeft = ['KeyA']; - codesRight = ['KeyD']; - onCanvasBlurObserver = null; - onKeyboardObserver = null; - public canMove = true; - - constructor(camera: BABYLON.UniversalCamera) { - super(); - this.camera = camera; - this.scene = this.camera.getScene(); - this.engine = this.scene.getEngine(); - } - - attachControl() { - if (this.onCanvasBlurObserver) { - return; - } - - this.onCanvasBlurObserver = this.engine.onCanvasBlurObservable.add(() => { - this.codes = []; - }); - - this.onKeyboardObserver = this.scene.onKeyboardObservable.add(({ event, type }) => { - const { code, shiftKey } = event; - this.preShift = shiftKey; - - if (type === BABYLON.KeyboardEventTypes.KEYDOWN) { - if (this.codesUp.indexOf(code) >= 0 || - this.codesDown.indexOf(code) >= 0 || - this.codesLeft.indexOf(code) >= 0 || - this.codesRight.indexOf(code) >= 0) { - const index = this.codes.findIndex(v => v.code === code); - if (index < 0) { // 存在しなかったら追加する - this.codes.push({ code }); - } - event.preventDefault(); - (event as KeyboardEvent).stopPropagation(); - } - } else { - if (this.codesUp.indexOf(code) >= 0 || - this.codesDown.indexOf(code) >= 0 || - this.codesLeft.indexOf(code) >= 0 || - this.codesRight.indexOf(code) >= 0) { - const index = this.codes.findIndex(v => v.code === code); - if (index >= 0) { // 存在したら削除する - this.codes.splice(index, 1); - } - event.preventDefault(); - (event as KeyboardEvent).stopPropagation(); - } - } - }); - } - - detachControl() { - this.codes = []; - - if (this.onKeyboardObserver) this.scene.onKeyboardObservable.remove(this.onKeyboardObserver); - if (this.onCanvasBlurObserver) this.engine.onCanvasBlurObservable.remove(this.onCanvasBlurObserver); - this.onKeyboardObserver = null; - this.onCanvasBlurObserver = null; - } - - checkInputs() { - if (!this.onKeyboardObserver) { - return; - } - for (let index = 0; index < this.codes.length; index++) { - const { code } = this.codes[index]; - - const local = new BABYLON.Vector3(); - if (this.codesLeft.indexOf(code) >= 0) { - local.x += -1; - } else if (this.codesUp.indexOf(code) >= 0) { - local.z += this.scene.useRightHandedSystem ? -1 : 1; - } else if (this.codesRight.indexOf(code) >= 0) { - local.x += 1; - } else if (this.codesDown.indexOf(code) >= 0) { - local.z += this.scene.useRightHandedSystem ? 1 : -1; - } - - if (local.length() === 0) { - continue; - } - - const dir = this.camera.getDirection(local.normalize()); - dir.y = 0; - dir.normalize(); - const rate = this.preShift ? 3 : 1; - const move = dir.scale(this.moveSpeed * rate); - - if (this.canMove) { - this.camera.cameraDirection.addInPlace(move); - } - } - } - - getClassName() { - return 'HorizontalCameraKeyboardMoveInput'; - } - - getSimpleName() { - return 'horizontalkeyboard'; - } -} - export class RoomEngine { private canvas: HTMLCanvasElement; private engine: BABYLON.Engine; @@ -952,9 +836,7 @@ export class RoomEngine { const stickyObjectIds = Array.from(this.def.objects.filter(o => o.sticky === mesh.metadata.objectId)).map(o => o.id); for (const soid of stickyObjectIds) { const soMesh = this.objectMeshs.get(soid)!; - soMesh.parent = null; - soMesh.position = soMesh.position.add(mesh.position); - soMesh.rotation = soMesh.rotation.add(mesh.rotation); + soMesh.setParent(null); removeStickyParentRecursively(soMesh); } }; @@ -1006,9 +888,7 @@ export class RoomEngine { for (const soid of stickyObjectIds) { const soMesh = this.objectMeshs.get(soid)!; setStickyParentRecursively(soMesh); - soMesh.parent = mesh; - soMesh.position = soMesh.position.subtract(mesh.position); - soMesh.rotation = soMesh.rotation.subtract(mesh.rotation); + soMesh.setParent(mesh); } }; setStickyParentRecursively(selectedObject); diff --git a/packages/frontend/src/utility/room/utility.ts b/packages/frontend/src/utility/room/utility.ts index b629bd6d7d..d60f118a60 100644 --- a/packages/frontend/src/utility/room/utility.ts +++ b/packages/frontend/src/utility/room/utility.ts @@ -31,3 +31,120 @@ export function yuge(room: RoomEngine, mesh: BABYLON.Mesh, offset: BABYLON.Vecto ps.preWarmCycles = Math.random() * 1000; ps.start(); } + +const _assumedFramesPerSecond = 60; + +export class HorizontalCameraKeyboardMoveInput extends BABYLON.BaseCameraPointersInput { + public camera: BABYLON.FreeCamera; + private engine: BABYLON.AbstractEngine; + private scene: BABYLON.Scene; + moveSpeed = 6 / _assumedFramesPerSecond; + preShift = false; + codes = []; + codesUp = ['KeyW']; + codesDown = ['KeyS']; + codesLeft = ['KeyA']; + codesRight = ['KeyD']; + onCanvasBlurObserver = null; + onKeyboardObserver = null; + public canMove = true; + + constructor(camera: BABYLON.UniversalCamera) { + super(); + this.camera = camera; + this.scene = this.camera.getScene(); + this.engine = this.scene.getEngine(); + } + + attachControl() { + if (this.onCanvasBlurObserver) { + return; + } + + this.onCanvasBlurObserver = this.engine.onCanvasBlurObservable.add(() => { + this.codes = []; + }); + + this.onKeyboardObserver = this.scene.onKeyboardObservable.add(({ event, type }) => { + const { code, shiftKey } = event; + this.preShift = shiftKey; + + if (type === BABYLON.KeyboardEventTypes.KEYDOWN) { + if (this.codesUp.indexOf(code) >= 0 || + this.codesDown.indexOf(code) >= 0 || + this.codesLeft.indexOf(code) >= 0 || + this.codesRight.indexOf(code) >= 0) { + const index = this.codes.findIndex(v => v.code === code); + if (index < 0) { // 存在しなかったら追加する + this.codes.push({ code }); + } + event.preventDefault(); + (event as KeyboardEvent).stopPropagation(); + } + } else { + if (this.codesUp.indexOf(code) >= 0 || + this.codesDown.indexOf(code) >= 0 || + this.codesLeft.indexOf(code) >= 0 || + this.codesRight.indexOf(code) >= 0) { + const index = this.codes.findIndex(v => v.code === code); + if (index >= 0) { // 存在したら削除する + this.codes.splice(index, 1); + } + event.preventDefault(); + (event as KeyboardEvent).stopPropagation(); + } + } + }); + } + + detachControl() { + this.codes = []; + + if (this.onKeyboardObserver) this.scene.onKeyboardObservable.remove(this.onKeyboardObserver); + if (this.onCanvasBlurObserver) this.engine.onCanvasBlurObservable.remove(this.onCanvasBlurObserver); + this.onKeyboardObserver = null; + this.onCanvasBlurObserver = null; + } + + checkInputs() { + if (!this.onKeyboardObserver) { + return; + } + for (let index = 0; index < this.codes.length; index++) { + const { code } = this.codes[index]; + + const local = new BABYLON.Vector3(); + if (this.codesLeft.indexOf(code) >= 0) { + local.x += -1; + } else if (this.codesUp.indexOf(code) >= 0) { + local.z += this.scene.useRightHandedSystem ? -1 : 1; + } else if (this.codesRight.indexOf(code) >= 0) { + local.x += 1; + } else if (this.codesDown.indexOf(code) >= 0) { + local.z += this.scene.useRightHandedSystem ? 1 : -1; + } + + if (local.length() === 0) { + continue; + } + + const dir = this.camera.getDirection(local.normalize()); + dir.y = 0; + dir.normalize(); + const rate = this.preShift ? 3 : 1; + const move = dir.scale(this.moveSpeed * rate); + + if (this.canMove) { + this.camera.cameraDirection.addInPlace(move); + } + } + } + + getClassName() { + return 'HorizontalCameraKeyboardMoveInput'; + } + + getSimpleName() { + return 'horizontalkeyboard'; + } +}