Skip to content

Commit

Permalink
Merge pull request #1893 from wonderunit/xr-attachable-skeleton-fix
Browse files Browse the repository at this point in the history
Xr Attachables bug
  • Loading branch information
setpixel authored Dec 16, 2019
2 parents 83a9068 + 05a0510 commit 65f64bc
Show file tree
Hide file tree
Showing 12 changed files with 422 additions and 134 deletions.
4 changes: 2 additions & 2 deletions src/js/main.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const env = require('../../env.json')

if (env.mode) {
/* if (env.mode) {
process.env.NODE_ENV = env.mode
}
} */

const {app, ipcMain, BrowserWindow, dialog, powerSaveBlocker} = electron = require('electron')

Expand Down
6 changes: 6 additions & 0 deletions src/js/shared/IK/utils/jsUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Array.prototype.remove = function(object) {
let indexOf = this.indexOf(object)
if(indexOf !== -1) {
this.splice(indexOf, 1)
}
}
26 changes: 20 additions & 6 deletions src/js/shared/reducers/shot-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1020,6 +1020,16 @@ const sceneObjectsReducer = (state = {}, action) => {
}

return withDisplayNames(draft)

case 'CREATE_OBJECTS':
if (
action.payload.objects == null ||
action.payload.objects.length === 0
) return
for(let object of action.payload.objects) {
draft[object.id] = object
}
return withDisplayNames(draft)

case 'DELETE_OBJECTS':
if (
Expand Down Expand Up @@ -1221,19 +1231,22 @@ const sceneObjectsReducer = (state = {}, action) => {
draft[action.payload.id].skeleton = draft[action.payload.id].skeleton || {}
for (let bone of action.payload.skeleton) {
let rotation = bone.rotation
let { x, y, z } = bone.position
let position = bone.position
if(draft[action.payload.id].skeleton[bone.name]) {
draft[action.payload.id].skeleton[bone.name].rotation = !rotation ?
draft[action.payload.id].skeleton[bone.name].rotation :
{ x: rotation.x, y: rotation.y, z: rotation.z }
draft[action.payload.id].skeleton[bone.name].position = !bone.position ?
draft[action.payload.id].skeleton[bone.name].position :
{ x: x, y: y, z: z }
{ x: position.x, y: position.y, z: position.z }
} else {
draft[action.payload.id].skeleton[bone.name] = {
rotation: { x: rotation.x, y: rotation.y, z: rotation.z },
position: { x: x, y: y, z: z },
}
draft[action.payload.id].skeleton[bone.name] = {}
draft[action.payload.id].skeleton[bone.name].rotation = !rotation ?
{} :
{ x: rotation.x, y: rotation.y, z: rotation.z }
draft[action.payload.id].skeleton[bone.name].position = !bone.position ?
{} :
{ x: position.x, y: position.y, z: position.z }
}

}
Expand Down Expand Up @@ -1708,6 +1721,7 @@ module.exports = {
deselectAttachable: id => ({ type: 'DESELECT_ATTACHABLE', payload: id}),

createObject: values => ({ type: 'CREATE_OBJECT', payload: values }),
createObjects: objects => ({ type: 'CREATE_OBJECTS', payload: {objects} }),
updateObject: (id, values) => ({ type: 'UPDATE_OBJECT', payload: { id, ...values } }),

// batch update
Expand Down
80 changes: 65 additions & 15 deletions src/js/shot-generator/Character.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ const Character = React.memo(({
largeRenderer,
deleteObjects,
updateObjects,
defaultPosePreset,
...props
}) => {
const [ready, setReady] = useState(false) // ready to load?
Expand All @@ -199,11 +200,13 @@ const Character = React.memo(({
scene.remove(object.current.bonesHelper)
scene.remove(object.current.orthoIcon)
scene.remove(object.current)
object.current.userData.id === null
object.current.remove(SGIkHelper.getInstance())
SGIkHelper.getInstance().deselectControlPoint()
SGIkHelper.getInstance().removeFromParent(id)
objectRotationControl.current.deselectObject()
object.current.bonesHelper = null
scene.remove(object.current)
object.current = null
}
}
Expand All @@ -212,7 +215,11 @@ const Character = React.memo(({
useEffect(() => {
setReady(false)
setLoaded(false)
// return function cleanup () { }
return () => {
// Because when we switch models we recreate character(create new component) we need to set prev component object's id to null
if(object.current)
object.current.userData.id = null
}
}, [props.model])

useEffect(() => {
Expand Down Expand Up @@ -276,10 +283,10 @@ const Character = React.memo(({
object.current.userData.boneLengthScale = boneLengthScale
object.current.userData.parentRotation = parentRotation
object.current.userData.parentPosition = parentPosition
object.current.resetToStandardSkeleton = resetToStandardSkeleton
scene.add(object.current.bonesHelper)

let domElement = largeRenderer.current.domElement

objectRotationControl.current = new ObjectRotationControl(scene, camera, domElement, object.current.uuid)
let boneRotation = objectRotationControl.current
boneRotation.setUpdateCharacter((name, rotation) => {updateCharacterSkeleton({
Expand All @@ -297,7 +304,7 @@ const Character = React.memo(({
}

return function cleanup () {
setAttachables(object.current ? object.current.attachables : null)
setAttachables(!object.current ? null : object.current.attachables ? object.current.attachables.concat([]) : null)
setModelChange(!object.current ? false : object.current.attachables ? true : false)
doCleanup()
// setLoaded(false)
Expand Down Expand Up @@ -333,17 +340,17 @@ const Character = React.memo(({
const fullyUpdateSkeleton = () => {
let skeleton = object.current.userData.skeleton
let changedSkeleton = []

let inverseMatrixWorld = object.current.getInverseMatrixWorld()
let position = new THREE.Vector3()
let scalarForBones = 1
if(props.posePresetId === defaultPosePreset.id)
scalarForBones = object.current.userData.boneLengthScale === 100 ? 100 : 1
for(let i = 0; i < skeleton.bones.length; i++) {
let bone = skeleton.bones[i]

if(bone.name.includes("leaf")) continue
let rotation = bone.rotation
bone.applyMatrix(object.current.matrixWorld)
position = bone.position.clone()
bone.applyMatrix(inverseMatrixWorld)
position.multiplyScalar( object.current.userData.boneLengthScale === 100 ? 100 : 1)

position = bone.position.clone().applyMatrix4(object.current.getInverseMatrixWorld())
position.multiplyScalar(scalarForBones)
changedSkeleton.push({
name: bone.name,
position: {
Expand All @@ -361,21 +368,30 @@ const Character = React.memo(({
updateCharacterIkSkeleton({id, skeleton:changedSkeleton})
}

const saveAttachablesPositions = () => {
if(!object.current || !object.current.attachables) return
object.current.updateWorldMatrix(true, true)
for(let i = 0; i < object.current.attachables.length; i++) {
object.current.attachables[i].saveToStore()
}
}

let startingDeviceRotation = useRef(null)
let currentBoneSelected = useRef(null)

const updateSkeleton = () => {
let skeleton = object.current.userData.skeleton

if (Object.values(props.skeleton).length) {
fixRootBone()

for (bone of skeleton.bones) {
let userState = props.skeleton[bone.name]
let systemState = originalSkeleton.current.getBoneByName(bone.name).clone()
let state = userState || systemState
bone.rotation.x = state.rotation.x
bone.rotation.y = state.rotation.y
bone.rotation.z = state.rotation.z

}
} else {
let skeleton = object.current.userData.skeleton
Expand Down Expand Up @@ -461,6 +477,8 @@ const Character = React.memo(({
object.current.position.z = props.y
object.current.position.y = props.z
object.current.orthoIcon.position.copy(object.current.position)
saveAttachablesPositions()

}
}, [props.model, props.x, props.y, props.z, ready])

Expand All @@ -480,7 +498,6 @@ const Character = React.memo(({
object.current.rotation.y = props.rotation
object.current.orthoIcon.icon.material.rotation = props.rotation + Math.PI
}

}
}, [props.model, props.rotation, ready])

Expand All @@ -491,6 +508,22 @@ const Character = React.memo(({
updateSkeleton()
}

const resetToStandardSkeleton = () => {
let skeleton = object.current.userData.skeleton
if (Object.values(props.skeleton).length) {
fixRootBone()
for (bone of skeleton.bones) {
let userState = defaultPosePreset.state.skeleton[bone.name]
let systemState = originalSkeleton.current.getBoneByName(bone.name).clone()
let state = userState || systemState
bone.rotation.x = state.rotation.x
bone.rotation.y = state.rotation.y
bone.rotation.z = state.rotation.z
bone.updateMatrixWorld(true)
}
}
}

const fixRootBone = () => {
let { boneLengthScale, parentRotation, parentPosition } = object.current.userData
let skeleton = object.current.userData.skeleton
Expand All @@ -512,6 +545,7 @@ const Character = React.memo(({
if (!props.posePresetId) return
resetPose()
fullyUpdateSkeleton()
saveAttachablesPositions()
}, [props.posePresetId])

useEffect(() => {
Expand All @@ -520,36 +554,52 @@ const Character = React.memo(({
if (!props.handSkeleton) return
resetPose()
updateSkeletonHand()
saveAttachablesPositions()
}, [props.handPosePresetId, props.handSkeleton])

useEffect(() => {
if(!props.characterPresetId) return
if(object.current && object.current.attachables) {
let attachablesToDelete = []
for(let i = 0; i < object.current.attachables.length; i++) {
attachablesToDelete.push(object.current.attachables[i].userData.id)
}
deleteObjects(attachablesToDelete)
}
}, [props.characterPresetId])

// HACK force reset skeleton pose on Board UUID change
useEffect(() => {
if (!ready) return
if (!boardUid) return

console.log(type, id, 'changed boards')
resetPose()
if(props.handSkeleton && Object.keys(props.handSkeleton).length > 0)
updateSkeletonHand()
}, [boardUid])

useEffect(() => {
if (!ready) return
if (!object.current) return

// console.log(type, id, 'skeleton')
//console.log(type, id, 'skeleton')
updateSkeleton()

if(props.handSkeleton && Object.keys(props.handSkeleton).length > 0)
updateSkeletonHand()
}, [props.model, props.skeleton, ready])



useEffect(() => {
if (!ready) return
if (props.model !== object.current.userData.modelSettings.id) return
if (object.current) {
if (object.current.userData.modelSettings.height) {
let originalHeight = object.current.userData.originalHeight
let scale = props.height / originalHeight
object.current.scale.set( scale, scale, scale )
object.current.userData.height = props.height
} else {
object.current.scale.setScalar( props.height )
}
Expand Down Expand Up @@ -663,7 +713,7 @@ const Character = React.memo(({
}
}

setAttachables( null)
setAttachables(null)
}
setModelChange(false)
}
Expand Down
Loading

0 comments on commit 65f64bc

Please sign in to comment.