Skip to content

Commit

Permalink
Same blueness everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed May 21, 2020
1 parent 83454b7 commit 42d08d5
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
5 changes: 2 additions & 3 deletions Source/Scene/SkyAtmosphere.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,8 @@ function SkyAtmosphere(ellipsoid) {
// outer radius, inner radius, dynamic atmosphere color flag, inverse scale
var radiiAndDynamicAtmosphereColorAndInverseScale = new Cartesian4();

radiiAndDynamicAtmosphereColorAndInverseScale.x = Cartesian3.maximumComponent(
Cartesian3.multiplyByScalar(ellipsoid.radii, 1.025, new Cartesian3())
);
radiiAndDynamicAtmosphereColorAndInverseScale.x =
ellipsoid.maximumRadius * 1.025;
radiiAndDynamicAtmosphereColorAndInverseScale.y = ellipsoid.maximumRadius;

// Toggles whether the sun position is used. 0 treats the sun as always directly overhead.
Expand Down
2 changes: 1 addition & 1 deletion Source/Shaders/GroundAtmosphere.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ AtmosphereColor computeGroundAtmosphereFromSpace(vec3 v3Pos, bool dynamicLightin
float fFar = length(v3Ray);
v3Ray /= fFar;

float fCameraHeight = length(czm_viewerPositionWC);
float fCameraHeight = czm_eyeHeight + fInnerRadius;
float fCameraHeight2 = fCameraHeight * fCameraHeight;

// This next line is an ANGLE workaround. It is equivalent to B = 2.0 * dot(czm_viewerPositionWC, v3Ray),
Expand Down
24 changes: 15 additions & 9 deletions Source/Shaders/SkyAtmosphereCommon.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,19 @@ vec3 getLightDirection(vec3 positionWC)
return normalize(lightDirection);
}

void calculateRayScatteringFromSpace(in vec3 positionWC, in vec3 ray, in float outerRadius, inout float far, out vec3 start, out float startOffset)
void calculateRayScatteringFromSpace(in vec3 positionWC, in vec3 ray, in float innerRadius, in float outerRadius, inout float far, out vec3 start, out float startOffset)
{
// Calculate the closest intersection of the ray with the outer atmosphere (which is the near point of the ray passing through the atmosphere)
float cameraHeight = length(positionWC);
float B = 2.0 * dot(positionWC, ray);
float cameraHeight = czm_eyeHeight + innerRadius;
vec3 adjustedPositionWC = normalize(positionWC) * cameraHeight;

float B = 2.0 * dot(adjustedPositionWC, ray);
float C = cameraHeight * cameraHeight - outerRadius * outerRadius;
float det = max(0.0, B * B - 4.0 * C);
float near = 0.5 * (-B - sqrt(det));

// Calculate the ray's starting position, then calculate its scattering offset
start = positionWC + ray * near;
start = adjustedPositionWC + ray * near;
far -= near;
float startAngle = dot(ray, start) / outerRadius;
float startDepth = exp(-1.0 / rayleighScaleDepth);
Expand All @@ -94,8 +96,8 @@ void calculateRayScatteringFromSpace(in vec3 positionWC, in vec3 ray, in float o
void calculateRayScatteringFromGround(in vec3 positionWC, in vec3 ray, in float atmosphereScale, in float innerRadius, out vec3 start, out float startOffset)
{
// Calculate the ray's starting position, then calculate its scattering offset
float cameraHeight = length(positionWC);
start = positionWC;
float cameraHeight = czm_eyeHeight + innerRadius;
start = normalize(positionWC) * cameraHeight;
float height = length(start);
float depth = exp((atmosphereScale / rayleighScaleDepth ) * (innerRadius - cameraHeight));
float startAngle = dot(ray, start) / height;
Expand Down Expand Up @@ -173,7 +175,7 @@ void calculateMieColorAndRayleighColor(vec3 outerPositionWC, out vec3 mieColor,
}
else
{
calculateRayScatteringFromSpace(startPositionWC, ray, outerRadius, far, start, startOffset);
calculateRayScatteringFromSpace(startPositionWC, ray, innerRadius, outerRadius, far, start, startOffset);
}
#else
calculateRayScatteringFromGround(startPositionWC, ray, atmosphereScale, innerRadius, start, startOffset);
Expand Down Expand Up @@ -244,11 +246,15 @@ vec4 calculateFinalColor(vec3 positionWC, vec3 toCamera, vec3 lightDirection, ve
float innerRadius = u_radiiAndDynamicAtmosphereColorAndInverseScale.y;
float lightEnum = u_radiiAndDynamicAtmosphereColorAndInverseScale.z;

float cameraHeight = czm_eyeHeight + innerRadius;
vec3 adjustedPositionWC = normalize(positionWC) * cameraHeight;


// Alter alpha based on how close the viewer is to the ground (1.0 = on ground, 0.0 = at edge of atmosphere)
float atmosphereAlpha = clamp((outerRadius - length(positionWC)) / (outerRadius - innerRadius), 0.0, 1.0);
float atmosphereAlpha = clamp((outerRadius - length(adjustedPositionWC)) / (outerRadius - innerRadius), 0.0, 1.0);

// Alter alpha based on time of day (0.0 = night , 1.0 = day)
float nightAlpha = (lightEnum != 0.0) ? clamp(dot(normalize(positionWC), lightDirection), 0.0, 1.0) : 1.0;
float nightAlpha = (lightEnum != 0.0) ? clamp(dot(normalize(adjustedPositionWC), lightDirection), 0.0, 1.0) : 1.0;
atmosphereAlpha *= pow(nightAlpha, 0.5);

vec4 finalColor = vec4(rgb, mix(clamp(rgbExposure.b, 0.0, 1.0), 1.0, atmosphereAlpha) * smoothstep(0.0, 1.0, czm_morphTime));
Expand Down

0 comments on commit 42d08d5

Please sign in to comment.