Skip to content

Commit

Permalink
Renamed mesh to obj and added functional fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dhruv-0-Arora committed Aug 21, 2024
1 parent 1e9dbcd commit 9cd80ac
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 68 deletions.
74 changes: 57 additions & 17 deletions fission/src/mirabuf/MirabufSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import MirabufInstance from "./MirabufInstance"
import MirabufParser, { ParseErrorSeverity, RigidNodeId, RigidNodeReadOnly } from "./MirabufParser"
import World from "@/systems/World"
import Jolt from "@barclah/jolt-physics"
import { JoltMat44_ThreeMatrix4 } from "@/util/TypeConversions"
import { JoltMat44_ThreeMatrix4, ThreeMatrix4_JoltMat44, ThreeQuaternion_JoltQuat } from "@/util/TypeConversions"
import * as THREE from "three"
import JOLT from "@/util/loading/JoltSyncLoader"
import { BodyAssociate, LayerReserve } from "@/systems/physics/PhysicsSystem"
Expand Down Expand Up @@ -176,6 +176,38 @@ class MirabufSceneObject extends SceneObject {
this.Eject()
}

/* Translates gizmo translations to the mirabuf assembly */
if (this._gizmo) {
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.UpdateBatches()
this.UpdateNameTag()
return
}

/** Updating the position of all mirabuf nodes */
this._mirabufInstance.parser.rigidNodes.forEach(rn => {
if (!this._mirabufInstance.meshes.size) return // if this.dispose() has been ran then return
const body = World.PhysicsSystem.GetBody(this._mechanism.GetBodyByNodeId(rn.id)!)
Expand Down Expand Up @@ -210,22 +242,8 @@ class MirabufSceneObject extends SceneObject {
}
})

this._mirabufInstance.batches.forEach(x => {
x.computeBoundingBox()
x.computeBoundingSphere()
})

/* Updating the position of the name tag according to the robots position on screen */
if (this._nameTag && PreferencesSystem.getGlobalPreference<boolean>("RenderSceneTags")) {
const boundingBox = this.ComputeBoundingBox()
this._nameTag.position = World.SceneRenderer.WorldToPixelSpace(
new THREE.Vector3(
(boundingBox.max.x + boundingBox.min.x) / 2,
boundingBox.max.y + 0.1,
(boundingBox.max.z + boundingBox.min.z) / 2
)
)
}
this.UpdateBatches()
this.UpdateNameTag()
}

