Skip to content

Commit

Permalink
Fixed issues with merging buffers that weren't a multiple of 4. Also …
Browse files Browse the repository at this point in the history
…fixed generate face normals to not crash on degenerate triangles.
  • Loading branch information
Tom Fili committed Jul 6, 2017
1 parent cb7af37 commit d445114
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
20 changes: 18 additions & 2 deletions lib/generateNormals.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ var clone = require('clone');
var Cartesian3 = Cesium.Cartesian3;
var GeometryPipeline = Cesium.GeometryPipeline;
var WebGLConstants = Cesium.WebGLConstants;
var CesiumMath = Cesium.Math;
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;

Expand Down Expand Up @@ -127,8 +128,23 @@ function generateFaceNormals(gltf, primitive, positionSemantic) {
Cartesian3.subtract(positionTwo, positionOne, scratchNormalOne);
Cartesian3.subtract(positionThree, positionOne, scratchNormalTwo);
var normal = new Cartesian3();
Cartesian3.cross(scratchNormalOne, scratchNormalTwo, normal);
Cartesian3.normalize(normal, normal);

// Check for degenerate triangles
if (scratchNormalOne.equals(Cartesian3.ZERO) || scratchNormalTwo.equals(Cartesian3.ZERO)) {
Cartesian3.clone(Cartesian3.UNIT_X, normal);
} else {
Cartesian3.normalize(scratchNormalOne, scratchNormalOne);
Cartesian3.normalize(scratchNormalTwo, scratchNormalTwo);

// Make sure normals aren't parallel
var dot = Math.abs(Cartesian3.dot(scratchNormalOne, scratchNormalTwo));
if (CesiumMath.equalsEpsilon(dot, 1.0, CesiumMath.EPSILON15)) {
Cartesian3.clone(Cartesian3.UNIT_X, normal);
} else {
Cartesian3.cross(scratchNormalOne, scratchNormalTwo, normal);
Cartesian3.normalize(normal, normal);
}
}
generatedNormals.push(normal);
generatedNormals.push(normal);
generatedNormals.push(normal);
Expand Down
17 changes: 14 additions & 3 deletions lib/mergeBuffers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,19 @@ function mergeBuffers(gltf, bufferId) {
bufferView.buffer = bufferId;
}
}
buffersToMerge.push(buffer.extras._pipeline.source);
lengthSoFar += buffer.extras._pipeline.source.length;

var sourceBuffer = buffer.extras._pipeline.source;
var currentBufferLength = sourceBuffer.length;
if (currentBufferLength % 4)
{
currentBufferLength += 4 - (currentBufferLength % 4);
var newBuffer = Buffer.alloc(currentBufferLength);
sourceBuffer.copy(newBuffer);
sourceBuffer = buffer.extras._pipeline.source = newBuffer;
}

buffersToMerge.push(sourceBuffer);
lengthSoFar += currentBufferLength;
}
}

Expand Down Expand Up @@ -85,4 +96,4 @@ function getBufferViewsForBuffers(gltf) {
}
}
return bufferViewsForBuffers;
}
}
6 changes: 3 additions & 3 deletions specs/lib/mergeBuffersSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('mergeBuffers', function() {
it('merges buffers', function() {
var buffer0 = Buffer.from([1, 2]);
var buffer1 = Buffer.from([3, 4, 5]);
var bufferMerged = Buffer.from([1, 2, 3, 4, 5]);
var bufferMerged = Buffer.from([1, 2, 0, 0, 3, 4, 5, 0]);
var gltf = {
"bufferViews": {
"bufferView_0": {
Expand Down Expand Up @@ -58,12 +58,12 @@ describe('mergeBuffers', function() {
"bufferView_1": {
"buffer": "mergedBuffers",
"byteLength": 3,
"byteOffset": 2
"byteOffset": 4
}
},
"buffers": {
"mergedBuffers": {
"byteLength": 5,
"byteLength": 8,
"type": "arraybuffer",
"uri": "data:,",
"extras": {
Expand Down

0 comments on commit d445114

Please sign in to comment.