Skip to content

Commit

Permalink
fix: restore Edge component to accept geometry as prop (#1978)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickthegroot committed Jun 26, 2024
1 parent 8dd9f4b commit a0c81c9
Showing 1 changed file with 17 additions and 15 deletions.
32 changes: 17 additions & 15 deletions src/core/Edges.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ export type EdgesProps = Partial<ThreeElements['mesh']> & {
threshold?: number
lineWidth?: number
} & Omit<LineMaterialParameters, 'vertexColors' | 'color'> &
Omit<ReactThreeFiber.Object3DNode<Line2, typeof Line2>, 'args'> &
Omit<ReactThreeFiber.Object3DNode<Line2, typeof Line2>, 'args' | 'geometry'> &
Omit<ReactThreeFiber.Object3DNode<LineMaterial, [LineMaterialParameters]>, 'color' | 'vertexColors' | 'args'> & {
geometry?: THREE.BufferGeometry
color?: THREE.ColorRepresentation
}

export const Edges: ForwardRefComponent<EdgesProps, EdgesRef> = /* @__PURE__ */ React.forwardRef<EdgesRef, EdgesProps>(
({ threshold = 15, ...props }: EdgesProps, fref) => {
({ threshold = 15, geometry: explicitGeometry, ...props }: EdgesProps, fref) => {
const ref = React.useRef<LineSegments2>(null!)
React.useImperativeHandle(fref, () => ref.current, [])

Expand All @@ -26,19 +27,20 @@ export const Edges: ForwardRefComponent<EdgesProps, EdgesRef> = /* @__PURE__ */

React.useLayoutEffect(() => {
const parent = ref.current.parent as THREE.Mesh
if (parent) {
const geometry = parent.geometry
if (geometry !== memoizedGeometry.current || threshold !== memoizedThreshold.current) {
memoizedGeometry.current = geometry
memoizedThreshold.current = threshold
const points = (new THREE.EdgesGeometry(geometry, threshold).attributes.position as THREE.BufferAttribute)
.array as Float32Array
ref.current.geometry.setPositions(points)
ref.current.geometry.attributes.instanceStart.needsUpdate = true
ref.current.geometry.attributes.instanceEnd.needsUpdate = true
ref.current.computeLineDistances()
}
}
const geometry = explicitGeometry ?? parent?.geometry
if (!geometry) return

const cached = memoizedGeometry.current === geometry && memoizedThreshold.current === threshold
if (cached) return
memoizedGeometry.current = geometry
memoizedThreshold.current = threshold

const points = (new THREE.EdgesGeometry(geometry, threshold).attributes.position as THREE.BufferAttribute)
.array as Float32Array
ref.current.geometry.setPositions(points)
ref.current.geometry.attributes.instanceStart.needsUpdate = true
ref.current.geometry.attributes.instanceEnd.needsUpdate = true
ref.current.computeLineDistances()
})

return <Line segments points={tmpPoints} ref={ref} raycast={() => null} {...props} />
Expand Down

0 comments on commit a0c81c9

Please sign in to comment.