Skip to content

Commit

Permalink
Cleaned code
Browse files Browse the repository at this point in the history
Need to merge to fix caching issue
  • Loading branch information
Dhruv-0-Arora committed Aug 22, 2024
1 parent 3181c05 commit ffdf3ff
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
40 changes: 14 additions & 26 deletions fission/src/mirabuf/MirabufSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}

Expand All @@ -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()
Expand Down Expand Up @@ -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 => {
Expand Down
15 changes: 15 additions & 0 deletions fission/src/systems/scene/GizmoSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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

0 comments on commit ffdf3ff

Please sign in to comment.