useEntities directly

This commit is contained in:
Kurt Hutten
2021-08-01 17:15:07 +10:00
parent d8998a73b3
commit 43477d33cc
2 changed files with 6 additions and 26 deletions

View File

@@ -10,12 +10,9 @@ import { TextureLoader } from 'three/src/loaders/TextureLoader'
const loader = new TextureLoader() const loader = new TextureLoader()
const colorMap = loader.load(texture) const colorMap = loader.load(texture)
let lastGroup
extend({ OrbitControls }) extend({ OrbitControls })
function Asset({ geometry: incomingGeo }) { function Asset({ geometry: incomingGeo }) {
const state = useThree()
const mesh = useRef() const mesh = useRef()
const ref = useRef<any>({}) const ref = useRef<any>({})
useLayoutEffect(() => { useLayoutEffect(() => {
@@ -25,20 +22,10 @@ function Asset({ geometry: incomingGeo }) {
}, [incomingGeo]) }, [incomingGeo])
if (!incomingGeo) return null if (!incomingGeo) return null
const groupData = incomingGeo.children ? incomingGeo : null if (incomingGeo.length)
if (lastGroup && lastGroup != groupData) { return incomingGeo.map((shape, index) => (
state.scene.remove(lastGroup) <primitive object={shape} key={index} />
lastGroup.children.forEach((c) => c?.geometry?.dispose()) ))
// returning <primitive object={groupData} /> does not add the new group to the scene
// there is probably some useRef magic that would make this work, but I don't have time to reseach it
/// FIXME - do this properly with useRef or other react magic
if (groupData) state.scene.add(groupData)
}
lastGroup = groupData
if (groupData) return <primitive object={groupData} />
if (incomingGeo.children) return <primitive object={incomingGeo} />
return ( return (
<mesh ref={mesh} scale={[1, 1, 1]}> <mesh ref={mesh} scale={[1, 1, 1]}>

View File

@@ -57,7 +57,7 @@ function CSG2Object3D(obj) {
switch (obj.type) { switch (obj.type) {
case 'mesh': case 'mesh':
geo.setIndex(new BufferAttribute(indices, 1)) geo.setIndex(new BufferAttribute(indices, 1))
mesh = new THREE.Mesh(geo, material) mesh = new Mesh(geo, material)
break break
case 'line': case 'line':
mesh = new Line(geo, material) mesh = new Line(geo, material)
@@ -99,20 +99,14 @@ self.addEventListener('message', (e)=>worker.postMessage(e.data))
const blob = new Blob([script], { type: 'text/javascript' }) const blob = new Blob([script], { type: 'text/javascript' })
scriptWorker = new Worker(window.URL.createObjectURL(blob)) scriptWorker = new Worker(window.URL.createObjectURL(blob))
scriptWorker.addEventListener('message', (e) => { scriptWorker.addEventListener('message', (e) => {
console.log('message from worker', e.data)
const data = e.data const data = e.data
if (data.action == 'entities') { if (data.action == 'entities') {
if (data.error) { if (data.error) {
response = createUnhealthyResponse(new Date(), data.error) response = createUnhealthyResponse(new Date(), data.error)
} else { } else {
const group = new Group()
data.entities
.map(CSG2Object3D)
.filter((o) => o)
.forEach((o) => group.add(o))
response = createHealthyResponse({ response = createHealthyResponse({
type: 'geometry', type: 'geometry',
data: group, data: [...data.entities.map(CSG2Object3D).filter((o) => o)],
consoleMessage: data.scriptStats, consoleMessage: data.scriptStats,
date: new Date(), date: new Date(),
}) })
@@ -138,7 +132,6 @@ self.addEventListener('message', (e)=>worker.postMessage(e.data))
await waitResult await waitResult
resolveReference = null resolveReference = null
console.log('response', response)
return response return response
} }