gpt4 book ai didi

javascript - bufferGeometry setFromPoints 与 react-three-fiber

转载 作者:行者123 更新时间:2023-12-04 00:02:29 41 4
gpt4 key购买 nike

考虑toLinePath函数:

const toLinePath = (arrayOfPoints, color = 0x000000) => {
const path = new THREE.Path();
const firstPoint = arrayOfPoints[0];

path.moveTo(firstPoint.x, firstPoint.y, firstPoint.z);
arrayOfPoints.forEach(point => path.lineTo(point.x, point.y, point.z));
path.closePath();

const points = path.getPoints();
const geometry = new THREE.BufferGeometry().setFromPoints(points);
const material = new THREE.LineBasicMaterial({ color });
const line = new THREE.Line(geometry, material);
return line;
};

我想使用 react-three-fiber 重新创建它并一直在尝试这样的事情:

import React from 'react'
import * as THREE from 'three'
import ReactDOM from 'react-dom'
import { Canvas } from 'react-three-fiber'

function LinePath(props) {
const vertices = React.useMemo(() => {
const path = new THREE.Path()
const firstPoint = props.vertices[0]

path.moveTo(firstPoint.x, firstPoint.y, firstPoint.z)
props.vertices.forEach(point => path.lineTo(point.x, point.y, point.z))
path.closePath()

return path.getPoints()
}, [props.vertices])

return (
<line>
<bufferGeometry attach="geometry" setFromPoints={vertices} />
<lineBasicMaterial attach="material" color="black" />
</line>
)
}


ReactDOM.render(
<Canvas>
<LinePath vertices={[new THREE.Vector3(0, 0, 0), new THREE.Vector3(2, 2, 0), new THREE.Vector3(-2, 2, 0)]} />
</Canvas>,
document.getElementById('root')
)

但是根本没有输出/错误。我想我完全误解了 react-three-fiber 的 API。我在这里做错了什么?谢谢,这里是 sandbox

最佳答案

所以我真的想通了。我要找的是useUpdate钩子(Hook)允许我们调用任何给定 ref 的方法。所以这就是需要做的:

import { Canvas, useUpdate } from 'react-three-fiber'

function LinePath(props) {
const vertices = ...

const ref = useUpdate(geometry => {
geometry.setFromPoints(vertices)
}, [])

return (
<line>
<bufferGeometry attach="geometry" ref={ref} />
...
</line>
)
}

关于javascript - bufferGeometry setFromPoints 与 react-three-fiber,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58009236/

41 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com