Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't hardcode WGS84 in shaders #8684

Merged
merged 4 commits into from
Mar 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Change Log

* Interacting with the Cesium canvas will now blur the previously focused element. This prevents unintended modification of input elements when interacting with the globe.
* `TileMapServiceImageryProvider` will now force `minimumLevel` to 0 if the `tilemapresource.xml` metadata request fails and the `rectangle` is too large for the given detail level [#8448](https://github.com/AnalyticalGraphicsInc/cesium/pull/8448)
* Fixed ground atmosphere rendering when using a samller ellipsoid. [#8683](https://github.com/CesiumGS/cesium/issues/8683)
* Fixed globe incorrectly occluding objects when using a smaller ellipsoid. [#7124](https://github.com/CesiumGS/cesium/issues/7124)

### 1.67.0 - 2020-03-02

Expand Down
28 changes: 28 additions & 0 deletions Source/Renderer/AutomaticUniforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -1864,6 +1864,34 @@ import WebGLConstants from '../Core/WebGLConstants.js';
getValue : function(uniformState) {
return uniformState.gamma;
}
}),

/**
* An automatic GLSL uniform that stores the ellipsoid radii.
*
* @alias czm_ellipsoidRadii
* @glslUniform
*/
czm_ellipsoidRadii : new AutomaticUniform({
size : 1,
datatype : WebGLConstants.FLOAT_VEC3,
getValue : function(uniformState) {
return uniformState.ellipsoid.radii;
}
}),

/**
* An automatic GLSL uniform that stores the ellipsoid inverse radii.
*
* @alias czm_ellipsoidRadii
* @glslUniform
*/
czm_ellipsoidInverseRadii : new AutomaticUniform({
size : 1,
datatype : WebGLConstants.FLOAT_VEC3,
getValue : function(uniformState) {
return uniformState.ellipsoid.oneOverRadii;
}
})
};
export default AutomaticUniforms;
15 changes: 15 additions & 0 deletions Source/Renderer/UniformState.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Cartographic from '../Core/Cartographic.js';
import Color from '../Core/Color.js';
import defaultValue from '../Core/defaultValue.js';
import defined from '../Core/defined.js';
import Ellipsoid from '../Core/Ellipsoid.js';
import EncodedCartesian3 from '../Core/EncodedCartesian3.js';
import CesiumMath from '../Core/Math.js';
import Matrix3 from '../Core/Matrix3.js';
Expand Down Expand Up @@ -135,6 +136,7 @@ import SunLight from '../Scene/SunLight.js';
this._pass = undefined;
this._mode = undefined;
this._mapProjection = undefined;
this._ellipsoid = undefined;
this._cameraDirection = new Cartesian3();
this._cameraRight = new Cartesian3();
this._cameraUp = new Cartesian3();
Expand Down Expand Up @@ -998,6 +1000,18 @@ import SunLight from '../Scene/SunLight.js';
get : function() {
return this._orthographicIn3D;
}
},

/**
* The current ellipsoid.
*
* @memberOf UniformState.prototype
* @type {Ellipsoid}
*/
ellipsoid : {
get : function() {
return defaultValue(this._ellipsoid, Ellipsoid.WGS84);
}
}
});

