Skip to content

Commit

Permalink
Make combined FadeRange parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrgags committed Jan 18, 2024
1 parent ca08ba9 commit 49e4629
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 27 deletions.
8 changes: 4 additions & 4 deletions packages/engine/Source/Renderer/AutomaticUniforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1711,19 +1711,19 @@ const AutomaticUniforms = {
},
}),

czm_atmosphereLightingFadeDistance: new AutomaticUniform({
czm_atmosphereLightingFadeRange: new AutomaticUniform({
size: 1,
datatype: WebGLConstants.FLOAT_VEC2,
getValue: function (uniformState) {
return uniformState.atmosphereLightingFadeDistance;
return uniformState.atmosphereLightingFadeRange;
},
}),

czm_atmosphereNightFadeDistance: new AutomaticUniform({
czm_atmosphereNightFadeRange: new AutomaticUniform({
size: 1,
datatype: WebGLConstants.FLOAT_VEC2,
getValue: function (uniformState) {
return uniformState.atmosphereNightFadeDistance;
return uniformState.atmosphereNightFadeRange;
},
}),

Expand Down
37 changes: 29 additions & 8 deletions packages/engine/Source/Renderer/UniformState.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,8 +173,8 @@ function UniformState() {
this._atmosphereDynamicLighting = undefined;

// (fadeOutDistance, fadeInDistance)
this._atmosphereLightingFadeDistance = new Cartesian2();
this._atmosphereNightFadeDistance = new Cartesian2();
this._atmosphereLightingFadeRange = new Cartesian2();
this._atmosphereNightFadeRange = new Cartesian2();

this._invertClassificationColor = undefined;

Expand Down Expand Up @@ -950,6 +950,7 @@ Object.defineProperties(UniformState.prototype, {
return this._atmosphereHsbShift;
},
},

/**
* The intensity of the light that is used for computing the atmosphere color
* @memberof UniformState.prototype
Expand All @@ -960,6 +961,7 @@ Object.defineProperties(UniformState.prototype, {
return this._atmosphereLightIntensity;
},
},

/**
* The Rayleigh scattering coefficient used in the atmospheric scattering equations for the sky atmosphere.
* @memberof UniformState.prototype
Expand All @@ -970,6 +972,7 @@ Object.defineProperties(UniformState.prototype, {
return this._atmosphereRayleighCoefficient;
},
},

/**
* The Rayleigh scale height used in the atmospheric scattering equations for the sky atmosphere, in meters.
* @memberof UniformState.prototype
Expand All @@ -980,6 +983,7 @@ Object.defineProperties(UniformState.prototype, {
return this._atmosphereRayleighScaleHeight;
},
},

/**
* The Mie scattering coefficient used in the atmospheric scattering equations for the sky atmosphere.
* @memberof UniformState.prototype
Expand All @@ -990,6 +994,7 @@ Object.defineProperties(UniformState.prototype, {
return this._atmosphereMieCoefficient;
},
},

/**
* The Mie scale height used in the atmospheric scattering equations for the sky atmosphere, in meters.
* @memberof UniformState.prototype
Expand All @@ -1000,6 +1005,7 @@ Object.defineProperties(UniformState.prototype, {
return this._atmosphereMieScaleHeight;
},
},

/**
* The anisotropy of the medium to consider for Mie scattering.
* @memberof UniformState.prototype
Expand All @@ -1010,6 +1016,7 @@ Object.defineProperties(UniformState.prototype, {
return this._atmosphereMieAnisotropy;
},
},

/**
* Which light source to use for dynamically lighting the atmosphere
*
Expand All @@ -1021,14 +1028,28 @@ Object.defineProperties(UniformState.prototype, {
return this._atmosphereDynamicLighting;
},
},
atmosphereLightingFadeDistance: {

/**
* The range of camera distances between which atmosphere lighting fades in/out
*
* @memberof UniformState.prototype
* @type {Cartesian2}
*/
atmosphereLightingFadeRange: {
get: function () {
return this._atmosphereLightingFadeDistance;
return this._atmosphereLightingFadeRange;
},
},
atmosphereNightFadeDistance: {

/**
* The range of camera distances between which day/night shading fades in/out
*
* @memberof UniformState.prototype
* @type {Cartesian2}
*/
atmosphereNightFadeRange: {
get: function () {
return this._atmosphereNightFadeDistance;
return this._atmosphereNightFadeRange;
},
},

Expand Down Expand Up @@ -1553,8 +1574,8 @@ UniformState.prototype.update = function (frameState) {
this._atmosphereDynamicLighting = atmosphere.dynamicLighting;
}

this._atmosphereLightingFadeDistance = atmosphere.lightingFadeDistance;
this._atmosphereNightFadeDistance = atmosphere.nightFadeDistance;
this._atmosphereLightingFadeRange = atmosphere.lightingFadeRange;
this._atmosphereNightFadeRange = atmosphere.nightFadeRange;

this._invertClassificationColor = frameState.invertClassificationColor;

Expand Down
40 changes: 36 additions & 4 deletions packages/engine/Source/Scene/Atmosphere.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian3 from "../Core/Cartesian3.js";
import DynamicAtmosphereLightingType from "./DynamicAtmosphereLightingType.js";

Expand Down Expand Up @@ -123,10 +124,41 @@ function Atmosphere() {
*/
this.dynamicLighting = DynamicAtmosphereLightingType.NONE;

this.lightingFadeOutDistance = 1.0e7;
this.lightingFadeInDistance = 2.0e7;
this.nightFadeInDistance = 5.0e7;
this.nightFadeOutDistance = 1.0e7;
/**
* The range of camera distances <code>[startDist, endDist]</code>
* between which ground atmosphere lighting fades in. When the camera is at or
* below startDist, no ground atmosphere lighting is applied. When the camera
* is at or above endDist,the lighting is at maximum intensity.
* <p>
* Ground atmosphere lighting only applies when {@link Atmosphere#showGroundAtmosphere}
* is <code>true.</code>
* </p>
* <p>
* Camera distances are measured from the center of the earth in meters.
* </p>
*
* @type {Cartesian2}
* @default Cartesian2(1.0e7, 2.0e7)
*/
this.lightingFadeRange = new Cartesian2(1.0e7, 2.0e7);
/**
* The range of camera distances <code>[startDist, endDist]</code>
* between which nighttime shading fades in. When the camera is at or below
* startDist, the entire globe is lit at full brightness. When the camera is
* at or above endDist, the side of the globe facing away from the sun
* will be in total darkness.
* <p>
* Nighttime shading only applies when {@link Atmosphere#showGroundAtmosphere}
* is <code>true</code>, and {@link Atmosphere#dynamicLighting} is not {@link DynamicAtmosphereLightingType.NONE}.
* </p>
* <p>
* Camera distances are measured from the center of the earth in meters.
* </p>
*
* @type {Cartesian2}
* @default Cartesian2(5.0e7, 1.0e7)
*/
this.nightFadeRange = new Cartesian2(1.0e7, 5.0e7);

/**
* Enable the ground atmosphere for 3D Tiles and models. The ground atmosphere
Expand Down
32 changes: 29 additions & 3 deletions packages/engine/Source/Scene/Model/AtmospherePipelineStage.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,37 @@ AtmospherePipelineStage.process = function (
);
};

shaderBuilder.addUniform("bool", "u_perFragmentGroundAtmosphere");
shaderBuilder.addUniform(
"bool",
"u_perFragmentGroundAtmosphere",
ShaderDestination.FRAGMENT
);
renderResources.uniformMap.u_perFragmentGroundAtmosphere = function () {
const cameraDistance = Cartesian3.magnitude(frameState.camera.positionWC);
const nightFadeOutDistance = frameState.atmosphere.nightFadeDistance.x;
return cameraDistance > nightFadeOutDistance;
const nightFadeStart = frameState.atmosphere.nightFadeRange.x;
return cameraDistance > nightFadeStart;
};

shaderBuilder.addUniform(
"bool",
"u_shouldColorCorrect",
ShaderDestination.FRAGMENT
);
renderResources.uniformMap.u_shouldColorCorrect = function () {
const atmosphere = frameState.atmosphere;
return !(
CesiumMath.equalsEpsilon(atmosphere.hueShift, 0.0, CesiumMath.EPSILON7) &&
CesiumMath.equalsEpsilon(
atmosphere.saturationShift,
0.0,
CesiumMath.EPSILON7
) &&
CesiumMath.equalsEpsilon(
atmosphere.brightnessShift,
0.0,
CesiumMath.EPSILON7
)
);
};
};

Expand Down
17 changes: 9 additions & 8 deletions packages/engine/Source/Shaders/Model/AtmosphereStageFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ void applyFog(inout vec4 color, vec4 groundAtmosphereColor, vec3 lightDirection,

#line 1010000

float fade(float x, vec2 range, vec2 clampRange) {
return clamp((x - range.y) / (range.y - range.x), clampRange.x, clampRange.y);
float fade(float x, vec2 range, vec2 outputRange) {
return clamp((x - range.y) / (range.y - range.x), outputRange.x, outputRange.y);
}

float getCameraHeight() {
Expand All @@ -86,7 +86,7 @@ void applyGroundAtmosphere(inout vec4 color, vec4 groundAtmosphereColor, vec3 po

// Blend between constant lighting when the camera is near the earth
// and Lambert diffuse shading when the camera is further away.
float lightingFade = fade(cameraHeight, czm_atmosphereLightingFadeDistance, vec2(0.0, 1.0));
float lightingFade = fade(cameraHeight, czm_atmosphereLightingFadeRange, vec2(0.0, 1.0));

// TODO: Would this work if there weren't normals?
float diffuseIntensity = clamp(dot(normalize(positionWC), atmosphereLightDirection), 0.0, 1.0);
Expand Down Expand Up @@ -117,7 +117,7 @@ void applyGroundAtmosphere(inout vec4 color, vec4 groundAtmosphereColor, vec3 po
vec3 dayNightColor = mix(groundAtmosphereColor.rgb, diffuseAndGroundAtmosphere, dayNightMask);

// Fade in the day/night color in as the camera height increases towards space.
float nightFade = fade(cameraHeight, czm_atmosphereNightFadeDistance, vec2(0.05, 1.0));
float nightFade = fade(cameraHeight, czm_atmosphereNightFadeRange, vec2(0.05, 1.0));
finalAtmosphereColor = mix(diffuseAndGroundAtmosphere, dayNightColor, nightFade);

//#endif
Expand Down Expand Up @@ -170,10 +170,11 @@ void atmosphereStage(inout vec4 color, in ProcessedAttributes attributes) {
opacity = v_atmosphereOpacity;
}

//color correct rayleigh and mie colors
const bool ignoreBlackPixels = true;
rayleighColor = czm_applyHSBShift(rayleighColor, czm_atmosphereHsbShift, ignoreBlackPixels);
mieColor = czm_applyHSBShift(mieColor, czm_atmosphereHsbShift, ignoreBlackPixels);
if (u_shouldColorCorrect) {
const bool ignoreBlackPixels = true;
rayleighColor = czm_applyHSBShift(rayleighColor, czm_atmosphereHsbShift, ignoreBlackPixels);
mieColor = czm_applyHSBShift(mieColor, czm_atmosphereHsbShift, ignoreBlackPixels);
}

vec4 groundAtmosphereColor = czm_computeAtmosphereColor(positionWC, lightDirection, rayleighColor, mieColor, opacity);

Expand Down

0 comments on commit 49e4629

Please sign in to comment.