Skip to content

Commit

Permalink
fix(Outlines): conservative check for null ref (#1735)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett authored Dec 3, 2023
1 parent 10509c3 commit e4ceaf0
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/core/Outlines.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,18 @@ export function Outlines({
angle = Math.PI,
...props
}: OutlinesProps) {
const ref = React.useRef<THREE.Group>(null!)
const ref = React.useRef<THREE.Group>()
const [material] = React.useState(() => new OutlinesMaterial({ side: THREE.BackSide }))
const { gl } = useThree()
const contextSize = gl.getDrawingBufferSize(new THREE.Vector2())
React.useMemo(() => extend({ OutlinesMaterial }), [])

const oldAngle = React.useRef(0)
const oldGeometry = React.useRef<THREE.BufferGeometry>(null!)
const oldGeometry = React.useRef<THREE.BufferGeometry>()
React.useLayoutEffect(() => {
const group = ref.current
if (!group) return

const parent = group.parent as THREE.Mesh & THREE.SkinnedMesh & THREE.InstancedMesh
if (parent && parent.geometry) {
if (oldAngle.current !== angle || oldGeometry.current !== parent.geometry) {
Expand Down Expand Up @@ -133,6 +135,8 @@ export function Outlines({

React.useLayoutEffect(() => {
const group = ref.current
if (!group) return

const mesh = group.children[0] as THREE.Mesh<THREE.BufferGeometry, THREE.Material>
if (mesh) {
mesh.renderOrder = renderOrder
Expand All @@ -154,13 +158,15 @@ export function Outlines({
return () => {
// Dispose everything on unmount
const group = ref.current
let mesh = group.children[0] as THREE.Mesh<THREE.BufferGeometry, THREE.Material>
if (!group) return

const mesh = group.children[0] as THREE.Mesh<THREE.BufferGeometry, THREE.Material>
if (mesh) {
if (angle) mesh.geometry.dispose()
group.remove(mesh)
}
}
}, [])

return <group ref={ref} {...props} />
return <group ref={ref as React.Ref<THREE.Group>} {...props} />
}

1 comment on commit e4ceaf0

@vercel
Copy link

@vercel vercel bot commented on e4ceaf0 Dec 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.