Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: restore Edge component to accept geometry as prop #1978

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading