Skip to content

Commit

Permalink
Merge pull request #184 from AnalyticalGraphicsInc/transform-oct-normals
Browse files Browse the repository at this point in the history
Transform oct-encoded normals
  • Loading branch information
lilleyse authored Nov 21, 2016
2 parents d4a7c0c + 980b053 commit fd7011b
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion lib/PrimitiveHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ var AccessorReader = require('./AccessorReader');
var getPrimitiveAttributeSemantics = require('./getPrimitiveAttributeSemantics');
var readAccessor = require('./readAccessor');

var AttributeCompression = Cesium.AttributeCompression;
var Cartesian3 = Cesium.Cartesian3;
var Matrix4 = Cesium.Matrix4;
var WebGLConstants = Cesium.WebGLConstants;
var defined = Cesium.defined;

module.exports = {
Expand Down Expand Up @@ -96,9 +98,12 @@ function transformPrimitives(gltf, primitives, transform) {
var normalAccessorReader;
var normalSemantics = getPrimitiveAttributeSemantics(primitive, 'NORMAL');
var normalAccessorId = attributes[normalSemantics[0]];
var isOctEncoded = false;
if (normalSemantics.length > 0) {
doneIndicesByAccessor[normalAccessorId] = {};
normalAccessorReader = new AccessorReader(gltf, accessors[normalAccessorId]);
var normalAccessor = accessors[normalAccessorId];
normalAccessorReader = new AccessorReader(gltf, normalAccessor);
isOctEncoded = normalAccessor.type === 'VEC2';
}
var keepReading = true;
while (keepReading) {
Expand All @@ -115,8 +120,26 @@ function transformPrimitives(gltf, primitives, transform) {
normalAccessorReader.index = index;
normalAccessorReader.read(scratchCartesianArray);
Cartesian3.unpack(scratchCartesianArray, 0, scratchCartesian);
if (isOctEncoded) {
// Un-encode oct-encoded normals
if (normalAccessorReader.componentType === WebGLConstants.BYTE) {
// Normalize if these are written to signed bytes
scratchCartesian.x += 128;
scratchCartesian.y += 128;
}
AttributeCompression.octDecode(scratchCartesian.x, scratchCartesian.y, scratchCartesian);
}
Matrix4.multiplyByPointAsVector(inverseTranspose, scratchCartesian, scratchCartesian);
Cartesian3.normalize(scratchCartesian, scratchCartesian);
if (isOctEncoded) {
// Re-encode oct-encoded normals
AttributeCompression.octEncode(scratchCartesian, scratchCartesian);
if (normalAccessorReader.componentType === WebGLConstants.BYTE) {
// De-normalize back to signed bytes
scratchCartesian.x -= 128;
scratchCartesian.y -= 128;
}
}
Cartesian3.pack(scratchCartesian, scratchCartesianArray);
normalAccessorReader.write(scratchCartesianArray);
doneIndicesByAccessor[normalAccessorId][index] = true;
Expand Down

0 comments on commit fd7011b

Please sign in to comment.