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

Hit*_*dap 5 javascript animation three.js jquery-animate

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

pai*_*ead 8

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

这意味着,每个定义此几何一块数据存储为一些类的实例(Vector3Vector2Face3)等,这些配有方便的方法,这样就可以点一个顶点与其他一些矢量,平移顶点,修改的UV,修改常态等。但是它在内存和性能上都有开销(创建所有这些实例,存储它们)。

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

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

Array[v0,v1... vN]
vs:
Float32Array[v0x,v0y,v0z,v1x,v1y,v1z... vNx,vNy,vNz] 
Run Code Online (Sandbox Code Playgroud)

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

如果要修改顶点:

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))
Run Code Online (Sandbox Code Playgroud)

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 
Run Code Online (Sandbox Code Playgroud)

然而

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
)
Run Code Online (Sandbox Code Playgroud)