public Dispose(): void {
Expand Down Expand Up @@ -310,6 +328,28 @@ class MirabufSceneObject extends SceneObject {
return mesh
}

/** Updates the batch computations */
private UpdateBatches() {
this._mirabufInstance.batches.forEach(x => {
x.computeBoundingBox()
x.computeBoundingSphere()
})
}

/** Updates the position of the nametag relative to the robots position */
private UpdateNameTag() {
if (this._nameTag && PreferencesSystem.getGlobalPreference<boolean>("RenderSceneTags")) {
const boundingBox = this.ComputeBoundingBox()
this._nameTag.position = World.SceneRenderer.WorldToPixelSpace(
new THREE.Vector3(
(boundingBox.max.x + boundingBox.min.x) / 2,
boundingBox.max.y + 0.1,
(boundingBox.max.z + boundingBox.min.z) / 2
)
)
}
}

public UpdateIntakeSensor() {
if (this._intakeSensor) {
World.SceneRenderer.RemoveSceneObject(this._intakeSensor.id)
Expand Down
51 changes: 13 additions & 38 deletions fission/src/systems/scene/GizmoSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { TransformControls } from "three/examples/jsm/controls/TransformControls
import InputSystem from "../input/InputSystem"
import World from "../World"
import MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
import { PerspectiveCamera } from "three"
import { ThreeMatrix4_JoltMat44, ThreeQuaternion_JoltQuat } from "@/util/TypeConversions"
import { Object3D, PerspectiveCamera } from "three"

export type GizmoMode = "translate" | "rotate" | "scale"

class GizmoSceneObject extends SceneObject {
private _gizmo: TransformControls
private _mesh: THREE.Mesh
private _obj: Object3D
private _parentObject: MirabufSceneObject | undefined

private _mainCamera: PerspectiveCamera
Expand All @@ -21,14 +20,18 @@ class GizmoSceneObject extends SceneObject {
return this._gizmo
}

public get mesh() {
return this._mesh
public get obj() {
return this._obj
}

public get isDragging() {
return this._gizmo.dragging
}

public constructor(mesh: THREE.Mesh, mode: GizmoMode, size: number, parentObject?: MirabufSceneObject) {
super()

this._mesh = mesh
this._obj = mesh
this._parentObject = parentObject
this._mainCamera = World.SceneRenderer.mainCamera

Expand All @@ -42,12 +45,12 @@ class GizmoSceneObject extends SceneObject {

public Setup(): void {
// adding the mesh and gizmo to the scene
World.SceneRenderer.AddObject(this._mesh)
World.SceneRenderer.AddObject(this._obj)
World.SceneRenderer.AddObject(this._gizmo)

// forcing the gizmo to rotate and transform with the object
this._gizmo.setSpace("local")
this._gizmo.attach(this._mesh)
this._gizmo.attach(this._obj)

this._gizmo.addEventListener("dragging-changed", (event: { target: TransformControls; value: unknown }) => {
// disable orbit controls when dragging the transform gizmo
Expand Down Expand Up @@ -94,9 +97,8 @@ class GizmoSceneObject extends SceneObject {
})
}
})

if (this._parentObject !== undefined) this._parentObject.DisablePhysics()
}

public Update(): void {
// updating the size of the gizmo based on the distance from the camera
const mainCameraFovRadians = (Math.PI * (this._mainCamera.fov * 0.5)) / 180
Expand All @@ -106,33 +108,6 @@ class GizmoSceneObject extends SceneObject {
1.9
)

// mapping the mesh transformations to the mirabuf object
if (this._parentObject !== undefined) {
this._parentObject.DisablePhysics()

if (this._gizmo.dragging) {
this._parentObject.mirabufInstance.parser.rigidNodes.forEach(rn => {
World.PhysicsSystem.SetBodyPosition(
this._parentObject!.mechanism.GetBodyByNodeId(rn.id)!,
ThreeMatrix4_JoltMat44(this._mesh.matrix).GetTranslation()
)
World.PhysicsSystem.SetBodyRotation(
this._parentObject!.mechanism.GetBodyByNodeId(rn.id)!,
ThreeQuaternion_JoltQuat(this._mesh.quaternion)
)

rn.parts.forEach(part => {
const partTransform = this._parentObject!.mirabufInstance.parser.globalTransforms.get(part)!
.clone()
.premultiply(this._mesh.matrix)

const meshes = this._parentObject!.mirabufInstance.meshes.get(part) ?? []
meshes.forEach(([batch, id]) => batch.setMatrixAt(id, partTransform))
})
})
}
}

// creating enter key and escape key event listeners
if (InputSystem.isKeyPressed("Enter") && this._parentObject) {
// confirming placement of object
Expand All @@ -150,7 +125,7 @@ class GizmoSceneObject extends SceneObject {
public Dispose(): void {
this._gizmo.detach()
if (this._parentObject) this._parentObject.RemoveGizmo()
World.SceneRenderer.RemoveObject(this._mesh)
World.SceneRenderer.RemoveObject(this._obj)
World.SceneRenderer.RemoveObject(this._gizmo)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function save(

const translation = new THREE.Vector3(0, 0, 0)
const rotation = new THREE.Quaternion(0, 0, 0, 1)
gizmo.mesh.matrixWorld.decompose(translation, rotation, new THREE.Vector3(1, 1, 1))
gizmo.obj.matrixWorld.decompose(translation, rotation, new THREE.Vector3(1, 1, 1))

const gizmoTransformation = new THREE.Matrix4().compose(translation, rotation, new THREE.Vector3(1, 1, 1))
const robotTransformation = JoltMat44_ThreeMatrix4(World.PhysicsSystem.GetBody(nodeBodyId).GetWorldTransform())
Expand Down Expand Up @@ -112,7 +112,7 @@ const ConfigureGamepiecePickupInterface: React.FC<ConfigPickupProps> = ({ select
return
}

transformGizmo.mesh.scale.set(zoneSize, zoneSize, zoneSize)
transformGizmo.obj.scale.set(zoneSize, zoneSize, zoneSize)
}, [zoneSize, transformGizmo])

// Not sure I like this, but made it a state and effect rather than a memo to add the cleanup to the end
Expand All @@ -131,7 +131,7 @@ const ConfigureGamepiecePickupInterface: React.FC<ConfigPickupProps> = ({ select
1.5
)

;(gizmo.mesh.material as THREE.Material).depthTest = false
;(gizmo.obj.material as THREE.Material).depthTest = false

const deltaTransformation = Array_ThreeMatrix4(selectedRobot.intakePreferences.deltaTransformation)

Expand All @@ -147,8 +147,8 @@ const ConfigureGamepiecePickupInterface: React.FC<ConfigPickupProps> = ({ select
const robotTransformation = JoltMat44_ThreeMatrix4(World.PhysicsSystem.GetBody(nodeBodyId).GetWorldTransform())
const gizmoTransformation = deltaTransformation.premultiply(robotTransformation)

gizmo.mesh.position.setFromMatrixPosition(gizmoTransformation)
gizmo.mesh.rotation.setFromRotationMatrix(gizmoTransformation)
gizmo.obj.position.setFromMatrixPosition(gizmoTransformation)
gizmo.obj.rotation.setFromRotationMatrix(gizmoTransformation)

setTransformGizmo(gizmo)

Expand Down Expand Up @@ -221,8 +221,8 @@ const ConfigureGamepiecePickupInterface: React.FC<ConfigPickupProps> = ({ select
const robotTransformation = JoltMat44_ThreeMatrix4(
World.PhysicsSystem.GetBody(selectedRobot.GetRootNodeId()!).GetWorldTransform()
)
transformGizmo.mesh.position.setFromMatrixPosition(robotTransformation)
transformGizmo.mesh.rotation.setFromRotationMatrix(robotTransformation)
transformGizmo.obj.position.setFromMatrixPosition(robotTransformation)
transformGizmo.obj.rotation.setFromRotationMatrix(robotTransformation)
}
setZoneSize(0.5)
setSelectedNode(selectedRobot?.rootNodeId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function save(
const translation = new THREE.Vector3(0, 0, 0)
const rotation = new THREE.Quaternion(0, 0, 0, 1)
const scale = new THREE.Vector3(1, 1, 1)
gizmo.mesh.matrixWorld.decompose(translation, rotation, scale)
gizmo.obj.matrixWorld.decompose(translation, rotation, scale)

const gizmoTransformation = new THREE.Matrix4().compose(translation, rotation, scale)
const fieldTransformation = JoltMat44_ThreeMatrix4(World.PhysicsSystem.GetBody(nodeBodyId).GetWorldTransform())
Expand Down Expand Up @@ -176,7 +176,7 @@ const ZoneConfigInterface: React.FC<ZoneConfigProps> = ({ selectedField, selecte
1.5
)

;(gizmo.mesh.material as THREE.Material).depthTest = false
;(gizmo.obj.material as THREE.Material).depthTest = false

const deltaTransformation = Array_ThreeMatrix4(zone.deltaTransformation)

Expand All @@ -190,9 +190,9 @@ const ZoneConfigInterface: React.FC<ZoneConfigProps> = ({ selectedField, selecte
const fieldTransformation = JoltMat44_ThreeMatrix4(World.PhysicsSystem.GetBody(nodeBodyId).GetWorldTransform())
const props = DeltaFieldTransforms_VisualProperties(deltaTransformation, fieldTransformation)

gizmo.mesh.position.set(props.translation.x, props.translation.y, props.translation.z)
gizmo.mesh.rotation.setFromQuaternion(props.rotation)
gizmo.mesh.scale.set(props.scale.x, props.scale.y, props.scale.z)
gizmo.obj.position.set(props.translation.x, props.translation.y, props.translation.z)
gizmo.obj.rotation.setFromQuaternion(props.rotation)
gizmo.obj.scale.set(props.scale.x, props.scale.y, props.scale.z)

setTransformGizmo(gizmo)

Expand Down Expand Up @@ -232,7 +232,7 @@ const ZoneConfigInterface: React.FC<ZoneConfigProps> = ({ selectedField, selecte
onClick={() => {
setAlliance(alliance == "blue" ? "red" : "blue")
if (transformGizmo) {
transformGizmo.mesh.material = alliance == "blue" ? redMaterial : blueMaterial
transformGizmo.obj.material = alliance == "blue" ? redMaterial : blueMaterial
}
}}
colorOverrideClass={`bg-match-${alliance}-alliance`}
Expand Down

0 comments on commit 9cd80ac

Please sign in to comment.