Skip to content

Globe Lighting #1251

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

Merged
merged 14 commits into from
Oct 22, 2013
Merged
Show file tree
Hide file tree
Changes from 7 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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Beta Releases
* Added prototype equals function to `NearFarScalar`, and `TimeIntervalCollection`.
* Added `Matrix4.fromTranslationQuaternionRotationScale` and `Matrix4.multiplyByScale`.
* Added `FrameState.events`.
* Added `enableLighting`, `lightingFadeOutDistance`, and `lightingFadeInDistance` properties to `CentralBody` to configure lighting.
* Added `Primitive.allowPicking` to save memory when picking is not needed.
* Added `debugShowBoundingVolume`, for debugging primitive rendering, to `Primitive`, `Polygon`, `ExtentPrimitive`, `EllipsoidPrimitive`, `BillboardCollection`, `LabelCollection`, and `PolylineCollection`.
* Added `DebugModelMatrixPrimitive` for debugging primitive's `modelMatrix`.
Expand Down
43 changes: 41 additions & 2 deletions Source/Scene/CentralBody.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,33 @@ define([
*/
this.tileCacheSize = 100;

/**
* Enable lighting the globe with the sun as a light source.
*
* @type {Boolean}
* @default false
*/
this.enableLighting = false;
this._enableLighting = false;

/**
* The distance where everything becomes lit. This only takes effect
* when <code>enableLighting</code> is <code>true</code>.
*
* @type {Number}
* @default 7500000.0
*/
this.lightingFadeOutDistance = 7500000.0;

/**
* The distance where lighting resumes. This only takes effect
* when <code>enableLighting</code> is <code>true</code>.
*
* @type {Number}
* @default 9000000.0
*/
this.lightingFadeInDistance = 9000000.0;

this._lastOceanNormalMapUrl = undefined;
this._oceanNormalMap = undefined;
this._zoomedOutOceanSpecularIntensity = 0.5;
Expand All @@ -227,6 +254,12 @@ define([
},
u_oceanNormalMap : function() {
return that._oceanNormalMap;
},
u_lightingFadeOutDistance : function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Make these a single vec2 uniform. Setting individual uniforms - especially for this particle shader - is one of our bottlenecks.

return that.lightingFadeOutDistance;
},
u_lightingFadeInDistance : function() {
return that.lightingFadeInDistance;
}
};
};
Expand Down Expand Up @@ -619,13 +652,15 @@ define([
var projectionChanged = this._projection !== projection;
var hasWaterMask = this._surface._terrainProvider.hasWaterMask();
var hasWaterMaskChanged = this._hasWaterMask !== hasWaterMask;
var hasEnableLightingChanged = this._enableLighting !== this.enableLighting;

if (!defined(this._surfaceShaderSet) ||
!defined(this._northPoleCommand.shaderProgram) ||
!defined(this._southPoleCommand.shaderProgram) ||
modeChanged ||
projectionChanged ||
hasWaterMaskChanged ||
hasEnableLightingChanged ||
(defined(this._oceanNormalMap)) !== this._showingPrettyOcean) {

var getPosition3DMode = 'vec4 getPosition(vec3 position3DWC) { return getPosition3DMode(position3DWC); }';
Expand Down Expand Up @@ -662,7 +697,10 @@ define([
}

this._surfaceShaderSet.baseVertexShaderString = createShaderSource({
defines : [hasWaterMask ? 'SHOW_REFLECTIVE_OCEAN' : ''],
defines : [
(hasWaterMask ? 'SHOW_REFLECTIVE_OCEAN' : ''),
(this.enableLighting ? 'ENABLE_LIGHTING' : '')
],
sources : [CentralBodyVS, getPositionMode, get2DYPositionFraction]
});

Expand All @@ -671,7 +709,8 @@ define([
this._surfaceShaderSet.baseFragmentShaderString = createShaderSource({
defines : [
(hasWaterMask ? 'SHOW_REFLECTIVE_OCEAN' : ''),
(showPrettyOcean ? 'SHOW_OCEAN_WAVES' : '')
(showPrettyOcean ? 'SHOW_OCEAN_WAVES' : ''),
(this.enableLighting ? 'ENABLE_LIGHTING' : '')
],
sources : [CentralBodyFS]
});
Expand Down
24 changes: 20 additions & 4 deletions Source/Shaders/CentralBodyFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ uniform float u_zoomedOutOceanSpecularIntensity;
uniform sampler2D u_oceanNormalMap;
#endif

#ifdef ENABLE_LIGHTING
uniform float u_lightingFadeOutDistance;
uniform float u_lightingFadeInDistance;
#endif

varying vec3 v_positionMC;
varying vec3 v_positionEC;
varying vec2 v_textureCoordinates;
Expand Down Expand Up @@ -122,6 +127,11 @@ void main()
#endif

vec4 color = vec4(startDayColor, 1.0);

#if defined(SHOW_REFLECTIVE_OCEAN) || defined(ENABLE_LIGHTING)
vec3 normalMC = normalize(czm_geodeticSurfaceNormal(v_positionMC, vec3(0.0), vec3(1.0))); // normalized surface normal in model coordinates
vec3 normalEC = normalize(czm_normal3D * normalMC); // normalized surface normal in eye coordiantes
#endif

#ifdef SHOW_REFLECTIVE_OCEAN
vec2 waterMaskTranslation = u_waterMaskTranslationAndScale.xy;
Expand All @@ -132,10 +142,8 @@ void main()

if (mask > 0.0)
{
vec3 normalMC = normalize(czm_geodeticSurfaceNormal(v_positionMC, vec3(0.0), vec3(1.0))); // normalized surface normal in model coordinates
vec3 normalEC = normalize(czm_normal3D * normalMC); // normalized surface normal in eye coordiantes
mat3 enuToEye = czm_eastNorthUpToEyeCoordinates(v_positionMC, normalEC);

vec2 ellipsoidTextureCoordinates = czm_ellipsoidWgs84TextureCoordinates(normalMC);
vec2 ellipsoidFlippedTextureCoordinates = czm_ellipsoidWgs84TextureCoordinates(normalMC.zyx);

Expand All @@ -144,8 +152,16 @@ void main()
color = computeWaterColor(v_positionEC, textureCoordinates, enuToEye, startDayColor, mask);
}
#endif


#ifdef ENABLE_LIGHTING
float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_sunDirectionEC, normalEC) * 5.0 + 0.3, 0.0, 1.0);
float cameraDist = length(czm_view[3]);
float t = clamp((cameraDist - u_lightingFadeOutDistance) / (u_lightingFadeInDistance - u_lightingFadeOutDistance), 0.0, 1.0);
diffuseIntensity = mix(1.0, diffuseIntensity, t);
gl_FragColor = vec4(color.rgb * diffuseIntensity, color.a);
#else
gl_FragColor = color;
#endif
}

#ifdef SHOW_REFLECTIVE_OCEAN
Expand Down
4 changes: 2 additions & 2 deletions Source/Shaders/CentralBodyVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ void main()
vec3 position3DWC = position3DAndHeight.xyz + u_center3D;

gl_Position = getPosition(position3DWC);
#ifdef SHOW_REFLECTIVE_OCEAN

#if defined(SHOW_REFLECTIVE_OCEAN) || defined(ENABLE_LIGHTING)
v_positionEC = (czm_modelView3D * vec4(position3DWC, 1.0)).xyz;
v_positionMC = position3DWC; // position in model coordinates
#endif
Expand Down