2021-06-18 13:05:06 +02:00
|
|
|
import type {VFC} from 'react'
|
2021-07-02 20:43:25 +02:00
|
|
|
import {useLayoutEffect} from 'react'
|
2021-06-29 15:15:14 +02:00
|
|
|
import React, {useEffect, useRef, Suspense} from 'react'
|
2021-06-18 13:05:06 +02:00
|
|
|
import {Canvas} from '@react-three/fiber'
|
|
|
|
import {useEditorStore} from '../store'
|
|
|
|
import {OrbitControls, Environment} from '@react-three/drei'
|
|
|
|
import shallow from 'zustand/shallow'
|
|
|
|
import root from 'react-shadow'
|
|
|
|
import styles from '../bundle.css.txt'
|
|
|
|
import UI from './UI'
|
|
|
|
import ProxyManager from './ProxyManager'
|
2021-07-02 20:43:25 +02:00
|
|
|
import {Button, PortalManager, IdProvider} from './elements'
|
2021-06-18 13:05:06 +02:00
|
|
|
import studio from '@theatre/studio'
|
2021-07-02 20:43:25 +02:00
|
|
|
import {useVal} from '@theatre/dataverse-react'
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
const EditorScene = () => {
|
|
|
|
const orbitControlsRef = useRef<typeof OrbitControls>()
|
|
|
|
|
|
|
|
const [
|
|
|
|
selectedHdr,
|
|
|
|
useHdrAsBackground,
|
|
|
|
showGrid,
|
|
|
|
showAxes,
|
|
|
|
helpersRoot,
|
|
|
|
setOrbitControlsRef,
|
|
|
|
] = useEditorStore(
|
|
|
|
(state) => [
|
|
|
|
state.selectedHdr,
|
|
|
|
state.useHdrAsBackground,
|
|
|
|
state.showGrid,
|
|
|
|
state.showAxes,
|
|
|
|
state.helpersRoot,
|
|
|
|
state.setOrbitControlsRef,
|
|
|
|
],
|
|
|
|
shallow,
|
|
|
|
)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setOrbitControlsRef(orbitControlsRef)
|
|
|
|
}, [setOrbitControlsRef])
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<Suspense fallback={null}>
|
|
|
|
{selectedHdr && (
|
|
|
|
<Environment
|
|
|
|
// @ts-ignore
|
|
|
|
files={selectedHdr}
|
|
|
|
path=""
|
|
|
|
background={useHdrAsBackground}
|
|
|
|
/>
|
|
|
|
)}
|
|
|
|
</Suspense>
|
|
|
|
{showGrid && <gridHelper args={[1000, 1000, 0x444444, 0x888888]} />}
|
|
|
|
{showAxes && <axesHelper args={[500]} />}
|
|
|
|
{/* @ts-ignore */}
|
|
|
|
<OrbitControls ref={orbitControlsRef} />
|
|
|
|
<primitive object={helpersRoot}></primitive>
|
|
|
|
<ProxyManager orbitControlsRef={orbitControlsRef} />
|
|
|
|
</>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
const Editor: VFC = () => {
|
2021-07-02 20:43:25 +02:00
|
|
|
const [editorObject, sceneSnapshot, initialEditorCamera, createSnapshot] =
|
|
|
|
useEditorStore(
|
|
|
|
(state) => [
|
|
|
|
state.editorObject,
|
|
|
|
state.sceneSnapshot,
|
|
|
|
state.initialEditorCamera,
|
|
|
|
state.createSnapshot,
|
|
|
|
],
|
|
|
|
shallow,
|
|
|
|
)
|
|
|
|
|
2021-07-02 20:47:54 +02:00
|
|
|
const editorOpen = useVal(editorObject?.props.isOpen)
|
2021-07-02 20:43:25 +02:00
|
|
|
useLayoutEffect(() => {
|
|
|
|
if (editorOpen) {
|
|
|
|
createSnapshot()
|
|
|
|
}
|
|
|
|
}, [editorOpen])
|
|
|
|
|
|
|
|
if (!editorObject) return <></>
|
2021-06-18 13:05:06 +02:00
|
|
|
|
|
|
|
return (
|
|
|
|
<root.div>
|
|
|
|
<div id="react-three-editable-editor-root">
|
|
|
|
<PortalManager>
|
|
|
|
<IdProvider>
|
|
|
|
<div className="relative z-50">
|
|
|
|
<div
|
|
|
|
className={`fixed ${editorOpen ? 'block' : 'hidden'} inset-0`}
|
|
|
|
>
|
|
|
|
{sceneSnapshot ? (
|
|
|
|
<>
|
|
|
|
<div className="relative z-0 h-full">
|
|
|
|
<Canvas
|
|
|
|
// @ts-ignore
|
|
|
|
colorManagement
|
|
|
|
camera={initialEditorCamera}
|
|
|
|
onCreated={({gl}) => {
|
|
|
|
gl.setClearColor('white')
|
|
|
|
}}
|
|
|
|
shadowMap
|
|
|
|
pixelRatio={window.devicePixelRatio}
|
|
|
|
onPointerMissed={() =>
|
|
|
|
studio.__experimental_setSelection([])
|
|
|
|
}
|
|
|
|
>
|
|
|
|
<EditorScene />
|
|
|
|
</Canvas>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<UI />
|
|
|
|
</>
|
2021-07-02 20:43:25 +02:00
|
|
|
) : null}
|
2021-06-18 13:05:06 +02:00
|
|
|
</div>
|
|
|
|
{editorOpen || (
|
|
|
|
<Button
|
|
|
|
className="fixed bottom-5 left-5"
|
|
|
|
onClick={() => {
|
2021-07-02 20:43:25 +02:00
|
|
|
studio.transaction(({set}) => {
|
2021-07-02 20:47:54 +02:00
|
|
|
set(editorObject.props.isOpen, true)
|
2021-07-02 20:43:25 +02:00
|
|
|
})
|
2021-06-18 13:05:06 +02:00
|
|
|
}}
|
|
|
|
>
|
|
|
|
Editor
|
|
|
|
</Button>
|
|
|
|
)}
|
|
|
|
</div>
|
2021-06-29 15:15:14 +02:00
|
|
|
|
2021-06-18 13:05:06 +02:00
|
|
|
<style type="text/css">{styles}</style>
|
|
|
|
</IdProvider>
|
|
|
|
</PortalManager>
|
|
|
|
</div>
|
|
|
|
</root.div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Editor
|