diff --git a/Source/Renderer/AutomaticUniforms.js b/Source/Renderer/AutomaticUniforms.js index e586a10dd367..0e2c2c34e51b 100644 --- a/Source/Renderer/AutomaticUniforms.js +++ b/Source/Renderer/AutomaticUniforms.js @@ -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; diff --git a/Source/Renderer/UniformState.js b/Source/Renderer/UniformState.js index 5b6abd4c795f..298e123004a7 100644 --- a/Source/Renderer/UniformState.js +++ b/Source/Renderer/UniformState.js @@ -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'; @@ -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(); @@ -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); + } } }); @@ -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; diff --git a/Source/Shaders/Builtin/Constants/ellipsoidInverseRadii.glsl b/Source/Shaders/Builtin/Constants/ellipsoidInverseRadii.glsl deleted file mode 100644 index 1350a9a8ded8..000000000000 --- a/Source/Shaders/Builtin/Constants/ellipsoidInverseRadii.glsl +++ /dev/null @@ -1,7 +0,0 @@ -/** - * The reciprocal of the radius of the WGS84 ellipsoid. - * - * @name czm_ellipsoidInverseRadii - * @glslConstant - */ -const vec3 czm_ellipsoidInverseRadii = vec3(1.0 / 6378137.0, 1.0 / 6378137.0, 1.0 / 6356752.314245); diff --git a/Source/Shaders/Builtin/Constants/ellipsoidRadii.glsl b/Source/Shaders/Builtin/Constants/ellipsoidRadii.glsl deleted file mode 100644 index 01574c8b1f1c..000000000000 --- a/Source/Shaders/Builtin/Constants/ellipsoidRadii.glsl +++ /dev/null @@ -1,7 +0,0 @@ -/** - * The radius of the WGS84 ellipsoid. - * - * @name czm_ellipsoidRadii - * @glslConstant - */ -const vec3 czm_ellipsoidRadii = vec3(6378137.0, 6378137.0, 6356752.314245); diff --git a/Specs/Renderer/AutomaticUniformSpec.js b/Specs/Renderer/AutomaticUniformSpec.js index 50f45be2f0bd..c4b37adc88bc 100644 --- a/Specs/Renderer/AutomaticUniformSpec.js +++ b/Specs/Renderer/AutomaticUniformSpec.js @@ -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'; @@ -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');