Expand Down Expand Up @@ -1143,6 +1157,7 @@ import SunLight from '../Scene/SunLight.js';
UniformState.prototype.update = function(frameState) {
this._mode = frameState.mode;
this._mapProjection = frameState.mapProjection;
this._ellipsoid = frameState.mapProjection.ellipsoid;
this._pixelRatio = frameState.pixelRatio;

var camera = frameState.camera;
Expand Down
7 changes: 0 additions & 7 deletions Source/Shaders/Builtin/Constants/ellipsoidInverseRadii.glsl

This file was deleted.

7 changes: 0 additions & 7 deletions Source/Shaders/Builtin/Constants/ellipsoidRadii.glsl

This file was deleted.

15 changes: 8 additions & 7 deletions Source/Shaders/GroundAtmosphere.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
// Code: http://sponeil.net/
// GPU Gems 2 Article: https://developer.nvidia.com/gpugems/GPUGems2/gpugems2_chapter16.html

const float fInnerRadius = 6378137.0;
const float fOuterRadius = 6378137.0 * 1.025;
const float fOuterRadius2 = fOuterRadius * fOuterRadius;

const float Kr = 0.0025;
const float Km = 0.0015;
const float ESun = 15.0;
Expand All @@ -48,9 +44,9 @@ const float fKmESun = Km * ESun;
const float fKr4PI = Kr * 4.0 * czm_pi;
const float fKm4PI = Km * 4.0 * czm_pi;

const float fScale = 1.0 / (fOuterRadius - fInnerRadius);
const vec3 v3InvWavelength = vec3(1.0 / pow(0.650, 4.0), 1.0 / pow(0.570, 4.0), 1.0 / pow(0.475, 4.0));

const float fScaleDepth = 0.25;
const float fScaleOverScaleDepth = fScale / fScaleDepth;

struct AtmosphereColor
{
Expand All @@ -69,7 +65,12 @@ float scale(float fCos)

AtmosphereColor computeGroundAtmosphereFromSpace(vec3 v3Pos, bool dynamicLighting, vec3 lightDirectionWC)
{
vec3 v3InvWavelength = vec3(1.0 / pow(0.650, 4.0), 1.0 / pow(0.570, 4.0), 1.0 / pow(0.475, 4.0));
float fInnerRadius = czm_ellipsoidRadii.x;
float fOuterRadius = czm_ellipsoidRadii.x * 1.025;
float fOuterRadius2 = fOuterRadius * fOuterRadius;

float fScale = 1.0 / (fOuterRadius - fInnerRadius);
float fScaleOverScaleDepth = fScale / fScaleDepth;

// Get the ray from the camera to the vertex and its length (which is the far point of the ray passing through the atmosphere)
vec3 v3Ray = v3Pos - czm_viewerPositionWC;
Expand Down
41 changes: 41 additions & 0 deletions Specs/Renderer/AutomaticUniformSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { Cartesian3 } from '../../Source/Cesium.js';
import { Color } from '../../Source/Cesium.js';
import { defaultValue } from '../../Source/Cesium.js';
import { DirectionalLight } from '../../Source/Cesium.js';
import { Ellipsoid } from '../../Source/Cesium.js';
import { GeographicProjection } from '../../Source/Cesium.js';
import { Matrix4 } from '../../Source/Cesium.js';
import { OrthographicFrustum } from '../../Source/Cesium.js';
import { OrthographicOffCenterFrustum } from '../../Source/Cesium.js';
Expand Down Expand Up @@ -1378,4 +1380,43 @@ describe('Renderer/AutomaticUniforms', function() {
}).contextToRender();
});

it('has czm_ellipsoidRadii', function() {
var us = context.uniformState;
var frameState = createFrameState(context, createMockCamera());
var ellipsoid = new Ellipsoid(1.0, 2.0, 3.0);
frameState.mapProjection = new GeographicProjection(ellipsoid);
us.update(frameState);
var fs =
'void main() {' +
' bool b0 = czm_ellipsoidRadii.x == 1.0;' +
' bool b1 = czm_ellipsoidRadii.y == 2.0;' +
' bool b2 = czm_ellipsoidRadii.z == 3.0;' +
' gl_FragColor = vec4(b0 && b1 && b2);' +
'}';
expect({
context : context,
fragmentShader : fs
}).contextToRender();
});

it('has czm_ellipsoidInverseRadii', function() {
var us = context.uniformState;
var frameState = createFrameState(context, createMockCamera());
var ellipsoid = new Ellipsoid(1.0, 1.0 / 2.0, 1.0 / 3.0);
frameState.mapProjection = new GeographicProjection(ellipsoid);
us.update(frameState);
var fs =
'float roundNumber(float number) { return floor(number + 0.5); }' +
'void main() {' +
' bool b0 = roundNumber(czm_ellipsoidInverseRadii.x) == 1.0;' +
' bool b1 = roundNumber(czm_ellipsoidInverseRadii.y) == 2.0;' +
' bool b2 = roundNumber(czm_ellipsoidInverseRadii.z) == 3.0;' +
' gl_FragColor = vec4(b0 && b1 && b2);' +
'}';
expect({
context : context,
fragmentShader : fs
}).contextToRender();
});

}, 'WebGL');