Skip to content

Commit

Permalink
Better buffer handling
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Mar 14, 2017
1 parent 998dcfe commit 15354eb
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions lib/gltf.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,9 @@ function createGltf(objData) {
}

var vertexBuffers = [];
var vertexByteOffset = 0;
var vertexBufferByteOffset = 0;
var indexBuffers = [];
var indexBuffersByteOffset = 0;
var indexBufferByteOffset = 0;
var accessorCount = 0;

function addVertexAttribute(array, components) {
Expand All @@ -157,7 +157,7 @@ function createGltf(objData) {
var type = (components === 3 ? 'VEC3' : 'VEC2');
var accessor = {
bufferView : vertexBufferViewId,
byteOffset : vertexByteOffset,
byteOffset : vertexBufferByteOffset,
byteStride : 0,
componentType : WebGLConstants.FLOAT,
count : count,
Expand All @@ -166,7 +166,7 @@ function createGltf(objData) {
type : type
};

vertexByteOffset += buffer.length;
vertexBufferByteOffset += buffer.length;
vertexBuffers.push(buffer);
var accessorId = 'accessor_' + accessorCount++;
gltf.accessors[accessorId] = accessor;
Expand All @@ -180,7 +180,7 @@ function createGltf(objData) {
var minMax = array.getMinMax(1);
var accessor = {
bufferView : indexBufferViewId,
byteOffset : indexBuffersByteOffset,
byteOffset : indexBufferByteOffset,
byteStride : 0,
componentType : componentType,
count : length,
Expand All @@ -189,7 +189,7 @@ function createGltf(objData) {
type : 'SCALAR'
};

indexBuffersByteOffset += buffer.length;
indexBufferByteOffset += buffer.length;
indexBuffers.push(buffer);

var accessorId = 'accessor_' + accessorCount++;
Expand Down Expand Up @@ -300,9 +300,13 @@ function createGltf(objData) {
}
}

var vertexBuffer = Buffer.concat(vertexBuffers);
var indexBuffer = Buffer.concat(indexBuffers);
var buffer = Buffer.concat([vertexBuffer, indexBuffer]);
var buffers = [];
buffers = buffers.concat(vertexBuffers, indexBuffers);
var buffer = Buffer.concat(buffers);

// Unload resources
vertexBuffers = undefined;
indexBuffers = undefined;

// Buffers larger than ~192MB cannot be base64 encoded due to a NodeJS limitation. Source: https://github.com/nodejs/node/issues/4266
var bufferUri;
Expand All @@ -317,15 +321,15 @@ function createGltf(objData) {

gltf.bufferViews[vertexBufferViewId] = {
buffer : bufferId,
byteLength : vertexBuffer.length,
byteLength : vertexBufferByteOffset,
byteOffset : 0,
target : WebGLConstants.ARRAY_BUFFER
};

gltf.bufferViews[indexBufferViewId] = {
buffer : bufferId,
byteLength : indexBuffer.length,
byteOffset : vertexBuffer.length,
byteLength : indexBufferByteOffset,
byteOffset : vertexBufferByteOffset,
target : WebGLConstants.ELEMENT_ARRAY_BUFFER
};

Expand Down

0 comments on commit 15354eb

Please sign in to comment.