From ffdf3ffc642abe4830e993c7300753a862cc9b17 Mon Sep 17 00:00:00 2001 From: Dhruv Arora Date: Thu, 22 Aug 2024 13:33:58 -0700 Subject: [PATCH] Cleaned code Need to merge to fix caching issue --- fission/src/mirabuf/MirabufSceneObject.ts | 40 +++++++------------ fission/src/systems/scene/GizmoSceneObject.ts | 15 +++++++ 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/fission/src/mirabuf/MirabufSceneObject.ts b/fission/src/mirabuf/MirabufSceneObject.ts index 16f23cf54..b79995a34 100644 --- a/fission/src/mirabuf/MirabufSceneObject.ts +++ b/fission/src/mirabuf/MirabufSceneObject.ts @@ -181,24 +181,8 @@ class MirabufSceneObject extends SceneObject { this.DisablePhysics() if (this._gizmo.isDragging) { this._mirabufInstance.parser.rigidNodes.forEach(rn => { - World.PhysicsSystem.SetBodyPosition( - this._mechanism.GetBodyByNodeId(rn.id)!, - ThreeMatrix4_JoltMat44(this._gizmo!.obj.matrix).GetTranslation() - ) - World.PhysicsSystem.SetBodyRotation( - this._mechanism.GetBodyByNodeId(rn.id)!, - ThreeQuaternion_JoltQuat(this._gizmo!.obj.quaternion) - ) - - rn.parts.forEach(part => { - const partTransform = this._mirabufInstance.parser.globalTransforms - .get(part)! - .clone() - .premultiply(this._gizmo!.obj.matrix) - - const meshes = this._mirabufInstance.meshes.get(part) ?? [] - meshes.forEach(([batch, id]) => batch.setMatrixAt(id, partTransform)) - }) + this._gizmo?.UpdateBodyPositionAndRotation(rn) + this.UpdateNodeParts(rn, this._gizmo!.obj.matrix) }) } @@ -212,14 +196,7 @@ class MirabufSceneObject extends SceneObject { if (!this._mirabufInstance.meshes.size) return // if this.dispose() has been ran then return const body = World.PhysicsSystem.GetBody(this._mechanism.GetBodyByNodeId(rn.id)!) const transform = JoltMat44_ThreeMatrix4(body.GetWorldTransform()) - rn.parts.forEach(part => { - const partTransform = this._mirabufInstance.parser.globalTransforms - .get(part)! - .clone() - .premultiply(transform) - const meshes = this._mirabufInstance.meshes.get(part) ?? [] - meshes.forEach(([batch, id]) => batch.setMatrixAt(id, partTransform)) - }) + this.UpdateNodeParts(rn, transform) if (isNaN(body.GetPosition().GetX())) { const vel = body.GetLinearVelocity() @@ -328,6 +305,17 @@ class MirabufSceneObject extends SceneObject { return mesh } + private UpdateNodeParts(rn: RigidNodeReadOnly, transform: THREE.Matrix4) { + rn.parts.forEach(part => { + const partTransform = this._mirabufInstance.parser.globalTransforms + .get(part)! + .clone() + .premultiply(transform) + const meshes = this._mirabufInstance.meshes.get(part) ?? [] + meshes.forEach(([batch, id]) => batch.setMatrixAt(id, partTransform)) + }) + } + /** Updates the batch computations */ private UpdateBatches() { this._mirabufInstance.batches.forEach(x => { diff --git a/fission/src/systems/scene/GizmoSceneObject.ts b/fission/src/systems/scene/GizmoSceneObject.ts index 16e14ea74..afc64aa9e 100644 --- a/fission/src/systems/scene/GizmoSceneObject.ts +++ b/fission/src/systems/scene/GizmoSceneObject.ts @@ -4,6 +4,8 @@ import InputSystem from "../input/InputSystem" import World from "../World" import MirabufSceneObject from "@/mirabuf/MirabufSceneObject" import { Object3D, PerspectiveCamera } from "three" +import { ThreeMatrix4_JoltMat44, ThreeQuaternion_JoltQuat } from "@/util/TypeConversions" +import { RigidNodeReadOnly } from "@/mirabuf/MirabufParser" export type GizmoMode = "translate" | "rotate" | "scale" @@ -133,6 +135,19 @@ class GizmoSceneObject extends SceneObject { public SetMode(mode: GizmoMode) { this._gizmo.setMode(mode) } + + /** updates body position and rotation for each body from the parent object */ + public UpdateBodyPositionAndRotation(rn: RigidNodeReadOnly) { + if (!this._parentObject) return + World.PhysicsSystem.SetBodyPosition( + this._parentObject.mechanism.GetBodyByNodeId(rn.id)!, + ThreeMatrix4_JoltMat44(this._obj.matrix).GetTranslation() + ) + World.PhysicsSystem.SetBodyRotation( + this._parentObject.mechanism.GetBodyByNodeId(rn.id)!, + ThreeQuaternion_JoltQuat(this._obj.quaternion) + ) + } } export default GizmoSceneObject