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

Added generateNormals stage #82

Merged
merged 32 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
19b3982
Added generateNormals stage
lilleyse Jun 13, 2016
88162d0
Fix test
lilleyse Jun 14, 2016
409b61e
Begin to use geometryPipeline
Jun 28, 2016
837acf8
Merge branch 'master' of github.com:AnalyticalGraphicsInc/gltf-pipeli…
lasalvavida Jul 1, 2016
26b4fa5
Close but not perfect
lasalvavida Jul 1, 2016
a4d9ec2
Fix test
lasalvavida Jul 1, 2016
f114b5c
Handle 'hard' normal generation
Jul 6, 2016
98f2eca
Semicolon
Jul 6, 2016
426068e
Merge remote-tracking branch 'origin' into generate-normals
Jul 6, 2016
172d5ce
split primitives
Jul 7, 2016
b64e878
Add to pipeline, cleanup
Jul 11, 2016
3d507cd
Merge master
Jul 11, 2016
ba0cf72
Add spec data
Jul 11, 2016
10bbafa
Merge my-generate normals
Jul 11, 2016
2142790
Uncomment tests
Jul 11, 2016
ec98e3c
Merge master
Jul 11, 2016
25a391b
Remove unnecessary models. Fix jsHint errors
Jul 11, 2016
89491aa
Missed a line
Jul 11, 2016
0d002a6
Address comments
Jul 13, 2016
663ed31
Merge master
Jul 13, 2016
e973bb3
One day I will consistently remember to check jsHint before committing
Jul 13, 2016
1a0a912
Merge branch 'master' into generate-normals
Jul 14, 2016
a61dc0c
Merge master, fix small, unrelated spec issue
Jul 14, 2016
e53a92a
Fix overwriting issue
Jul 15, 2016
aaf55d2
parseInt so I'm not extending strings in getIdNumber
Jul 15, 2016
22079db
Remove unused code
Jul 18, 2016
8a3706d
Remove more duplicate code
Jul 18, 2016
a00c6ff
Move generateNormals to after combine stages, resolves above issues
Jul 18, 2016
64be9c7
Merge branch 'master' into generate-normals
Jul 18, 2016
061a81d
Spec fix
Jul 18, 2016
99ef424
Fix test I butchered
Jul 18, 2016
64777c4
Address above comments
Jul 19, 2016
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
Prev Previous commit
Next Next commit
Handle 'hard' normal generation
  • Loading branch information
JoshuaStorm committed Jul 6, 2016
commit f114b5cfd5a783a79431a10c368824b4280639cf
132 changes: 87 additions & 45 deletions lib/generateNormals.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,84 @@ var writeAccessor = require('./writeAccessor');

module.exports = generateNormals;


Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove extra line.

function hardNormals(oldPositions, indices) {
// Unpack reused positions and find center
var indicesLength = indices.length;
var positions = new Array(indicesLength);
var normals = new Array(indicesLength);
var maxIndex = 0;
var index;
var i;
for (i = 0; i < indicesLength; i++) {
index = indices[i];
maxIndex = Math.max(maxIndex, index);
}
var seenIndices = {};
var nextIndex = maxIndex + 1;
for (i = 0; i < indicesLength; i++) {
index = indices[i];
var position = oldPositions[index];
if (defined(seenIndices[index])) {
positions[nextIndex] = position;
indices[i] = nextIndex;
nextIndex++;
} else {
positions[index] = position;
seenIndices[index] = true;
}
}

// Add face normal to each vertex normal
for (i = 0; i < indicesLength; i += 3) {
var index1 = indices[i];
var index2 = indices[i + 1];
var index3 = indices[i + 2];
var faceNormal = getFaceNormal(positions[index1], positions[index2], positions[index3]);
normals[index1] = faceNormal;
normals[index2] = faceNormal.clone();
normals[index3] = faceNormal.clone();
}
return {
normals : normals,
positions : positions
};
}

function softNormals(positions, indices) {
// Generate one normal for each position
var normalsLength = positions.length;
var trianglesLength = indices.length / 3;

// Initialize the normals to 0
var normals = new Array(normalsLength);
for (var i = 0; i < normalsLength; i++) {
normals[i] = new Cartesian3();
}

// Add face normal to each vertex normal
for (i = 0; i < trianglesLength; ++i) {
var index1 = indices[i * 3 + 0];
var index2 = indices[i * 3 + 1];
var index3 = indices[i * 3 + 2];
var faceNormal = getFaceNormal(positions[index1], positions[index2], positions[index3]);
Cartesian3.add(normals[index1], faceNormal, normals[index1]);
Cartesian3.add(normals[index2], faceNormal, normals[index2]);
Cartesian3.add(normals[index3], faceNormal, normals[index3]);
}

return normals
}

