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

Sun lighting option for Models and 3D Tilesets #6160

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 `sunLighting` option to `Cesium3DTileset`, `Model`, and `Model.fromGltf` 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)
* Added `Cartographic.toCartesian` to convert from Cartographic to Cartesian3. [#6163](https://github.com/AnalyticalGraphicsInc/cesium/pull/6163)

### 1.41 - 2018-01-02
Expand Down
15 changes: 15 additions & 0 deletions Source/DataSources/ModelGraphics.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ define([
* @param {Property} [options.clampAnimations=true] A boolean Property specifying if glTF animations should hold the last pose for time durations with no keyframes.
* @param {Property} [options.nodeTransformations] An object, where keys are names of nodes, and values are {@link TranslationRotationScale} Properties describing the transformation to apply to that node.
* @param {Property} [options.shadows=ShadowMode.ENABLED] An enum Property specifying whether the model casts or receives shadows from each light source.
* @param {Property} [options.sunLighting=true] An boolean Property specifying whether to enable lighting the model with the sun as a light source. This value is ignored if the model has built-in shaders.
* @param {Property} [options.heightReference=HeightReference.NONE] A Property specifying what the height is relative to.
* @param {Property} [options.distanceDisplayCondition] A Property specifying at what distance from the camera that this model will be displayed.
* @param {Property} [options.silhouetteColor=Color.RED] A Property specifying the {@link Color} of the silhouette.
Expand All @@ -73,6 +74,8 @@ define([
this._incrementallyLoadTexturesSubscription = undefined;
this._shadows = undefined;
this._shadowsSubscription = undefined;
this._sunLighting = undefined;
this._sunLightingSubsciption = undefined;
this._uri = undefined;
this._uriSubscription = undefined;
this._runAnimations = undefined;
Expand Down Expand Up @@ -169,6 +172,16 @@ define([
*/
shadows : createPropertyDescriptor('shadows'),

/**
* Get or sets the boolean Property specifying whether to
* enable lighting the model with the sun as a light source.
* This value is ignored if the model has built-in shaders.
* @memberof ModelGraphics.prototype
* @type {Property}
* @default true
*/
sunLighting : createPropertyDescriptor('sunLighting'),

/**
* Gets or sets the string Property specifying the URI of the glTF asset.
* @memberof ModelGraphics.prototype
Expand Down Expand Up @@ -281,6 +294,7 @@ define([
result.maximumScale = this.maximumScale;
result.incrementallyLoadTextures = this.incrementallyLoadTextures;
result.shadows = this.shadows;
result.sunLighting = this.sunLighting;
result.uri = this.uri;
result.runAnimations = this.runAnimations;
result.clampAnimations = this.clampAnimations;
Expand Down Expand Up @@ -316,6 +330,7 @@ define([
this.maximumScale = defaultValue(this.maximumScale, source.maximumScale);
this.incrementallyLoadTextures = defaultValue(this.incrementallyLoadTextures, source.incrementallyLoadTextures);
this.shadows = defaultValue(this.shadows, source.shadows);
this.sunLighting = defaultValue(this.sunLighting, source.sunLighting);
this.uri = defaultValue(this.uri, source.uri);
this.runAnimations = defaultValue(this.runAnimations, source.runAnimations);
this.clampAnimations = defaultValue(this.clampAnimations, source.clampAnimations);
Expand Down
3 changes: 3 additions & 0 deletions Source/DataSources/ModelVisualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ define([
var defaultIncrementallyLoadTextures = true;
var defaultClampAnimations = true;
var defaultShadows = ShadowMode.ENABLED;
var defaultSunLighting = true;
var defaultHeightReference = HeightReference.NONE;
var defaultSilhouetteColor = Color.RED;
var defaultSilhouetteSize = 0.0;
Expand Down Expand Up @@ -124,6 +125,7 @@ define([
model = Model.fromGltf({
url : resource,
incrementallyLoadTextures : Property.getValueOrDefault(modelGraphics._incrementallyLoadTextures, time, defaultIncrementallyLoadTextures),
sunLighting : Property.getValueOrDefault(modelGraphics._sunLighting, time, defaultSunLighting),
scene : this._scene
});

Expand All @@ -148,6 +150,7 @@ define([
model.maximumScale = Property.getValueOrUndefined(modelGraphics._maximumScale, time);
model.modelMatrix = Matrix4.clone(modelMatrix, model.modelMatrix);
model.shadows = Property.getValueOrDefault(modelGraphics._shadows, time, defaultShadows);
model.sunLighting = Property.getValueOrDefault(modelGraphics._sunLighting, time, defaultSunLighting);
model.heightReference = Property.getValueOrDefault(modelGraphics._heightReference, time, defaultHeightReference);
model.distanceDisplayCondition = Property.getValueOrUndefined(modelGraphics._distanceDisplayCondition, time);
model.silhouetteColor = Property.getValueOrDefault(modelGraphics._silhouetteColor, time, defaultSilhouetteColor, model._silhouetteColor);
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,
sunLighting : tileset._sunLighting,
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.sunLighting=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._sunLighting = defaultValue(options.sunLighting, 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,
sunLighting : content._tileset._sunLighting,
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.sunLighting=true] Enable lighting the model with the sun as a light source. This value is ignored if the model 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._sunLighting = defaultValue(options.sunLighting, 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.sunLighting=true] Enable lighting the model with the sun as a light source. This value is ignored if the model 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,
sunLighting: this._sunLighting,
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.sunLighting=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._sunLighting = defaultValue(options.sunLighting, 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,
sunLighting : collection._sunLighting,
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 sunLighting = content._tileset._sunLighting;

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 (sunLighting) {
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 sunLighting = defaultValue(options.sunLighting, 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 && sunLighting) {
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.sunLighting;

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 sunLighting = defaultValue(options.sunLighting, 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 && sunLighting) {
fragmentShader += ' vec3 l = normalize(czm_sunDirectionEC);\n';
} else {
fragmentShader += ' vec3 l = vec3(0.0, 0.0, 1.0);\n';
Expand Down
Loading