Skip to content

Commit

Permalink
Remove old intersection code
Browse files Browse the repository at this point in the history
  • Loading branch information
lilleyse committed May 22, 2020
1 parent 3c9af3e commit 1234027
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 90 deletions.
27 changes: 10 additions & 17 deletions Source/Scene/SkyAtmosphere.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import Cartesian3 from "../Core/Cartesian3.js";
import Cartesian4 from "../Core/Cartesian4.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import Ellipsoid from "../Core/Ellipsoid.js";
import EllipsoidGeometry from "../Core/EllipsoidGeometry.js";
import GeometryPipeline from "../Core/GeometryPipeline.js";
import CesiumMath from "../Core/Math.js";
import Matrix3 from "../Core/Matrix3.js";
import Matrix4 from "../Core/Matrix4.js";
import VertexFormat from "../Core/VertexFormat.js";
import BufferUsage from "../Renderer/BufferUsage.js";
Expand Down Expand Up @@ -109,27 +107,22 @@ function SkyAtmosphere(ellipsoid) {

this._hueSaturationBrightness = new Cartesian3();

// outer radius, inner radius, dynamic atmosphere color flag, inverse scale
var radiiAndDynamicAtmosphereColorAndInverseScale = new Cartesian4();
// outer radius, inner radius, dynamic atmosphere color flag
var radiiAndDynamicAtmosphereColor = new Cartesian3();

radiiAndDynamicAtmosphereColorAndInverseScale.x =
ellipsoid.maximumRadius * 1.025;
radiiAndDynamicAtmosphereColorAndInverseScale.y = ellipsoid.maximumRadius;
radiiAndDynamicAtmosphereColor.x = ellipsoid.maximumRadius * 1.025;
radiiAndDynamicAtmosphereColor.y = ellipsoid.maximumRadius;

// Toggles whether the sun position is used. 0 treats the sun as always directly overhead.
radiiAndDynamicAtmosphereColorAndInverseScale.z = 0;
radiiAndDynamicAtmosphereColor.z = 0;

// Controls the distance below the horizon at which atmosphere transitions from its brightest to a more neutral blue color
// Higher values push the transition point further below the horizon
radiiAndDynamicAtmosphereColorAndInverseScale.w = 1.003;

this._radiiAndDynamicAtmosphereColorAndInverseScale = radiiAndDynamicAtmosphereColorAndInverseScale;
this._radiiAndDynamicAtmosphereColor = radiiAndDynamicAtmosphereColor;

var that = this;

this._command.uniformMap = {
u_radiiAndDynamicAtmosphereColorAndInverseScale: function () {
return that._radiiAndDynamicAtmosphereColorAndInverseScale;
u_radiiAndDynamicAtmosphereColor: function () {
return that._radiiAndDynamicAtmosphereColor;
},
u_hsbShift: function () {
that._hueSaturationBrightness.x = that.hueShift;
Expand Down Expand Up @@ -163,7 +156,7 @@ SkyAtmosphere.prototype.setDynamicAtmosphereColor = function (
useSunDirection
) {
var lightEnum = enableLighting ? (useSunDirection ? 2.0 : 1.0) : 0.0;
this._radiiAndDynamicAtmosphereColorAndInverseScale.z = lightEnum;
this._radiiAndDynamicAtmosphereColor.z = lightEnum;
};

var scratchModelMatrix = new Matrix4();
Expand Down Expand Up @@ -288,7 +281,7 @@ SkyAtmosphere.prototype.update = function (frameState) {
var cameraPosition = frameState.camera.positionWC;
var cameraHeight = Cartesian3.magnitude(cameraPosition);

if (cameraHeight > this._radiiAndDynamicAtmosphereColorAndInverseScale.x) {
if (cameraHeight > this._radiiAndDynamicAtmosphereColor.x) {
// Camera in space
command.shaderProgram = this._spSkyFromSpace;
} else {
Expand Down
84 changes: 14 additions & 70 deletions Source/Shaders/SkyAtmosphereCommon.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const float g2 = g * g;
uniform vec3 u_hsbShift; // Hue, saturation, brightness
#endif

uniform vec4 u_radiiAndDynamicAtmosphereColorAndInverseScale; // outer radius, inner radius, dynamic atmosphere color flag, inverse scale
uniform vec3 u_radiiAndDynamicAtmosphereColor; // outer radius, inner radius, dynamic atmosphere color flag

float scale(float cosAngle)
{
Expand All @@ -66,7 +66,7 @@ float scale(float cosAngle)

vec3 getLightDirection(vec3 positionWC)
{
float lightEnum = u_radiiAndDynamicAtmosphereColorAndInverseScale.z;
float lightEnum = u_radiiAndDynamicAtmosphereColor.z;
vec3 lightDirection =
positionWC * float(lightEnum == 0.0) +
czm_lightDirectionWC * float(lightEnum == 1.0) +
Expand Down Expand Up @@ -104,81 +104,28 @@ void calculateRayScatteringFromGround(in vec3 positionWC, in vec3 ray, in float
startOffset = depth*scale(startAngle);
}

czm_raySegment rayEllipsoidIntersection(czm_ray ray, vec3 ellipsoid_center, vec3 ellipsoid_inverseRadii)
{
vec3 o = ellipsoid_inverseRadii * (czm_inverseModelView * vec4(ray.origin, 1.0)).xyz;
vec3 d = ellipsoid_inverseRadii * (czm_inverseModelView * 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);
}

void calculateMieColorAndRayleighColor(vec3 outerPositionWC, out vec3 mieColor, out vec3 rayleighColor)
{
// Unpack attributes
float outerRadius = u_radiiAndDynamicAtmosphereColorAndInverseScale.x;
float innerRadius = u_radiiAndDynamicAtmosphereColorAndInverseScale.y;
float inverseScale = u_radiiAndDynamicAtmosphereColorAndInverseScale.w;

vec3 directionWC = normalize(outerPositionWC - czm_viewerPositionWC);
vec3 directionEC = czm_viewRotation * directionWC;
czm_ray viewRay = czm_ray(vec3(0.0), directionEC);
czm_raySegment raySegment = rayEllipsoidIntersection(viewRay, vec3(czm_view[3]), czm_ellipsoidInverseRadii * inverseScale);
bool intersectsEllipsoid = false;//raySegment.start >= 0.0;

vec3 startPositionWC = czm_viewerPositionWC;
if (intersectsEllipsoid)
{
startPositionWC = czm_viewerPositionWC + raySegment.stop * directionWC;
}
float outerRadius = u_radiiAndDynamicAtmosphereColor.x;
float innerRadius = u_radiiAndDynamicAtmosphereColor.y;

vec3 lightDirection = getLightDirection(startPositionWC);
vec3 lightDirection = getLightDirection(czm_viewerPositionWC);

// Get the ray from the start position to the outer position and its length (which is the far point of the ray passing through the atmosphere)
vec3 ray = outerPositionWC - startPositionWC;
float distance = length(ray);
ray /= distance;

float maxDistance = intersectsEllipsoid ? innerRadius * 0.1 : distance;
float far = min(distance, maxDistance);
vec3 ray = outerPositionWC - czm_viewerPositionWC;
float far = length(ray);
ray /= far;

float atmosphereScale = 1.0 / (outerRadius - innerRadius);

vec3 start;
float startOffset;

#ifdef SKY_FROM_SPACE
if (intersectsEllipsoid)
{
calculateRayScatteringFromGround(startPositionWC, ray, atmosphereScale, innerRadius, start, startOffset);
}
else
{
calculateRayScatteringFromSpace(startPositionWC, ray, innerRadius, outerRadius, far, start, startOffset);
}
calculateRayScatteringFromSpace(czm_viewerPositionWC, ray, innerRadius, outerRadius, far, start, startOffset);
#else
calculateRayScatteringFromGround(startPositionWC, ray, atmosphereScale, innerRadius, start, startOffset);
calculateRayScatteringFromGround(czm_viewerPositionWC, ray, atmosphereScale, innerRadius, start, startOffset);
#endif

// Initialize the scattering loop variables
Expand Down Expand Up @@ -209,7 +156,6 @@ void calculateMieColorAndRayleighColor(vec3 outerPositionWC, out vec3 mieColor,
// Cap mie and rayleigh colors to prevent NaNs when vertex interpolation happens
mieColor = min(mieColor, vec3(10000000.0));
rayleighColor = min(rayleighColor, vec3(10000000.0));

}

vec4 calculateFinalColor(vec3 positionWC, vec3 toCamera, vec3 lightDirection, vec3 mieColor, vec3 rayleighColor)
Expand All @@ -219,9 +165,7 @@ vec4 calculateFinalColor(vec3 positionWC, vec3 toCamera, vec3 lightDirection, ve
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 rayleighFinal = rayleighPhase * rayleighColor;
vec3 mieFinal = miePhase * mieColor;
vec3 rgb = rayleighFinal + mieFinal;
vec3 rgb = rayleighPhase * rayleighColor + miePhase * mieColor;

const float exposure = 2.0;
vec3 rgbExposure = vec3(1.0) - exp(-exposure * rgb);
Expand All @@ -241,9 +185,9 @@ vec4 calculateFinalColor(vec3 positionWC, vec3 toCamera, vec3 lightDirection, ve
rgb = czm_HSBToRGB(hsb);
#endif

float outerRadius = u_radiiAndDynamicAtmosphereColorAndInverseScale.x;
float innerRadius = u_radiiAndDynamicAtmosphereColorAndInverseScale.y;
float lightEnum = u_radiiAndDynamicAtmosphereColorAndInverseScale.z;
float outerRadius = u_radiiAndDynamicAtmosphereColor.x;
float innerRadius = u_radiiAndDynamicAtmosphereColor.y;
float lightEnum = u_radiiAndDynamicAtmosphereColor.z;

float cameraHeight = czm_eyeHeight + innerRadius;

Expand Down
6 changes: 3 additions & 3 deletions Specs/Scene/SkyAtmosphereSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe(

var command = s.update(scene.frameState);
expect(command).toBeDefined();
expect(s._radiiAndDynamicAtmosphereColorAndInverseScale.z).toBe(1);
expect(s._radiiAndDynamicAtmosphereColor.z).toBe(1);
command.execute(scene.context); // Not reliable enough across browsers to test pixels

s.destroy();
Expand All @@ -72,7 +72,7 @@ describe(

var command = s.update(scene.frameState);
expect(command).toBeDefined();
expect(s._radiiAndDynamicAtmosphereColorAndInverseScale.z).toBe(2);
expect(s._radiiAndDynamicAtmosphereColor.z).toBe(2);
command.execute(scene.context); // Not reliable enough across browsers to test pixels

s.destroy();
Expand All @@ -87,7 +87,7 @@ describe(

var command = s.update(scene.frameState);
expect(command).toBeDefined();
expect(s._radiiAndDynamicAtmosphereColorAndInverseScale.z).toBe(0);
expect(s._radiiAndDynamicAtmosphereColor.z).toBe(0);
command.execute(scene.context); // Not reliable enough across browsers to test pixels

s.destroy();
Expand Down

0 comments on commit 1234027

Please sign in to comment.