-
Notifications
You must be signed in to change notification settings - Fork 250
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
Changes from 1 commit
19b3982
88162d0
409b61e
837acf8
26b4fa5
a4d9ec2
f114b5c
98f2eca
426068e
172d5ce
b64e878
3d507cd
ba0cf72
10bbafa
2142790
ec98e3c
25a391b
89491aa
0d002a6
663ed31
e973bb3
1a0a912
a61dc0c
e53a92a
aaf55d2
22079db
8a3706d
a00c6ff
64be9c7
061a81d
99ef424
64777c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,15 +13,84 @@ var writeAccessor = require('./writeAccessor'); | |
|
||
module.exports = generateNormals; | ||
|
||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove. |
||
|
@@ -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); | ||
|
@@ -146,7 +188,7 @@ function generateNormals(gltf) { | |
byteOffset : 0, | ||
byteStride : 0, | ||
componentType : WebGLConstants.FLOAT, | ||
count : indicesLength, | ||
count : normalsLength, | ||
type : 'VEC3' | ||
}; | ||
gltf.accessors[normalAccessorId] = normalAccessor; | ||
|
@@ -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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,10 +59,11 @@ function processJSON(gltf, options, callback) { | |
} | ||
|
||
function processJSONWithExtras(gltfWithExtras, options, callback) { | ||
var hard = defaultValue(options.hardNormals, false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this. |
||
var stats = new OptimizationStatistics(); | ||
addDefaults(gltfWithExtras, stats); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this. |
||
if (options.printStats) { | ||
printStats(stats); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove extra line.