2021-06-18 13:05:06 +02:00
|
|
|
import type {VFC} from 'react'
|
|
|
|
import React, {
|
|
|
|
useEffect,
|
|
|
|
useLayoutEffect,
|
|
|
|
useMemo,
|
|
|
|
useRef,
|
|
|
|
useState,
|
|
|
|
} from 'react'
|
|
|
|
import {useEditorStore} from '../store'
|
|
|
|
import {createPortal} from '@react-three/fiber'
|
|
|
|
import EditableProxy from './EditableProxy'
|
2021-07-30 10:59:59 +02:00
|
|
|
import type {OrbitControls} from 'three-stdlib'
|
2021-06-18 13:05:06 +02:00
|
|
|
import TransformControls from './TransformControls'
|
|
|
|
import shallow from 'zustand/shallow'
|
|
|
|
import type {Material, Mesh, Object3D} from 'three'
|
|
|
|
import {MeshBasicMaterial, MeshPhongMaterial} from 'three'
|
|
|
|
import studio from '@theatre/studio'
|
|
|
|
import type {ISheetObject} from '@theatre/core'
|
|
|
|
import type {$FixMe} from '../types'
|
|
|
|
import {useSelected} from './useSelected'
|
2021-07-03 15:04:06 +02:00
|
|
|
import {useVal} from '@theatre/dataverse-react'
|
2021-08-07 21:17:29 +02:00
|
|
|
import useInvalidate from './useInvalidate'
|
2021-08-08 22:25:48 +02:00
|
|
|
import {getEditorSheetObject} from './editorStuff'
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
export interface ProxyManagerProps {
|
2021-07-30 10:59:59 +02:00
|
|
|
orbitControlsRef: React.MutableRefObject<OrbitControls | null>
|
2021-06-18 13:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type IEditableProxy = {
|
|
|
|
portal: ReturnType<typeof createPortal>
|
|
|
|
object: Object3D
|
|
|
|
sheetObject: ISheetObject<$FixMe>
|
|
|
|
}
|
|
|
|
|
|
|
|
const ProxyManager: VFC<ProxyManagerProps> = ({orbitControlsRef}) => {
|
|
|
|
const isBeingEdited = useRef(false)
|
2021-08-08 22:25:48 +02:00
|
|
|
const editorObject = getEditorSheetObject()
|
|
|
|
const [sceneSnapshot, sheetObjects] = useEditorStore(
|
|
|
|
(state) => [state.sceneSnapshot, state.sheetObjects],
|
2021-07-03 15:08:14 +02:00
|
|
|
shallow,
|
|
|
|
)
|
2021-07-03 15:04:06 +02:00
|
|
|
const transformControlsMode =
|
2021-07-07 16:39:53 +02:00
|
|
|
useVal(editorObject?.props.transformControls.mode) ?? 'translate'
|
2021-07-03 15:04:06 +02:00
|
|
|
|
|
|
|
const transformControlsSpace =
|
2021-07-07 16:39:53 +02:00
|
|
|
useVal(editorObject?.props.transformControls.space) ?? 'world'
|
2021-07-03 15:04:06 +02:00
|
|
|
|
2021-07-03 15:08:14 +02:00
|
|
|
const viewportShading =
|
2021-07-07 16:39:53 +02:00
|
|
|
useVal(editorObject?.props.viewport.shading) ?? 'rendered'
|
2021-07-03 15:08:14 +02:00
|
|
|
|
2021-06-18 13:05:06 +02:00
|
|
|
const sceneProxy = useMemo(() => sceneSnapshot?.clone(), [sceneSnapshot])
|
2021-07-16 15:06:15 +02:00
|
|
|
const [editableProxies, setEditableProxies] = useState<
|
|
|
|
{
|
|
|
|
[name in string]?: IEditableProxy
|
|
|
|
}
|
|
|
|
>({})
|
2021-06-18 13:05:06 +02:00
|
|
|
|
2021-08-07 21:17:29 +02:00
|
|
|
const invalidate = useInvalidate()
|
|
|
|
|
2021-06-18 13:05:06 +02:00
|
|
|
// set up scene proxies
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (!sceneProxy) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const editableProxies: {[name: string]: IEditableProxy} = {}
|
|
|
|
|
|
|
|
sceneProxy.traverse((object) => {
|
|
|
|
if (object.userData.__editable) {
|
|
|
|
// there are duplicate uniqueNames in the scene, only display one instance in the editor
|
|
|
|
if (editableProxies[object.userData.__editableName]) {
|
|
|
|
object.parent!.remove(object)
|
|
|
|
} else {
|
|
|
|
const uniqueName = object.userData.__editableName
|
|
|
|
|
|
|
|
editableProxies[uniqueName] = {
|
|
|
|
portal: createPortal(
|
|
|
|
<EditableProxy
|
|
|
|
editableName={object.userData.__editableName}
|
|
|
|
editableType={object.userData.__editableType}
|
|
|
|
object={object}
|
|
|
|
/>,
|
|
|
|
object.parent!,
|
|
|
|
),
|
|
|
|
object: object,
|
|
|
|
sheetObject: sheetObjects[uniqueName]!,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
setEditableProxies(editableProxies)
|
|
|
|
}, [orbitControlsRef, sceneProxy])
|
|
|
|
|
|
|
|
const selected = useSelected()
|
2021-07-16 15:06:15 +02:00
|
|
|
const editableProxyOfSelected = selected && editableProxies[selected]
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
// subscribe to external changes
|
|
|
|
useEffect(() => {
|
2021-07-16 15:06:15 +02:00
|
|
|
if (!editableProxyOfSelected) return
|
|
|
|
const object = editableProxyOfSelected.object
|
|
|
|
const sheetObject = editableProxyOfSelected.sheetObject
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
const setFromTheatre = (newValues: any) => {
|
|
|
|
object.position.set(
|
|
|
|
newValues.position.x,
|
|
|
|
newValues.position.y,
|
|
|
|
newValues.position.z,
|
|
|
|
)
|
|
|
|
object.rotation.set(
|
|
|
|
newValues.rotation.x,
|
|
|
|
newValues.rotation.y,
|
|
|
|
newValues.rotation.z,
|
|
|
|
)
|
|
|
|
object.scale.set(newValues.scale.x, newValues.scale.y, newValues.scale.z)
|
2021-08-07 21:17:29 +02:00
|
|
|
invalidate()
|
2021-06-18 13:05:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setFromTheatre(sheetObject.value)
|
|
|
|
|
|
|
|
const untap = sheetObject.onValuesChange(setFromTheatre)
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
untap()
|
|
|
|
}
|
2021-07-16 15:06:15 +02:00
|
|
|
}, [editableProxyOfSelected, selected])
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
// set up viewport shading modes
|
|
|
|
const [renderMaterials, setRenderMaterials] = useState<{
|
|
|
|
[id: string]: Material | Material[]
|
|
|
|
}>({})
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (!sceneProxy) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const renderMaterials: {
|
|
|
|
[id: string]: Material | Material[]
|
|
|
|
} = {}
|
|
|
|
|
|
|
|
sceneProxy.traverse((object) => {
|
|
|
|
const mesh = object as Mesh
|
|
|
|
if (mesh.isMesh && !mesh.userData.helper) {
|
|
|
|
renderMaterials[mesh.id] = mesh.material
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
setRenderMaterials(renderMaterials)
|
|
|
|
|
|
|
|
return () => {
|
2021-07-02 20:43:25 +02:00
|
|
|
// @todo do we need this cleanup?
|
|
|
|
// Object.entries(renderMaterials).forEach(([id, material]) => {
|
|
|
|
// ;(sceneProxy.getObjectById(Number.parseInt(id)) as Mesh).material =
|
|
|
|
// material
|
|
|
|
// })
|
2021-06-18 13:05:06 +02:00
|
|
|
}
|
|
|
|
}, [sceneProxy])
|
|
|
|
|
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (!sceneProxy) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sceneProxy.traverse((object) => {
|
|
|
|
const mesh = object as Mesh
|
|
|
|
if (mesh.isMesh && !mesh.userData.helper) {
|
|
|
|
let material
|
|
|
|
switch (viewportShading) {
|
|
|
|
case 'wireframe':
|
|
|
|
mesh.material = new MeshBasicMaterial({
|
|
|
|
wireframe: true,
|
|
|
|
color: 'black',
|
|
|
|
})
|
|
|
|
break
|
|
|
|
case 'flat':
|
|
|
|
// it is possible that renderMaterials hasn't updated yet
|
|
|
|
if (!renderMaterials[mesh.id]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
material = new MeshBasicMaterial()
|
|
|
|
if (renderMaterials[mesh.id].hasOwnProperty('color')) {
|
|
|
|
material.color = (renderMaterials[mesh.id] as any).color
|
|
|
|
}
|
|
|
|
if (renderMaterials[mesh.id].hasOwnProperty('map')) {
|
|
|
|
material.map = (renderMaterials[mesh.id] as any).map
|
|
|
|
}
|
|
|
|
if (renderMaterials[mesh.id].hasOwnProperty('vertexColors')) {
|
|
|
|
material.vertexColors = (
|
|
|
|
renderMaterials[mesh.id] as any
|
|
|
|
).vertexColors
|
|
|
|
}
|
|
|
|
mesh.material = material
|
|
|
|
break
|
|
|
|
case 'solid':
|
|
|
|
// it is possible that renderMaterials hasn't updated yet
|
|
|
|
if (!renderMaterials[mesh.id]) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
material = new MeshPhongMaterial()
|
|
|
|
if (renderMaterials[mesh.id].hasOwnProperty('color')) {
|
|
|
|
material.color = (renderMaterials[mesh.id] as any).color
|
|
|
|
}
|
|
|
|
if (renderMaterials[mesh.id].hasOwnProperty('map')) {
|
|
|
|
material.map = (renderMaterials[mesh.id] as any).map
|
|
|
|
}
|
|
|
|
if (renderMaterials[mesh.id].hasOwnProperty('vertexColors')) {
|
|
|
|
material.vertexColors = (
|
|
|
|
renderMaterials[mesh.id] as any
|
|
|
|
).vertexColors
|
|
|
|
}
|
|
|
|
mesh.material = material
|
|
|
|
break
|
|
|
|
case 'rendered':
|
|
|
|
mesh.material = renderMaterials[mesh.id]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}, [viewportShading, renderMaterials, sceneProxy])
|
|
|
|
|
|
|
|
if (!sceneProxy) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<primitive object={sceneProxy} />
|
2021-07-16 15:06:15 +02:00
|
|
|
{selected && editableProxyOfSelected && (
|
2021-06-18 13:05:06 +02:00
|
|
|
<TransformControls
|
|
|
|
mode={transformControlsMode}
|
|
|
|
space={transformControlsSpace}
|
|
|
|
orbitControlsRef={orbitControlsRef}
|
2021-07-16 15:06:15 +02:00
|
|
|
object={editableProxyOfSelected.object}
|
2021-06-18 13:05:06 +02:00
|
|
|
onObjectChange={() => {
|
2021-07-16 15:06:15 +02:00
|
|
|
const sheetObject = editableProxyOfSelected.sheetObject
|
|
|
|
const obj = editableProxyOfSelected.object
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
studio.transaction(({set}) => {
|
|
|
|
set(sheetObject.props, {
|
|
|
|
position: {
|
|
|
|
x: obj.position.x,
|
|
|
|
y: obj.position.y,
|
|
|
|
z: obj.position.z,
|
|
|
|
},
|
|
|
|
rotation: {
|
|
|
|
x: obj.rotation.x,
|
|
|
|
y: obj.rotation.y,
|
|
|
|
z: obj.rotation.z,
|
|
|
|
},
|
|
|
|
scale: {
|
|
|
|
x: obj.scale.x,
|
|
|
|
y: obj.scale.y,
|
|
|
|
z: obj.scale.z,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}}
|
|
|
|
onDraggingChange={(event) => (isBeingEdited.current = event.value)}
|
|
|
|
/>
|
|
|
|
)}
|
2021-07-16 15:06:15 +02:00
|
|
|
{Object.values(editableProxies).map(
|
|
|
|
(editableProxy) => editableProxy!.portal,
|
|
|
|
)}
|
2021-06-18 13:05:06 +02:00
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default ProxyManager
|