首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >带react-three-fiber的bufferGeometry setFromPoints

带react-three-fiber的bufferGeometry setFromPoints
EN

Stack Overflow用户
提问于 2019-09-19 18:42:45
回答 1查看 3.5K关注 0票数 3

考虑toLinePath函数:

代码语言:javascript
复制
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重新创建它,并尝试如下所示:

代码语言:javascript
复制
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的应用编程接口。我在这里做错了什么?谢谢,这是sandbox

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-19 20:16:49

所以我真的搞清楚了。我正在寻找的是useUpdate钩子,它允许我们调用任何给定ref的方法。所以这就是我们需要做的:

代码语言:javascript
复制
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>
  )
}
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58009236

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档