Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed alignment issues #298

Merged
merged 2 commits into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When this is merged, please update #242 to note that this check is here.

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
19 changes: 16 additions & 3 deletions lib/mergeBuffers.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,21 @@ function mergeBuffers(gltf, bufferId) {
bufferView.buffer = bufferId;
}
}
buffersToMerge.push(buffer.extras._pipeline.source);
lengthSoFar += buffer.extras._pipeline.source.length;

// We check that each buffer is 4 byte aligned. Float buffers must start on a 4-byte boundary
// so merging in a buffer that isn't 4-byte aligned requires that padding be added.
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 +98,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