Skip to content

Commit ad65407

Browse files
Mirabuf Scene Object OBB memory fixes [SYNTH-278] (#1446)
2 parents eda7a3d + 853085e commit ad65407

1 file changed

Lines changed: 24 additions & 36 deletions

File tree

fission/src/mirabuf/MirabufSceneObject.ts

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -416,14 +416,14 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
416416
)
417417

418418
const yUnitVec = new JOLT.Vec3(0, 1, 0)
419-
const initialRotation = JOLT.Quat.prototype.sRotation(yUnitVec, initialPos.yaw)
419+
const initialRotation = JOLT.Quat.prototype.sRotation(yUnitVec, initialPos.yaw) // STATIC_ALIAS
420420

421421
const blankVec = new JOLT.Vec3()
422422
this.mirabufInstance.parser.rigidNodes.forEach(rn => {
423423
const jBodyId = this.mechanism.getBodyByNodeId(rn.id)
424424
if (!jBodyId) return
425425

426-
const position = World.physicsSystem.getBody(jBodyId)!.GetPosition()
426+
const position = World.physicsSystem.getBody(jBodyId)!.GetPosition() // STATIC_ALIAS
427427
const offset = convertJoltRVec3ToJoltVec3(position.Sub(bodyCenter))
428428

429429
World.physicsSystem.setBodyPositionRotationAndVelocity(
@@ -435,15 +435,13 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
435435
false
436436
)
437437

438-
JOLT.destroy(position)
439438
JOLT.destroy(offset)
440439
})
441440

442441
this.updateMeshTransforms()
443442

444443
JOLT.destroy(bodyCenter)
445444
JOLT.destroy(initialTranslation)
446-
JOLT.destroy(initialRotation)
447445
JOLT.destroy(yUnitVec)
448446
JOLT.destroy(blankVec)
449447
}
@@ -461,6 +459,11 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
461459
public dispose(): void {
462460
this.mirabufInstance.dispose(World.sceneRenderer.scene)
463461

462+
if (this._unrotatedRootNodeToCenterPositionTranslation) {
463+
JOLT.destroy(this._unrotatedRootNodeToCenterPositionTranslation)
464+
this._unrotatedRootNodeToCenterPositionTranslation = undefined
465+
}
466+
464467
if (this._brain?.isSynthesis()) {
465468
this._brain.clearControls()
466469
}
@@ -545,7 +548,7 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
545548
* Matches mesh transforms to their Jolt counterparts.
546549
*/
547550
public updateMeshTransforms() {
548-
let weightedCOM = new JOLT.RVec3(0, 0, 0)
551+
const weightedCom = new THREE.Vector3()
549552
let totalMass = 0
550553

551554
// If this.dispose() has been ran then return
@@ -555,18 +558,15 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
555558
const body = World.physicsSystem.getBody(bodyId)
556559
if (!body) return
557560

558-
const transform = convertJoltMat44ToThreeMatrix4(body.GetWorldTransform())
561+
const transform = convertJoltMat44ToThreeMatrix4(body.GetWorldTransform()) // STATIC_ALIAS
559562
this.updateNodeParts(rn, transform)
560563

561-
const position = body.GetPosition()
564+
const position = body.GetPosition() // STATIC_ALIAS
562565
if (Number.isNaN(position.GetX())) {
563-
const vel = body.GetLinearVelocity()
566+
const vel = body.GetLinearVelocity() // STATIC_ALIAS
564567
console.warn(
565568
`Invalid Position.\nPosition => ${position.GetX()}, ${position.GetY()}, ${position.GetZ()}\nVelocity => ${vel.GetX()}, ${vel.GetY()}, ${vel.GetZ()}`
566569
)
567-
568-
JOLT.destroy(vel)
569-
JOLT.destroy(position)
570570
}
571571

572572
if (this._debugBodies) {
@@ -584,32 +584,24 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
584584
const inverseMass = body.GetMotionProperties().GetInverseMass()
585585

586586
if (inverseMass > 0) {
587-
const oldWeighedCOM = weightedCOM
588-
589587
const mass = 1 / inverseMass
590-
const com = body.GetCenterOfMassPosition().Mul(mass)
588+
const comPosition = body.GetCenterOfMassPosition() // STATIC_ALIAS
591589

592-
weightedCOM = weightedCOM.AddRVec3(com)
590+
weightedCom.addScaledVector(convertJoltVec3ToThreeVector3(comPosition, false), mass)
593591
totalMass += mass
594-
595-
JOLT.destroy(oldWeighedCOM)
596-
JOLT.destroy(com)
597592
}
598593
}
599594
})
600595
}
601596

