diff --git a/fission/src/mirabuf/MirabufSceneObject.ts b/fission/src/mirabuf/MirabufSceneObject.ts index 6474974c7..16f23cf54 100644 --- a/fission/src/mirabuf/MirabufSceneObject.ts +++ b/fission/src/mirabuf/MirabufSceneObject.ts @@ -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" @@ -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)!) @@ -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("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 { @@ -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("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) diff --git a/fission/src/systems/scene/GizmoSceneObject.ts b/fission/src/systems/scene/GizmoSceneObject.ts index 65b3192a0..16e14ea74 100644 --- a/fission/src/systems/scene/GizmoSceneObject.ts +++ b/fission/src/systems/scene/GizmoSceneObject.ts @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) } diff --git a/fission/src/ui/panels/configuring/assembly-config/interfaces/ConfigureGamepiecePickupInterface.tsx b/fission/src/ui/panels/configuring/assembly-config/interfaces/ConfigureGamepiecePickupInterface.tsx index e52467612..ab55d8afc 100644 --- a/fission/src/ui/panels/configuring/assembly-config/interfaces/ConfigureGamepiecePickupInterface.tsx +++ b/fission/src/ui/panels/configuring/assembly-config/interfaces/ConfigureGamepiecePickupInterface.tsx @@ -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()) @@ -112,7 +112,7 @@ const ConfigureGamepiecePickupInterface: React.FC = ({ 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 @@ -131,7 +131,7 @@ const ConfigureGamepiecePickupInterface: React.FC = ({ 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) @@ -147,8 +147,8 @@ const ConfigureGamepiecePickupInterface: React.FC = ({ 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) @@ -221,8 +221,8 @@ const ConfigureGamepiecePickupInterface: React.FC = ({ 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) diff --git a/fission/src/ui/panels/configuring/assembly-config/interfaces/scoring/ZoneConfigInterface.tsx b/fission/src/ui/panels/configuring/assembly-config/interfaces/scoring/ZoneConfigInterface.tsx index 6fb0f00bd..000af2dfa 100644 --- a/fission/src/ui/panels/configuring/assembly-config/interfaces/scoring/ZoneConfigInterface.tsx +++ b/fission/src/ui/panels/configuring/assembly-config/interfaces/scoring/ZoneConfigInterface.tsx @@ -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()) @@ -176,7 +176,7 @@ const ZoneConfigInterface: React.FC = ({ 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) @@ -190,9 +190,9 @@ const ZoneConfigInterface: React.FC = ({ 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) @@ -232,7 +232,7 @@ const ZoneConfigInterface: React.FC = ({ 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`}