Skip to content

Commit

Permalink
Fix height
Browse files Browse the repository at this point in the history
  • Loading branch information
ggetz committed Oct 22, 2024
1 parent abd0e39 commit 12588d2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Apps/Sandcastle/gallery/development/PBR Lighting.html
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<input
type="range"
min="0.0"
max="200000.0"
max="280000.0"
step="200"
data-bind="value: height, valueUpdate: 'input'"
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ Object.defineProperties(DynamicEnvironmentMapManager.prototype, {
Cartesian3.equalsEpsilon(
value,
this._position,
0.0,
this.maximumPositionEpsilon,
)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ void czm_computeScattering(

// Return empty colors if no intersection with the atmosphere geometry.
if (primaryRayAtmosphereIntersect == czm_emptyRaySegment) {
rayleighColor = vec3(1.0, 0.0, 1.0);
return;
}

Expand Down
45 changes: 23 additions & 22 deletions packages/engine/Source/Shaders/ComputeRadianceMapFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -26,38 +26,42 @@ void main() {
float ellipsoidHeight = height - atmosphereInnerRadius;
float atmosphereOuterRadius = u_radiiAndDynamicAtmosphereColor.x;
float atmosphereHeight = atmosphereOuterRadius - atmosphereInnerRadius;
float radius = max(atmosphereOuterRadius - height, 2.0 * ellipsoidHeight);

vec3 direction = (u_enuToFixedFrame * getCubeMapDirection(v_textureCoordinates, u_faceDirection)).xyz;
vec3 normalizedDirection = normalize(direction);

czm_ray ray = czm_ray(u_positionWC, normalizedDirection);
czm_raySegment intersection = czm_raySphereIntersectionInterval(ray, vec3(0.0), atmosphereInnerRadius);
float d = czm_branchFreeTernary(czm_isEmpty(intersection), radius, clamp(intersection.start, ellipsoidHeight, radius));
if (!czm_isEmpty(intersection)) {
intersection = czm_rayEllipsoidIntersectionInterval(ray, vec3(0.0), czm_ellipsoidInverseRadii);
}

bool onEllipsoid = intersection.start >= 0.0;
float rayLength = czm_branchFreeTernary(onEllipsoid, intersection.start, atmosphereOuterRadius);

// Compute sky color for each position on a sphere at radius centered around the provided position's origin
vec3 skyPositionWC = u_positionWC + normalizedDirection * d;
vec3 skyPositionWC = u_positionWC + normalizedDirection * rayLength;

float lightEnum = u_radiiAndDynamicAtmosphereColor.z;
vec3 lightDirectionWC = czm_getDynamicAtmosphereLightDirection(skyPositionWC, lightEnum);
vec3 lightDirectionWC = normalize(czm_getDynamicAtmosphereLightDirection(skyPositionWC, lightEnum));
vec3 mieColor;
vec3 rayleighColor;
float opacity;
czm_computeScattering(
ray,
d,
rayLength,
lightDirectionWC,
atmosphereInnerRadius,
rayleighColor,
mieColor,
opacity
);

vec4 skyColor = czm_computeAtmosphereColor(ray, lightDirectionWC, rayleighColor, mieColor, opacity);
vec4 atmopshereColor = czm_computeAtmosphereColor(ray, lightDirectionWC, rayleighColor, mieColor, opacity);

#ifdef ATMOSPHERE_COLOR_CORRECT
const bool ignoreBlackPixels = true;
skyColor.rgb = czm_applyHSBShift(skyColor.rgb, czm_atmosphereHsbShift, ignoreBlackPixels);
atmopshereColor.rgb = czm_applyHSBShift(atmopshereColor.rgb, czm_atmosphereHsbShift, ignoreBlackPixels);
#endif

vec3 lookupDirection = -normalizedDirection;
Expand All @@ -66,26 +70,23 @@ void main() {
lookupDirection = -normalize(czm_temeToPseudoFixed * lookupDirection);
lookupDirection.x = -lookupDirection.x;

vec4 sceneSkyBoxColor = czm_textureCube(czm_environmentMap, lookupDirection);
vec3 skyBackgroundColor = mix(czm_backgroundColor.rgb, sceneSkyBoxColor.rgb, sceneSkyBoxColor.a);
// Values outside the atmopshere are rendered as black, when they should be treated as transparent
float skyAlpha = clamp((1.0 - ellipsoidHeight / atmosphereHeight) * atmopshereColor.a, 0.0, 1.0);
skyAlpha = czm_branchFreeTernary(length(atmopshereColor.rgb) <= czm_epsilon7, 0.0, skyAlpha); // Treat black as transparent

// Apply intensity to sky light
// Blend starmap with atmopshere scattering
float intensity = u_brightnessSaturationGammaIntensity.w;
vec4 adjustedSkyColor = skyColor;
adjustedSkyColor.rgb = skyColor.rgb * intensity;

// Compute ground color based on amount of reflected light
vec3 groundColor = u_groundColor.rgb * u_groundColor.a * adjustedSkyColor.rgb;
vec4 sceneSkyBoxColor = czm_textureCube(czm_environmentMap, lookupDirection);
vec3 skyBackgroundColor = mix(czm_backgroundColor.rgb, sceneSkyBoxColor.rgb, sceneSkyBoxColor.a);
vec4 combinedSkyColor = vec4(mix(skyBackgroundColor, atmopshereColor.rgb * intensity, skyAlpha), 1.0);

// Interpolate the ground color based on angle of sun exposure and distance from the ground
// Compute ground color based on amount of reflected light, then blend it with ground atmosphere based on height
vec3 up = normalize(u_positionWC);
float occlusion = max(dot(lightDirectionWC, up), 0.15);
groundColor = mix(vec3(0.0), u_groundColor.rgb, occlusion * (1.0 - d / radius));

// Only show the stars when not obscured by the ellipsoid
vec3 backgroundColor = czm_branchFreeTernary(czm_isEmpty(intersection), skyBackgroundColor, groundColor);
float occlusion = max(dot(lightDirectionWC, up), 0.05);
vec4 groundColor = vec4(u_groundColor.rgb * u_groundColor.a * (vec3(intensity * occlusion) + atmopshereColor.rgb), 1.0);
vec4 blendedGroundColor = mix(groundColor, atmopshereColor, clamp(ellipsoidHeight / atmosphereHeight, 0.0, 1.0));

vec4 color = vec4(mix(backgroundColor, adjustedSkyColor.rgb, adjustedSkyColor.a), 1.0);
vec4 color = czm_branchFreeTernary(onEllipsoid, blendedGroundColor, combinedSkyColor);

float brightness = u_brightnessSaturationGammaIntensity.x;
float saturation = u_brightnessSaturationGammaIntensity.y;
Expand Down

0 comments on commit 12588d2

Please sign in to comment.