Skip to content

Commit

Permalink
fix: Added new initial config panel to import local.
Browse files Browse the repository at this point in the history
Also added in a couple cleanups here and there.
  • Loading branch information
HunterBarclay committed Sep 22, 2024
1 parent b2b648a commit ceaf33f
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 45 deletions.
2 changes: 0 additions & 2 deletions fission/src/mirabuf/MirabufInstance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,6 @@ class MirabufInstance {
}
}

console.debug(batchMap)

// Construct batched meshes
batchMap.forEach((materialBodyMap, material) => {
const count = countMap.get(material)!
Expand Down
8 changes: 3 additions & 5 deletions fission/src/mirabuf/MirabufSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { SceneOverlayTag } from "@/ui/components/SceneOverlayEvents"
import { ProgressHandle } from "@/ui/components/ProgressNotificationData"
import SynthesisBrain from "@/systems/simulation/synthesis_brain/SynthesisBrain"
import GizmoSceneObject from "@/systems/scene/GizmoSceneObject"
import { threeMatrix4ToString } from "@/util/debug/DebugPrint"

const DEBUG_BODIES = false

Expand Down Expand Up @@ -176,6 +175,8 @@ class MirabufSceneObject extends SceneObject {
this.UpdateScoringZones()

setSpotlightAssembly(this)

this.UpdateBatches()
}

public Update(): void {
Expand Down Expand Up @@ -410,10 +411,7 @@ class MirabufSceneObject extends SceneObject {

const jBody = World.PhysicsSystem.GetBody(jRootId)
const comTransform = JoltMat44_ThreeMatrix4(jBody.GetCenterOfMassTransform())
gizmo.UpdateGizmoObjectPositionAndRotation(comTransform)

console.debug(`Source:\n${threeMatrix4ToString(comTransform)}`)
console.debug(`New:\n${threeMatrix4ToString(gizmo.obj.matrix)}`)
gizmo.SetTransform(comTransform)
}

private getPreferences(): void {
Expand Down
2 changes: 0 additions & 2 deletions fission/src/systems/analytics/AnalyticsSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,6 @@ class AnalyticsSystem extends WorldSystem {
betaCode = betaCode.substring(betaCode.indexOf("=") + 1, betaCode.indexOf(";"))

this.SetUserProperty("Beta Code", betaCode)
} else {
console.debug("No code match")
}

if (MOBILE_USER_AGENT_REGEX.test(navigator.userAgent)) {
Expand Down
40 changes: 31 additions & 9 deletions fission/src/systems/scene/GizmoSceneObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import MirabufSceneObject from "@/mirabuf/MirabufSceneObject"
import { Object3D, PerspectiveCamera } from "three"
import { ThreeQuaternion_JoltQuat, JoltMat44_ThreeMatrix4, ThreeVector3_JoltVec3 } from "@/util/TypeConversions"
import { RigidNodeId } from "@/mirabuf/MirabufParser"
import { threeMatrix4ToString } from "@/util/debug/DebugPrint"

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

class GizmoSceneObject extends SceneObject {
private _gizmo: TransformControls
private _obj: Object3D
private _forceUpdate: boolean = false

private _parentObject: MirabufSceneObject | undefined
private _relativeTransformations?: Map<RigidNodeId, THREE.Matrix4>
Expand Down Expand Up @@ -66,10 +66,7 @@ class GizmoSceneObject extends SceneObject {

if (this._parentObject) {
this._relativeTransformations = new Map<RigidNodeId, THREE.Matrix4>()
const c = this._obj.matrix.clone()
console.debug(`Clone:\n${threeMatrix4ToString(c)}`)

const gizmoTransformInv = c.invert()
const gizmoTransformInv = this._obj.matrix.clone().invert()

/** Due to the limited math functionality exposed to JS for Jolt, we need everything in ThreeJS. */
this._parentObject.mirabufInstance.parser.rigidNodes.forEach(rn => {
Expand Down Expand Up @@ -153,7 +150,8 @@ class GizmoSceneObject extends SceneObject {
/** Translating the obj changes to the mirabuf scene object */
if (this._parentObject) {
this._parentObject.DisablePhysics()
if (this.isDragging) {
if (this.isDragging || this._forceUpdate) {
this._forceUpdate = false
this._parentObject.mirabufInstance.parser.rigidNodes.forEach(rn => {
this.UpdateNodeTransform(rn.id)
})
Expand All @@ -167,14 +165,20 @@ class GizmoSceneObject extends SceneObject {
this._parentObject?.EnablePhysics()
World.SceneRenderer.RemoveObject(this._obj)
World.SceneRenderer.RemoveObject(this._gizmo)

this._relativeTransformations?.clear()
}

/** changes the mode of the gizmo */
public SetMode(mode: GizmoMode) {
this._gizmo.setMode(mode)
}

/** updates body position and rotation for each body from the parent mirabuf */
/**
* Updates a given node to follow the gizmo.
*
* @param rnId Target node to update.
*/
public UpdateNodeTransform(rnId: RigidNodeId) {
if (!this._parentObject || !this._relativeTransformations || !this._relativeTransformations.has(rnId)) return

Expand All @@ -194,8 +198,13 @@ class GizmoSceneObject extends SceneObject {
)
}

/** */
public UpdateGizmoObjectPositionAndRotation(gizmoTransformation: THREE.Matrix4) {
/**
* Updates the gizmos location.
*
* @param gizmoTransformation Transform for the gizmo to take on.
*/
public SetTransform(gizmoTransformation: THREE.Matrix4) {
// Super hacky, prolly has something to do with how the transform controls update the attached object.
const position = new THREE.Vector3(0,0,0)
const rotation = new THREE.Quaternion(0,0,0,1)
const scale = new THREE.Vector3(1,1,1)
Expand All @@ -204,6 +213,19 @@ class GizmoSceneObject extends SceneObject {

this._obj.position.setFromMatrixPosition(gizmoTransformation)
this._obj.rotation.setFromRotationMatrix(gizmoTransformation)

this._forceUpdate = true
}

public SetRotation(rotation: THREE.Quaternion) {
const position = new THREE.Vector3(0,0,0)
const scale = new THREE.Vector3(1,1,1)
this._obj.matrix.decompose(position, new THREE.Quaternion(0,0,0,1), scale)
this._obj.matrix.compose(position, rotation, scale)

this._obj.rotation.setFromQuaternion(rotation)

this._forceUpdate = true
}

/** @return true if gizmo is attached to mirabufSceneObject */
Expand Down
3 changes: 0 additions & 3 deletions fission/src/ui/components/Scene.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ function Scene({ useStats }: SceneProps) {
World.InitWorld()

if (refContainer.current) {
console.debug("Adding ThreeJs to DOM")

const sr = World.SceneRenderer
sr.renderer.domElement.style.width = "100%"
sr.renderer.domElement.style.height = "100%"
Expand All @@ -30,7 +28,6 @@ function Scene({ useStats }: SceneProps) {
})

if (useStats && !stats) {
console.log("Adding stat")
stats = new Stats()
stats.dom.style.position = "absolute"
stats.dom.style.top = "0px"
Expand Down
50 changes: 30 additions & 20 deletions fission/src/ui/components/TransformGizmoControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import TransformGizmoControlProps from "./TransformGizmoControlProps";
import GizmoSceneObject, { GizmoMode } from "@/systems/scene/GizmoSceneObject";
import { ToggleButton, ToggleButtonGroup } from "./ToggleButtonGroup";
import World from "@/systems/World";
import Button, { ButtonSize } from "./Button";
import * as THREE from "three"

/**
* Creates GizmoSceneObject and gives you a toggle button group to control the modes of the gizmo.
Expand Down Expand Up @@ -30,8 +32,6 @@ function TransformGizmoControl({
const [gizmo, setGizmo] = useState<GizmoSceneObject | undefined>(undefined)

useEffect(() => {
console.debug('Gizmo Recreation')

const gizmo = new GizmoSceneObject(
"translate",
size,
Expand Down Expand Up @@ -70,25 +70,35 @@ function TransformGizmoControl({

// If there are no modes enabled, consider the UI pointless.
return disableOptions ? (<></>) : (
<ToggleButtonGroup
value={mode}
exclusive
onChange={(_, v) => {
if (v == undefined) return
<>
<ToggleButtonGroup
value={mode}
exclusive
onChange={(_, v) => {
if (v == undefined) return

setMode(v)
gizmo?.SetMode(v)
}}
sx={{
...(sx ?? {}),
alignSelf: "center",
}}
>
{/* { translateDisabled ? <></> : <ToggleButton value={"translate"}>Move</ToggleButton> }
{ rotateDisabled ? <></> : <ToggleButton value={"rotate"}>Rotate</ToggleButton> }
{ scaleDisabled ? <></> : <ToggleButton value={"scale"}>Scale</ToggleButton> } */}
{ buttons }
</ToggleButtonGroup>
setMode(v)
gizmo?.SetMode(v)
}}
sx={{
...(sx ?? {}),
alignSelf: "center",
}}
>
{/* { translateDisabled ? <></> : <ToggleButton value={"translate"}>Move</ToggleButton> }
{ rotateDisabled ? <></> : <ToggleButton value={"rotate"}>Rotate</ToggleButton> }
{ scaleDisabled ? <></> : <ToggleButton value={"scale"}>Scale</ToggleButton> } */}
{ buttons }
</ToggleButtonGroup>
{rotateDisabled ? <></> : <Button
value={"Reset Orientation"}
size={ButtonSize.Small}
className="self-center"
onClick={() => {
gizmo?.SetRotation(new THREE.Quaternion(0,0,0,1))
}}
/>}
</>
)
}

Expand Down
9 changes: 6 additions & 3 deletions fission/src/ui/modals/mirabuf/ImportLocalMirabufModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { CreateMirabuf } from "@/mirabuf/MirabufSceneObject"
import { SynthesisIcons } from "@/ui/components/StyledComponents"
import { ToggleButton, ToggleButtonGroup } from "@/ui/components/ToggleButtonGroup"
import { usePanelControlContext } from "@/ui/PanelContext"
import { PAUSE_REF_ASSEMBLY_SPAWNING } from "@/systems/physics/PhysicsSystem"
import { Global_OpenPanel } from "@/ui/components/GlobalUIControls"

const ImportLocalMirabufModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
// update tooltip based on type of drivetrain, receive message from Synthesis
Expand Down Expand Up @@ -49,15 +51,16 @@ const ImportLocalMirabufModal: React.FC<ModalPropsImpl> = ({ modalId }) => {
])

const hashBuffer = await selectedFile.arrayBuffer()
World.PhysicsSystem.HoldPause(PAUSE_REF_ASSEMBLY_SPAWNING)
await MirabufCachingService.CacheAndGetLocal(hashBuffer, miraType)
.then(x => CreateMirabuf(x!))
.then(x => {
if (x) {
World.SceneRenderer.RegisterSceneObject(x)
}
})

if (miraType == MiraType.ROBOT) openPanel("choose-scheme")
Global_OpenPanel?.("initial-config")
}
}).finally(() => setTimeout(() => World.PhysicsSystem.ReleasePause(PAUSE_REF_ASSEMBLY_SPAWNING), 500))
}
}}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const ConfigureGamepiecePickupInterface: React.FC<ConfigPickupProps> = ({ select
const robotTransformation = JoltMat44_ThreeMatrix4(World.PhysicsSystem.GetBody(nodeBodyId).GetWorldTransform())
const gizmoTransformation = deltaTransformation.premultiply(robotTransformation)

gizmo.UpdateGizmoObjectPositionAndRotation(gizmoTransformation)
gizmo.SetTransform(gizmoTransformation)
}

return (<TransformGizmoControl
Expand Down

0 comments on commit ceaf33f

Please sign in to comment.