gpt4 book ai didi

javascript - Three.js 中的 BoxBufferGeometry 与 BoxGeometry 有什么区别?

转载 作者:数据小太阳 更新时间:2023-10-29 06:04:39 29 4
gpt4 key购买 nike

我正在学习 Three.js。我找不到关于 BoxBufferGeometry 与 BoxGeometry 之间区别的正确答案。帮助我。

最佳答案

[Primitive]Geometry 类是操作友好的,内存不友好的所有 JS 几何类。

这意味着定义此几何的每条数据都存储为某个类的实例(Vector3Vector2Face3 ) 等等。这些都带有方便的方法,所以你可以用一些其他向量点一个顶点,平移顶点,修改 uv,修改法线等等。但它在内存和性能方面有开销(创建所有这些实例,存储它们)。

[Primitive]BufferGeometry 类是性能友好的几何类,它依赖类型化数组以 WebGL 友好格式存储数据。

这意味着顶点不是 Vector3 的数组而是类型化数组:

Array[v0,v1... vN]
vs:
Float32Array[v0x,v0y,v0z,v1x,v1y,v1z... vNx,vNy,vNz]

它们的存储效率更高,但更难操作。

如果你想修改一个顶点:

使用几何

//with Geometry you just get vertex 5 and have access to it's x...
//AND the methods of the class -> Vector3.add(Vector3)
myGeom.vertices[5].add(new THREE.Vector3(1,2,3))

使用BufferGeometry

//xyz are just numbers, so with a stride of 3
//we select x , and then the next two for y and z
//we have to know that the 15th number in this array is x of vertex 5...
const stride = 3
const index = 5
let offset = index * stride
myGeom.attributes.position.array[offset++] += 1
myGeom.attributes.position.array[offset++] += 2
myGeom.attributes.position.array[offset ] += 3

但是

THREE.BufferAttribute 确实有几种方法可以从该数组写入和读取内容。它仍然更加冗长:

//add x: 1 y: 2 z: 3 to 5th vertex

const index = 5
const attribute = myGeometry.attributes.position
const v3add = new THREE.Vector3(1,2,3)

attribute.setXYZ(
index,
attribute.getX(index) + v3add.x,
attribute.getY(index) + v3add.y,
attribute.getZ(index) + v3add.z
)

关于javascript - Three.js 中的 BoxBufferGeometry 与 BoxGeometry 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49956422/

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