Skip to content

Commit

Permalink
Enable lighting option for Models and 3D Tilesets
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed Jan 26, 2018
1 parent 248adb2 commit 2a0d36a
Show file tree
Hide file tree
Showing 14 changed files with 185 additions and 56 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Change Log
* Only one mesh per node is supported.
* Only one primitive per mesh is supported.
* Updated documentation links to reflect new locations on cesiumjs.org and cesium.com.
* Updated 'Viewer.zoomTo' and 'Viewer.flyTo' to take in Cesium3DTilesets as a target and updated sandcastle 3DTileset examples to reflect this change
* Updated `Viewer.zoomTo` and `Viewer.flyTo` to take in Cesium3DTilesets as a target and updated sandcastle 3DTileset examples to reflect this change
* Fixed a glTF animation bug that caused certain animations to jitter. [#5740](https://github.com/AnalyticalGraphicsInc/cesium/pull/5740)
* Fixed a bug when creating billboard and model entities without a globe. [#6109](https://github.com/AnalyticalGraphicsInc/cesium/pull/6109)
* Added support for vertex shader uniforms when `tileset.colorBlendMode` is `MIX` or `REPLACE`. [#5874](https://github.com/AnalyticalGraphicsInc/cesium/pull/5874)
Expand All @@ -40,6 +40,7 @@ Change Log
* Fixed sandcastle Particle System example for better visual [#6132](https://github.com/AnalyticalGraphicsInc/cesium/pull/6132)
* Fixed camera movement and look functions for 2D mode [#5884](https://github.com/AnalyticalGraphicsInc/cesium/issues/5884)
* Fixed discrepancy between default value used and commented value for default value for halfAxes of OrientedBoundingBox. [#6147](https://github.com/AnalyticalGraphicsInc/cesium/pull/6147)
* Added `enableLighting` option to `Cesium3DTileset` and `Model` to control whether the tileset or model is shaded based on the sun direction. This option is ignored if the model has built-in shaders. [#6160](https://github.com/AnalyticalGraphicsInc/cesium/pull/6160)

### 1.41 - 2018-01-02

Expand Down
1 change: 1 addition & 0 deletions Source/Scene/Batched3DModel3DTileContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ define([
shadows: tileset.shadows,
debugWireframe: tileset.debugWireframe,
incrementallyLoadTextures : false,
enableLighting : tileset._enableLighting,
vertexShaderLoaded : getVertexShaderCallback(content),
fragmentShaderLoaded : getFragmentShaderCallback(content),
uniformMapLoaded : batchTable.getUniformMapCallback(),
Expand Down
3 changes: 3 additions & 0 deletions Source/Scene/Cesium3DTileset.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ define([
* @param {Boolean} [options.show=true] Determines if the tileset will be shown.
* @param {Matrix4} [options.modelMatrix=Matrix4.IDENTITY] A 4x4 transformation matrix that transforms the tileset's root tile.
* @param {ShadowMode} [options.shadows=ShadowMode.ENABLED] Determines whether the tileset casts or receives shadows from each light source.
* @param {Boolean} [options.enableLighting=true] Enable lighting the tileset with the sun as a light source.
* @param {Number} [options.maximumScreenSpaceError=16] The maximum screen space error used to drive level of detail refinement.
* @param {Number} [options.maximumMemoryUsage=512] The maximum amount of memory in MB that can be used by the tileset.
* @param {Boolean} [options.cullWithChildrenBounds=true] Optimization option. Whether to cull tiles using the union of their children bounding volumes.
Expand Down Expand Up @@ -223,6 +224,8 @@ define([

this._modelMatrix = defined(options.modelMatrix) ? Matrix4.clone(options.modelMatrix) : Matrix4.clone(Matrix4.IDENTITY);

this._enableLighting = defaultValue(options.enableLighting, true);

this._statistics = new Cesium3DTilesetStatistics();
this._statisticsLastColor = new Cesium3DTilesetStatistics();
this._statisticsLastPick = new Cesium3DTilesetStatistics();
Expand Down
1 change: 1 addition & 0 deletions Source/Scene/Instanced3DModel3DTileContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ define([
url : undefined,
requestType : RequestType.TILES3D,
gltf : undefined,
enableLighting : content._tileset._enableLighting,
basePath : undefined,
incrementallyLoadTextures : false,
upAxis : content._tileset._gltfUpAxis,
Expand Down
4 changes: 4 additions & 0 deletions Source/Scene/Model.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ define([
* @param {Boolean} [options.asynchronous=true] Determines if model WebGL resource creation will be spread out over several frames or block until completion once all glTF files are loaded.
* @param {Boolean} [options.clampAnimations=true] Determines if the model's animations should hold a pose over frames where no keyframes are specified.
* @param {ShadowMode} [options.shadows=ShadowMode.ENABLED] Determines whether the model casts or receives shadows from each light source.
* @param {Boolean} [options.enableLighting=true] Enable lighting the model with the sun as a light source. This value is ignored if the glTF has built-in shaders.
* @param {Boolean} [options.debugShowBoundingVolume=false] For debugging only. Draws the bounding sphere for each draw command in the model.
* @param {Boolean} [options.debugWireframe=false] For debugging only. Draws the model in wireframe.
* @param {HeightReference} [options.heightReference] Determines how the model is drawn relative to terrain.
Expand Down Expand Up @@ -474,6 +475,7 @@ define([

this._defaultTexture = undefined;
this._incrementallyLoadTextures = defaultValue(options.incrementallyLoadTextures, true);
this._enableLighting = defaultValue(options.enableLighting, true);
this._asynchronous = defaultValue(options.asynchronous, true);

/**
Expand Down Expand Up @@ -1056,6 +1058,7 @@ define([
* @param {Boolean} [options.asynchronous=true] Determines if model WebGL resource creation will be spread out over several frames or block until completion once all glTF files are loaded.
* @param {Boolean} [options.clampAnimations=true] Determines if the model's animations should hold a pose over frames where no keyframes are specified.
* @param {ShadowMode} [options.shadows=ShadowMode.ENABLED] Determines whether the model casts or receives shadows from each light source.
* @param {Boolean} [options.enableLighting=true] Enable lighting the model with the sun as a light source. This value is ignored if the glTF has built-in shaders.
* @param {Boolean} [options.debugShowBoundingVolume=false] For debugging only. Draws the bounding sphere for each {@link DrawCommand} in the model.
* @param {Boolean} [options.debugWireframe=false] For debugging only. Draws the model in wireframe.
* @param {HeightReference} [options.heightReference] Determines how the model is drawn relative to terrain.
Expand Down Expand Up @@ -4102,6 +4105,7 @@ define([
if (!this._updatedGltfVersion) {
var options = {
optimizeForCesium: true,
enableLighting: this._enableLighting,
addBatchIdToGeneratedShaders : this._addBatchIdToGeneratedShaders
};
frameState.brdfLutGenerator.update(frameState);
Expand Down
3 changes: 3 additions & 0 deletions Source/Scene/ModelInstanceCollection.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ define([
* @param {Boolean} [options.asynchronous=true] Determines if model WebGL resource creation will be spread out over several frames or block until completion once all glTF files are loaded.
* @param {Boolean} [options.incrementallyLoadTextures=true] Determine if textures may continue to stream in after the model is loaded.
* @param {ShadowMode} [options.shadows=ShadowMode.ENABLED] Determines whether the collection casts or receives shadows from each light source.
* @param {Boolean} [options.enableLighting=true] Enable lighting the instances with the sun as a light source.
* @param {Boolean} [options.debugShowBoundingVolume=false] For debugging only. Draws the bounding sphere for the collection.
* @param {Boolean} [options.debugWireframe=false] For debugging only. Draws the instances in wireframe.
*
Expand Down Expand Up @@ -155,6 +156,7 @@ define([
this._asynchronous = options.asynchronous;
this._incrementallyLoadTextures = options.incrementallyLoadTextures;
this._upAxis = options.upAxis; // Undocumented option
this._enableLighting = defaultValue(options.enableLighting, true);

this.shadows = defaultValue(options.shadows, ShadowMode.ENABLED);
this._shadows = this.shadows;
Expand Down Expand Up @@ -605,6 +607,7 @@ define([
asynchronous : collection._asynchronous,
allowPicking : allowPicking,
incrementallyLoadTextures : collection._incrementallyLoadTextures,
enableLighting : collection._enableLighting,
upAxis : collection._upAxis,
precreatedAttributes : undefined,
vertexShaderLoaded : undefined,
Expand Down
10 changes: 8 additions & 2 deletions Source/Scene/PointCloud3DTileContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -818,6 +818,7 @@ define([
var backFaceCulling = content._backFaceCulling;
var vertexArray = content._drawCommand.vertexArray;
var clippingPlanes = content._tileset.clippingPlanes;
var enableLighting = content._tileset._enableLighting;

var colorStyleFunction;
var showStyleFunction;
Expand Down Expand Up @@ -1039,8 +1040,13 @@ define([
vs += ' color = color * u_highlightColor; \n';

if (hasNormals) {
vs += ' normal = czm_normal * normal; \n' +
' float diffuseStrength = czm_getLambertDiffuse(czm_sunDirectionEC, normal); \n' +
vs += ' normal = czm_normal * normal; \n';
if (enableLighting) {
vs += ' vec3 lightDirection = normalize(czm_sunDirectionEC);\n';
} else {
vs += ' vec3 lightDirection = vec3(0.0, 0.0, 1.0);\n';
}
vs += ' float diffuseStrength = czm_getLambertDiffuse(lightDirection, normal); \n' +
' diffuseStrength = max(diffuseStrength, 0.4); \n' + // Apply some ambient lighting
' color *= diffuseStrength; \n';
}
Expand Down
10 changes: 6 additions & 4 deletions Source/ThirdParty/GltfPipeline/processModelMaterialsCommon.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ define([
ForEach.material(gltf, function(material) {
if (defined(material.extensions) && defined(material.extensions.KHR_materials_common)) {
var khrMaterialsCommon = material.extensions.KHR_materials_common;
var techniqueKey = getTechniqueKey(khrMaterialsCommon);
var techniqueKey = getTechniqueKey(khrMaterialsCommon, options);
var technique = techniques[techniqueKey];
if (!defined(technique)) {
technique = generateTechnique(gltf, khrMaterialsCommon, lightParameters, options);
Expand Down Expand Up @@ -228,6 +228,7 @@ define([

function generateTechnique(gltf, khrMaterialsCommon, lightParameters, options) {
var optimizeForCesium = defaultValue(options.optimizeForCesium, false);
var enableLighting = defaultValue(options.enableLighting, true);
var hasCesiumRTCExtension = defined(gltf.extensions) && defined(gltf.extensions.CESIUM_RTC);
var addBatchIdToGeneratedShaders = defaultValue(options.addBatchIdToGeneratedShaders, false);

Expand Down Expand Up @@ -547,7 +548,7 @@ define([
}

if (!hasNonAmbientLights && (lightingModel !== 'CONSTANT')) {
if (optimizeForCesium) {
if (optimizeForCesium && enableLighting) {
fragmentLightingBlock += ' vec3 l = normalize(czm_sunDirectionEC);\n';
} else {
fragmentLightingBlock += ' vec3 l = vec3(0.0, 0.0, 1.0);\n';
Expand Down Expand Up @@ -766,7 +767,7 @@ define([
}
}

function getTechniqueKey(khrMaterialsCommon) {
function getTechniqueKey(khrMaterialsCommon, options) {
var techniqueKey = '';
techniqueKey += 'technique:' + khrMaterialsCommon.technique + ';';

Expand All @@ -792,7 +793,8 @@ define([
if (jointCount > 0) {
techniqueKey += skinningInfo.type + ';';
}
techniqueKey += primitiveInfo.hasVertexColors;
techniqueKey += primitiveInfo.hasVertexColors + ';';
techniqueKey += options.enableLighting;

return techniqueKey;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ define([

function generateTechnique(gltf, material, options) {
var optimizeForCesium = defaultValue(options.optimizeForCesium, false);
var enableLighting = defaultValue(options.enableLighting, true);
var hasCesiumRTCExtension = defined(gltf.extensions) && defined(gltf.extensions.CESIUM_RTC);
var addBatchIdToGeneratedShaders = defaultValue(options.addBatchIdToGeneratedShaders, false);

Expand Down Expand Up @@ -504,7 +505,7 @@ define([

// Generate fragment shader's lighting block
fragmentShader += ' vec3 lightColor = vec3(1.0, 1.0, 1.0);\n';
if (optimizeForCesium) {
if (optimizeForCesium && enableLighting) {
fragmentShader += ' vec3 l = normalize(czm_sunDirectionEC);\n';
} else {
fragmentShader += ' vec3 l = vec3(0.0, 0.0, 1.0);\n';
Expand Down
16 changes: 14 additions & 2 deletions Specs/Scene/Batched3DModel3DTileContentSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ defineSuite([
'Core/Plane',
'Core/Transforms',
'Specs/Cesium3DTilesTester',
'Specs/createScene'
'Specs/createScene',
'ThirdParty/when'
], function(
Batched3DModel3DTileContent,
Cartesian3,
Expand All @@ -19,7 +20,8 @@ defineSuite([
Plane,
Transforms,
Cesium3DTilesTester,
createScene) {
createScene,
when) {
'use strict';

var scene;
Expand Down Expand Up @@ -323,6 +325,16 @@ defineSuite([
});
});

it('sets enableLighting', function() {
return when.all([
Cesium3DTilesTester.loadTileset(scene, withKHRMaterialsCommonUrl, {enableLighting : true}),
Cesium3DTilesTester.loadTileset(scene, withKHRMaterialsCommonUrl, {enableLighting : false})
]).then(function(tilesets) {
expect(tilesets[0]._root.content._model._enableLighting).toBe(true);
expect(tilesets[1]._root.content._model._enableLighting).toBe(false);
});
});

it('destroys', function() {
return Cesium3DTilesTester.tileDestroys(scene, withoutBatchTableUrl);
});
Expand Down
16 changes: 14 additions & 2 deletions Specs/Scene/Instanced3DModel3DTileContentSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ defineSuite([
'Core/Transforms',
'Scene/TileBoundingSphere',
'Specs/Cesium3DTilesTester',
'Specs/createScene'
'Specs/createScene',
'ThirdParty/when'
], 'Scene/Instanced3DModel3DTileContent', function(
Cartesian3,
ClippingPlaneCollection,
Expand All @@ -19,7 +20,8 @@ defineSuite([
Transforms,
TileBoundingSphere,
Cesium3DTilesTester,
createScene) {
createScene,
when) {
'use strict';

var scene;
Expand Down Expand Up @@ -336,6 +338,16 @@ defineSuite([
});
});

it('sets enableLighting', function() {
return when.all([
Cesium3DTilesTester.loadTileset(scene, withBatchTableUrl, {enableLighting : true}),
Cesium3DTilesTester.loadTileset(scene, withBatchTableUrl, {enableLighting : false})
]).then(function(tilesets) {
expect(tilesets[0]._root.content._modelInstanceCollection._model._enableLighting).toBe(true);
expect(tilesets[1]._root.content._modelInstanceCollection._model._enableLighting).toBe(false);
});
});

it('destroys', function() {
return Cesium3DTilesTester.tileDestroys(scene, withoutBatchTableUrl);
});
Expand Down
18 changes: 18 additions & 0 deletions Specs/Scene/ModelInstanceCollectionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,24 @@ defineSuite([
});
});

it('enableLighting', function() {
return when.all([
loadCollection({
gltf : boxGltf,
instances : createInstances(4),
enableLighting : true
}),
loadCollection({
gltf : boxGltf,
instances : createInstances(4),
enableLighting : false
})
]).then(function(collections) {
expect(collections[0]._model._enableLighting).toBe(true);
expect(collections[1]._model._enableLighting).toBe(false);
});
});

it('picks', function() {
return loadCollection({
gltf : boxGltf,
Expand Down
Loading

0 comments on commit 2a0d36a

Please sign in to comment.