123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
-
-
- const float Kr = 0.0025;
- const float Kr4PI = Kr * 4.0 * czm_pi;
- const float Km = 0.0015;
- const float Km4PI = Km * 4.0 * czm_pi;
- const float ESun = 15.0;
- const float KmESun = Km * ESun;
- const float KrESun = Kr * ESun;
- const vec3 InvWavelength = vec3(
- 5.60204474633241,
- 9.473284437923038,
- 19.643802610477206);
- const float rayleighScaleDepth = 0.25;
- const int nSamples = 2;
- const float fSamples = 2.0;
- const float g = -0.95;
- const float g2 = g * g;
- #ifdef COLOR_CORRECT
- uniform vec3 u_hsbShift;
- #endif
- uniform vec3 u_radiiAndDynamicAtmosphereColor;
- float scale(float cosAngle)
- {
- float x = 1.0 - cosAngle;
- return rayleighScaleDepth * exp(-0.00287 + x*(0.459 + x*(3.83 + x*(-6.80 + x*5.25))));
- }
- vec3 getLightDirection(vec3 positionWC)
- {
- float lightEnum = u_radiiAndDynamicAtmosphereColor.z;
- vec3 lightDirection =
- positionWC * float(lightEnum == 0.0) +
- czm_lightDirectionWC * float(lightEnum == 1.0) +
- czm_sunDirectionWC * float(lightEnum == 2.0);
- return normalize(lightDirection);
- }
- void calculateRayScatteringFromSpace(in vec3 positionWC, in vec3 ray, in float innerRadius, in float outerRadius, inout float far, out vec3 start, out float startOffset)
- {
-
- float cameraHeight = length(positionWC);
- float B = 2.0 * dot(positionWC, ray);
- float C = cameraHeight * cameraHeight - outerRadius * outerRadius;
- float det = max(0.0, B * B - 4.0 * C);
- float near = 0.5 * (-B - sqrt(det));
-
- start = positionWC + ray * near;
- far -= near;
- float startAngle = dot(ray, start) / outerRadius;
- float startDepth = exp(-1.0 / rayleighScaleDepth);
- startOffset = startDepth * scale(startAngle);
- }
- void calculateRayScatteringFromGround(in vec3 positionWC, in vec3 ray, in float atmosphereScale, in float innerRadius, out vec3 start, out float startOffset)
- {
-
- float cameraHeight = length(positionWC);
- start = positionWC;
- float height = length(start);
- float depth = exp((atmosphereScale / rayleighScaleDepth ) * (innerRadius - cameraHeight));
- float startAngle = dot(ray, start) / height;
- startOffset = depth*scale(startAngle);
- }
- czm_raySegment rayEllipsoidIntersection(czm_ray ray, vec3 inverseRadii)
- {
- vec3 o = inverseRadii * (czm_inverseView * vec4(ray.origin, 1.0)).xyz;
- vec3 d = inverseRadii * (czm_inverseView * vec4(ray.direction, 0.0)).xyz;
- float a = dot(d, d);
- float b = dot(d, o);
- float c = dot(o, o) - 1.0;
- float discriminant = b * b - a * c;
- if (discriminant < 0.0)
- {
- return czm_emptyRaySegment;
- }
- discriminant = sqrt(discriminant);
- float t1 = (-b - discriminant) / a;
- float t2 = (-b + discriminant) / a;
- if (t1 < 0.0 && t2 < 0.0)
- {
- return czm_emptyRaySegment;
- }
- if (t1 < 0.0 && t2 >= 0.0)
- {
- t1 = 0.0;
- }
- return czm_raySegment(t1, t2);
- }
- vec3 getAdjustedPosition(vec3 positionWC, float innerRadius)
- {
-
- float cameraHeight = czm_eyeHeight + innerRadius;
- return normalize(positionWC) * cameraHeight;
- }
- vec3 getTranslucentPosition(vec3 positionWC, vec3 outerPositionWC, float innerRadius, out bool intersectsEllipsoid)
- {
- vec3 directionWC = normalize(outerPositionWC - positionWC);
- vec3 directionEC = czm_viewRotation * directionWC;
- czm_ray viewRay = czm_ray(vec3(0.0), directionEC);
- czm_raySegment raySegment = rayEllipsoidIntersection(viewRay, czm_ellipsoidInverseRadii);
- intersectsEllipsoid = raySegment.start >= 0.0;
- if (intersectsEllipsoid)
- {
- return positionWC + raySegment.stop * directionWC;
- }
- return getAdjustedPosition(positionWC, innerRadius);
- }
- void calculateMieColorAndRayleighColor(vec3 outerPositionWC, out vec3 mieColor, out vec3 rayleighColor)
- {
-
- float outerRadius = u_radiiAndDynamicAtmosphereColor.x;
- float innerRadius = u_radiiAndDynamicAtmosphereColor.y;
- #ifdef GLOBE_TRANSLUCENT
- bool intersectsEllipsoid = false;
- vec3 startPositionWC = getTranslucentPosition(czm_viewerPositionWC, outerPositionWC, innerRadius, intersectsEllipsoid);
- #else
- vec3 startPositionWC = getAdjustedPosition(czm_viewerPositionWC, innerRadius);
- #endif
- vec3 lightDirection = getLightDirection(startPositionWC);
-
- vec3 ray = outerPositionWC - startPositionWC;
- float far = length(ray);
- ray /= far;
- float atmosphereScale = 1.0 / (outerRadius - innerRadius);
- vec3 start;
- float startOffset;
- #ifdef SKY_FROM_SPACE
- #ifdef GLOBE_TRANSLUCENT
- if (intersectsEllipsoid)
- {
- calculateRayScatteringFromGround(startPositionWC, ray, atmosphereScale, innerRadius, start, startOffset);
- }
- else
- {
- calculateRayScatteringFromSpace(startPositionWC, ray, innerRadius, outerRadius, far, start, startOffset);
- }
- #else
- calculateRayScatteringFromSpace(startPositionWC, ray, innerRadius, outerRadius, far, start, startOffset);
- #endif
- #else
- calculateRayScatteringFromGround(startPositionWC, ray, atmosphereScale, innerRadius, start, startOffset);
- #endif
-
- float sampleLength = far / fSamples;
- float scaledLength = sampleLength * atmosphereScale;
- vec3 sampleRay = ray * sampleLength;
- vec3 samplePoint = start + sampleRay * 0.5;
-
- vec3 frontColor = vec3(0.0, 0.0, 0.0);
- for (int i = 0; i<nSamples; i++)
- {
- float height = length(samplePoint);
- float depth = exp((atmosphereScale / rayleighScaleDepth ) * (innerRadius - height));
- float fLightAngle = dot(lightDirection, samplePoint) / height;
- float fCameraAngle = dot(ray, samplePoint) / height;
- float fScatter = (startOffset + depth*(scale(fLightAngle) - scale(fCameraAngle)));
- vec3 attenuate = exp(-fScatter * (InvWavelength * Kr4PI + Km4PI));
- frontColor += attenuate * (depth * scaledLength);
- samplePoint += sampleRay;
- }
-
- mieColor = frontColor * KmESun;
- rayleighColor = frontColor * (InvWavelength * KrESun);
-
- mieColor = min(mieColor, vec3(10000000.0));
- rayleighColor = min(rayleighColor, vec3(10000000.0));
- }
- vec4 calculateFinalColor(vec3 positionWC, vec3 toCamera, vec3 lightDirection, vec3 mieColor, vec3 rayleighColor)
- {
-
- float cosAngle = dot(lightDirection, normalize(toCamera)) / length(toCamera);
- float rayleighPhase = 0.75 * (1.0 + cosAngle * cosAngle);
- float miePhase = 1.5 * ((1.0 - g2) / (2.0 + g2)) * (1.0 + cosAngle * cosAngle) / pow(1.0 + g2 - 2.0 * g * cosAngle, 1.5);
- vec3 rgb = rayleighPhase * rayleighColor + miePhase * mieColor;
- const float exposure = 2.0;
- vec3 rgbExposure = vec3(1.0) - exp(-exposure * rgb);
- #ifndef HDR
- rgb = rgbExposure;
- #endif
- #ifdef COLOR_CORRECT
-
- vec3 hsb = czm_RGBToHSB(rgb);
-
- hsb.x += u_hsbShift.x;
- hsb.y = clamp(hsb.y + u_hsbShift.y, 0.0, 1.0);
- hsb.z = hsb.z > czm_epsilon7 ? hsb.z + u_hsbShift.z : 0.0;
-
- rgb = czm_HSBToRGB(hsb);
- #endif
- float outerRadius = u_radiiAndDynamicAtmosphereColor.x;
- float innerRadius = u_radiiAndDynamicAtmosphereColor.y;
- float lightEnum = u_radiiAndDynamicAtmosphereColor.z;
- float cameraHeight = czm_eyeHeight + innerRadius;
-
- float atmosphereAlpha = clamp((outerRadius - cameraHeight) / (outerRadius - innerRadius), 0.0, 1.0);
-
- float nightAlpha = (lightEnum != 0.0) ? clamp(dot(normalize(positionWC), 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));
- if (mieColor.b > 1.0)
- {
-
-
- float strength = mieColor.b;
- float minDistance = outerRadius;
- float maxDistance = outerRadius * 3.0;
- float maxStrengthLerp = 1.0 - clamp((maxDistance - cameraHeight) / (maxDistance - minDistance), 0.0, 1.0);
- float maxStrength = mix(100.0, 10000.0, maxStrengthLerp);
- strength = min(strength, maxStrength);
- float alpha = 1.0 - (strength / maxStrength);
- finalColor.a = alpha;
- }
- return finalColor;
- }
|