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

What is difference between BoxBufferGeometry vs BoxGeometry in Three.js?

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

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

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

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

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

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

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

如果要修改顶点:

Geometry

//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
)