Skip to content

Commit

Permalink
Revert "Solve struct ellipsoid caused the model to dark"
Browse files Browse the repository at this point in the history
This reverts commit d6f91aa.
  • Loading branch information
Zhouhai authored and Zhouhai committed Jun 19, 2019
1 parent d6f91aa commit b2588e4
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 42 deletions.
1 change: 0 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -211,4 +211,3 @@ See [CONTRIBUTING.md](CONTRIBUTING.md) for details on how to contribute to Cesiu
* [Trubie Turner](https://github.com/flexei)
* [Merijn Wijngaard](https://github.com/mwijngaard)
* [Dennis Adams](https://github.com/dennisadams)
* [Hai Zhou](https://github.com/verybigzhouhai)
4 changes: 2 additions & 2 deletions Source/Scene/processPbrMaterials.js
Original file line number Diff line number Diff line change
Expand Up @@ -740,9 +740,9 @@ define([

fragmentShader += ' vec3 r = normalize(czm_inverseViewRotation * normalize(reflect(v, n)));\n';
// Figure out if the reflection vector hits the ellipsoid
fragmentShader += ' czm_getWgs84EllipsoidEC();\n';
fragmentShader += ' czm_ellipsoid ellipsoid = czm_getWgs84EllipsoidEC();\n';
fragmentShader += ' float vertexRadius = length(positionWC);\n';
fragmentShader += ' float horizonDotNadir = 1.0 - min(1.0, czm_ellipsoid_radii.x / vertexRadius);\n';
fragmentShader += ' float horizonDotNadir = 1.0 - min(1.0, ellipsoid.radii.x / vertexRadius);\n';
fragmentShader += ' float reflectionDotNadir = dot(r, normalize(positionWC));\n';
// Flipping the X vector is a cheap way to get the inverse of czm_temeToPseudoFixed, since that's a rotation about Z.
fragmentShader += ' r.x = -r.x;\n';
Expand Down
6 changes: 2 additions & 4 deletions Source/Shaders/Builtin/Functions/ellipsoidNew.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ czm_ellipsoid czm_ellipsoidNew(vec3 center, vec3 radii)
{
vec3 inverseRadii = vec3(1.0 / radii.x, 1.0 / radii.y, 1.0 / radii.z);
vec3 inverseRadiiSquared = inverseRadii * inverseRadii;
czm_ellipsoid_center = center;
czm_ellipsoid_radii = radii;
czm_ellipsoid_inverseRadii = inverseRadii;
czm_ellipsoid_inverseRadiiSquared = inverseRadiiSquared;
czm_ellipsoid temp = czm_ellipsoid(center, radii, inverseRadii, inverseRadiiSquared);
return temp;
}
10 changes: 4 additions & 6 deletions Source/Shaders/Builtin/Functions/getWgs84EllipsoidEC.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,18 @@
* @name czm_getWgs84EllipsoidEC
* @glslFunction
*
* @returns {czm_ellipsoid} The WGS84 ellipsoid, with its center at the origin of world coordinates, in eye coordinates.
*
* @see Ellipsoid.WGS84
*
* @example
* czm_getWgs84EllipsoidEC();
* czm_ellipsoid ellipsoid = czm_getWgs84EllipsoidEC();
*/
czm_ellipsoid czm_getWgs84EllipsoidEC()
{
vec3 radii = vec3(6378137.0, 6378137.0, 6356752.314245);
vec3 inverseRadii = vec3(1.0 / radii.x, 1.0 / radii.y, 1.0 / radii.z);
vec3 inverseRadiiSquared = inverseRadii * inverseRadii;
czm_ellipsoid_center = czm_view[3].xyz;
czm_ellipsoid_radii = radii;
czm_ellipsoid_inverseRadii = inverseRadii;
czm_ellipsoid_inverseRadiiSquared = inverseRadiiSquared;

czm_ellipsoid temp = czm_ellipsoid(czm_view[3].xyz, radii, inverseRadii, inverseRadiiSquared);
return temp;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
* @name czm_rayEllipsoidIntersectionInterval
* @glslFunction
*/
czm_raySegment czm_rayEllipsoidIntersectionInterval(czm_ray ray, vec3 center, vec3 inverseRadii)
czm_raySegment czm_rayEllipsoidIntersectionInterval(czm_ray ray, czm_ellipsoid ellipsoid)
{
// ray and ellipsoid center in eye coordinates. radii in model coordinates.
vec3 q = inverseRadii * (czm_inverseModelView * vec4(ray.origin, 1.0)).xyz;
vec3 w = inverseRadii * (czm_inverseModelView * vec4(ray.direction, 0.0)).xyz;

q = q - inverseRadii * (czm_inverseModelView * vec4(center, 1.0)).xyz;

// ray and ellipsoid center in eye coordinates. radii in model coordinates.
vec3 q = ellipsoid.inverseRadii * (czm_inverseModelView * vec4(ray.origin, 1.0)).xyz;
vec3 w = ellipsoid.inverseRadii * (czm_inverseModelView * vec4(ray.direction, 0.0)).xyz;
q = q - ellipsoid.inverseRadii * (czm_inverseModelView * vec4(ellipsoid.center, 1.0)).xyz;
float q2 = dot(q, q);
float qw = dot(q, w);

if (q2 > 1.0) // Outside ellipsoid.
{
if (qw >= 0.0) // Looking outward or tangent (0 intersections).
Expand All @@ -27,11 +27,11 @@ czm_raySegment czm_rayEllipsoidIntersectionInterval(czm_ray ray, vec3 center, ve
float difference = q2 - 1.0; // Positively valued.
float w2 = dot(w, w);
float product = w2 * difference;

if (qw2 < product) // Imaginary roots (0 intersections).
{
return czm_emptyRaySegment;
}
return czm_emptyRaySegment;
}
else if (qw2 > product) // Distinct roots (2 intersections).
{
float discriminant = qw * qw - product;
Expand Down
20 changes: 12 additions & 8 deletions Source/Shaders/Builtin/Structs/ellipsoid.glsl
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
/**
*
struct ellipsoid
*/
vec3 czm_ellipsoid_center;
vec3 czm_ellipsoid_radii;
vec3 czm_ellipsoid_inverseRadii;
vec3 czm_ellipsoid_inverseRadiiSquared;
/** DOC_TBA
*
* @name czm_ellipsoid
* @glslStruct
*/
struct czm_ellipsoid
{
vec3 center;
vec3 radii;
vec3 inverseRadii;
vec3 inverseRadiiSquared;
};
4 changes: 2 additions & 2 deletions Source/Shaders/DepthPlaneFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ varying vec4 positionEC;
void main()
{
// TODO: make arbitrary ellipsoid
czm_getWgs84EllipsoidEC();
czm_ellipsoid ellipsoid = czm_getWgs84EllipsoidEC();

vec3 direction = normalize(positionEC.xyz);
czm_ray ray = czm_ray(vec3(0.0), direction);

czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, czm_ellipsoid_center, czm_ellipsoid_inverseRadii);
czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid);
if (!czm_isEmpty(intersection))
{
gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0);
Expand Down
4 changes: 2 additions & 2 deletions Source/Shaders/EllipsoidFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ void main()

// March ray forward to intersection with larger sphere and find
// actual intersection point with ellipsoid.
czm_ellipsoidNew(ellipsoidCenter, u_radii);
czm_ellipsoid ellipsoid = czm_ellipsoidNew(ellipsoidCenter, u_radii);
czm_ray ray = czm_ray(t * direction, direction);
czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, czm_ellipsoid_center, czm_ellipsoid_inverseRadii);
czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid);

if (czm_isEmpty(intersection))
{
Expand Down
7 changes: 3 additions & 4 deletions Source/Shaders/GlobeFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,7 @@ void main()
float fadeOutDist = u_lightingFadeDistance.x;
float fadeInDist = u_lightingFadeDistance.y;
if (czm_sceneMode != czm_sceneMode3D) {
czm_getWgs84EllipsoidEC();
vec3 radii = czm_ellipsoid_radii;
vec3 radii = czm_getWgs84EllipsoidEC().radii;
float maxRadii = max(radii.x, max(radii.y, radii.z));
fadeOutDist -= maxRadii;
fadeInDist -= maxRadii;
Expand Down Expand Up @@ -377,7 +376,7 @@ void main()
}

#if defined(PER_FRAGMENT_GROUND_ATMOSPHERE) && (defined(ENABLE_DAYNIGHT_SHADING) || defined(ENABLE_VERTEX_LIGHTING))
czm_getWgs84EllipsoidEC();
czm_ellipsoid ellipsoid = czm_getWgs84EllipsoidEC();

float mpp = czm_metersPerPixel(vec4(0.0, 0.0, -czm_currentFrustum.x, 1.0));
vec2 xy = gl_FragCoord.xy / czm_viewport.zw * 2.0 - vec2(1.0);
Expand All @@ -386,7 +385,7 @@ void main()
vec3 direction = normalize(vec3(xy, -czm_currentFrustum.x));
czm_ray ray = czm_ray(vec3(0.0), direction);

czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, czm_ellipsoid_center, czm_ellipsoid_inverseRadii);
czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid);

vec3 ellipsoidPosition = czm_pointAlongRay(ray, intersection.start);
ellipsoidPosition = (czm_inverseView * vec4(ellipsoidPosition, 1.0)).xyz;
Expand Down
3 changes: 1 addition & 2 deletions Source/Shaders/GlobeVS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ void main()
#endif

#ifdef APPLY_MATERIAL
czm_getWgs84EllipsoidEC();
float northPoleZ = czm_ellipsoid_radii.z;
float northPoleZ = czm_getWgs84EllipsoidEC().radii.z;
vec3 northPolePositionMC = vec3(0.0, 0.0, northPoleZ);
vec3 ellipsoidNormal = normalize(v_positionMC); // For a sphere this is correct, but not generally for an ellipsoid.
vec3 vectorEastMC = normalize(cross(northPolePositionMC - v_positionMC, ellipsoidNormal));
Expand Down

0 comments on commit b2588e4

Please sign in to comment.