Skip to content

Commit

Permalink
fix: yarn
Browse files Browse the repository at this point in the history
  • Loading branch information
drcmda committed Nov 18, 2024
1 parent b30ab59 commit adae937
Showing 1 changed file with 19 additions and 17 deletions.
36 changes: 19 additions & 17 deletions src/core/Decal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export const Decal: ForwardRefComponent<DecalProps, THREE.Mesh> = /* @__PURE__ *
const ref = React.useRef<THREE.Mesh>(null!)
React.useImperativeHandle(forwardRef, () => ref.current)
const helper = React.useRef<THREE.Mesh>(null!)
const state = React.useRef({
position: new THREE.Vector3(),
rotation: new THREE.Euler(),
scale: new THREE.Vector3(1, 1, 1),
})

React.useLayoutEffect(() => {
const parent = mesh?.current || ref.current.parent
Expand All @@ -50,22 +55,16 @@ export const Decal: ForwardRefComponent<DecalProps, THREE.Mesh> = /* @__PURE__ *
throw new Error('Decal must have a Mesh as parent or specify its "mesh" prop')
}

const state = {
position: new THREE.Vector3(),
rotation: new THREE.Euler(),
scale: new THREE.Vector3(1, 1, 1),
}

if (parent) {
applyProps(state as any, { position, scale })
applyProps(state.current as any, { position, scale })

// Zero out the parents matrix world for this operation
const matrixWorld = parent.matrixWorld.clone()
parent.matrixWorld.identity()

if (!rotation || typeof rotation === 'number') {
const o = new THREE.Object3D()
o.position.copy(state.position)
o.position.copy(state.current.position)

// Thanks https://x.com/N8Programs !
const vertices = parent.geometry.attributes.position.array
Expand Down Expand Up @@ -100,25 +99,28 @@ export const Decal: ForwardRefComponent<DecalProps, THREE.Mesh> = /* @__PURE__ *
o.rotateY(Math.PI)

if (typeof rotation === 'number') o.rotateZ(rotation)
applyProps(state as any, { rotation: o.rotation })
applyProps(state.current as any, { rotation: o.rotation })
} else {
applyProps(state as any, { rotation })
applyProps(state.current as any, { rotation })
}

target.geometry = new DecalGeometry(parent, state.position, state.rotation, state.scale)
if (helper.current) {
applyProps(helper.current as any, state)
// Prevent the helpers from blocking rays
helper.current.traverse((child) => (child.raycast = () => null))
}
target.geometry = new DecalGeometry(parent, state.current.position, state.current.rotation, state.current.scale)
// Reset parents matix-world
parent.matrixWorld = matrixWorld

return () => {
target.geometry.dispose()
}
}
}, [mesh, debug, ...vecToArray(position), ...vecToArray(scale), ...vecToArray(rotation)])
}, [mesh, ...vecToArray(position), ...vecToArray(scale), ...vecToArray(rotation)])

React.useLayoutEffect(() => {
if (helper.current) {
applyProps(helper.current as any, state.current)
// Prevent the helpers from blocking rays
helper.current.traverse((child) => (child.raycast = () => null))
}
}, [debug])

// <meshStandardMaterial transparent polygonOffset polygonOffsetFactor={-10} {...props} />}
return (
Expand Down

0 comments on commit adae937

Please sign in to comment.