Expand vertex, normal and uv indices into vertex normal and uv data that is ready for your WebGL element and array buffers
3d model file formats will typically encoded their position, normal and uv data across three separate position, normal and uv indices. This is great for maintaining a compact file, but poses a problem when you want to buffer your data onto the GPU.
WebGL supports one ELEMENT_ARRAY_BUFFER, so having three indexes for your vertex data is not an option.
expand-vertex-data makes your vertex data accessible using only one index (
vertexPositionIndices) by creating new indices and
duplicating data into those indices where necessary.
You only need to do this once, before creating your
You'll typically want to do this at runtime to save download time and disk space (at the one time expense of some CPU cycles).
In some cases you may wish to expand this data before runtime via a script to save CPU cycles (at the expense of persisting your larger expanded 3d model data to disk)
$ npm install --save expand-vertex-data
var fs =var colladaFile = fsvar wavefrontFile = fs// Note, you can use a different parser, you'll just need to move your// parsed data around to make sure that it conforms to `expand-vertex-data`'s// expected formatvar parsedCollada = colladaFilevar parsedWavefront = wavefrontFile// Pass this data into your ELEMENT_ARRAY_BUFFER and ARRAY_BUFFERSvar expandedCollada =var expandedWavefront =
See something broken, confusing or ripe for improvement? Feel free to open an issue or PR!
expandVertexData(compressedVertexData, opts) ->
An object containing your model's vertex data. If you're using
you can pass their returned data right in.
If you're using your own parser, you're still all good. Just rename your parsed properties accordingly before passing them into
Use this when working with JSON that came from
Array lengths described below,
n is the number of vertexPositionIndices that were returned after expansion
n will often times be more than the number of indices that you passed in.
This is because more position indices will be created if your data needs to be expanded.
Array[n * 3]
Array[n * 3]
Array[n * 2]
Array[n * 4]
There are four joint influences per vertex, even if there aren't four influencing joints. In cases where there is no joint there will be a weight of zero. This is because you need every joint to have the same number of weights when you vertex shader attributes.
Array[n * 4]
An array of joint weights. A weight is a number between
1 that signifies how
much the corresponding joint should affect the corresponding vertex.
There are four weights per vertex, even if there aren't four influencing joints. In cases where there is no joint there will be a weight of zero. This is because you need every joint to have the same number of weights when you vertex shader attributes.
- Support 3 weights per bone
- Are we expanding as minimally as possible? Can our output be smaller?
$ npm run test