602597
if (this._centerOfMassIndicator) {
603-
const setPositionAndVisibility = (netCoM: Jolt.RVec3) => {
604-
this._centerOfMassIndicator!.position.set(netCoM.GetX(), netCoM.GetY(), netCoM.GetZ())
605-
this._centerOfMassIndicator!.visible = PreferencesSystem.getUserPreference("ShowCenterOfMassIndicators")
598+
if (totalMass > 0) {
599+
weightedCom.divideScalar(totalMass)
606600
}
607601

608-
const com = totalMass > 0 ? weightedCOM.Div(totalMass) : weightedCOM
609-
setPositionAndVisibility(com)
602+
this._centerOfMassIndicator.position.copy(weightedCom)
603+
this._centerOfMassIndicator.visible = PreferencesSystem.getUserPreference("ShowCenterOfMassIndicators")
610604
}
611-
612-
JOLT.destroy(weightedCOM)
613605
}
614606

615607
public updateNodeParts(rn: RigidNodeReadOnly, transform: THREE.Matrix4) {
@@ -827,9 +819,9 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
827819

828820
const inverseRotation = this.getInverseRotationOfBody()
829821

830-
const biggest = JOLT.AABox.prototype.sBiggest()
822+
const biggest = JOLT.AABox.prototype.sBiggest() // STATIC_ALIAS
831823
const scale = new JOLT.Vec3(1, 1, 1)
832-
const identity = JOLT.Quat.prototype.sIdentity()
824+
const identity = JOLT.Quat.prototype.sIdentity() // STATIC_ALIAS
833825

834826
this.mirabufInstance.parser.rigidNodes.forEach(rigidNode => {
835827
const bodyId = this.mechanism.getBodyByNodeId(rigidNode.id)
@@ -883,8 +875,6 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
883875
JOLT.destroy(inverseRotation)
884876

885877
JOLT.destroy(scale)
886-
JOLT.destroy(biggest)
887-
JOLT.destroy(identity)
888878

889879
const mins = [this._furthestVertices.x.min, this._furthestVertices.y.min, this._furthestVertices.z.min]
890880
const maxes = [this._furthestVertices.x.max, this._furthestVertices.y.max, this._furthestVertices.z.max]
@@ -937,11 +927,12 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
937927

938928
// Finally, we just offset the root node to get the true center
939929
const position = convertJoltRVec3ToJoltVec3(rootBody.GetPosition().Add(offset))
940-
const transform = JOLT.Mat44.prototype.sRotationTranslation(rotation, position)
930+
const transform = JOLT.Mat44.prototype.sRotationTranslation(rotation, position) // STATIC_ALIAS
941931

942932
const orientedBoundingBox = new JOLT.OrientedBox(transform, halfExtent)
943933

944-
JOLT.destroy(transform)
934+
JOLT.destroy(offset)
935+
JOLT.destroy(position)
945936
JOLT.destroy(halfExtent)
946937

947938
return orientedBoundingBox
@@ -985,9 +976,8 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
985976

986977
const shape = body.GetShape()
987978
const scale = new JOLT.Vec3(1, 1, 1)
988-
const biggest = JOLT.AABox.prototype.sBiggest()
989-
990-
const identity = JOLT.Quat.prototype.sIdentity()
979+
const biggest = JOLT.AABox.prototype.sBiggest() // STATIC_ALIAS
980+
const identity = JOLT.Quat.prototype.sIdentity() // STATIC_ALIAS
991981
const triangleContext = new JOLT.ShapeGetTriangles(shape, biggest, shape.GetCenterOfMass(), identity, scale)
992982

993983
try {
@@ -1008,8 +998,6 @@ class MirabufSceneObject extends SceneObject implements ContextSupplier {
1008998
} finally {
1009999
JOLT.destroy(triangleContext)
10101000
JOLT.destroy(scale)
1011-
JOLT.destroy(biggest)
1012-
JOLT.destroy(identity)
10131001
}
10141002
})
10151003

0 commit comments

Comments
 (0)