合并顶点缓冲区

Combining vertex buffers

我有一个问题,问题是顶点数组单独存储在目标文件中,所以它们看起来像那样(不是真实数据):

positions: [
  1, 0.5, 0.3,
  3.0, 0.5, 0.3,
  0.3, 0.5, 0.3,
  -0.4, 0.5, 0.2
],
normals: [
  0.5, 0.1, 0.3,
  0.2, 0.5, 0.2,
  0.3, 0.1, 0.3,
  0.6, 0.3, 0.7
],
uv: [
  0.3, 0.2,
  0.1, 0.1,
  0.7, 0.6,
  0.4, 0.3
]

问题是我不能将它存储在单个顶点缓冲区中,所以我应该有这样的东西:

vertices: [
  1, 0.5, 0.3, 0.5, 0.1, 0.3, 0.3, 0.2,
  3.0, 0.5, 0.3, 0.2, 0.5, 0.2, 0.1, 0.1,
  0.3, 0.5, 0.3, 0.3, 0.1, 0.3, 0.7, 0.6,
  -0.4, 0.5, 0.2, 0.6, 0.3, 0.7, 0.4, 0.3
]
layout: [["float", 3], ["float", 3], ["float", 2]]

例如连接每个数组的每一行,以便我可以在顶点数组中使用它

是否有任何算法或至少,如何调用该过程来知道在哪里挖掘

以下应该有效:

function interleaveVertices(spec, sources) {
  const vertices = [];
  while (sources[0].length > 0) {
    for (let i = 0; i < spec.length; i++) {
      Array.prototype.push.apply(vertices, sources[i].splice(0, spec[i]));
    }
  }
  return { vertices: vertices, layout: spec.map(s => ["float", s]) };
}

调用为

interleaveVertices([3,3,2], [data.positions, data.normals, data.uv]);