diff --git a/src/core/Edges.tsx b/src/core/Edges.tsx index 894c1b64a..d25a058fb 100644 --- a/src/core/Edges.tsx +++ b/src/core/Edges.tsx @@ -10,13 +10,14 @@ export type EdgesProps = Partial & { threshold?: number lineWidth?: number } & Omit & - Omit, 'args'> & + Omit, 'args' | 'geometry'> & Omit, 'color' | 'vertexColors' | 'args'> & { + geometry?: THREE.BufferGeometry color?: THREE.ColorRepresentation } export const Edges: ForwardRefComponent = /* @__PURE__ */ React.forwardRef( - ({ threshold = 15, ...props }: EdgesProps, fref) => { + ({ threshold = 15, geometry: explicitGeometry, ...props }: EdgesProps, fref) => { const ref = React.useRef(null!) React.useImperativeHandle(fref, () => ref.current, []) @@ -26,19 +27,20 @@ export const Edges: ForwardRefComponent = /* @__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 null} {...props} />