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

Add ground atmosphere #6877

Merged
merged 39 commits into from
Sep 24, 2018
Merged
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
b3aeee9
Add back ground atmosphere from space.
bagnell Jul 25, 2018
c052aa0
Fix fog.
bagnell Jul 25, 2018
381c0b2
Merge branch 'master' into atmosphere
bagnell Aug 1, 2018
75e14f7
Add option to enable disable ground atmosphere.
bagnell Aug 1, 2018
139a529
Fix sky atmosphere.
bagnell Aug 1, 2018
edc42fa
Revert sky atmosphere changes.
bagnell Aug 1, 2018
e642085
Remove unused code in sky atmosphere shader.
bagnell Aug 1, 2018
5f11c50
Fix night atmosphere color.
bagnell Aug 2, 2018
d3a9950
Use atmosphere with correct sun lighting.
bagnell Aug 2, 2018
bb402de
Make night fade in/out distances a uniform.
bagnell Aug 2, 2018
2e2b825
Update doc.
bagnell Aug 2, 2018
a708860
Fix 2D/CV.
bagnell Aug 2, 2018
31de234
Update CHANGES.md.
bagnell Aug 2, 2018
f6bf14c
Merge branch 'master' into atmosphere
bagnell Aug 3, 2018
dbccbd8
Merge branch 'master' into atmosphere
bagnell Aug 9, 2018
a7234d7
Merge branch 'master' into atmosphere
bagnell Aug 20, 2018
9d31730
Make ground atmosphere per-fragment for a smoother transition from ni…
bagnell Aug 20, 2018
2279eac
Only use per-fragment ground atmosphere when the camera is greater th…
bagnell Aug 20, 2018
f6a1522
Merge branch 'master' into atmosphere
bagnell Aug 23, 2018
1e6aafe
Merge branch 'master' into atmosphere
bagnell Aug 29, 2018
681fb0f
Merge branch 'master' into atmosphere
bagnell Sep 5, 2018
2225851
Update CHANGES.md after merge.
bagnell Sep 5, 2018
8da546f
Merge branch 'master' into atmosphere
bagnell Sep 6, 2018
fc3e0c7
Merge branch 'master' into atmosphere
bagnell Sep 11, 2018
4426bd2
Clean up globe shader parameters.
bagnell Sep 11, 2018
bfd11bd
More aggressive fade distances. Have enable lighting off by default.
bagnell Sep 11, 2018
7180803
Change water specular intensity when ground atmosphere is enabled.
bagnell Sep 11, 2018
80e591d
Fix day/night terrain shading.
bagnell Sep 11, 2018
e1005fe
Tweak params
lilleyse Sep 20, 2018
61cf20e
Merge branch 'master' into atmosphere
bagnell Sep 20, 2018
85b08e5
Fade water effect when zooming.
bagnell Sep 20, 2018
14e3114
Fix for when atmosphere is disabled.
bagnell Sep 20, 2018
3636dbb
Add atmosphere lighting example.
bagnell Sep 20, 2018
5e36c99
More tweaks
lilleyse Sep 21, 2018
a53cd15
Reset sliders and text box when the reset button is clicked in the at…
bagnell Sep 21, 2018
3fc5808
Update from review.
bagnell Sep 21, 2018
fe410fa
Remove input ids.
bagnell Sep 21, 2018
a623027
Fix fog.
bagnell Sep 21, 2018
b75d1cf
Update docs and changes a bit
lilleyse Sep 24, 2018
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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
==========

### 1.50 - 2018-10-01