/**
* Generates normals for primitives if they do not exist.
*
* @param gltf
* @param {boolean} isHard
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove this.

*/
function generateNormals(gltf) {
function generateNormals(gltf, isHard) {
var i;
var j;
var index;
var position;
var normal;
var generatedNormals = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove.

Expand All @@ -37,57 +106,30 @@ function generateNormals(gltf) {
var indicesAccessor = gltf.accessors[indicesAccessorId];
var positionAccessor = gltf.accessors[positionAccessorId];
var indices = readAccessor(gltf, indicesAccessor).data;
var oldPositions = readAccessor(gltf, positionAccessor).data;

// Unpack reused positions and find center
var indicesLength = indices.length;
var positions = new Array(indicesLength);
var normals = new Array(indicesLength);
var maxIndex = 0;
for (j = 0; j < indicesLength; j++) {
index = indices[j];
maxIndex = Math.max(maxIndex, index);
}
var seenIndices = {};
var nextIndex = maxIndex + 1;
var center = new Cartesian3();
for (j = 0; j < indicesLength; j++) {
index = indices[j];
position = oldPositions[index];
if (defined(seenIndices[index])) {
positions[nextIndex] = position;
indices[j] = nextIndex;
nextIndex++;
} else {
positions[index] = position;
seenIndices[index] = true;
}
Cartesian3.add(center, position, center);
}
Cartesian3.divideByScalar(center, indicesLength, center);

// Add face normal to each vertex normal
for (j = 0; j < indicesLength; j += 3) {
var index1 = indices[j];
var index2 = indices[j + 1];
var index3 = indices[j + 2];
var faceNormal = getFaceNormal(positions[index1], positions[index2], positions[index3]);
normals[index1] = faceNormal;
normals[index2] = faceNormal.clone();
normals[index3] = faceNormal.clone();
var positions = readAccessor(gltf, positionAccessor).data;
var normals;


if (isHard) {
var results = hardNormals(positions, indices);
normals = results.normals;
positions = results.positions;
} else {
normals = softNormals(positions, indices);
}

// Normalize the normals
for (j = 0; j < indicesLength; ++j) {
var normalsLength = normals.length;
for (j = 0; j < normalsLength; ++j) {
normal = normals[j];
Cartesian3.normalize(normal, normal);
}

// Place positions and normals into a buffer
var bufferLength = indicesLength * 3 * 4;
var bufferLength = normalsLength * 3 * 4;
var normalsBuffer = new Buffer(bufferLength);
var positionBuffer = new Buffer(bufferLength);
for (j = 0; j < indicesLength; ++j) {
for (j = 0; j < normalsLength; ++j) {
normal = normals[j];
normalsBuffer.writeFloatLE(normal.x, (j * 3 + 0) * 4);
normalsBuffer.writeFloatLE(normal.y, (j * 3 + 1) * 4);
Expand Down Expand Up @@ -146,7 +188,7 @@ function generateNormals(gltf) {
byteOffset : 0,
byteStride : 0,
componentType : WebGLConstants.FLOAT,
count : indicesLength,
count : normalsLength,
type : 'VEC3'
};
gltf.accessors[normalAccessorId] = normalAccessor;
Expand All @@ -156,7 +198,7 @@ function generateNormals(gltf) {
byteOffset : 0,
byteStride : 0,
componentType : WebGLConstants.FLOAT,
count : indicesLength,
count : positions.length,
type : 'VEC3'
};
gltf.accessors[positionAccessorId] = positionAccessor;
Expand Down
3 changes: 2 additions & 1 deletion lib/gltfPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,11 @@ function processJSON(gltf, options, callback) {
}

function processJSONWithExtras(gltfWithExtras, options, callback) {
var hard = defaultValue(options.hardNormals, false);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove this.

var stats = new OptimizationStatistics();
addDefaults(gltfWithExtras, stats);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did this change? I'm pretty sure this should still be options.

removeUnused(gltfWithExtras, stats);
generateNormals(gltfWithExtras);
generateNormals(gltfWithExtras, hard);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Remove this.

if (options.printStats) {
printStats(stats);
}
Expand Down