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

Naive AO is ready for a first look! #96

Closed
wants to merge 8 commits into from
Prev Previous commit
addressed some PR issues: cleaned up AO specs and gltfPipeline integr…
…ation
  • Loading branch information
likangning93 committed Jun 21, 2016
commit 51ccfd1537263b2f14154b6b966ab0d97eabd814
18 changes: 10 additions & 8 deletions bin/gltf-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ var separate = defaultValue(defaultValue(argv.s, argv.separate), false);
var separateImage = defaultValue(defaultValue(argv.t, argv.separateImage), false);
var quantize = defaultValue(defaultValue(argv.q, argv.quantize), false);

var aoOptions = {
runAO : defaultValue(argv.ao_diffuse, false),
scene : argv.ao_scene,
rayDepth : defaultValue(argv.ao_rayDepth, 1.0),
resolution : defaultValue(argv.ao_resolution, 128),
numberSamples : defaultValue(argv.ao_samples, 16)
};
var aoOptions;
if (argv.ao_diffuse) {
aoOptions = {
scene : argv.ao_scene,
rayDepth : defaultValue(argv.ao_rayDepth, 1.0),
resolution : defaultValue(argv.ao_resolution, 128),
numberSamples : defaultValue(argv.ao_samples, 16)
};
}

if (!defined(gltfPath)) {
throw new DeveloperError('Input path is undefined.');
Expand All @@ -64,7 +66,7 @@ var options = {
embedImage : !separateImage,
quantize : quantize,
aoOptions : aoOptions,
imageProcess : aoOptions.runAO
imageProcess : defined(aoOptions)
};

processFileToDisk(gltfPath, outputPath, options);
34 changes: 16 additions & 18 deletions lib/bakeAmbientOcclusion.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,25 @@ var quaternionScratch = new Quaternion();
var matrix3Scratch = new Matrix3();

function bakeAmbientOcclusion(gltf, options) {
if (defined(options) && options.runAO) {
// Requires each mesh to occur only once in the scene
var sceneID = defaultValue(options.scene, gltf.scene);
if (!defined(sceneID)) {
sceneID = Object.keys(gltf.scenes)[0];
}
var scene = gltf.scenes[sceneID];
// Requires each mesh to occur only once in the scene
var sceneID = defaultValue(options.scene, gltf.scene);
if (!defined(sceneID)) {
sceneID = Object.keys(gltf.scenes)[0];
}
var scene = gltf.scenes[sceneID];

// Generate triangle soup
var raytracerScene = generateRaytracerScene(gltf, scene, options);
// Generate triangle soup
var raytracerScene = generateRaytracerScene(gltf, scene, options);

// Raytrace to a new texture for each primitive
var parameters = {
raytracerScene: raytracerScene,
resolution: options.resolution
};
NodeHelpers.forEachPrimitiveInScene(gltf, scene, raytracePrimitiveTexels, parameters);
// Raytrace to a new texture for each primitive
var parameters = {
raytracerScene: raytracerScene,
resolution: options.resolution
};
NodeHelpers.forEachPrimitiveInScene(gltf, scene, raytracePrimitiveTexels, parameters);

// Add to the gltf
bakeToDiffuse(gltf, scene, options.resolution, raytracerScene);
}
// Add to the gltf
bakeToDiffuse(gltf, scene, options.resolution, raytracerScene);
}

////////// adding to the gltf //////////
Expand Down
6 changes: 5 additions & 1 deletion lib/gltfPipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var cacheOptimizeIndices = require('./cacheOptimizeIndices');
var encodeImages = require('./encodeImages');
var Cesium = require('cesium');
var defaultValue = Cesium.defaultValue;
var defined = Cesium.defined;
var bakeAmbientOcclusion = require('./bakeAmbientOcclusion').bakeAmbientOcclusion;

module.exports = {
Expand Down Expand Up @@ -53,7 +54,10 @@ function processJSONWithExtras(gltfWithExtras, options, callback) {
convertDagToTree(gltfWithExtras);
combineMeshes(gltfWithExtras);
combinePrimitives(gltfWithExtras);
bakeAmbientOcclusion(gltfWithExtras, options.aoOptions);

if (defined(options.aoOptions)) {
bakeAmbientOcclusion(gltfWithExtras, options.aoOptions);
}

// Merging duplicate vertices again to prevent repeat data in newly combined primitives
mergeDuplicateVertices(gltfWithExtras);
Expand Down
58 changes: 9 additions & 49 deletions specs/lib/bakeAmbientOcclusionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,11 @@
var Cesium = require('cesium');
var CesiumMath = Cesium.Math;
var Cartesian3 = Cesium.Cartesian3;

var fs = require('fs');
var path = require('path');
var clone = require('clone');
var loadGltfUris = require('../../lib/loadGltfUris');
var addPipelineExtras = require('../../lib/addPipelineExtras');
var bakeAmbientOcclusion = require('../../lib/bakeAmbientOcclusion');
var readAccessor = require('../../lib/readAccessor');
var Jimp = require('jimp');
var clone = require('clone');
var NodeHelpers = require('../../lib/NodeHelpers');
var readGltf = require('../../lib/readGltf');

var boxGltfPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.gltf';
var boxOverGroundGltfPath = './specs/data/ambientOcclusion/cube_over_ground.gltf';

function cloneGltfWithJimps(gltf) {
Expand All @@ -30,19 +23,12 @@ function cloneGltfWithJimps(gltf) {
image.extras._pipeline.jimpImage = originalJimp.clone();
}
}

return gltfClone;
}

describe('bakeAmbientOcclusion', function() {
var boxGltf;
var boxOverGroundGltf;

var loadGltfUriOptions = {
basePath: path.dirname(boxGltfPath),
imageProcess: true
};

var indices = [0,1,2,0,2,3];
var indicesBuffer = new Buffer(indices.length * 2);
for (var i = 0; i < indices.length; i++) {
Expand Down Expand Up @@ -196,38 +182,12 @@ describe('bakeAmbientOcclusion', function() {
};

beforeAll(function(done) {
var loadModel2 = function () {
fs.readFile(boxOverGroundGltfPath, function (err, data) {
if (err) {
throw err;
}
else {
boxOverGroundGltf = JSON.parse(data);
addPipelineExtras(boxOverGroundGltf);
loadGltfUris(boxOverGroundGltf, loadGltfUriOptions, function (err, gltf) {
if (err) {
throw err;
}
done();
});
}
});
var options = {
imageProcess: true
};

fs.readFile(boxGltfPath, function(err, data) {
if (err) {
throw err;
}
else {
boxGltf = JSON.parse(data);
addPipelineExtras(boxGltf);
loadGltfUris(boxGltf, loadGltfUriOptions, function(err, gltf) {
if (err) {
throw err;
}
loadModel2();
});
}
readGltf(boxOverGroundGltfPath, options, function(gltf) {
boxOverGroundGltf = gltf;
done();
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Hm just noticed, this area could be simplified by using readGltf if that works.

});
Copy link
Contributor

Choose a reason for hiding this comment

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

Should only call done() when both are done.


Expand Down Expand Up @@ -331,7 +291,7 @@ describe('bakeAmbientOcclusion', function() {
count: new Array(4).fill(0.0)
};

var bottomCenter = new Cartesian3(0.0, -0.99, 0.0);
var bottomCenter = new Cartesian3(0.0, -1.0, 0.0);

var texelPoints = [
{
Expand Down Expand Up @@ -364,7 +324,7 @@ describe('bakeAmbientOcclusion', function() {
var samples = aoBuffer.samples;

expect(samples[0]).toEqual(16);
expect(samples[1] < 4).toEqual(true); // randomized, but stratification should ensure this.
expect(samples[1]).toEqual(0); // randomized, but stratification should ensure this.
expect(samples[2] > 6 && samples[2] < 10).toEqual(true); // randomized, but stratification should ensure this.
});

Expand Down