##### Additions :tada:
* Added ground atmosphere lighting in 3D. This can be toggled with `Globe.showGroundAtmosphere`. [6877](https://github.com/AnalyticalGraphicsInc/cesium/pull/6877)

### 1.49 - 2018-09-04

##### Breaking Changes :mega:
Expand Down
35 changes: 32 additions & 3 deletions Source/Scene/Globe.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,17 @@ define([
* Enable lighting the globe with the sun as a light source.
*
* @type {Boolean}
* @default false
* @default true
*/
this.enableLighting = false;
this.enableLighting = true;

/**
* Enable the ground atmosphere.
*
* @type {Boolean}
* @default true
*/
this.showGroundAtmosphere = true;

/**
* The distance where everything becomes lit. This only takes effect
Expand All @@ -153,6 +161,24 @@ define([
*/
this.lightingFadeInDistance = 9000000.0;

/**
* The distance where the darkness of night from the ground atmosphere fades out to a lit ground atmosphere.
* This only takes effect when <code>showGroundAtmosphere</code> is <code>true</code>.
*
* @type {Number}
* @default 10000000.0
*/
this.nightFadeOutDistance = 10000000.0;

/**
* The distance where the darkness of night from the ground atmosphere fades in to an unlit ground atmosphere.
* This only takes effect when <code>showGroundAtmosphere</code> is <code>true</code>.
*
* @type {Number}
* @default 40000000.0
*/
this.nightFadeInDistance = 40000000.0;

/**
* True if an animated wave effect should be shown in areas of the globe
* covered by water; otherwise, false. This property is ignored if the
Expand Down Expand Up @@ -364,7 +390,7 @@ define([

var requireNormals = defined(globe._material) && (globe._material.shaderSource.match(/slope/) || globe._material.shaderSource.match('normalEC'));

var fragmentSources = [];
var fragmentSources = [GroundAtmosphere];
if (defined(globe._material) && (!requireNormals || globe._terrainProvider.requestVertexNormals)) {
fragmentSources.push(globe._material.shaderSource);
defines.push('APPLY_MATERIAL');
Expand Down Expand Up @@ -645,10 +671,13 @@ define([
tileProvider.terrainProvider = this.terrainProvider;
tileProvider.lightingFadeOutDistance = this.lightingFadeOutDistance;
tileProvider.lightingFadeInDistance = this.lightingFadeInDistance;
tileProvider.nightFadeOutDistance = this.nightFadeOutDistance;
tileProvider.nightFadeInDistance = this.nightFadeInDistance;
tileProvider.zoomedOutOceanSpecularIntensity = this._zoomedOutOceanSpecularIntensity;
tileProvider.hasWaterMask = hasWaterMask;
tileProvider.oceanNormalMap = this._oceanNormalMap;
tileProvider.enableLighting = this.enableLighting;
tileProvider.showGroundAtmosphere = this.showGroundAtmosphere;
tileProvider.shadows = this.shadows;

surface.beginFrame(frameState);
Expand Down
26 changes: 18 additions & 8 deletions Source/Scene/GlobeSurfaceShaderSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ define([
return useWebMercatorProjection ? get2DYPositionFractionMercatorProjection : get2DYPositionFractionGeographicProjection;
}

GlobeSurfaceShaderSet.prototype.getShaderProgram = function(frameState, surfaceTile, numberOfDayTextures, applyBrightness, applyContrast, applyHue, applySaturation, applyGamma, applyAlpha, applySplit, showReflectiveOcean, showOceanWaves, enableLighting, hasVertexNormals, useWebMercatorProjection, enableFog, enableClippingPlanes, clippingPlanes) {
GlobeSurfaceShaderSet.prototype.getShaderProgram = function(frameState, surfaceTile, numberOfDayTextures, applyBrightness, applyContrast, applyHue, applySaturation, applyGamma, applyAlpha, applySplit, showReflectiveOcean, showOceanWaves, enableLighting, showGroundAtmosphere, perFragmentGroundAtmosphere, hasVertexNormals, useWebMercatorProjection, enableFog, enableClippingPlanes, clippingPlanes) {
var quantization = 0;
var quantizationDefine = '';

Expand Down Expand Up @@ -95,13 +95,15 @@ define([
(showReflectiveOcean << 8) |
(showOceanWaves << 9) |
(enableLighting << 10) |
(hasVertexNormals << 11) |
(useWebMercatorProjection << 12) |
(enableFog << 13) |
(quantization << 14) |
(applySplit << 15) |
(enableClippingPlanes << 16) |
(vertexLogDepth << 17);
(showGroundAtmosphere << 11) |
(perFragmentGroundAtmosphere << 12) |
(hasVertexNormals << 13) |
(useWebMercatorProjection << 14) |
(enableFog << 15) |
(quantization << 16) |
(applySplit << 17) |
(enableClippingPlanes << 18) |
(vertexLogDepth << 19);

var currentClippingShaderState = 0;
if (defined(clippingPlanes)) {
Expand Down Expand Up @@ -172,6 +174,14 @@ define([
}
}

if (showGroundAtmosphere) {
vs.defines.push('GROUND_ATMOSPHERE');
fs.defines.push('GROUND_ATMOSPHERE');
if (perFragmentGroundAtmosphere) {
fs.defines.push('PER_FRAGMENT_GROUND_ATMOSPHERE');
}
}

vs.defines.push('INCLUDE_WEB_MERCATOR_Y');
fs.defines.push('INCLUDE_WEB_MERCATOR_Y');

Expand Down
27 changes: 26 additions & 1 deletion Source/Scene/GlobeSurfaceTileProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ define([
this.oceanNormalMap = undefined;
this.zoomedOutOceanSpecularIntensity = 0.5;
this.enableLighting = false;
this.showGroundAtmosphere = false;
this.shadows = ShadowMode.RECEIVE_ONLY;

this._quadtree = undefined;
Expand Down Expand Up @@ -839,6 +840,9 @@ define([
u_lightingFadeDistance : function() {
return this.properties.lightingFadeDistance;
},
u_nightFadeDistance : function() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor, but batch this with u_lightingFadeDistance?

return this.properties.nightFadeDistance;
},
u_center3D : function() {
return this.properties.center3D;
},
Expand Down Expand Up @@ -941,6 +945,7 @@ define([
zoomedOutOceanSpecularIntensity : 0.5,
oceanNormalMap : undefined,
lightingFadeDistance : new Cartesian2(6500000.0, 9000000.0),
nightFadeDistance : new Cartesian2(10000000.0, 40000000.0),

center3D : undefined,
rtc : new Cartesian3(),
Expand Down Expand Up @@ -1114,9 +1119,27 @@ define([
var showOceanWaves = showReflectiveOcean && defined(oceanNormalMap);
var hasVertexNormals = tileProvider.terrainProvider.ready && tileProvider.terrainProvider.hasVertexNormals;
var enableFog = frameState.fog.enabled;
var showGroundAtmosphere = tileProvider.showGroundAtmosphere;
var castShadows = ShadowMode.castShadows(tileProvider.shadows);
var receiveShadows = ShadowMode.receiveShadows(tileProvider.shadows);

var perFragmentGroundAtmosphere = false;
if (showGroundAtmosphere) {
var mode = frameState.mode;
var camera = frameState.camera;
var cameraDistance;
if (mode === SceneMode.SCENE2D || mode === SceneMode.COLUMBUS_VIEW) {
cameraDistance = camera.positionCartographic.height;
} else {
cameraDistance = Cartesian3.magnitude(camera.positionWC);
}
var fadeOutDistance = tileProvider.nightFadeOutDistance;
if (mode !== SceneMode.SCENE3D) {
fadeOutDistance -= frameState.mapProjection.ellipsoid.maximumRadius;
}
perFragmentGroundAtmosphere = cameraDistance > fadeOutDistance;
}

if (showReflectiveOcean) {
--maxTextures;
}
Expand Down Expand Up @@ -1244,6 +1267,8 @@ define([
uniformMapProperties.oceanNormalMap = oceanNormalMap;
uniformMapProperties.lightingFadeDistance.x = tileProvider.lightingFadeOutDistance;
uniformMapProperties.lightingFadeDistance.y = tileProvider.lightingFadeInDistance;
uniformMapProperties.nightFadeDistance.x = tileProvider.nightFadeOutDistance;
uniformMapProperties.nightFadeDistance.y = tileProvider.nightFadeInDistance;
uniformMapProperties.zoomedOutOceanSpecularIntensity = tileProvider.zoomedOutOceanSpecularIntensity;

uniformMapProperties.center3D = surfaceTile.center;
Expand Down Expand Up @@ -1357,7 +1382,7 @@ define([
uniformMap = combine(uniformMap, tileProvider.uniformMap);
}

command.shaderProgram = tileProvider._surfaceShaderSet.getShaderProgram(frameState, surfaceTile, numberOfDayTextures, applyBrightness, applyContrast, applyHue, applySaturation, applyGamma, applyAlpha, applySplit, showReflectiveOcean, showOceanWaves, tileProvider.enableLighting, hasVertexNormals, useWebMercatorProjection, applyFog, clippingPlanesEnabled, clippingPlanes);
command.shaderProgram = tileProvider._surfaceShaderSet.getShaderProgram(frameState, surfaceTile, numberOfDayTextures, applyBrightness, applyContrast, applyHue, applySaturation, applyGamma, applyAlpha, applySplit, showReflectiveOcean, showOceanWaves, tileProvider.enableLighting, showGroundAtmosphere, perFragmentGroundAtmosphere, hasVertexNormals, useWebMercatorProjection, applyFog, clippingPlanesEnabled, clippingPlanes);
command.castShadows = castShadows;
command.receiveShadows = receiveShadows;
command.renderState = renderState;
Expand Down
2 changes: 1 addition & 1 deletion Source/Scene/Scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -2622,7 +2622,7 @@ define([
environmentState.moonCommand = undefined;
} else {
if (defined(skyAtmosphere) && defined(globe)) {
skyAtmosphere.setDynamicAtmosphereColor(globe.enableLighting);
skyAtmosphere.setDynamicAtmosphereColor(globe.enableLighting || globe.showGroundAtmosphere);
environmentState.isReadyForAtmosphere = environmentState.isReadyForAtmosphere || globe._surface._tilesToRender.length > 0;
}
environmentState.skyAtmosphereCommand = defined(skyAtmosphere) ? skyAtmosphere.update(frameState) : undefined;
Expand Down
83 changes: 67 additions & 16 deletions Source/Shaders/GlobeFS.glsl
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//#define SHOW_TILE_BOUNDARIES
uniform vec4 u_initialColor;

#if TEXTURE_UNITS > 0
Expand Down Expand Up @@ -47,17 +46,21 @@ uniform float u_zoomedOutOceanSpecularIntensity;
uniform sampler2D u_oceanNormalMap;
#endif

#ifdef ENABLE_DAYNIGHT_SHADING
#if defined(ENABLE_DAYNIGHT_SHADING) || defined(GROUND_ATMOSPHERE)
uniform vec2 u_lightingFadeDistance;
#endif

#ifdef GROUND_ATMOSPHERE
Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, maybe not.

uniform vec2 u_nightFadeDistance;
#endif

#ifdef ENABLE_CLIPPING_PLANES
uniform sampler2D u_clippingPlanes;
uniform mat4 u_clippingPlanesMatrix;
uniform vec4 u_clippingPlanesEdgeStyle;
#endif

#if defined(FOG) && (defined(ENABLE_VERTEX_LIGHTING) || defined(ENABLE_DAYNIGHT_SHADING))
#if defined(FOG) && (defined(ENABLE_VERTEX_LIGHTING) || defined(ENABLE_DAYNIGHT_SHADING)) || defined(GROUND_ATMOSPHERE)
uniform float u_minimumBrightness;
#endif

Expand All @@ -72,8 +75,13 @@ varying float v_height;
varying float v_slope;
#endif

#ifdef FOG
#if defined(FOG) || defined(GROUND_ATMOSPHERE)
varying float v_distance;
varying vec3 v_fogRayleighColor;
varying vec3 v_fogMieColor;
#endif

#ifdef GROUND_ATMOSPHERE
varying vec3 v_rayleighColor;
varying vec3 v_mieColor;
#endif
Expand Down Expand Up @@ -209,11 +217,7 @@ void main()
color.xyz = mix(color.xyz, material.diffuse, material.alpha);
#endif

#ifdef ENABLE_VERTEX_LIGHTING
float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_sunDirectionEC, normalize(v_normalEC)) * 0.9 + 0.3, 0.0, 1.0);
vec4 finalColor = vec4(color.rgb * diffuseIntensity, color.a);
#elif defined(ENABLE_DAYNIGHT_SHADING)
float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_sunDirectionEC, normalEC) * 5.0 + 0.3, 0.0, 1.0);
#if defined(ENABLE_DAYNIGHT_SHADING) || defined(GROUND_ATMOSPHERE)
float cameraDist;
if (czm_sceneMode == czm_sceneMode2D)
{
Expand All @@ -235,8 +239,15 @@ void main()
fadeOutDist -= maxRadii;
fadeInDist -= maxRadii;
}
float t = clamp((cameraDist - fadeOutDist) / (fadeInDist - fadeOutDist), 0.0, 1.0);
diffuseIntensity = mix(1.0, diffuseIntensity, t);
float fade = clamp((cameraDist - fadeOutDist) / (fadeInDist - fadeOutDist), 0.0, 1.0);
#endif

#if defined(ENABLE_VERTEX_LIGHTING) && !defined(GROUND_ATMOSPHERE)
float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_sunDirectionEC, normalize(v_normalEC)) * 0.9 + 0.3, 0.0, 1.0);
vec4 finalColor = vec4(color.rgb * diffuseIntensity, color.a);
#elif defined(ENABLE_DAYNIGHT_SHADING) && !defined(GROUND_ATMOSPHERE)
float diffuseIntensity = clamp(czm_getLambertDiffuse(czm_sunDirectionEC, normalEC) * 5.0 + 0.3, 0.0, 1.0);
diffuseIntensity = mix(1.0, diffuseIntensity, fade);
vec4 finalColor = vec4(color.rgb * diffuseIntensity, color.a);
#else
vec4 finalColor = color;
Expand All @@ -253,20 +264,60 @@ void main()
}
#endif

#ifdef FOG
#if defined(FOG) || defined(GROUND_ATMOSPHERE)
const float fExposure = 2.0;
vec3 fogColor = v_mieColor + finalColor.rgb * v_rayleighColor;
vec3 fogColor = v_fogMieColor + finalColor.rgb * v_fogRayleighColor;
fogColor = vec3(1.0) - exp(-fExposure * fogColor);
#endif

#if defined(ENABLE_VERTEX_LIGHTING) || defined(ENABLE_DAYNIGHT_SHADING)
#ifdef FOG
#if defined(ENABLE_VERTEX_LIGHTING) || defined(ENABLE_DAYNIGHT_SHADING) || defined(GROUND_ATMOSPHERE)
float darken = clamp(dot(normalize(czm_viewerPositionWC), normalize(czm_sunPositionWC)), u_minimumBrightness, 1.0);
fogColor *= darken;
#endif

gl_FragColor = vec4(czm_fog(v_distance, finalColor.rgb, fogColor), finalColor.a);
finalColor = vec4(czm_fog(v_distance, finalColor.rgb, fogColor), finalColor.a);
#endif

#ifdef GROUND_ATMOSPHERE
if (czm_sceneMode != czm_sceneMode3D)
{
gl_FragColor = finalColor;
return;
}

#ifdef PER_FRAGMENT_GROUND_ATMOSPHERE
czm_ellipsoid ellipsoid = czm_getWgs84EllipsoidEC();

float mpp = czm_metersPerPixel(vec4(0.0, 0.0, -czm_currentFrustum.x, 1.0));
vec2 xy = gl_FragCoord.xy / czm_viewport.zw * 2.0 - vec2(1.0);
xy *= czm_viewport.zw * mpp * 0.5;

vec3 direction = normalize(vec3(xy, -czm_currentFrustum.x));
czm_ray ray = czm_ray(vec3(0.0), direction);

czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid);

vec3 ellipsoidPosition = czm_pointAlongRay(ray, intersection.start);
ellipsoidPosition = (czm_inverseView * vec4(ellipsoidPosition, 1.0)).xyz;
AtmosphereColor atmosColor = computeGroundAtmosphereFromSpace(ellipsoidPosition, true);

vec3 groundAtmosphereColor = atmosColor.mie + finalColor.rgb * atmosColor.rayleigh;
groundAtmosphereColor = vec3(1.0) - exp(-fExposure * groundAtmosphereColor);

fadeInDist = u_nightFadeDistance.x;
fadeOutDist = u_nightFadeDistance.y;

float sunlitAtmosphereIntensity = clamp((cameraDist - fadeOutDist) / (fadeInDist - fadeOutDist), 0.0, 1.0);
groundAtmosphereColor = mix(groundAtmosphereColor, fogColor, sunlitAtmosphereIntensity);
#else
gl_FragColor = finalColor;
vec3 groundAtmosphereColor = fogColor;
#endif

finalColor = vec4(mix(finalColor.rgb, groundAtmosphereColor, fade), finalColor.a);
#endif

gl_FragColor = finalColor;
}

#ifdef SHOW_REFLECTIVE_OCEAN
Expand Down
18 changes: 9 additions & 9 deletions Source/Shaders/GlobeVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ varying float v_slope;
varying float v_height;
#endif

#ifdef FOG
#if defined(FOG) || defined(GROUND_ATMOSPHERE)
varying float v_distance;
varying vec3 v_mieColor;
varying vec3 v_rayleighColor;
varying vec3 v_fogMieColor;
varying vec3 v_fogRayleighColor;
#endif

// These functions are generated at runtime.
Expand Down Expand Up @@ -158,19 +158,19 @@ void main()

#if defined(ENABLE_VERTEX_LIGHTING) || defined(GENERATE_POSITION_AND_NORMAL) || defined(APPLY_MATERIAL)
v_positionEC = (u_modifiedModelView * vec4(position, 1.0)).xyz;
v_positionMC = position3DWC; // position in model coordinates
v_positionMC = position3DWC; // position in model coordinates
vec3 normalMC = czm_octDecode(encodedNormal);
v_normalMC = normalMC;
v_normalEC = czm_normal3D * v_normalMC;
#elif defined(SHOW_REFLECTIVE_OCEAN) || defined(ENABLE_DAYNIGHT_SHADING) || defined(GENERATE_POSITION)
v_positionEC = (u_modifiedModelView * vec4(position, 1.0)).xyz;
v_positionMC = position3DWC; // position in model coordinates
v_positionMC = position3DWC; // position in model coordinates
#endif

#ifdef FOG
AtmosphereColor atmosColor = computeGroundAtmosphereFromSpace(position3DWC);
v_mieColor = atmosColor.mie;
v_rayleighColor = atmosColor.rayleigh;
#if defined(FOG) || defined(GROUND_ATMOSPHERE)
AtmosphereColor atmosFogColor = computeGroundAtmosphereFromSpace(position3DWC, false);
v_fogMieColor = atmosFogColor.mie;
v_fogRayleighColor = atmosFogColor.rayleigh;
v_distance = length((czm_modelView3D * vec4(position3DWC, 1.0)).xyz);
#endif

Expand Down
Loading