1
0
mirror of https://github.com/misskey-dev/misskey.git synced 2026-05-24 13:54:12 +02:00

Update engine.ts

This commit is contained in:
syuilo
2026-04-22 11:54:24 +09:00
parent b734ab3419
commit af8a0bdf12

View File

@@ -600,7 +600,8 @@ export class RoomEngine extends EventEmitter<RoomEngineEvents> {
newRotation.y = Math.round(newRotation.y / (Math.PI / 8)) * (Math.PI / 8); newRotation.y = Math.round(newRotation.y / (Math.PI / 8)) * (Math.PI / 8);
} }
let sticky: string | null = null; let stickyOtherObject: string | null = null;
let sticky = false;
const isCollisionTarget = (m: BABYLON.AbstractMesh) => { const isCollisionTarget = (m: BABYLON.AbstractMesh) => {
return m.metadata?.objectId !== grabbing.objectId && return m.metadata?.objectId !== grabbing.objectId &&
@@ -608,11 +609,12 @@ export class RoomEngine extends EventEmitter<RoomEngineEvents> {
!grabbing.descendantStickyObjectIds.includes(m.metadata?.objectId); !grabbing.descendantStickyObjectIds.includes(m.metadata?.objectId);
}; };
if (placement === 'side') { if (placement === 'side' || placement === 'wall') {
// 前方に向かってレイを飛ばす // 前方に向かってレイを飛ばす
const ray = new BABYLON.Ray(this.camera.position, dir, cm(1000)); const ray = new BABYLON.Ray(this.camera.position, dir, cm(1000));
const hit = this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_WALL__') || m.name.includes('__SIDE__'))); const hit = placement === 'side' ? this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_WALL__') || m.name.includes('__SIDE__'))) : this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_WALL__')));
if (hit != null && hit.pickedPoint != null && hit.pickedMesh != null) { if (hit != null && hit.pickedPoint != null && hit.pickedMesh != null) {
sticky = true;
const pickedMeshNormal = hit.getNormal(true, true); const pickedMeshNormal = hit.getNormal(true, true);
const targetRotationY = Math.atan2(pickedMeshNormal.x, pickedMeshNormal.z); const targetRotationY = Math.atan2(pickedMeshNormal.x, pickedMeshNormal.z);
newRotation.y = targetRotationY; newRotation.y = targetRotationY;
@@ -621,7 +623,7 @@ export class RoomEngine extends EventEmitter<RoomEngineEvents> {
} else { } else {
newPos.z = hit.pickedPoint.z; newPos.z = hit.pickedPoint.z;
} }
sticky = hit.pickedMesh.metadata?.objectId ?? null; stickyOtherObject = hit.pickedMesh.metadata?.objectId ?? null;
if (this.gridSnapping.enabled) { if (this.gridSnapping.enabled) {
this.gridPlane.rotationQuaternion = null; this.gridPlane.rotationQuaternion = null;
@@ -631,79 +633,47 @@ export class RoomEngine extends EventEmitter<RoomEngineEvents> {
this.gridPlane.isVisible = true; this.gridPlane.isVisible = true;
} }
} }
} else if (placement === 'wall') { } else if (placement === 'bottom' || placement === 'ceiling') {
// 前方に向かってレイを飛ばす
const ray = new BABYLON.Ray(this.camera.position, dir, cm(1000));
const hit = this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_WALL__')));
if (hit != null && hit.pickedPoint != null && hit.pickedMesh != null) {
const pickedMeshNormal = hit.getNormal(true, true);
const targetRotationY = Math.atan2(pickedMeshNormal.x, pickedMeshNormal.z);
newRotation.y = targetRotationY;
if (getYRotationDirection(targetRotationY) === '+x' || getYRotationDirection(targetRotationY) === '-x') {
newPos.x = hit.pickedPoint.x;
} else {
newPos.z = hit.pickedPoint.z;
}
if (this.gridSnapping.enabled) {
this.gridPlane.rotationQuaternion = null;
this.gridPlane.rotation.x = Math.PI;
this.gridPlane.rotation.y = targetRotationY;
this.gridPlane.position = new BABYLON.Vector3(newPos.x, newPos.y, newPos.z).addInPlace(pickedMeshNormal.scale(cm(0.1)));
this.gridPlane.isVisible = true;
}
}
} else if (placement === 'bottom') {
// 上に向かってレイを飛ばす // 上に向かってレイを飛ばす
const ray = new BABYLON.Ray(newPos, new BABYLON.Vector3(0, 1, 0), cm(1000)); const ray = new BABYLON.Ray(newPos, new BABYLON.Vector3(0, 1, 0), cm(1000));
const hit = this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_CEILING__') || m.name.includes('__ROOM_BOTTOM__') || m.name.includes('__BOTTOM__'))); const hit = placement === 'bottom' ? this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_CEILING__') || m.name.includes('__ROOM_BOTTOM__') || m.name.includes('__BOTTOM__'))) : this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_CEILING__')));
if (hit != null && hit.pickedPoint != null && hit.pickedMesh != null) { if (hit != null && hit.pickedPoint != null && hit.pickedMesh != null) {
sticky = true;
newPos.y = hit.pickedPoint.y; newPos.y = hit.pickedPoint.y;
sticky = hit.pickedMesh.metadata?.objectId ?? null; stickyOtherObject = hit.pickedMesh.metadata?.objectId ?? null;
}
if (newPos.y > cm(250)) newPos.y = cm(250);
} else if (placement === 'ceiling') {
newPos.y = cm(250);
if (this.gridSnapping.enabled) { if (this.gridSnapping.enabled) {
this.gridPlane.rotationQuaternion = null; this.gridPlane.rotationQuaternion = null;
this.gridPlane.rotation.x = Math.PI * 1.5; this.gridPlane.rotation.x = Math.PI * 1.5;
this.gridPlane.rotation.y = 0; this.gridPlane.rotation.y = 0;
this.gridPlane.position = new BABYLON.Vector3(grabbing.mesh.position.x, cm(250) - cm(0.1), grabbing.mesh.position.z); this.gridPlane.position = new BABYLON.Vector3(grabbing.mesh.position.x, grabbing.mesh.position.y - cm(0.1), grabbing.mesh.position.z);
this.gridPlane.isVisible = true; this.gridPlane.isVisible = true;
}
} }
} else if (placement === 'floor') { } else { // top or floor
newPos.y = 0;
if (this.gridSnapping.enabled) {
this.gridPlane.rotationQuaternion = null;
this.gridPlane.rotation.x = 0;
this.gridPlane.rotation.y = 0;
this.gridPlane.position = new BABYLON.Vector3(grabbing.mesh.position.x, cm(0.1), grabbing.mesh.position.z);
this.gridPlane.isVisible = true;
}
} else {
// 下に向かってレイを飛ばす // 下に向かってレイを飛ばす
const ray = new BABYLON.Ray(newPos, new BABYLON.Vector3(0, -1, 0), cm(1000)); const ray = new BABYLON.Ray(newPos, new BABYLON.Vector3(0, -1, 0), cm(1000));
const hit = this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_FLOOR__') || m.name.includes('__ROOM_TOP__') || m.name.includes('__TOP__'))); const hit = placement === 'top' ? this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_FLOOR__') || m.name.includes('__ROOM_TOP__') || m.name.includes('__TOP__'))) : this.scene.pickWithRay(ray, (m) => isCollisionTarget(m) && (m.name.includes('__ROOM_FLOOR__')));
if (hit != null && hit.pickedPoint != null && hit.pickedMesh != null) { if (hit != null && hit.pickedPoint != null && hit.pickedMesh != null) {
sticky = true;
newPos.y = hit.pickedPoint.y; newPos.y = hit.pickedPoint.y;
sticky = hit.pickedMesh.metadata?.objectId ?? null; stickyOtherObject = hit.pickedMesh.metadata?.objectId ?? null;
}
if (newPos.y < 0) newPos.y = 0;
if (this.gridSnapping.enabled) { if (this.gridSnapping.enabled) {
this.gridPlane.rotationQuaternion = null; this.gridPlane.rotationQuaternion = null;
this.gridPlane.rotation.x = Math.PI / 2; this.gridPlane.rotation.x = Math.PI / 2;
this.gridPlane.rotation.y = 0; this.gridPlane.rotation.y = 0;
this.gridPlane.position = new BABYLON.Vector3(grabbing.mesh.position.x, grabbing.mesh.position.y + cm(0.1), grabbing.mesh.position.z); this.gridPlane.position = new BABYLON.Vector3(grabbing.mesh.position.x, grabbing.mesh.position.y + cm(0.1), grabbing.mesh.position.z);
this.gridPlane.isVisible = true; this.gridPlane.isVisible = true;
}
} }
} }
grabbing.mesh.rotation = newRotation; grabbing.mesh.rotation = newRotation;
grabbing.mesh.position = newPos; grabbing.mesh.position = newPos;
console.log(sticky);
//const pos = new BABYLON.Vector3(this.camera.position.x, this.camera.position.y, this.camera.position.z); //const pos = new BABYLON.Vector3(this.camera.position.x, this.camera.position.y, this.camera.position.z);
//const _dir = newPos.subtract(pos).normalize(); //const _dir = newPos.subtract(pos).normalize();
//for (let i = 0; i < grabbing.distance; i++) { //for (let i = 0; i < grabbing.distance; i++) {
@@ -749,7 +719,7 @@ export class RoomEngine extends EventEmitter<RoomEngineEvents> {
grabbing.onMove?.({ grabbing.onMove?.({
position: newPos, position: newPos,
rotation: newRotation, rotation: newRotation,
sticky, sticky: stickyOtherObject,
}); });
} }