Considering toLinePath
function:
JavaScript
x
15
15
1
const toLinePath = (arrayOfPoints, color = 0x000000) => {
2
const path = new THREE.Path();
3
const firstPoint = arrayOfPoints[0];
4
5
path.moveTo(firstPoint.x, firstPoint.y, firstPoint.z);
6
arrayOfPoints.forEach(point => path.lineTo(point.x, point.y, point.z));
7
path.closePath();
8
9
const points = path.getPoints();
10
const geometry = new THREE.BufferGeometry().setFromPoints(points);
11
const material = new THREE.LineBasicMaterial({ color });
12
const line = new THREE.Line(geometry, material);
13
return line;
14
};
15
I want to recreate it using react-three-fiber
and been trying something like this:
JavaScript
1
33
33
1
import React from 'react'
2
import * as THREE from 'three'
3
import ReactDOM from 'react-dom'
4
import { Canvas } from 'react-three-fiber'
5
6
function LinePath(props) {
7
const vertices = React.useMemo(() => {
8
const path = new THREE.Path()
9
const firstPoint = props.vertices[0]
10
11
path.moveTo(firstPoint.x, firstPoint.y, firstPoint.z)
12
props.vertices.forEach(point => path.lineTo(point.x, point.y, point.z))
13
path.closePath()
14
15
return path.getPoints()
16
}, [props.vertices])
17
18
return (
19
<line>
20
<bufferGeometry attach="geometry" setFromPoints={vertices} />
21
<lineBasicMaterial attach="material" color="black" />
22
</line>
23
)
24
}
25
26
27
ReactDOM.render(
28
<Canvas>
29
<LinePath vertices={[new THREE.Vector3(0, 0, 0), new THREE.Vector3(2, 2, 0), new THREE.Vector3(-2, 2, 0)]} />
30
</Canvas>,
31
document.getElementById('root')
32
)
33
But there’s no output/error at all. I suppose I’ve completely misunderstood react-three-fiber
s API. What am I doing wrong here? Thanks and here’s the sandbox
Advertisement
Answer
For a future googler, useUpdate has been removed here:
https://github.com/pmndrs/react-three-fiber/pull/996.
Use useLayoutEffect instead.
JavaScript
1
14
14
1
const ref = React.useRef<BufferGeometry>(null!);
2
useLayoutEffect(() => {
3
if (ref.current) {
4
ref.current.setFromPoints(points);
5
}
6
}, []);
7
8
return (
9
<line>
10
<bufferGeometry attach='geometry' ref={ref} />
11
<lineBasicMaterial color={0xff0000} />
12
</line>
13
);
14