-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
Model lighting options #7025
Model lighting options #7025
Changes from 8 commits
ae40eb9
d741cb2
7a3a4f4
0b2029c
c73dbc8
7c0f5bc
9e17dc6
f90dc62
d18d192
69437f4
ca42a1b
7dba70b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
define([ | ||
'../Core/BoundingSphere', | ||
'../Core/Cartesian2', | ||
'../Core/Cartesian3', | ||
'../Core/Cartesian4', | ||
'../Core/Cartographic', | ||
'../Core/Check', | ||
'../Core/clone', | ||
'../Core/Color', | ||
'../Core/combine', | ||
|
@@ -74,9 +76,11 @@ define([ | |
'./ShadowMode' | ||
], function( | ||
BoundingSphere, | ||
Cartesian2, | ||
Cartesian3, | ||
Cartesian4, | ||
Cartographic, | ||
Check, | ||
clone, | ||
Color, | ||
combine, | ||
|
@@ -282,6 +286,8 @@ define([ | |
* @param {Number} [options.silhouetteSize=0.0] The size of the silhouette in pixels. | ||
* @param {ClippingPlaneCollection} [options.clippingPlanes] The {@link ClippingPlaneCollection} used to selectively disable rendering the model. | ||
* @param {Boolean} [options.dequantizeInShader=true] Determines if a {@link https://github.com/google/draco|Draco} encoded model is dequantized on the GPU. This decreases total memory usage for encoded models. | ||
* @param {Number} [options.imageBasedLightingFactor=1.0] Scales the IBL lighting from the earth, sky, atmosphere and star skybox. | ||
* @param {Color} [options.lightColor] The color and intensity of the sunlight used to shade the model. | ||
* | ||
* @see Model.fromGltf | ||
* | ||
|
@@ -656,6 +662,11 @@ define([ | |
this._rtcCenter2D = undefined; // in projected world coordinates | ||
|
||
this._keepPipelineExtras = options.keepPipelineExtras; // keep the buffers in memory for use in other applications | ||
|
||
this._imageBasedLightingFactor = new Cartesian2(1.0, 1.0); | ||
Cartesian2.clone(options.imageBasedLightingFactor, this._imageBasedLightingFactor); | ||
this._lightColor = Color.clone(options.lightColor); | ||
this._regenerateShaders = false; | ||
} | ||
|
||
defineProperties(Model.prototype, { | ||
|
@@ -1073,6 +1084,59 @@ define([ | |
get : function() { | ||
return this._pickIds; | ||
} | ||
}, | ||
|
||
/** | ||
* Cesium adds lighting from the earth, sky, atmosphere, and star skybox. This cartesian is used to scale the final | ||
* diffuse and specular lighting contribution from those sources to the final color. A value of 0.0 will disable those light sources. | ||
* | ||
* @memberof Model.prototype | ||
* | ||
* @type {Cartesian2} | ||
* @default Cartesian2(1.0, 1.0) | ||
*/ | ||
imageBasedLightingFactor : { | ||
get : function() { | ||
return this._imageBasedLightingFactor; | ||
}, | ||
set : function(value) { | ||
//>>includeStart('debug', pragmas.debug); | ||
Check.typeOf.object('imageBasedLightingFactor', value); | ||
Check.typeOf.number.greaterThanOrEquals('imageBasedLightingFactor.x', value.x, 0.0); | ||
Check.typeOf.number.lessThanOrEquals('imageBasedLightingFactor.x', value.x, 1.0); | ||
Check.typeOf.number.greaterThanOrEquals('imageBasedLightingFactor.y', value.y, 0.0); | ||
Check.typeOf.number.lessThanOrEquals('imageBasedLightingFactor.y', value.y, 1.0); | ||
//>>includeEnd('debug'); | ||
this._regenerateShaders = this._regenerateShaders || (this._imageBasedLightingFactor.x > 0.0 && value.x === 0.0) || (this._imageBasedLightingFactor.x === 0.0 && value.x > 0.0); | ||
this._regenerateShaders = this._regenerateShaders || (this._imageBasedLightingFactor.y > 0.0 && value.y === 0.0) || (this._imageBasedLightingFactor.y === 0.0 && value.y > 0.0); | ||
Cartesian2.clone(value, this._imageBasedLightingFactor); | ||
} | ||
}, | ||
|
||
/** | ||
* The color and intensity of the sunlight used to shade the model. | ||
* <p> | ||
* For example, disabling additional light sources by setting <code>model.imageBasedLightingFactor = 0.0</code> will make the | ||
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. Same note here. |
||
* model much darker. Here, increasing the intensity of the light source will make the model brighter. | ||
* </p> | ||
* | ||
* @memberof Model.prototype | ||
* | ||
* @type {Color} | ||
* @default undefined | ||
*/ | ||
lightColor : { | ||
get : function() { | ||
return this._lightColor; | ||
}, | ||
set : function(value) { | ||
var lightColor = this._lightColor; | ||
if (value === lightColor || Color.equals(value, lightColor)) { | ||
return; | ||
} | ||
this._regenerateShaders = this._regenerateShaders || (defined(lightColor) && !defined(value)) || (defined(value) && !defined(lightColor)); | ||
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. Should it also regenerate the shader if the previous color was not Some of the other documentation makes it seem as if setting 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. Meant to delete this comment. See #7025 (comment) instead. |
||
this._lightColor = Color.clone(value, lightColor); | ||
} | ||
} | ||
}); | ||
|
||
|
@@ -1936,6 +2000,14 @@ define([ | |
drawFS = 'uniform vec4 czm_pickColor;\n' + drawFS; | ||
} | ||
|
||
if (model._imageBasedLightingFactor.x > 0.0 || model._imageBasedLightingFactor.y > 0.0) { | ||
drawFS = '#define USE_IBL_LIGHTING \n\n' + drawFS; | ||
} | ||
|
||
if (defined(model._lightColor)) { | ||
drawFS = '#define USE_CUSTOM_LIGHT_COLOR \n\n' + drawFS; | ||
} | ||
|
||
createAttributesAndProgram(programId, techniqueId, drawFS, drawVS, model, context); | ||
} | ||
|
||
|
@@ -1978,6 +2050,14 @@ define([ | |
drawFS = 'uniform vec4 czm_pickColor;\n' + drawFS; | ||
} | ||
|
||
if (model._imageBasedLightingFactor.x > 0.0 || model._imageBasedLightingFactor.y > 0.0) { | ||
drawFS = '#define USE_IBL_LIGHTING \n\n' + drawFS; | ||
} | ||
|
||
if (defined(model._lightColor)) { | ||
drawFS = '#define USE_CUSTOM_LIGHT_COLOR \n\n' + drawFS; | ||
} | ||
|
||
createAttributesAndProgram(programId, techniqueId, drawFS, drawVS, model, context); | ||
} | ||
|
||
|
@@ -2846,6 +2926,18 @@ define([ | |
}; | ||
} | ||
|
||
function createIBLFactorFunction(model) { | ||
return function() { | ||
return model._imageBasedLightingFactor; | ||
}; | ||
} | ||
|
||
function createLightColorFunction(model) { | ||
return function() { | ||
return model._lightColor; | ||
}; | ||
} | ||
|
||
function triangleCountFromPrimitiveIndices(primitive, indicesCount) { | ||
switch (primitive.mode) { | ||
case PrimitiveType.TRIANGLES: | ||
|
@@ -2938,7 +3030,9 @@ define([ | |
gltf_colorBlend : createColorBlendFunction(model), | ||
gltf_clippingPlanes: createClippingPlanesFunction(model), | ||
gltf_clippingPlanesEdgeStyle: createClippingPlanesEdgeStyleFunction(model), | ||
gltf_clippingPlanesMatrix: createClippingPlanesMatrixFunction(model) | ||
gltf_clippingPlanesMatrix: createClippingPlanesMatrixFunction(model), | ||
gltf_iblFactor : createIBLFactorFunction(model), | ||
gltf_lightColor : createLightColorFunction(model) | ||
}); | ||
|
||
// Allow callback to modify the uniformMap | ||
|
@@ -4256,7 +4350,7 @@ define([ | |
currentClippingPlanesState = clippingPlanes.clippingPlanesState; | ||
} | ||
|
||
var shouldRegenerateShaders = this._clippingPlanesState !== currentClippingPlanesState; | ||
var shouldRegenerateShaders = this._clippingPlanesState !== currentClippingPlanesState || this._regenerateShaders; | ||
this._clippingPlanesState = currentClippingPlanesState; | ||
|
||
// Regenerate shaders if color shading changed from last update | ||
|
@@ -4272,6 +4366,8 @@ define([ | |
updateColor(this, frameState, false); | ||
updateSilhouette(this, frameState, false); | ||
} | ||
|
||
this._regenerateShaders = false; | ||
} | ||
|
||
if (justLoaded) { | ||
|
@@ -4365,7 +4461,7 @@ define([ | |
destroyIfNotCached(rendererResources, cachedRendererResources); | ||
|
||
var programId; | ||
if (isClippingEnabled(model) || isColorShadingEnabled(model)) { | ||
if (isClippingEnabled(model) || isColorShadingEnabled(model) || model._regenerateShaders) { | ||
rendererResources.programs = {}; | ||
rendererResources.silhouettePrograms = {}; | ||
|
||
|
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.
The doc should be
Cartesian2
instead ofNumber
.