diff --git a/CHANGES.md b/CHANGES.md index f85e84649901..586052c56bf7 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -6,6 +6,8 @@ Beta Releases ### b21 - 2013-10-01 * Added `CorridorOutlineGeometry`. +* Improved runtime generation of GLSL shaders. +* Added new built-in GLSL functions `czm_getLambertDiffuse` and `czm_getSpecular`. ### b20 - 2013-09-03 diff --git a/Source/Renderer/AutomaticUniforms.js b/Source/Renderer/AutomaticUniforms.js new file mode 100644 index 000000000000..2e387a323515 --- /dev/null +++ b/Source/Renderer/AutomaticUniforms.js @@ -0,0 +1,1525 @@ +/*global define*/ +define([ + './UniformDatatype' + ],function( + UniformDatatype) { + + "use strict"; + + return { + /** + * An automatic GLSL uniform containing the viewport's x, y, width, + * and height properties in an vec4's x, y, z, + * and w components, respectively. + * + * @alias czm_viewport + * @glslUniform + * + * @see Context#getViewport + * + * @example + * // GLSL declaration + * uniform vec4 czm_viewport; + * + * // Scale the window coordinate components to [0, 1] by dividing + * // by the viewport's width and height. + * vec2 v = gl_FragCoord.xy / czm_viewport.zw; + */ + czm_viewport : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR4; + }, + + getValue : function(uniformState) { + var v = uniformState.getViewport(); + return { + x : v.x, + y : v.y, + z : v.width, + w : v.height + }; + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 orthographic projection matrix that + * transforms window coordinates to clip coordinates. Clip coordinates is the + * coordinate system for a vertex shader's gl_Position output. + *

+ * This transform is useful when a vertex shader inputs or manipulates window coordinates + * as done by {@link BillboardCollection}. + *

+ * Do not confuse {@link czm_viewportTransformation} with czm_viewportOrthographic. + * The former transforms from normalized device coordinates to window coordinates; the later transforms + * from window coordinates to clip coordinates, and is often used to assign to gl_Position. + * + * @alias czm_viewportOrthographic + * @glslUniform + * + * @see UniformState#getViewportOrthographic + * @see czm_viewport + * @see czm_viewportTransformation + * @see BillboardCollection + * + * @example + * // GLSL declaration + * uniform mat4 czm_viewportOrthographic; + * + * // Example + * gl_Position = czm_viewportOrthographic * vec4(windowPosition, 0.0, 1.0); + */ + czm_viewportOrthographic : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getViewportOrthographic(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 transformation matrix that + * transforms normalized device coordinates to window coordinates. The context's + * full viewport is used, and the depth range is assumed to be near = 0 + * and far = 1. + *

+ * This transform is useful when there is a need to manipulate window coordinates + * in a vertex shader as done by {@link BillboardCollection}. In many cases, + * this matrix will not be used directly; instead, {@link czm_modelToWindowCoordinates} + * will be used to transform directly from model to window coordinates. + *

+ * Do not confuse czm_viewportTransformation with {@link czm_viewportOrthographic}. + * The former transforms from normalized device coordinates to window coordinates; the later transforms + * from window coordinates to clip coordinates, and is often used to assign to gl_Position. + * + * @alias czm_viewportTransformation + * @glslUniform + * + * @see UniformState#getViewportTransformation + * @see czm_viewport + * @see czm_viewportOrthographic + * @see czm_modelToWindowCoordinates + * @see BillboardCollection + * + * @example + * // GLSL declaration + * uniform mat4 czm_viewportTransformation; + * + * // Use czm_viewportTransformation as part of the + * // transform from model to window coordinates. + * vec4 q = czm_modelViewProjection * positionMC; // model to clip coordinates + * q.xyz /= q.w; // clip to normalized device coordinates (ndc) + * q.xyz = (czm_viewportTransformation * vec4(q.xyz, 1.0)).xyz; // ndc to window coordinates + */ + czm_viewportTransformation : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getViewportTransformation(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 model transformation matrix that + * transforms model coordinates to world coordinates. + * + * @alias czm_model + * @glslUniform + * + * @see UniformState#getModel + * @see czm_inverseModel + * @see czm_modelView + * @see czm_modelViewProjection + * + * @example + * // GLSL declaration + * uniform mat4 czm_model; + * + * // Example + * vec4 worldPosition = czm_model * modelPosition; + */ + czm_model : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getModel(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 model transformation matrix that + * transforms world coordinates to model coordinates. + * + * @alias czm_inverseModel + * @glslUniform + * + * @see UniformState#getInverseModel + * @see czm_model + * @see czm_inverseModelView + * + * @example + * // GLSL declaration + * uniform mat4 czm_inverseModel; + * + * // Example + * vec4 modelPosition = czm_inverseModel * worldPosition; + */ + czm_inverseModel : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getInverseModel(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 view transformation matrix that + * transforms world coordinates to eye coordinates. + * + * @alias czm_view + * @glslUniform + * + * @see UniformState#getView + * @see czm_viewRotation + * @see czm_modelView + * @see czm_viewProjection + * @see czm_modelViewProjection + * @see czm_inverseView + * + * @example + * // GLSL declaration + * uniform mat4 czm_view; + * + * // Example + * vec4 eyePosition = czm_view * worldPosition; + */ + czm_view : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getView(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 view transformation matrix that + * transforms 3D world coordinates to eye coordinates. In 3D mode, this is identical to + * {@link czm_view}, but in 2D and Columbus View it represents the view matrix + * as if the camera were at an equivalent location in 3D mode. This is useful for lighting + * 2D and Columbus View in the same way that 3D is lit. + * + * @alias czm_view3D + * @glslUniform + * + * @see UniformState#getView3D + * @see czm_view + * + * @example + * // GLSL declaration + * uniform mat4 czm_view3D; + * + * // Example + * vec4 eyePosition3D = czm_view3D * worldPosition3D; + */ + czm_view3D : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getView3D(); + } + }, + + /** + * An automatic GLSL uniform representing a 3x3 view rotation matrix that + * transforms vectors in world coordinates to eye coordinates. + * + * @alias czm_viewRotation + * @glslUniform + * + * @see UniformState#getViewRotation + * @see czm_view + * @see czm_inverseView + * @see czm_inverseViewRotation + * + * @example + * // GLSL declaration + * uniform mat3 czm_viewRotation; + * + * // Example + * vec3 eyeVector = czm_viewRotation * worldVector; + */ + czm_viewRotation : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX3; + }, + + getValue : function(uniformState) { + return uniformState.getViewRotation(); + } + }, + + /** + * An automatic GLSL uniform representing a 3x3 view rotation matrix that + * transforms vectors in 3D world coordinates to eye coordinates. In 3D mode, this is identical to + * {@link czm_viewRotation}, but in 2D and Columbus View it represents the view matrix + * as if the camera were at an equivalent location in 3D mode. This is useful for lighting + * 2D and Columbus View in the same way that 3D is lit. + * + * @alias czm_viewRotation3D + * @glslUniform + * + * @see UniformState#getViewRotation3D + * @see czm_viewRotation + * + * @example + * // GLSL declaration + * uniform mat3 czm_viewRotation3D; + * + * // Example + * vec3 eyeVector = czm_viewRotation3D * worldVector; + */ + czm_viewRotation3D : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX3; + }, + + getValue : function(uniformState) { + return uniformState.getViewRotation3D(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 transformation matrix that + * transforms from eye coordinates to world coordinates. + * + * @alias czm_inverseView + * @glslUniform + * + * @see UniformState#getInverseView + * @see czm_view + * @see czm_inverseNormal + * + * @example + * // GLSL declaration + * uniform mat4 czm_inverseView; + * + * // Example + * vec4 worldPosition = czm_inverseView * eyePosition; + */ + czm_inverseView : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getInverseView(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 transformation matrix that + * transforms from 3D eye coordinates to world coordinates. In 3D mode, this is identical to + * {@link czm_inverseView}, but in 2D and Columbus View it represents the inverse view matrix + * as if the camera were at an equivalent location in 3D mode. This is useful for lighting + * 2D and Columbus View in the same way that 3D is lit. + * + * @alias czm_inverseView3D + * @glslUniform + * + * @see UniformState#getInverseView3D + * @see czm_inverseView + * + * @example + * // GLSL declaration + * uniform mat4 czm_inverseView3D; + * + * // Example + * vec4 worldPosition = czm_inverseView3D * eyePosition; + */ + czm_inverseView3D : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getInverseView3D(); + } + }, + + /** + * An automatic GLSL uniform representing a 3x3 rotation matrix that + * transforms vectors from eye coordinates to world coordinates. + * + * @alias czm_inverseViewRotation + * @glslUniform + * + * @see UniformState#getInverseView + * @see czm_view + * @see czm_viewRotation + * @see czm_inverseViewRotation + * + * @example + * // GLSL declaration + * uniform mat3 czm_inverseViewRotation; + * + * // Example + * vec4 worldVector = czm_inverseViewRotation * eyeVector; + */ + czm_inverseViewRotation : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX3; + }, + + getValue : function(uniformState) { + return uniformState.getInverseViewRotation(); + } + }, + + /** + * An automatic GLSL uniform representing a 3x3 rotation matrix that + * transforms vectors from 3D eye coordinates to world coordinates. In 3D mode, this is identical to + * {@link czm_inverseViewRotation}, but in 2D and Columbus View it represents the inverse view matrix + * as if the camera were at an equivalent location in 3D mode. This is useful for lighting + * 2D and Columbus View in the same way that 3D is lit. + * + * @alias czm_inverseViewRotation3D + * @glslUniform + * + * @see UniformState#getInverseView3D + * @see czm_inverseViewRotation + * + * @example + * // GLSL declaration + * uniform mat3 czm_inverseViewRotation3D; + * + * // Example + * vec4 worldVector = czm_inverseViewRotation3D * eyeVector; + */ + czm_inverseViewRotation3D : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX3; + }, + + getValue : function(uniformState) { + return uniformState.getInverseViewRotation3D(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 projection transformation matrix that + * transforms eye coordinates to clip coordinates. Clip coordinates is the + * coordinate system for a vertex shader's gl_Position output. + * + * @alias czm_projection + * @glslUniform + * + * @see UniformState#getProjection + * @see czm_viewProjection + * @see czm_modelViewProjection + * @see czm_infiniteProjection + * + * @example + * // GLSL declaration + * uniform mat4 czm_projection; + * + * // Example + * gl_Position = czm_projection * eyePosition; + */ + czm_projection : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getProjection(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 inverse projection transformation matrix that + * transforms from clip coordinates to eye coordinates. Clip coordinates is the + * coordinate system for a vertex shader's gl_Position output. + * + * @alias czm_inverseProjection + * @glslUniform + * + * @see UniformState#getInverseProjection + * @see czm_projection + * + * @example + * // GLSL declaration + * uniform mat4 czm_inverseProjection; + * + * // Example + * vec4 eyePosition = czm_inverseProjection * clipPosition; + */ + czm_inverseProjection : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getInverseProjection(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 projection transformation matrix with the far plane at infinity, + * that transforms eye coordinates to clip coordinates. Clip coordinates is the + * coordinate system for a vertex shader's gl_Position output. An infinite far plane is used + * in algorithms like shadow volumes and GPU ray casting with proxy geometry to ensure that triangles + * are not clipped by the far plane. + * + * @alias czm_infiniteProjection + * @glslUniform + * + * @see UniformState#getInfiniteProjection + * @see czm_projection + * @see czm_modelViewInfiniteProjection + * + * @example + * // GLSL declaration + * uniform mat4 czm_infiniteProjection; + * + * // Example + * gl_Position = czm_infiniteProjection * eyePosition; + */ + czm_infiniteProjection : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getInfiniteProjection(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 model-view transformation matrix that + * transforms model coordinates to eye coordinates. + *

+ * Positions should be transformed to eye coordinates using czm_modelView and + * normals should be transformed using {@link czm_normal}. + * + * @alias czm_modelView + * @glslUniform + * + * @see UniformState#getModelView + * @see czm_model + * @see czm_view + * @see czm_modelViewProjection + * @see czm_normal + * + * @example + * // GLSL declaration + * uniform mat4 czm_modelView; + * + * // Example + * vec4 eyePosition = czm_modelView * modelPosition; + * + * // The above is equivalent to, but more efficient than: + * vec4 eyePosition = czm_view * czm_model * modelPosition; + */ + czm_modelView : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getModelView(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 model-view transformation matrix that + * transforms 3D model coordinates to eye coordinates. In 3D mode, this is identical to + * {@link czm_modelView}, but in 2D and Columbus View it represents the model-view matrix + * as if the camera were at an equivalent location in 3D mode. This is useful for lighting + * 2D and Columbus View in the same way that 3D is lit. + *

+ * Positions should be transformed to eye coordinates using czm_modelView3D and + * normals should be transformed using {@link czm_normal3D}. + * + * @alias czm_modelView3D + * @glslUniform + * + * @see UniformState#getModelView3D + * @see czm_modelView + * + * @example + * // GLSL declaration + * uniform mat4 czm_modelView3D; + * + * // Example + * vec4 eyePosition = czm_modelView3D * modelPosition; + * + * // The above is equivalent to, but more efficient than: + * vec4 eyePosition = czm_view3D * czm_model * modelPosition; + */ + czm_modelView3D : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getModelView3D(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 model-view transformation matrix that + * transforms model coordinates, relative to the eye, to eye coordinates. This is used + * in conjunction with {@link czm_translateRelativeToEye}. + * + * @alias czm_modelViewRelativeToEye + * @glslUniform + * + * @example + * // GLSL declaration + * uniform mat4 czm_modelViewRelativeToEye; + * + * // Example + * attribute vec3 positionHigh; + * attribute vec3 positionLow; + * + * void main() + * { + * vec4 p = czm_translateRelativeToEye(positionHigh, positionLow); + * gl_Position = czm_projection * (czm_modelViewRelativeToEye * p); + * } + * + * @see czm_modelViewProjectionRelativeToEye + * @see czm_translateRelativeToEye + * @see EncodedCartesian3 + */ + czm_modelViewRelativeToEye : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getModelViewRelativeToEye(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 transformation matrix that + * transforms from eye coordinates to model coordinates. + * + * @alias czm_inverseModelView + * @glslUniform + * + * @see UniformState#getInverseModelView + * @see czm_modelView + * + * @example + * // GLSL declaration + * uniform mat4 czm_inverseModelView; + * + * // Example + * vec4 modelPosition = czm_inverseModelView * eyePosition; + */ + czm_inverseModelView : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getInverseModelView(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 transformation matrix that + * transforms from eye coordinates to 3D model coordinates. In 3D mode, this is identical to + * {@link czm_inverseModelView}, but in 2D and Columbus View it represents the inverse model-view matrix + * as if the camera were at an equivalent location in 3D mode. This is useful for lighting + * 2D and Columbus View in the same way that 3D is lit. + * + * @alias czm_inverseModelView3D + * @glslUniform + * + * @see UniformState#getInverseModelView + * @see czm_inverseModelView + * @see czm_modelView3D + * + * @example + * // GLSL declaration + * uniform mat4 czm_inverseModelView3D; + * + * // Example + * vec4 modelPosition = czm_inverseModelView3D * eyePosition; + */ + czm_inverseModelView3D : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getInverseModelView3D(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 view-projection transformation matrix that + * transforms world coordinates to clip coordinates. Clip coordinates is the + * coordinate system for a vertex shader's gl_Position output. + * + * @alias czm_viewProjection + * @glslUniform + * + * @see UniformState#getViewProjection + * @see czm_view + * @see czm_projection + * @see czm_modelViewProjection + * + * @example + * // GLSL declaration + * uniform mat4 czm_viewProjection; + * + * // Example + * vec4 gl_Position = czm_viewProjection * czm_model * modelPosition; + * + * // The above is equivalent to, but more efficient than: + * gl_Position = czm_projection * czm_view * czm_model * modelPosition; + */ + czm_viewProjection : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getViewProjection(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 model-view-projection transformation matrix that + * transforms model coordinates to clip coordinates. Clip coordinates is the + * coordinate system for a vertex shader's gl_Position output. + * + * @alias czm_modelViewProjection + * @glslUniform + * + * @see UniformState#getModelViewProjection + * @see czm_model + * @see czm_view + * @see czm_projection + * @see czm_modelView + * @see czm_viewProjection + * @see czm_modelViewInfiniteProjection + * + * @example + * // GLSL declaration + * uniform mat4 czm_modelViewProjection; + * + * // Example + * vec4 gl_Position = czm_modelViewProjection * modelPosition; + * + * // The above is equivalent to, but more efficient than: + * gl_Position = czm_projection * czm_view * czm_model * modelPosition; + */ + czm_modelViewProjection : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getModelViewProjection(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 model-view-projection transformation matrix that + * transforms model coordinates, relative to the eye, to clip coordinates. Clip coordinates is the + * coordinate system for a vertex shader's gl_Position output. This is used in + * conjunction with {@link czm_translateRelativeToEye}. + * + * @alias czm_modelViewProjectionRelativeToEye + * @glslUniform + * + * @example + * // GLSL declaration + * uniform mat4 czm_modelViewProjectionRelativeToEye; + * + * // Example + * attribute vec3 positionHigh; + * attribute vec3 positionLow; + * + * void main() + * { + * vec4 p = czm_translateRelativeToEye(positionHigh, positionLow); + * gl_Position = czm_modelViewProjectionRelativeToEye * p; + * } + * + * @see czm_modelViewRelativeToEye + * @see czm_translateRelativeToEye + * @see EncodedCartesian3 + */ + czm_modelViewProjectionRelativeToEye : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getModelViewProjectionRelativeToEye(); + } + }, + + /** + * An automatic GLSL uniform representing a 4x4 model-view-projection transformation matrix that + * transforms model coordinates to clip coordinates. Clip coordinates is the + * coordinate system for a vertex shader's gl_Position output. The projection matrix places + * the far plane at infinity. This is useful in algorithms like shadow volumes and GPU ray casting with + * proxy geometry to ensure that triangles are not clipped by the far plane. + * + * @alias czm_modelViewInfiniteProjection + * @glslUniform + * + * @see UniformState#getModelViewInfiniteProjection + * @see czm_model + * @see czm_view + * @see czm_infiniteProjection + * @see czm_modelViewProjection + * + * @example + * // GLSL declaration + * uniform mat4 czm_modelViewInfiniteProjection; + * + * // Example + * vec4 gl_Position = czm_modelViewInfiniteProjection * modelPosition; + * + * // The above is equivalent to, but more efficient than: + * gl_Position = czm_infiniteProjection * czm_view * czm_model * modelPosition; + */ + czm_modelViewInfiniteProjection : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX4; + }, + + getValue : function(uniformState) { + return uniformState.getModelViewInfiniteProjection(); + } + }, + + /** + * An automatic GLSL uniform representing a 3x3 normal transformation matrix that + * transforms normal vectors in model coordinates to eye coordinates. + *

+ * Positions should be transformed to eye coordinates using {@link czm_modelView} and + * normals should be transformed using czm_normal. + * + * @alias czm_normal + * @glslUniform + * + * @see UniformState#getNormal + * @see czm_inverseNormal + * @see czm_modelView + * + * @example + * // GLSL declaration + * uniform mat3 czm_normal; + * + * // Example + * vec3 eyeNormal = czm_normal * normal; + */ + czm_normal : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX3; + }, + + getValue : function(uniformState) { + return uniformState.getNormal(); + } + }, + + /** + * An automatic GLSL uniform representing a 3x3 normal transformation matrix that + * transforms normal vectors in 3D model coordinates to eye coordinates. + * In 3D mode, this is identical to + * {@link czm_normal}, but in 2D and Columbus View it represents the normal transformation + * matrix as if the camera were at an equivalent location in 3D mode. This is useful for lighting + * 2D and Columbus View in the same way that 3D is lit. + *

+ * Positions should be transformed to eye coordinates using {@link czm_modelView3D} and + * normals should be transformed using czm_normal3D. + * + * @alias czm_normal3D + * @glslUniform + * + * @see UniformState#getNormal3D + * @see czm_normal + * + * @example + * // GLSL declaration + * uniform mat3 czm_normal3D; + * + * // Example + * vec3 eyeNormal = czm_normal3D * normal; + */ + czm_normal3D : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX3; + }, + + getValue : function(uniformState) { + return uniformState.getNormal3D(); + } + }, + + /** + * An automatic GLSL uniform representing a 3x3 normal transformation matrix that + * transforms normal vectors in eye coordinates to model coordinates. This is + * the opposite of the transform provided by {@link czm_normal}. + * + * @alias czm_inverseNormal + * @glslUniform + * + * @see UniformState#getInverseNormal + * @see czm_normal + * @see czm_modelView + * @see czm_inverseView + * + * @example + * // GLSL declaration + * uniform mat3 czm_inverseNormal; + * + * // Example + * vec3 normalMC = czm_inverseNormal * normalEC; + */ + czm_inverseNormal : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX3; + }, + + getValue : function(uniformState) { + return uniformState.getInverseNormal(); + } + }, + + /** + * An automatic GLSL uniform representing a 3x3 normal transformation matrix that + * transforms normal vectors in eye coordinates to 3D model coordinates. This is + * the opposite of the transform provided by {@link czm_normal}. + * In 3D mode, this is identical to + * {@link czm_inverseNormal}, but in 2D and Columbus View it represents the inverse normal transformation + * matrix as if the camera were at an equivalent location in 3D mode. This is useful for lighting + * 2D and Columbus View in the same way that 3D is lit. + * + * @alias czm_inverseNormal3D + * @glslUniform + * + * @see UniformState#getInverseNormal3D + * @see czm_inverseNormal + * + * @example + * // GLSL declaration + * uniform mat3 czm_inverseNormal3D; + * + * // Example + * vec3 normalMC = czm_inverseNormal3D * normalEC; + */ + czm_inverseNormal3D : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX3; + }, + + getValue : function(uniformState) { + return uniformState.getInverseNormal3D(); + } + }, + + /** + * An automatic GLSL uniform containing height (x) and height squared (y) + * of the eye (camera) in the 2D scene in meters. + * + * @alias czm_eyeHeight2D + * @glslUniform + * + * @see UniformState#getEyeHeight2D + */ + czm_eyeHeight2D : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR2; + }, + + getValue : function(uniformState) { + return uniformState.getEyeHeight2D(); + } + }, + + /** + * An automatic GLSL uniform containing the near distance (x) and the far distance (y) + * of the frustum defined by the camera. This is the largest possible frustum, not an individual + * frustum used for multi-frustum rendering. + * + * @alias czm_entireFrustum + * @glslUniform + * + * @see UniformState#getEntireFrustum + * @see czm_currentFrustum + * + * @example + * // GLSL declaration + * uniform vec2 czm_entireFrustum; + * + * // Example + * float frustumLength = czm_entireFrustum.y - czm_entireFrustum.x; + */ + czm_entireFrustum : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR2; + }, + + getValue : function(uniformState) { + return uniformState.getEntireFrustum(); + } + }, + + /** + * An automatic GLSL uniform containing the near distance (x) and the far distance (y) + * of the frustum defined by the camera. This is the individual + * frustum used for multi-frustum rendering. + * + * @alias czm_currentFrustum + * @glslUniform + * + * @see UniformState#getCurrentFrustum + * @see czm_entireFrustum + * + * @example + * // GLSL declaration + * uniform vec2 czm_currentFrustum; + * + * // Example + * float frustumLength = czm_currentFrustum.y - czm_currentFrustum.x; + */ + czm_currentFrustum : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR2; + }, + + getValue : function(uniformState) { + return uniformState.getCurrentFrustum(); + } + }, + + /** + * An automatic GLSL uniform representing the size of a pixel in meters at a distance of one meter + * from the camera. The pixel size is linearly proportional to the distance from the camera. + * + * @alias czm_pixelSizeInMeters + * @glslUniform + * + * @example + * // GLSL declaration + * uniform float czm_pixelSizeInMeters; + * + * // Example: the pixel size at a position in eye coordinates + * float pixelSize = czm_pixelSizeInMeters * positionEC.z; + */ + czm_pixelSizeInMeters : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT; + }, + + getValue : function(uniformState) { + return uniformState.getPixelSize(); + } + }, + + /** + * An automatic GLSL uniform representing the sun position in world coordinates. + * + * @alias czm_sunPositionWC + * @glslUniform + * + * @see UniformState#getSunPositionWC + * @see czm_sunPositionColumbusView + * @see czm_sunDirectionWC + * + * @example + * // GLSL declaration + * uniform vec3 czm_sunPositionWC; + */ + czm_sunPositionWC : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR3; + }, + + getValue : function(uniformState) { + return uniformState.getSunPositionWC(); + } + }, + + /** + * An automatic GLSL uniform representing the sun position in Columbus view world coordinates. + * + * @alias czm_sunPositionColumbusView + * @glslUniform + * + * @see UniformState#getSunPositionColumbusView + * @see czm_sunPositionWC + * + * @example + * // GLSL declaration + * uniform vec3 czm_sunPositionColumbusView; + */ + czm_sunPositionColumbusView : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR3; + }, + + getValue : function(uniformState) { + return uniformState.getSunPositionColumbusView(); + } + }, + + /** + * An automatic GLSL uniform representing the normalized direction to the sun in eye coordinates. + * This is commonly used for directional lighting computations. + * + * @alias czm_sunDirectionEC + * @glslUniform + * + * @see UniformState#getSunDirectionEC + * @see czm_moonDirectionEC + * @see czm_sunDirectionWC + * + * @example + * // GLSL declaration + * uniform vec3 czm_sunDirectionEC; + * + * // Example + * float diffuse = max(dot(czm_sunDirectionEC, normalEC), 0.0); + */ + czm_sunDirectionEC : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR3; + }, + + getValue : function(uniformState) { + return uniformState.getSunDirectionEC(); + } + }, + + /** + * An automatic GLSL uniform representing the normalized direction to the sun in world coordinates. + * This is commonly used for directional lighting computations. + * + * @alias czm_sunDirectionWC + * @glslUniform + * + * @see UniformState#getSunDirectionWC + * @see czm_sunPositionWC + * @see czm_sunDirectionEC + * + * @example + * // GLSL declaration + * uniform vec3 czm_sunDirectionWC; + */ + czm_sunDirectionWC : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR3; + }, + + getValue : function(uniformState) { + return uniformState.getSunDirectionWC(); + } + }, + + /** + * An automatic GLSL uniform representing the normalized direction to the moon in eye coordinates. + * This is commonly used for directional lighting computations. + * + * @alias czm_moonDirectionEC + * @glslUniform + * + * @see UniformState#getMoonDirectionEC + * @see czm_sunDirectionEC + * + * @example + * // GLSL declaration + * uniform vec3 czm_moonDirectionEC; + * + * // Example + * float diffuse = max(dot(czm_moonDirectionEC, normalEC), 0.0); + */ + czm_moonDirectionEC : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR3; + }, + + getValue : function(uniformState) { + return uniformState.getMoonDirectionEC(); + } + }, + + /** + * An automatic GLSL uniform representing the high bits of the camera position in model + * coordinates. This is used for GPU RTE to eliminate jittering artifacts when rendering + * as described in Precisions, Precisions. + * + * @alias czm_encodedCameraPositionMCHigh + * @glslUniform + * + * @see czm_encodedCameraPositionMCLow + * @see czm_modelViewRelativeToEye + * @see czm_modelViewProjectionRelativeToEye + * + * @example + * // GLSL declaration + * uniform vec3 czm_encodedCameraPositionMCHigh; + */ + czm_encodedCameraPositionMCHigh : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR3; + }, + + getValue : function(uniformState) { + return uniformState.getEncodedCameraPositionMCHigh(); + } + }, + + /** + * An automatic GLSL uniform representing the low bits of the camera position in model + * coordinates. This is used for GPU RTE to eliminate jittering artifacts when rendering + * as described in Precisions, Precisions. + * + * @alias czm_encodedCameraPositionMCLow + * @glslUniform + * + * @see czm_encodedCameraPositionMCHigh + * @see czm_modelViewRelativeToEye + * @see czm_modelViewProjectionRelativeToEye + * + * @example + * // GLSL declaration + * uniform vec3 czm_encodedCameraPositionMCLow; + */ + czm_encodedCameraPositionMCLow : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR3; + }, + + getValue : function(uniformState) { + return uniformState.getEncodedCameraPositionMCLow(); + } + }, + + /** + * An automatic GLSL uniform representing the position of the viewer (camera) in world coordinates. + * + * @alias czm_viewerPositionWC + * @glslUniform + * + * @example + * // GLSL declaration + * uniform vec3 czm_viewerPositionWC; + */ + czm_viewerPositionWC : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_VECTOR3; + }, + + getValue : function(uniformState) { + return uniformState.getInverseView().getTranslation(); + } + }, + + /** + * An automatic GLSL uniform representing the frame number. This uniform is automatically incremented + * every frame. + * + * @alias czm_frameNumber + * @glslUniform + * + * @example + * // GLSL declaration + * uniform float czm_frameNumber; + */ + czm_frameNumber : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT; + }, + + getValue : function(uniformState) { + return uniformState.getFrameState().frameNumber; + } + }, + + /** + * An automatic GLSL uniform representing the current morph transition time between + * 2D/Columbus View and 3D, with 0.0 being 2D or Columbus View and 1.0 being 3D. + * + * @alias czm_morphTime + * @glslUniform + * + * @example + * // GLSL declaration + * uniform float czm_morphTime; + * + * // Example + * vec4 p = czm_columbusViewMorph(position2D, position3D, czm_morphTime); + */ + czm_morphTime : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT; + }, + + getValue : function(uniformState) { + return uniformState.getFrameState().morphTime; + } + }, + + /** + * An automatic GLSL uniform representing the current {@link SceneMode} enumeration, expressed + * as a float. + * + * @alias czm_sceneMode + * @glslUniform + * + * @see czm_sceneMode2D + * @see czm_sceneModeColumbusView + * @see czm_sceneMode3D + * @see czm_sceneModeMorphing + * + * @example + * // GLSL declaration + * uniform float czm_sceneMode; + * + * // Example + * if (czm_sceneMode == czm_sceneMode2D) + * { + * eyeHeightSq = czm_eyeHeight2D.y; + * } + */ + czm_sceneMode : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT; + }, + + getValue : function(uniformState) { + return uniformState.getFrameState().mode.value; + } + }, + + /** + * An automatic GLSL uniform representing a 3x3 rotation matrix that transforms + * from True Equator Mean Equinox (TEME) axes to the pseudo-fixed axes at the current scene time. + * + * @alias czm_temeToPseudoFixed + * @glslUniform + * + * @see UniformState#getTemeToPseudoFixedMatrix + * @see Transforms.computeTemeToPseudoFixedMatrix + * + * @example + * // GLSL declaration + * uniform mat3 czm_temeToPseudoFixed; + * + * // Example + * vec3 pseudoFixed = czm_temeToPseudoFixed * teme; + */ + czm_temeToPseudoFixed : { + getSize : function() { + return 1; + }, + + getDatatype : function() { + return UniformDatatype.FLOAT_MATRIX3; + }, + + getValue : function(uniformState) { + return uniformState.getTemeToPseudoFixedMatrix(); + } + } + }; +}); \ No newline at end of file diff --git a/Source/Renderer/ShaderProgram.js b/Source/Renderer/ShaderProgram.js index 38aa8c542d62..358582975780 100644 --- a/Source/Renderer/ShaderProgram.js +++ b/Source/Renderer/ShaderProgram.js @@ -8,42 +8,9 @@ define([ '../Core/Matrix2', '../Core/Matrix3', '../Core/Matrix4', + './AutomaticUniforms', './UniformDatatype', - '../Shaders/Builtin/Structs', - '../Shaders/Builtin/Constants', - '../Shaders/Builtin/Functions/RGBToXYZ', - '../Shaders/Builtin/Functions/XYZToRGB', - '../Shaders/Builtin/Functions/antialias', - '../Shaders/Builtin/Functions/cellular', - '../Shaders/Builtin/Functions/columbusViewMorph', - '../Shaders/Builtin/Functions/computePosition', - '../Shaders/Builtin/Functions/eastNorthUpToEyeCoordinates', - '../Shaders/Builtin/Functions/ellipsoidContainsPoint', - '../Shaders/Builtin/Functions/ellipsoidNew', - '../Shaders/Builtin/Functions/ellipsoidWgs84TextureCoordinates', - '../Shaders/Builtin/Functions/equalsEpsilon', - '../Shaders/Builtin/Functions/eyeOffset', - '../Shaders/Builtin/Functions/eyeToWindowCoordinates', - '../Shaders/Builtin/Functions/geodeticSurfaceNormal', - '../Shaders/Builtin/Functions/getDefaultMaterial', - '../Shaders/Builtin/Functions/getWaterNoise', - '../Shaders/Builtin/Functions/getWgs84EllipsoidEC', - '../Shaders/Builtin/Functions/hue', - '../Shaders/Builtin/Functions/isEmpty', - '../Shaders/Builtin/Functions/isFull', - '../Shaders/Builtin/Functions/latitudeToWebMercatorFraction', - '../Shaders/Builtin/Functions/luminance', - '../Shaders/Builtin/Functions/modelToWindowCoordinates', - '../Shaders/Builtin/Functions/multiplyWithColorBalance', - '../Shaders/Builtin/Functions/phong', - '../Shaders/Builtin/Functions/pointAlongRay', - '../Shaders/Builtin/Functions/rayEllipsoidIntersectionInterval', - '../Shaders/Builtin/Functions/saturation', - '../Shaders/Builtin/Functions/snoise', - '../Shaders/Builtin/Functions/tangentToEyeSpaceMatrix', - '../Shaders/Builtin/Functions/translateRelativeToEye', - '../Shaders/Builtin/Functions/transpose', - '../Shaders/Builtin/Functions/windowToEyeCoordinates' + '../Shaders/Builtin/CzmBuiltins' ], function( defined, DeveloperError, @@ -53,1742 +20,12 @@ define([ Matrix2, Matrix3, Matrix4, + AutomaticUniforms, UniformDatatype, - ShadersBuiltinStructs, - ShadersBuiltinConstants, - czm_RGBToXYZ, - czm_XYZToRGB, - czm_antialias, - czm_cellular, - czm_columbusViewMorph, - czm_computePosition, - czm_eastNorthUpToEyeCoordinates, - czm_ellipsoidContainsPoint, - czm_ellipsoidNew, - czm_ellipsoidWgs84TextureCoordinates, - czm_equalsEpsilon, - czm_eyeOffset, - czm_eyeToWindowCoordinates, - czm_geodeticSurfaceNormal, - czm_getDefaultMaterial, - czm_getWaterNoise, - czm_getWgs84EllipsoidEC, - czm_hue, - czm_isEmpty, - czm_isFull, - czm_latitudeToWebMercatorFraction, - czm_luminance, - czm_modelToWindowCoordinates, - czm_multiplyWithColorBalance, - czm_phong, - czm_pointAlongRay, - czm_rayEllipsoidIntersectionInterval, - czm_saturation, - czm_snoise, - czm_tangentToEyeSpaceMatrix, - czm_translateRelativeToEye, - czm_transpose, - czm_windowToEyeCoordinates) { + CzmBuiltins) { "use strict"; /*global console*/ - var allAutomaticUniforms = { - /** - * An automatic GLSL uniform containing the viewport's x, y, width, - * and height properties in an vec4's x, y, z, - * and w components, respectively. - *

- * Like all automatic uniforms, czm_viewport does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_viewport - * @glslUniform - * - * @see Context#getViewport - * - * @example - * // GLSL declaration - * uniform vec4 czm_viewport; - * - * // Scale the window coordinate components to [0, 1] by dividing - * // by the viewport's width and height. - * vec2 v = gl_FragCoord.xy / czm_viewport.zw; - */ - czm_viewport : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR4; - }, - - getValue : function(uniformState) { - var v = uniformState.getViewport(); - return { - x : v.x, - y : v.y, - z : v.width, - w : v.height - }; - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 orthographic projection matrix that - * transforms window coordinates to clip coordinates. Clip coordinates is the - * coordinate system for a vertex shader's gl_Position output. - *

- * This transform is useful when a vertex shader inputs or manipulates window coordinates - * as done by {@link BillboardCollection}. - *

- * Do not confuse {@link czm_viewportTransformation} with czm_viewportOrthographic. - * The former transforms from normalized device coordinates to window coordinates; the later transforms - * from window coordinates to clip coordinates, and is often used to assign to gl_Position. - *

- * Like all automatic uniforms, czm_viewportOrthographic does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_viewportOrthographic - * @glslUniform - * - * @see UniformState#getViewportOrthographic - * @see czm_viewport - * @see czm_viewportTransformation - * @see BillboardCollection - * - * @example - * // GLSL declaration - * uniform mat4 czm_viewportOrthographic; - * - * // Example - * gl_Position = czm_viewportOrthographic * vec4(windowPosition, 0.0, 1.0); - */ - czm_viewportOrthographic : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getViewportOrthographic(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 transformation matrix that - * transforms normalized device coordinates to window coordinates. The context's - * full viewport is used, and the depth range is assumed to be near = 0 - * and far = 1. - *

- * This transform is useful when there is a need to manipulate window coordinates - * in a vertex shader as done by {@link BillboardCollection}. In many cases, - * this matrix will not be used directly; instead, {@link czm_modelToWindowCoordinates} - * will be used to transform directly from model to window coordinates. - *

- * Do not confuse czm_viewportTransformation with {@link czm_viewportOrthographic}. - * The former transforms from normalized device coordinates to window coordinates; the later transforms - * from window coordinates to clip coordinates, and is often used to assign to gl_Position. - *

- * Like all automatic uniforms, czm_viewportTransformation does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_viewportTransformation - * @glslUniform - * - * @see UniformState#getViewportTransformation - * @see czm_viewport - * @see czm_viewportOrthographic - * @see czm_modelToWindowCoordinates - * @see BillboardCollection - * - * @example - * // GLSL declaration - * uniform mat4 czm_viewportTransformation; - * - * // Use czm_viewportTransformation as part of the - * // transform from model to window coordinates. - * vec4 q = czm_modelViewProjection * positionMC; // model to clip coordinates - * q.xyz /= q.w; // clip to normalized device coordinates (ndc) - * q.xyz = (czm_viewportTransformation * vec4(q.xyz, 1.0)).xyz; // ndc to window coordinates - */ - czm_viewportTransformation : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getViewportTransformation(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 model transformation matrix that - * transforms model coordinates to world coordinates. - *

- * Like all automatic uniforms, czm_model does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_model - * @glslUniform - * - * @see UniformState#getModel - * @see czm_inverseModel - * @see czm_modelView - * @see czm_modelViewProjection - * - * @example - * // GLSL declaration - * uniform mat4 czm_model; - * - * // Example - * vec4 worldPosition = czm_model * modelPosition; - */ - czm_model : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getModel(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 model transformation matrix that - * transforms world coordinates to model coordinates. - *

- * Like all automatic uniforms, czm_inverseModel does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseModel - * @glslUniform - * - * @see UniformState#getInverseModel - * @see czm_model - * @see czm_inverseModelView - * - * @example - * // GLSL declaration - * uniform mat4 czm_inverseModel; - * - * // Example - * vec4 modelPosition = czm_inverseModel * worldPosition; - */ - czm_inverseModel : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getInverseModel(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 view transformation matrix that - * transforms world coordinates to eye coordinates. - *

- * Like all automatic uniforms, czm_view does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_view - * @glslUniform - * - * @see UniformState#getView - * @see czm_viewRotation - * @see czm_modelView - * @see czm_viewProjection - * @see czm_modelViewProjection - * @see czm_inverseView - * - * @example - * // GLSL declaration - * uniform mat4 czm_view; - * - * // Example - * vec4 eyePosition = czm_view * worldPosition; - */ - czm_view : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getView(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 view transformation matrix that - * transforms 3D world coordinates to eye coordinates. In 3D mode, this is identical to - * {@link czm_view}, but in 2D and Columbus View it represents the view matrix - * as if the camera were at an equivalent location in 3D mode. This is useful for lighting - * 2D and Columbus View in the same way that 3D is lit. - *

- * Like all automatic uniforms, czm_view3D does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_view3D - * @glslUniform - * - * @see UniformState#getView3D - * @see czm_view - * - * @example - * // GLSL declaration - * uniform mat4 czm_view3D; - * - * // Example - * vec4 eyePosition3D = czm_view3D * worldPosition3D; - */ - czm_view3D : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getView3D(); - } - }, - - /** - * An automatic GLSL uniform representing a 3x3 view rotation matrix that - * transforms vectors in world coordinates to eye coordinates. - *

- * Like all automatic uniforms, czm_viewRotation does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_viewRotation - * @glslUniform - * - * @see UniformState#getViewRotation - * @see czm_view - * @see czm_inverseView - * @see czm_inverseViewRotation - * - * @example - * // GLSL declaration - * uniform mat3 czm_viewRotation; - * - * // Example - * vec3 eyeVector = czm_viewRotation * worldVector; - */ - czm_viewRotation : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX3; - }, - - getValue : function(uniformState) { - return uniformState.getViewRotation(); - } - }, - - /** - * An automatic GLSL uniform representing a 3x3 view rotation matrix that - * transforms vectors in 3D world coordinates to eye coordinates. In 3D mode, this is identical to - * {@link czm_viewRotation}, but in 2D and Columbus View it represents the view matrix - * as if the camera were at an equivalent location in 3D mode. This is useful for lighting - * 2D and Columbus View in the same way that 3D is lit. - *

- * Like all automatic uniforms, czm_viewRotation3D does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_viewRotation3D - * @glslUniform - * - * @see UniformState#getViewRotation3D - * @see czm_viewRotation - * - * @example - * // GLSL declaration - * uniform mat3 czm_viewRotation3D; - * - * // Example - * vec3 eyeVector = czm_viewRotation3D * worldVector; - */ - czm_viewRotation3D : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX3; - }, - - getValue : function(uniformState) { - return uniformState.getViewRotation3D(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 transformation matrix that - * transforms from eye coordinates to world coordinates. - *

- * Like all automatic uniforms, czm_inverseView does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseView - * @glslUniform - * - * @see UniformState#getInverseView - * @see czm_view - * @see czm_inverseNormal - * - * @example - * // GLSL declaration - * uniform mat4 czm_inverseView; - * - * // Example - * vec4 worldPosition = czm_inverseView * eyePosition; - */ - czm_inverseView : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getInverseView(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 transformation matrix that - * transforms from 3D eye coordinates to world coordinates. In 3D mode, this is identical to - * {@link czm_inverseView}, but in 2D and Columbus View it represents the inverse view matrix - * as if the camera were at an equivalent location in 3D mode. This is useful for lighting - * 2D and Columbus View in the same way that 3D is lit. - *

- * Like all automatic uniforms, czm_inverseView3D does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseView3D - * @glslUniform - * - * @see UniformState#getInverseView3D - * @see czm_inverseView - * - * @example - * // GLSL declaration - * uniform mat4 czm_inverseView3D; - * - * // Example - * vec4 worldPosition = czm_inverseView3D * eyePosition; - */ - czm_inverseView3D : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getInverseView3D(); - } - }, - - /** - * An automatic GLSL uniform representing a 3x3 rotation matrix that - * transforms vectors from eye coordinates to world coordinates. - *

- * Like all automatic uniforms, czm_inverseViewRotation does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseViewRotation - * @glslUniform - * - * @see UniformState#getInverseView - * @see czm_view - * @see czm_viewRotation - * @see czm_inverseViewRotation - * - * @example - * // GLSL declaration - * uniform mat3 czm_inverseViewRotation; - * - * // Example - * vec4 worldVector = czm_inverseViewRotation * eyeVector; - */ - czm_inverseViewRotation : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX3; - }, - - getValue : function(uniformState) { - return uniformState.getInverseViewRotation(); - } - }, - - /** - * An automatic GLSL uniform representing a 3x3 rotation matrix that - * transforms vectors from 3D eye coordinates to world coordinates. In 3D mode, this is identical to - * {@link czm_inverseViewRotation}, but in 2D and Columbus View it represents the inverse view matrix - * as if the camera were at an equivalent location in 3D mode. This is useful for lighting - * 2D and Columbus View in the same way that 3D is lit. - *

- * Like all automatic uniforms, czm_inverseViewRotation3D does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseViewRotation3D - * @glslUniform - * - * @see UniformState#getInverseView3D - * @see czm_inverseViewRotation - * - * @example - * // GLSL declaration - * uniform mat3 czm_inverseViewRotation3D; - * - * // Example - * vec4 worldVector = czm_inverseViewRotation3D * eyeVector; - */ - czm_inverseViewRotation3D : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX3; - }, - - getValue : function(uniformState) { - return uniformState.getInverseViewRotation3D(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 projection transformation matrix that - * transforms eye coordinates to clip coordinates. Clip coordinates is the - * coordinate system for a vertex shader's gl_Position output. - *

- * Like all automatic uniforms, czm_projection does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_projection - * @glslUniform - * - * @see UniformState#getProjection - * @see czm_viewProjection - * @see czm_modelViewProjection - * @see czm_infiniteProjection - * - * @example - * // GLSL declaration - * uniform mat4 czm_projection; - * - * // Example - * gl_Position = czm_projection * eyePosition; - */ - czm_projection : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getProjection(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 inverse projection transformation matrix that - * transforms from clip coordinates to eye coordinates. Clip coordinates is the - * coordinate system for a vertex shader's gl_Position output. - *

- * Like all automatic uniforms, czm_inverseProjection does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseProjection - * @glslUniform - * - * @see UniformState#getInverseProjection - * @see czm_projection - * - * @example - * // GLSL declaration - * uniform mat4 czm_inverseProjection; - * - * // Example - * vec4 eyePosition = czm_inverseProjection * clipPosition; - */ - czm_inverseProjection : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getInverseProjection(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 projection transformation matrix with the far plane at infinity, - * that transforms eye coordinates to clip coordinates. Clip coordinates is the - * coordinate system for a vertex shader's gl_Position output. An infinite far plane is used - * in algorithms like shadow volumes and GPU ray casting with proxy geometry to ensure that triangles - * are not clipped by the far plane. - *

- * Like all automatic uniforms, czm_infiniteProjection does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_infiniteProjection - * @glslUniform - * - * @see UniformState#getInfiniteProjection - * @see czm_projection - * @see czm_modelViewInfiniteProjection - * - * @example - * // GLSL declaration - * uniform mat4 czm_infiniteProjection; - * - * // Example - * gl_Position = czm_infiniteProjection * eyePosition; - */ - czm_infiniteProjection : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getInfiniteProjection(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 model-view transformation matrix that - * transforms model coordinates to eye coordinates. - *

- * Positions should be transformed to eye coordinates using czm_modelView and - * normals should be transformed using {@link czm_normal}. - *

- * Like all automatic uniforms, czm_modelView does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_modelView - * @glslUniform - * - * @see UniformState#getModelView - * @see czm_model - * @see czm_view - * @see czm_modelViewProjection - * @see czm_normal - * - * @example - * // GLSL declaration - * uniform mat4 czm_modelView; - * - * // Example - * vec4 eyePosition = czm_modelView * modelPosition; - * - * // The above is equivalent to, but more efficient than: - * vec4 eyePosition = czm_view * czm_model * modelPosition; - */ - czm_modelView : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getModelView(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 model-view transformation matrix that - * transforms 3D model coordinates to eye coordinates. In 3D mode, this is identical to - * {@link czm_modelView}, but in 2D and Columbus View it represents the model-view matrix - * as if the camera were at an equivalent location in 3D mode. This is useful for lighting - * 2D and Columbus View in the same way that 3D is lit. - *

- * Positions should be transformed to eye coordinates using czm_modelView3D and - * normals should be transformed using {@link czm_normal3D}. - *

- * Like all automatic uniforms, czm_modelView3D does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_modelView3D - * @glslUniform - * - * @see UniformState#getModelView3D - * @see czm_modelView - * - * @example - * // GLSL declaration - * uniform mat4 czm_modelView3D; - * - * // Example - * vec4 eyePosition = czm_modelView3D * modelPosition; - * - * // The above is equivalent to, but more efficient than: - * vec4 eyePosition = czm_view3D * czm_model * modelPosition; - */ - czm_modelView3D : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getModelView3D(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 model-view transformation matrix that - * transforms model coordinates, relative to the eye, to eye coordinates. This is used - * in conjunction with {@link czm_translateRelativeToEye}. - *

- * Like all automatic uniforms, czm_modelViewRelativeToEye does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_modelViewRelativeToEye - * @glslUniform - * - * @example - * // GLSL declaration - * uniform mat4 czm_modelViewRelativeToEye; - * - * // Example - * attribute vec3 positionHigh; - * attribute vec3 positionLow; - * - * void main() - * { - * vec4 p = czm_translateRelativeToEye(positionHigh, positionLow); - * gl_Position = czm_projection * (czm_modelViewRelativeToEye * p); - * } - * - * @see czm_modelViewProjectionRelativeToEye - * @see czm_translateRelativeToEye - * @see EncodedCartesian3 - */ - czm_modelViewRelativeToEye : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getModelViewRelativeToEye(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 transformation matrix that - * transforms from eye coordinates to model coordinates. - *

- * Like all automatic uniforms, czm_inverseModelView does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseModelView - * @glslUniform - * - * @see UniformState#getInverseModelView - * @see czm_modelView - * - * @example - * // GLSL declaration - * uniform mat4 czm_inverseModelView; - * - * // Example - * vec4 modelPosition = czm_inverseModelView * eyePosition; - */ - czm_inverseModelView : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getInverseModelView(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 transformation matrix that - * transforms from eye coordinates to 3D model coordinates. In 3D mode, this is identical to - * {@link czm_inverseModelView}, but in 2D and Columbus View it represents the inverse model-view matrix - * as if the camera were at an equivalent location in 3D mode. This is useful for lighting - * 2D and Columbus View in the same way that 3D is lit. - *

- * Like all automatic uniforms, czm_inverseModelView3D does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseModelView3D - * @glslUniform - * - * @see UniformState#getInverseModelView - * @see czm_inverseModelView - * @see czm_modelView3D - * - * @example - * // GLSL declaration - * uniform mat4 czm_inverseModelView3D; - * - * // Example - * vec4 modelPosition = czm_inverseModelView3D * eyePosition; - */ - czm_inverseModelView3D : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getInverseModelView3D(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 view-projection transformation matrix that - * transforms world coordinates to clip coordinates. Clip coordinates is the - * coordinate system for a vertex shader's gl_Position output. - *

- * Like all automatic uniforms, czm_viewProjection does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_viewProjection - * @glslUniform - * - * @see UniformState#getViewProjection - * @see czm_view - * @see czm_projection - * @see czm_modelViewProjection - * - * @example - * // GLSL declaration - * uniform mat4 czm_viewProjection; - * - * // Example - * vec4 gl_Position = czm_viewProjection * czm_model * modelPosition; - * - * // The above is equivalent to, but more efficient than: - * gl_Position = czm_projection * czm_view * czm_model * modelPosition; - */ - czm_viewProjection : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getViewProjection(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 model-view-projection transformation matrix that - * transforms model coordinates to clip coordinates. Clip coordinates is the - * coordinate system for a vertex shader's gl_Position output. - *

- * Like all automatic uniforms, czm_modelViewProjection does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_modelViewProjection - * @glslUniform - * - * @see UniformState#getModelViewProjection - * @see czm_model - * @see czm_view - * @see czm_projection - * @see czm_modelView - * @see czm_viewProjection - * @see czm_modelViewInfiniteProjection - * - * @example - * // GLSL declaration - * uniform mat4 czm_modelViewProjection; - * - * // Example - * vec4 gl_Position = czm_modelViewProjection * modelPosition; - * - * // The above is equivalent to, but more efficient than: - * gl_Position = czm_projection * czm_view * czm_model * modelPosition; - */ - czm_modelViewProjection : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getModelViewProjection(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 model-view-projection transformation matrix that - * transforms model coordinates, relative to the eye, to clip coordinates. Clip coordinates is the - * coordinate system for a vertex shader's gl_Position output. This is used in - * conjunction with {@link czm_translateRelativeToEye}. - *

- * Like all automatic uniforms, czm_modelViewProjectionRelativeToEye does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_modelViewProjectionRelativeToEye - * @glslUniform - * - * @example - * // GLSL declaration - * uniform mat4 czm_modelViewProjectionRelativeToEye; - * - * // Example - * attribute vec3 positionHigh; - * attribute vec3 positionLow; - * - * void main() - * { - * vec4 p = czm_translateRelativeToEye(positionHigh, positionLow); - * gl_Position = czm_modelViewProjectionRelativeToEye * p; - * } - * - * @see czm_modelViewRelativeToEye - * @see czm_translateRelativeToEye - * @see EncodedCartesian3 - */ - czm_modelViewProjectionRelativeToEye : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getModelViewProjectionRelativeToEye(); - } - }, - - /** - * An automatic GLSL uniform representing a 4x4 model-view-projection transformation matrix that - * transforms model coordinates to clip coordinates. Clip coordinates is the - * coordinate system for a vertex shader's gl_Position output. The projection matrix places - * the far plane at infinity. This is useful in algorithms like shadow volumes and GPU ray casting with - * proxy geometry to ensure that triangles are not clipped by the far plane. - *

- * Like all automatic uniforms, czm_modelViewInfiniteProjection does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_modelViewInfiniteProjection - * @glslUniform - * - * @see UniformState#getModelViewInfiniteProjection - * @see czm_model - * @see czm_view - * @see czm_infiniteProjection - * @see czm_modelViewProjection - * - * @example - * // GLSL declaration - * uniform mat4 czm_modelViewInfiniteProjection; - * - * // Example - * vec4 gl_Position = czm_modelViewInfiniteProjection * modelPosition; - * - * // The above is equivalent to, but more efficient than: - * gl_Position = czm_infiniteProjection * czm_view * czm_model * modelPosition; - */ - czm_modelViewInfiniteProjection : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX4; - }, - - getValue : function(uniformState) { - return uniformState.getModelViewInfiniteProjection(); - } - }, - - /** - * An automatic GLSL uniform representing a 3x3 normal transformation matrix that - * transforms normal vectors in model coordinates to eye coordinates. - *

- * Positions should be transformed to eye coordinates using {@link czm_modelView} and - * normals should be transformed using czm_normal. - *

- * Like all automatic uniforms, czm_normal does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_normal - * @glslUniform - * - * @see UniformState#getNormal - * @see czm_inverseNormal - * @see czm_modelView - * - * @example - * // GLSL declaration - * uniform mat3 czm_normal; - * - * // Example - * vec3 eyeNormal = czm_normal * normal; - */ - czm_normal : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX3; - }, - - getValue : function(uniformState) { - return uniformState.getNormal(); - } - }, - - /** - * An automatic GLSL uniform representing a 3x3 normal transformation matrix that - * transforms normal vectors in 3D model coordinates to eye coordinates. - * In 3D mode, this is identical to - * {@link czm_normal}, but in 2D and Columbus View it represents the normal transformation - * matrix as if the camera were at an equivalent location in 3D mode. This is useful for lighting - * 2D and Columbus View in the same way that 3D is lit. - *

- * Positions should be transformed to eye coordinates using {@link czm_modelView3D} and - * normals should be transformed using czm_normal3D. - *

- * Like all automatic uniforms, czm_normal3D does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_normal3D - * @glslUniform - * - * @see UniformState#getNormal3D - * @see czm_normal - * - * @example - * // GLSL declaration - * uniform mat3 czm_normal3D; - * - * // Example - * vec3 eyeNormal = czm_normal3D * normal; - */ - czm_normal3D : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX3; - }, - - getValue : function(uniformState) { - return uniformState.getNormal3D(); - } - }, - - /** - * An automatic GLSL uniform representing a 3x3 normal transformation matrix that - * transforms normal vectors in eye coordinates to model coordinates. This is - * the opposite of the transform provided by {@link czm_normal}. - *

- * Like all automatic uniforms, czm_inverseNormal does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseNormal - * @glslUniform - * - * @see UniformState#getInverseNormal - * @see czm_normal - * @see czm_modelView - * @see czm_inverseView - * - * @example - * // GLSL declaration - * uniform mat3 czm_inverseNormal; - * - * // Example - * vec3 normalMC = czm_inverseNormal * normalEC; - */ - czm_inverseNormal : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX3; - }, - - getValue : function(uniformState) { - return uniformState.getInverseNormal(); - } - }, - - /** - * An automatic GLSL uniform representing a 3x3 normal transformation matrix that - * transforms normal vectors in eye coordinates to 3D model coordinates. This is - * the opposite of the transform provided by {@link czm_normal}. - * In 3D mode, this is identical to - * {@link czm_inverseNormal}, but in 2D and Columbus View it represents the inverse normal transformation - * matrix as if the camera were at an equivalent location in 3D mode. This is useful for lighting - * 2D and Columbus View in the same way that 3D is lit. - *

- * Like all automatic uniforms, czm_inverseNormal3D does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_inverseNormal3D - * @glslUniform - * - * @see UniformState#getInverseNormal3D - * @see czm_inverseNormal - * - * @example - * // GLSL declaration - * uniform mat3 czm_inverseNormal3D; - * - * // Example - * vec3 normalMC = czm_inverseNormal3D * normalEC; - */ - czm_inverseNormal3D : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX3; - }, - - getValue : function(uniformState) { - return uniformState.getInverseNormal3D(); - } - }, - - /** - * An automatic GLSL uniform containing the near distance (x) and the far distance (y) - * of the frustum defined by the camera. This is the largest possible frustum, not an individual - * frustum used for multi-frustum rendering. - *

- * Like all automatic uniforms, czm_entireFrustum does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_entireFrustum - * @glslUniform - * - * @see UniformState#getEntireFrustum - * @see czm_currentFrustum - * - * @example - * // GLSL declaration - * uniform vec2 czm_entireFrustum; - * - * // Example - * float frustumLength = czm_entireFrustum.y - czm_entireFrustum.x; - */ - czm_entireFrustum : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR2; - }, - - getValue : function(uniformState) { - return uniformState.getEntireFrustum(); - } - }, - - /** - * An automatic GLSL uniform containing height (x) and height squared (y) - * of the eye (camera) in the 2D scene in meters. - *

- * Like all automatic uniforms, czm_eyeHeight2D does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_eyeHeight2D - * @glslUniform - * - * @see UniformState#getEyeHeight2D - */ - czm_eyeHeight2D : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR2; - }, - - getValue : function(uniformState) { - return uniformState.getEyeHeight2D(); - } - }, - - /** - * An automatic GLSL uniform containing the near distance (x) and the far distance (y) - * of the frustum defined by the camera. This is the individual - * frustum used for multi-frustum rendering. - *

- * Like all automatic uniforms, czm_currentFrustum does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_currentFrustum - * @glslUniform - * - * @see UniformState#getCurrentFrustum - * @see czm_entireFrustum - * - * @example - * // GLSL declaration - * uniform vec2 czm_currentFrustum; - * - * // Example - * float frustumLength = czm_currentFrustum.y - czm_currentFrustum.x; - */ - czm_currentFrustum : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR2; - }, - - getValue : function(uniformState) { - return uniformState.getCurrentFrustum(); - } - }, - - /** - * An automatic GLSL uniform representing the size of a pixel in meters at a distance of one meter - * from the camera. The pixel size is linearly proportional to the distance from the camera. - *

- * Like all automatic uniforms, czm_pixelSizeInMeters does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_pixelSizeInMeters - * @glslUniform - * - * @example - * // GLSL declaration - * uniform float czm_pixelSizeInMeters; - * - * // Example: the pixel size at a position in eye coordinates - * float pixelSize = czm_pixelSizeInMeters * positionEC.z; - */ - czm_pixelSizeInMeters : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT; - }, - - getValue : function(uniformState) { - return uniformState.getPixelSize(); - } - }, - - /** - * An automatic GLSL uniform representing the sun position in world coordinates. - *

- * Like all automatic uniforms, czm_sunPositionWC does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_sunPositionWC - * @glslUniform - * - * @see UniformState#getSunPositionWC - * @see czm_sunPositionColumbusView - * @see czm_sunDirectionWC - * - * @example - * // GLSL declaration - * uniform vec3 czm_sunPositionWC; - */ - czm_sunPositionWC : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR3; - }, - - getValue : function(uniformState) { - return uniformState.getSunPositionWC(); - } - }, - - /** - * An automatic GLSL uniform representing the sun position in Columbus view world coordinates. - *

- * Like all automatic uniforms, czm_sunPositionColumbusView does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_sunPositionColumbusView - * @glslUniform - * - * @see UniformState#getSunPositionColumbusView - * @see czm_sunPositionWC - * - * @example - * // GLSL declaration - * uniform vec3 czm_sunPositionColumbusView; - */ - czm_sunPositionColumbusView : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR3; - }, - - getValue : function(uniformState) { - return uniformState.getSunPositionColumbusView(); - } - }, - - /** - * An automatic GLSL uniform representing the normalized direction to the sun in eye coordinates. - * This is commonly used for directional lighting computations. - *

- * Like all automatic uniforms, czm_sunDirectionEC does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_sunDirectionEC - * @glslUniform - * - * @see UniformState#getSunDirectionEC - * @see czm_moonDirectionEC - * @see czm_sunDirectionWC - * - * @example - * // GLSL declaration - * uniform vec3 czm_sunDirectionEC; - * - * // Example - * float diffuse = max(dot(czm_sunDirectionEC, normalEC), 0.0); - */ - czm_sunDirectionEC : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR3; - }, - - getValue : function(uniformState) { - return uniformState.getSunDirectionEC(); - } - }, - - /** - * An automatic GLSL uniform representing the normalized direction to the sun in world coordinates. - * This is commonly used for directional lighting computations. - *

- * Like all automatic uniforms, czm_sunDirectionWC does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_sunDirectionWC - * @glslUniform - * - * @see UniformState#getSunDirectionWC - * @see czm_sunPositionWC - * @see czm_sunDirectionEC - * - * @example - * // GLSL declaration - * uniform vec3 czm_sunDirectionWC; - */ - czm_sunDirectionWC : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR3; - }, - - getValue : function(uniformState) { - return uniformState.getSunDirectionWC(); - } - }, - - /** - * An automatic GLSL uniform representing the normalized direction to the moon in eye coordinates. - * This is commonly used for directional lighting computations. - *

- * Like all automatic uniforms, czm_moonDirectionEC does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_moonDirectionEC - * @glslUniform - * - * @see UniformState#getMoonDirectionEC - * @see czm_sunDirectionEC - * - * @example - * // GLSL declaration - * uniform vec3 czm_moonDirectionEC; - * - * // Example - * float diffuse = max(dot(czm_moonDirectionEC, normalEC), 0.0); - */ - czm_moonDirectionEC : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR3; - }, - - getValue : function(uniformState) { - return uniformState.getMoonDirectionEC(); - } - }, - - /** - * An automatic GLSL uniform representing the high bits of the camera position in model - * coordinates. This is used for GPU RTE to eliminate jittering artifacts when rendering - * as described in Precisions, Precisions. - *

- * Like all automatic uniforms, czm_encodedCameraPositionMCHigh does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_encodedCameraPositionMCHigh - * @glslUniform - * - * @see czm_encodedCameraPositionMCLow - * @see czm_modelViewRelativeToEye - * @see czm_modelViewProjectionRelativeToEye - * - * @example - * // GLSL declaration - * uniform vec3 czm_encodedCameraPositionMCHigh; - */ - czm_encodedCameraPositionMCHigh : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR3; - }, - - getValue : function(uniformState) { - return uniformState.getEncodedCameraPositionMCHigh(); - } - }, - - /** - * An automatic GLSL uniform representing the low bits of the camera position in model - * coordinates. This is used for GPU RTE to eliminate jittering artifacts when rendering - * as described in Precisions, Precisions. - *

- * Like all automatic uniforms, czm_encodedCameraPositionMCHigh does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_encodedCameraPositionMCLow - * @glslUniform - * - * @see czm_encodedCameraPositionMCHigh - * @see czm_modelViewRelativeToEye - * @see czm_modelViewProjectionRelativeToEye - * - * @example - * // GLSL declaration - * uniform vec3 czm_encodedCameraPositionMCLow; - */ - czm_encodedCameraPositionMCLow : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR3; - }, - - getValue : function(uniformState) { - return uniformState.getEncodedCameraPositionMCLow(); - } - }, - - /** - * An automatic GLSL uniform representing the position of the viewer (camera) in world coordinates. - *

- * Like all automatic uniforms, czm_sunDirectionWC does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_viewerPositionWC - * @glslUniform - * - * @example - * // GLSL declaration - * uniform vec3 czm_viewerPositionWC; - */ - czm_viewerPositionWC : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_VECTOR3; - }, - - getValue : function(uniformState) { - return uniformState.getInverseView().getTranslation(); - } - }, - - /** - * An automatic GLSL uniform representing the frame number. This uniform is automatically incremented - * every frame. - *

- * Like all automatic uniforms, czm_frameNumber does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_frameNumber - * @glslUniform - * - * @example - * // GLSL declaration - * uniform float czm_frameNumber; - */ - czm_frameNumber : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT; - }, - - getValue : function(uniformState) { - return uniformState.getFrameState().frameNumber; - } - }, - - /** - * An automatic GLSL uniform representing the current morph transition time between - * 2D/Columbus View and 3D, with 0.0 being 2D or Columbus View and 1.0 being 3D. - *

- * Like all automatic uniforms, czm_morphTime does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_morphTime - * @glslUniform - * - * @example - * // GLSL declaration - * uniform float czm_morphTime; - * - * // Example - * vec4 p = czm_columbusViewMorph(position2D, position3D, czm_morphTime); - */ - czm_morphTime : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT; - }, - - getValue : function(uniformState) { - return uniformState.getFrameState().morphTime; - } - }, - - /** - * An automatic GLSL uniform representing the current {@link SceneMode} enumeration, expressed - * as a float. - *

- * Like all automatic uniforms, czm_sceneMode does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_sceneMode - * @glslUniform - * - * @see czm_sceneMode2D - * @see czm_sceneModeColumbusView - * @see czm_sceneMode3D - * @see czm_sceneModeMorphing - * - * @example - * // GLSL declaration - * uniform float czm_sceneMode; - * - * // Example - * if (czm_sceneMode == czm_sceneMode2D) - * { - * eyeHeightSq = czm_eyeHeight2D.y; - * } - */ - czm_sceneMode : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT; - }, - - getValue : function(uniformState) { - return uniformState.getFrameState().mode.value; - } - }, - - /** - * An automatic GLSL uniform representing a 3x3 rotation matrix that transforms - * from True Equator Mean Equinox (TEME) axes to the pseudo-fixed axes at the current scene time. - *

- * Like all automatic uniforms, czm_temeToPseudoFixed does not need to be explicitly declared. - * However, it can be explicitly declared when a shader is also used by other applications such - * as a third-party authoring tool. - * - * @alias czm_temeToPseudoFixed - * @glslUniform - * - * @see UniformState#getTemeToPseudoFixedMatrix - * @see Transforms.computeTemeToPseudoFixedMatrix - * - * @example - * // GLSL declaration - * uniform mat3 czm_temeToPseudoFixed; - * - * // Example - * vec3 pseudoFixed = czm_temeToPseudoFixed * teme; - */ - czm_temeToPseudoFixed : { - getSize : function() { - return 1; - }, - - getDatatype : function() { - return UniformDatatype.FLOAT_MATRIX3; - }, - - getValue : function(uniformState) { - return uniformState.getTemeToPseudoFixedMatrix(); - } - } - }; - function getUniformDatatype(gl, activeUniformType) { switch (activeUniformType) { case gl.FLOAT: @@ -1873,6 +110,20 @@ define([ scratchUniformMatrix4 = new Float32Array(16); } + function getAutomaticUniformDeclaration(uniforms, uniform) { + var automaticUniform = uniforms[uniform]; + var declaration = 'uniform ' + automaticUniform.getDatatype().getGLSL() + ' ' + uniform; + + var size = automaticUniform.getSize(); + if (size === 1) { + declaration += ';'; + } else { + declaration += '[' + size.toString() + '];'; + } + + return declaration; + } + /** * A shader program's uniform, including the uniform's value. This is most commonly used to change * the value of a uniform, but can also be used retrieve a uniform's name and datatype, @@ -2329,8 +580,9 @@ define([ * @alias ShaderProgram * @internalConstructor * + * @exception {DeveloperError} A circular dependency was found in the Cesium built-in functions/structs/constants. + * * @see Context#createShaderProgram - * @see Context#getShaderCache */ var ShaderProgram = function(gl, logShaderCompilation, vertexShaderSource, fragmentShaderSource, attributeLocations) { var program = createAndLinkProgram(gl, logShaderCompilation, vertexShaderSource, fragmentShaderSource, attributeLocations); @@ -2371,6 +623,24 @@ define([ this.fragmentShaderSource = fragmentShaderSource; }; + /** + * For ShaderProgram testing + * @private + */ + ShaderProgram._czmBuiltinsAndUniforms = {}; + + // combine automatic uniforms and Cesium built-ins + for ( var builtin in CzmBuiltins) { + if (CzmBuiltins.hasOwnProperty(builtin)) { + ShaderProgram._czmBuiltinsAndUniforms[builtin] = CzmBuiltins[builtin]; + } + } + for ( var uniform in AutomaticUniforms) { + if (AutomaticUniforms.hasOwnProperty(uniform)) { + ShaderProgram._czmBuiltinsAndUniforms[uniform] = getAutomaticUniformDeclaration(AutomaticUniforms, uniform); + } + } + function extractShaderVersion(source) { // This will fail if the first #version is actually in a comment. var index = source.indexOf('#version'); @@ -2402,116 +672,159 @@ define([ }; } - var builtInFunctions = { - czm_RGBToXYZ : czm_RGBToXYZ, - czm_XYZToRGB : czm_XYZToRGB, - czm_antialias : czm_antialias, - czm_cellular : czm_cellular, - czm_columbusViewMorph : czm_columbusViewMorph, - czm_computePosition : czm_computePosition, - czm_eastNorthUpToEyeCoordinates : czm_eastNorthUpToEyeCoordinates, - czm_ellipsoidContainsPoint : czm_ellipsoidContainsPoint, - czm_ellipsoidNew : czm_ellipsoidNew, - czm_ellipsoidWgs84TextureCoordinates : czm_ellipsoidWgs84TextureCoordinates, - czm_equalsEpsilon : czm_equalsEpsilon, - czm_eyeOffset : czm_eyeOffset, - czm_eyeToWindowCoordinates : czm_eyeToWindowCoordinates, - czm_geodeticSurfaceNormal : czm_geodeticSurfaceNormal, - czm_getDefaultMaterial : czm_getDefaultMaterial, - czm_getWaterNoise : czm_getWaterNoise, - czm_getWgs84EllipsoidEC : czm_getWgs84EllipsoidEC, - czm_hue : czm_hue, - czm_isEmpty : czm_isEmpty, - czm_isFull : czm_isFull, - czm_latitudeToWebMercatorFraction : czm_latitudeToWebMercatorFraction, - czm_luminance : czm_luminance, - czm_modelToWindowCoordinates : czm_modelToWindowCoordinates, - czm_multiplyWithColorBalance : czm_multiplyWithColorBalance, - czm_phong : czm_phong, - czm_pointAlongRay : czm_pointAlongRay, - czm_rayEllipsoidIntersectionInterval : czm_rayEllipsoidIntersectionInterval, - czm_saturation : czm_saturation, - czm_snoise : czm_snoise, - czm_tangentToEyeSpaceMatrix : czm_tangentToEyeSpaceMatrix, - czm_translateRelativeToEye : czm_translateRelativeToEye, - czm_transpose : czm_transpose, - czm_windowToEyeCoordinates : czm_windowToEyeCoordinates - }; + function getDependencyNode(name, glslSource, nodes) { + var dependencyNode; + + // check if already loaded + for ( var i = 0; i < nodes.length; ++i) { + if (nodes[i].name === name) { + dependencyNode = nodes[i]; + } + } + + if (!defined(dependencyNode)) { + // strip doc comments so we don't accidentally try to determine a dependency for something found + // in a comment + var commentBlocks = glslSource.match(/\/\*\*[\s\S]*?\*\//gm); + if (defined(commentBlocks) && commentBlocks !== null) { + for (i = 0; i < commentBlocks.length; ++i) { + var commentBlock = commentBlocks[i]; + + // preserve the number of lines in the comment block so the line numbers will be correct when debugging shaders + var numberOfLines = commentBlock.match(/\n/gm).length; + var modifiedComment = ''; + for ( var lineNumber = 0; lineNumber < numberOfLines; ++lineNumber) { + if (lineNumber === 0) { + modifiedComment += '// Comment replaced to prevent problems when determining dependencies on built-in functions\n'; + } else { + modifiedComment += '//\n'; + } + } - function getBuiltinFunctions(source) { - // This expects well-behaved shaders, e.g., the built-in functions are not commented out or redeclared. - var definitions = ''; - var functions = builtInFunctions; - for (var f in functions) { - if (functions.hasOwnProperty(f)) { - if (source.indexOf(f) !== -1) { - definitions += functions[f] + ' \n'; + glslSource = glslSource.replace(commentBlock, modifiedComment); } } + + // create new node + dependencyNode = { + name : name, + glslSource : glslSource, + dependsOn : [], + requiredBy : [], + evaluated : false + }; + nodes.push(dependencyNode); } - return definitions; + return dependencyNode; } - function getFragmentShaderPrecision() { - return '#ifdef GL_FRAGMENT_PRECISION_HIGH \n' + - ' precision highp float; \n' + - '#else \n' + - ' precision mediump float; \n' + - '#endif \n\n'; + function generateDependencies(currentNode, dependencyNodes) { + if (currentNode.evaluated) { + return; + } + + currentNode.evaluated = true; + + // identify all dependencies that are referenced from this glsl source code + var czmMatches = currentNode.glslSource.match(/\bczm_[a-zA-Z0-9_]*/g); + if (defined(czmMatches) && czmMatches !== null) { + // remove duplicates + czmMatches = czmMatches.filter(function(elem, pos) { + return czmMatches.indexOf(elem) === pos; + }); + + czmMatches.forEach(function(element, index, array) { + if (element !== currentNode.name && ShaderProgram._czmBuiltinsAndUniforms.hasOwnProperty(element)) { + var referencedNode = getDependencyNode(element, ShaderProgram._czmBuiltinsAndUniforms[element], dependencyNodes); + currentNode.dependsOn.push(referencedNode); + referencedNode.requiredBy.push(currentNode); + + // recursive call to find any dependencies of the new node + generateDependencies(referencedNode, dependencyNodes); + } + }); + } } - function getAutomaticUniformDeclaration(uniforms, uniform) { - var automaticUniform = uniforms[uniform]; - var declaration = 'uniform ' + automaticUniform.getDatatype().getGLSL() + ' ' + uniform; + function sortDependencies(dependencyNodes) { + var nodesWithoutIncomingEdges = []; + var allNodes = []; - var size = automaticUniform.getSize(); - if (size === 1) { - declaration += ';'; - } else { - declaration += '[' + size.toString() + '];'; + while (dependencyNodes.length > 0) { + var node = dependencyNodes.pop(); + allNodes.push(node); + + if (node.requiredBy.length === 0) { + nodesWithoutIncomingEdges.push(node); + } } - return declaration; - } + while (nodesWithoutIncomingEdges.length > 0) { + var currentNode = nodesWithoutIncomingEdges.shift(); - function getAutomaticUniforms(source) { - // This expects well-behaved shaders, e.g., the automatic uniform is not commented out or redeclared. - var declarations = ''; - var uniforms = allAutomaticUniforms; - for (var uniform in uniforms) { - if (uniforms.hasOwnProperty(uniform)) { - if (source.indexOf(uniform) !== -1) { - declarations += getAutomaticUniformDeclaration(uniforms, uniform) + ' \n'; + dependencyNodes.push(currentNode); + + for ( var i = 0; i < currentNode.dependsOn.length; ++i) { + // remove the edge from the graph + var referencedNode = currentNode.dependsOn[i]; + var index = referencedNode.requiredBy.indexOf(currentNode); + referencedNode.requiredBy.splice(index, 1); + + // if referenced node has no more incoming edges, add to list + if (referencedNode.requiredBy.length === 0) { + nodesWithoutIncomingEdges.push(referencedNode); } } } - return declarations; + // if there are any nodes left with incoming edges, then there was a circular dependency somewhere in the graph + var badNodes = []; + for ( var j = 0; j < allNodes.length; ++j) { + if (allNodes[j].requiredBy.length !== 0) { + badNodes.push(allNodes[j]); + } + } + if (badNodes.length !== 0) { + var message = 'A circular dependency was found in the following built-in functions/structs/constants: \n'; + for (j = 0; j < badNodes.length; ++j) { + message = message + badNodes[j].name + '\n'; + } + throw new DeveloperError(message); + } + } + + function getBuiltinsAndAutomaticUniforms(shaderSource) { + // generate a dependency graph for builtin functions + var dependencyNodes = []; + var root = getDependencyNode('main', shaderSource, dependencyNodes); + generateDependencies(root, dependencyNodes, ShaderProgram._czmBuiltinsAndUniforms); + sortDependencies(dependencyNodes); + + // Concatenate the source code for the function dependencies. + // Iterate in reverse so that dependent items are declared before they are used. + var builtinsSource = ''; + for ( var i = dependencyNodes.length - 1; i >= 0; --i) { + builtinsSource = builtinsSource + dependencyNodes[i].glslSource + '\n'; + } + + return builtinsSource.replace(root.glslSource, ''); + } + + function getFragmentShaderPrecision() { + return '#ifdef GL_FRAGMENT_PRECISION_HIGH \n' + + ' precision highp float; \n' + + '#else \n' + + ' precision mediump float; \n' + + '#endif \n\n'; } function createAndLinkProgram(gl, logShaderCompilation, vertexShaderSource, fragmentShaderSource, attributeLocations) { var vsSourceVersioned = extractShaderVersion(vertexShaderSource); var fsSourceVersioned = extractShaderVersion(fragmentShaderSource); - var vsAndBuiltinFunctions = getBuiltinFunctions(vsSourceVersioned.source) + - '\n#line 0\n' + - vsSourceVersioned.source; - var vsSource = vsSourceVersioned.version + - ShadersBuiltinConstants + - ShadersBuiltinStructs + - getAutomaticUniforms(vsAndBuiltinFunctions) + - vsAndBuiltinFunctions; - - var fsAndBuiltinFunctions = getBuiltinFunctions(fsSourceVersioned.source) + - '\n#line 0\n' + - fsSourceVersioned.source; - var fsSource = fsSourceVersioned.version + - getFragmentShaderPrecision() + - ShadersBuiltinConstants + - ShadersBuiltinStructs + - getAutomaticUniforms(fsAndBuiltinFunctions) + - fsAndBuiltinFunctions; + var vsSource = vsSourceVersioned.version + getBuiltinsAndAutomaticUniforms(vsSourceVersioned.source) + '\n#line 0\n' + vsSourceVersioned.source; + var fsSource = fsSourceVersioned.version + getFragmentShaderPrecision() + getBuiltinsAndAutomaticUniforms(fsSourceVersioned.source) + '\n#line 0\n' + fsSourceVersioned.source; var vertexShader = gl.createShader(gl.VERTEX_SHADER); gl.shaderSource(vertexShader, vsSource); @@ -2601,8 +914,7 @@ define([ for ( var i = 0; i < numberOfUniforms; ++i) { var activeUniform = gl.getActiveUniform(program, i); var suffix = '[0]'; - var uniformName = activeUniform.name.indexOf(suffix, activeUniform.name.length - suffix.length) !== -1 ? - activeUniform.name.slice(0, activeUniform.name.length - 3) : activeUniform.name; + var uniformName = activeUniform.name.indexOf(suffix, activeUniform.name.length - suffix.length) !== -1 ? activeUniform.name.slice(0, activeUniform.name.length - 3) : activeUniform.name; // Ignore GLSL built-in uniforms returned in Firefox. if (uniformName.indexOf('gl_') !== 0) { @@ -2684,9 +996,9 @@ define([ var automaticUniforms = []; var manualUniforms = {}; - for (var uniform in uniforms) { + for ( var uniform in uniforms) { if (uniforms.hasOwnProperty(uniform)) { - var automaticUniform = allAutomaticUniforms[uniform]; + var automaticUniform = AutomaticUniforms[uniform]; if (automaticUniform) { automaticUniforms.push({ uniform : uniforms[uniform], @@ -2779,7 +1091,7 @@ define([ var automaticUniforms = this._automaticUniforms; if (uniformMap) { - for (var uniform in manualUniforms) { + for ( var uniform in manualUniforms) { if (manualUniforms.hasOwnProperty(uniform)) { manualUniforms[uniform].value = uniformMap[uniform](); } diff --git a/Source/Shaders/Builtin/Constants.glsl b/Source/Shaders/Builtin/Constants.glsl deleted file mode 100644 index 9d5df9dd39b4..000000000000 --- a/Source/Shaders/Builtin/Constants.glsl +++ /dev/null @@ -1,329 +0,0 @@ -/** - * A built-in GLSL floating-point constant for Math.PI. - * - * @alias czm_pi - * @glslConstant - * - * @see CesiumMath.PI - * - * @example - * // GLSL declaration - * const float czm_pi = ...; - * - * // Example - * float twoPi = 2.0 * czm_pi; - */ -const float czm_pi = 3.141592653589793; - -/** - * A built-in GLSL floating-point constant for 1/pi. - * - * @alias czm_oneOverPi - * @glslConstant - * - * @see CesiumMath.ONE_OVER_PI - * - * @example - * // GLSL declaration - * const float czm_oneOverPi = ...; - * - * // Example - * float pi = 1.0 / czm_oneOverPi; - */ -const float czm_oneOverPi = 0.3183098861837907; - -/** - * A built-in GLSL floating-point constant for pi/2. - * - * @alias czm_piOverTwo - * @glslConstant - * - * @see CesiumMath.PI_OVER_TWO - * - * @example - * // GLSL declaration - * const float czm_piOverTwo = ...; - * - * // Example - * float pi = 2.0 * czm_piOverTwo; - */ -const float czm_piOverTwo = 1.5707963267948966; - -/** - * A built-in GLSL floating-point constant for pi/3. - * - * @alias czm_piOverThree - * @glslConstant - * - * @see CesiumMath.PI_OVER_THREE - * - * @example - * // GLSL declaration - * const float czm_piOverThree = ...; - * - * // Example - * float pi = 3.0 * czm_piOverThree; - */ -const float czm_piOverThree = 1.0471975511965976; - -/** - * A built-in GLSL floating-point constant for pi/4. - * - * @alias czm_piOverFour - * @glslConstant - * - * @see CesiumMath.PI_OVER_FOUR - * - * @example - * // GLSL declaration - * const float czm_piOverFour = ...; - * - * // Example - * float pi = 4.0 * czm_piOverFour; - */ -const float czm_piOverFour = 0.7853981633974483; - -/** - * A built-in GLSL floating-point constant for pi/6. - * - * @alias czm_piOverSix - * @glslConstant - * - * @see CesiumMath.PI_OVER_SIX - * - * @example - * // GLSL declaration - * const float czm_piOverSix = ...; - * - * // Example - * float pi = 6.0 * czm_piOverSix; - */ -const float czm_piOverSix = 0.5235987755982988; - -/** - * A built-in GLSL floating-point constant for 3pi/2. - * - * @alias czm_threePiOver2 - * @glslConstant - * - * @see CesiumMath.THREE_PI_OVER_TWO - * - * @example - * // GLSL declaration - * const float czm_threePiOver2 = ...; - * - * // Example - * float pi = (2.0 / 3.0) * czm_threePiOver2; - */ -const float czm_threePiOver2 = 4.71238898038469; - -/** - * A built-in GLSL floating-point constant for 2pi. - * - * @alias czm_twoPi - * @glslConstant - * - * @see CesiumMath.TWO_PI - * - * @example - * // GLSL declaration - * const float czm_twoPi = ...; - * - * // Example - * float pi = czm_twoPi / 2.0; - */ -const float czm_twoPi = 6.283185307179586; - -/** - * A built-in GLSL floating-point constant for 1/2pi. - * - * @alias czm_oneOverTwoPi - * @glslConstant - * - * @see CesiumMath.ONE_OVER_TWO_PI - * - * @example - * // GLSL declaration - * const float czm_oneOverTwoPi = ...; - * - * // Example - * float pi = 2.0 * czm_oneOverTwoPi; - */ -const float czm_oneOverTwoPi = 0.15915494309189535; - -/** - * A built-in GLSL floating-point constant for converting degrees to radians. - * - * @alias czm_radiansPerDegree - * @glslConstant - * - * @see CesiumMath.RADIANS_PER_DEGREE - * - * @example - * // GLSL declaration - * const float czm_radiansPerDegree = ...; - * - * // Example - * float rad = czm_radiansPerDegree * deg; - */ -const float czm_radiansPerDegree = 0.017453292519943295; - -/** - * A built-in GLSL floating-point constant for converting radians to degrees. - * - * @alias czm_degreesPerRadian - * @glslConstant - * - * @see CesiumMath.DEGREES_PER_RADIAN - * - * @example - * // GLSL declaration - * const float czm_degreesPerRadian = ...; - * - * // Example - * float deg = czm_degreesPerRadian * rad; - */ -const float czm_degreesPerRadian = 57.29577951308232; - -/** - * A built-in GLSL floating-point constant for one solar radius. - * - * @alias czm_solarRadius - * @glslConstant - * - * @see CesiumMath.SOLAR_RADIUS - * - * @example - * // GLSL declaration - * const float czm_solarRadius = ...; - */ -const float czm_solarRadius = 699500000.0; - -/** - * DOC_TBA - * - * @name czm_infinity - * @glslConstant - */ -const float czm_infinity = 5906376272000.0; // Distance from the Sun to Pluto in meters. TODO: What is best given lowp, mediump, and highp? - -/** - * 0.1 - * - * @name czm_epsilon1 - * @glslConstant - */ -const float czm_epsilon1 = 0.1; - -/** - * 0.01 - * - * @name czm_epsilon2 - * @glslConstant - */ -const float czm_epsilon2 = 0.01; - -/** - * 0.001 - * - * @name czm_epsilon3 - * @glslConstant - */ -const float czm_epsilon3 = 0.001; - -/** - * 0.0001 - * - * @name czm_epsilon4 - * @glslConstant - */ -const float czm_epsilon4 = 0.0001; - -/** - * 0.00001 - * - * @name czm_epsilon5 - * @glslConstant - */ -const float czm_epsilon5 = 0.00001; - -/** - * 0.000001 - * - * @name czm_epsilon6 - * @glslConstant - */ -const float czm_epsilon6 = 0.000001; - -/** - * 0.0000001 - * - * @name czm_epsilon7 - * @glslConstant - */ -const float czm_epsilon7 = 0.0000001; - -/** - * The maximum latitude, in radians, both North and South, supported by a Web Mercator - * (EPSG:3857) projection. Technically, the Mercator projection is defined - * for any latitude up to (but not including) 90 degrees, but it makes sense - * to cut it off sooner because it grows exponentially with increasing latitude. - * The logic behind this particular cutoff value, which is the one used by - * Google Maps, Bing Maps, and Esri, is that it makes the projection - * square. That is, the extent is equal in the X and Y directions. - * - * The constant value is computed as follows: - * czm_pi * 0.5 - (2.0 * atan(exp(-czm_pi))) - * - * @name czm_webMercatorMaxLatitude - * @glslConstant - */ -const float czm_webMercatorMaxLatitude = 1.4844222297453324; - -/** - * The constant identifier for the 2D {@link SceneMode} - * - * @name czm_sceneMode2D - * @glslConstant - * @see czm_sceneMode - * @see czm_sceneModeColumbusView - * @see czm_sceneMode3D - * @see czm_sceneModeMorphing - */ -const float czm_sceneMode2D = 0.0; - -/** - * The constant identifier for the Columbus View {@link SceneMode} - * - * @name czm_sceneModeColumbusView - * @glslConstant - * @see czm_sceneMode - * @see czm_sceneMode2D - * @see czm_sceneMode3D - * @see czm_sceneModeMorphing - */ -const float czm_sceneModeColumbusView = 1.0; - -/** - * The constant identifier for the 3D {@link SceneMode} - * - * @name czm_sceneMode3D - * @glslConstant - * @see czm_sceneMode - * @see czm_sceneMode2D - * @see czm_sceneModeColumbusView - * @see czm_sceneModeMorphing - */ -const float czm_sceneMode3D = 2.0; - -/** - * The constant identifier for the Morphing {@link SceneMode} - * - * @name czm_sceneModeMorphing - * @glslConstant - * @see czm_sceneMode - * @see czm_sceneMode2D - * @see czm_sceneModeColumbusView - * @see czm_sceneMode3D - */ -const float czm_sceneModeMorphing = 3.0; diff --git a/Source/Shaders/Builtin/Constants/degreesPerRadian.glsl b/Source/Shaders/Builtin/Constants/degreesPerRadian.glsl new file mode 100644 index 000000000000..feb332ba776b --- /dev/null +++ b/Source/Shaders/Builtin/Constants/degreesPerRadian.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for converting radians to degrees. + * + * @alias czm_degreesPerRadian + * @glslConstant + * + * @see CesiumMath.DEGREES_PER_RADIAN + * + * @example + * // GLSL declaration + * const float czm_degreesPerRadian = ...; + * + * // Example + * float deg = czm_degreesPerRadian * rad; + */ +const float czm_degreesPerRadian = 57.29577951308232; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/epsilon1.glsl b/Source/Shaders/Builtin/Constants/epsilon1.glsl new file mode 100644 index 000000000000..1fd5c9867d54 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/epsilon1.glsl @@ -0,0 +1,7 @@ +/** + * 0.1 + * + * @name czm_epsilon1 + * @glslConstant + */ +const float czm_epsilon1 = 0.1; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/epsilon2.glsl b/Source/Shaders/Builtin/Constants/epsilon2.glsl new file mode 100644 index 000000000000..88d94d82b2ea --- /dev/null +++ b/Source/Shaders/Builtin/Constants/epsilon2.glsl @@ -0,0 +1,7 @@ +/** + * 0.01 + * + * @name czm_epsilon2 + * @glslConstant + */ +const float czm_epsilon2 = 0.01; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/epsilon3.glsl b/Source/Shaders/Builtin/Constants/epsilon3.glsl new file mode 100644 index 000000000000..688e3a3a9a98 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/epsilon3.glsl @@ -0,0 +1,7 @@ +/** + * 0.001 + * + * @name czm_epsilon3 + * @glslConstant + */ +const float czm_epsilon3 = 0.001; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/epsilon4.glsl b/Source/Shaders/Builtin/Constants/epsilon4.glsl new file mode 100644 index 000000000000..26adac01f362 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/epsilon4.glsl @@ -0,0 +1,7 @@ +/** + * 0.0001 + * + * @name czm_epsilon4 + * @glslConstant + */ +const float czm_epsilon4 = 0.0001; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/epsilon5.glsl b/Source/Shaders/Builtin/Constants/epsilon5.glsl new file mode 100644 index 000000000000..8f2d126f83bc --- /dev/null +++ b/Source/Shaders/Builtin/Constants/epsilon5.glsl @@ -0,0 +1,7 @@ +/** + * 0.00001 + * + * @name czm_epsilon5 + * @glslConstant + */ +const float czm_epsilon5 = 0.00001; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/epsilon6.glsl b/Source/Shaders/Builtin/Constants/epsilon6.glsl new file mode 100644 index 000000000000..e49e17b46883 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/epsilon6.glsl @@ -0,0 +1,7 @@ +/** + * 0.000001 + * + * @name czm_epsilon6 + * @glslConstant + */ +const float czm_epsilon6 = 0.000001; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/epsilon7.glsl b/Source/Shaders/Builtin/Constants/epsilon7.glsl new file mode 100644 index 000000000000..8ab9ec9f0c95 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/epsilon7.glsl @@ -0,0 +1,7 @@ +/** + * 0.0000001 + * + * @name czm_epsilon7 + * @glslConstant + */ +const float czm_epsilon7 = 0.0000001; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/infinity.glsl b/Source/Shaders/Builtin/Constants/infinity.glsl new file mode 100644 index 000000000000..00449e1b6854 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/infinity.glsl @@ -0,0 +1,7 @@ +/** + * DOC_TBA + * + * @name czm_infinity + * @glslConstant + */ +const float czm_infinity = 5906376272000.0; // Distance from the Sun to Pluto in meters. TODO: What is best given lowp, mediump, and highp? \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/oneOverPi.glsl b/Source/Shaders/Builtin/Constants/oneOverPi.glsl new file mode 100644 index 000000000000..6ade399beefa --- /dev/null +++ b/Source/Shaders/Builtin/Constants/oneOverPi.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for 1/pi. + * + * @alias czm_oneOverPi + * @glslConstant + * + * @see CesiumMath.ONE_OVER_PI + * + * @example + * // GLSL declaration + * const float czm_oneOverPi = ...; + * + * // Example + * float pi = 1.0 / czm_oneOverPi; + */ +const float czm_oneOverPi = 0.3183098861837907; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/oneOverTwoPi.glsl b/Source/Shaders/Builtin/Constants/oneOverTwoPi.glsl new file mode 100644 index 000000000000..4ed522243af5 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/oneOverTwoPi.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for 1/2pi. + * + * @alias czm_oneOverTwoPi + * @glslConstant + * + * @see CesiumMath.ONE_OVER_TWO_PI + * + * @example + * // GLSL declaration + * const float czm_oneOverTwoPi = ...; + * + * // Example + * float pi = 2.0 * czm_oneOverTwoPi; + */ +const float czm_oneOverTwoPi = 0.15915494309189535; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/pi.glsl b/Source/Shaders/Builtin/Constants/pi.glsl new file mode 100644 index 000000000000..a12b87cf2ed7 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/pi.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for Math.PI. + * + * @alias czm_pi + * @glslConstant + * + * @see CesiumMath.PI + * + * @example + * // GLSL declaration + * const float czm_pi = ...; + * + * // Example + * float twoPi = 2.0 * czm_pi; + */ +const float czm_pi = 3.141592653589793; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/piOverFour.glsl b/Source/Shaders/Builtin/Constants/piOverFour.glsl new file mode 100644 index 000000000000..269835adecb8 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/piOverFour.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for pi/4. + * + * @alias czm_piOverFour + * @glslConstant + * + * @see CesiumMath.PI_OVER_FOUR + * + * @example + * // GLSL declaration + * const float czm_piOverFour = ...; + * + * // Example + * float pi = 4.0 * czm_piOverFour; + */ +const float czm_piOverFour = 0.7853981633974483; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/piOverSix.glsl b/Source/Shaders/Builtin/Constants/piOverSix.glsl new file mode 100644 index 000000000000..a9fc87965790 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/piOverSix.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for pi/6. + * + * @alias czm_piOverSix + * @glslConstant + * + * @see CesiumMath.PI_OVER_SIX + * + * @example + * // GLSL declaration + * const float czm_piOverSix = ...; + * + * // Example + * float pi = 6.0 * czm_piOverSix; + */ +const float czm_piOverSix = 0.5235987755982988; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/piOverThree.glsl b/Source/Shaders/Builtin/Constants/piOverThree.glsl new file mode 100644 index 000000000000..4ca6dee0629a --- /dev/null +++ b/Source/Shaders/Builtin/Constants/piOverThree.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for pi/3. + * + * @alias czm_piOverThree + * @glslConstant + * + * @see CesiumMath.PI_OVER_THREE + * + * @example + * // GLSL declaration + * const float czm_piOverThree = ...; + * + * // Example + * float pi = 3.0 * czm_piOverThree; + */ +const float czm_piOverThree = 1.0471975511965976; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/piOverTwo.glsl b/Source/Shaders/Builtin/Constants/piOverTwo.glsl new file mode 100644 index 000000000000..450a711e0180 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/piOverTwo.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for pi/2. + * + * @alias czm_piOverTwo + * @glslConstant + * + * @see CesiumMath.PI_OVER_TWO + * + * @example + * // GLSL declaration + * const float czm_piOverTwo = ...; + * + * // Example + * float pi = 2.0 * czm_piOverTwo; + */ +const float czm_piOverTwo = 1.5707963267948966; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/radiansPerDegree.glsl b/Source/Shaders/Builtin/Constants/radiansPerDegree.glsl new file mode 100644 index 000000000000..9ca9b4bdcf56 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/radiansPerDegree.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for converting degrees to radians. + * + * @alias czm_radiansPerDegree + * @glslConstant + * + * @see CesiumMath.RADIANS_PER_DEGREE + * + * @example + * // GLSL declaration + * const float czm_radiansPerDegree = ...; + * + * // Example + * float rad = czm_radiansPerDegree * deg; + */ +const float czm_radiansPerDegree = 0.017453292519943295; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/sceneMode2D.glsl b/Source/Shaders/Builtin/Constants/sceneMode2D.glsl new file mode 100644 index 000000000000..d5e904da11e9 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/sceneMode2D.glsl @@ -0,0 +1,11 @@ +/** + * The constant identifier for the 2D {@link SceneMode} + * + * @name czm_sceneMode2D + * @glslConstant + * @see czm_sceneMode + * @see czm_sceneModeColumbusView + * @see czm_sceneMode3D + * @see czm_sceneModeMorphing + */ +const float czm_sceneMode2D = 0.0; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/sceneMode3D.glsl b/Source/Shaders/Builtin/Constants/sceneMode3D.glsl new file mode 100644 index 000000000000..76111e1ababa --- /dev/null +++ b/Source/Shaders/Builtin/Constants/sceneMode3D.glsl @@ -0,0 +1,11 @@ +/** + * The constant identifier for the 3D {@link SceneMode} + * + * @name czm_sceneMode3D + * @glslConstant + * @see czm_sceneMode + * @see czm_sceneMode2D + * @see czm_sceneModeColumbusView + * @see czm_sceneModeMorphing + */ +const float czm_sceneMode3D = 2.0; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/sceneModeColumbusView.glsl b/Source/Shaders/Builtin/Constants/sceneModeColumbusView.glsl new file mode 100644 index 000000000000..a1365702d5e5 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/sceneModeColumbusView.glsl @@ -0,0 +1,11 @@ +/** + * The constant identifier for the Columbus View {@link SceneMode} + * + * @name czm_sceneModeColumbusView + * @glslConstant + * @see czm_sceneMode + * @see czm_sceneMode2D + * @see czm_sceneMode3D + * @see czm_sceneModeMorphing + */ +const float czm_sceneModeColumbusView = 1.0; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/sceneModeMorphing.glsl b/Source/Shaders/Builtin/Constants/sceneModeMorphing.glsl new file mode 100644 index 000000000000..902af21ad85d --- /dev/null +++ b/Source/Shaders/Builtin/Constants/sceneModeMorphing.glsl @@ -0,0 +1,11 @@ +/** + * The constant identifier for the Morphing {@link SceneMode} + * + * @name czm_sceneModeMorphing + * @glslConstant + * @see czm_sceneMode + * @see czm_sceneMode2D + * @see czm_sceneModeColumbusView + * @see czm_sceneMode3D + */ +const float czm_sceneModeMorphing = 3.0; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/solarRadius.glsl b/Source/Shaders/Builtin/Constants/solarRadius.glsl new file mode 100644 index 000000000000..0e8d9d6b4a31 --- /dev/null +++ b/Source/Shaders/Builtin/Constants/solarRadius.glsl @@ -0,0 +1,13 @@ +/** + * A built-in GLSL floating-point constant for one solar radius. + * + * @alias czm_solarRadius + * @glslConstant + * + * @see CesiumMath.SOLAR_RADIUS + * + * @example + * // GLSL declaration + * const float czm_solarRadius = ...; + */ +const float czm_solarRadius = 699500000.0; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/threePiOver2.glsl b/Source/Shaders/Builtin/Constants/threePiOver2.glsl new file mode 100644 index 000000000000..087818b0e2bd --- /dev/null +++ b/Source/Shaders/Builtin/Constants/threePiOver2.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for 3pi/2. + * + * @alias czm_threePiOver2 + * @glslConstant + * + * @see CesiumMath.THREE_PI_OVER_TWO + * + * @example + * // GLSL declaration + * const float czm_threePiOver2 = ...; + * + * // Example + * float pi = (2.0 / 3.0) * czm_threePiOver2; + */ +const float czm_threePiOver2 = 4.71238898038469; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/twoPi.glsl b/Source/Shaders/Builtin/Constants/twoPi.glsl new file mode 100644 index 000000000000..8be092d5f0cf --- /dev/null +++ b/Source/Shaders/Builtin/Constants/twoPi.glsl @@ -0,0 +1,16 @@ +/** + * A built-in GLSL floating-point constant for 2pi. + * + * @alias czm_twoPi + * @glslConstant + * + * @see CesiumMath.TWO_PI + * + * @example + * // GLSL declaration + * const float czm_twoPi = ...; + * + * // Example + * float pi = czm_twoPi / 2.0; + */ +const float czm_twoPi = 6.283185307179586; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Constants/webMercatorMaxLatitude.glsl b/Source/Shaders/Builtin/Constants/webMercatorMaxLatitude.glsl new file mode 100644 index 000000000000..4b318d81a08b --- /dev/null +++ b/Source/Shaders/Builtin/Constants/webMercatorMaxLatitude.glsl @@ -0,0 +1,16 @@ +/** + * The maximum latitude, in radians, both North and South, supported by a Web Mercator + * (EPSG:3857) projection. Technically, the Mercator projection is defined + * for any latitude up to (but not including) 90 degrees, but it makes sense + * to cut it off sooner because it grows exponentially with increasing latitude. + * The logic behind this particular cutoff value, which is the one used by + * Google Maps, Bing Maps, and Esri, is that it makes the projection + * square. That is, the extent is equal in the X and Y directions. + * + * The constant value is computed as follows: + * czm_pi * 0.5 - (2.0 * atan(exp(-czm_pi))) + * + * @name czm_webMercatorMaxLatitude + * @glslConstant + */ +const float czm_webMercatorMaxLatitude = 1.4844222297453324; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Functions/getLambertDiffuse.glsl b/Source/Shaders/Builtin/Functions/getLambertDiffuse.glsl new file mode 100644 index 000000000000..e25ed49f26f2 --- /dev/null +++ b/Source/Shaders/Builtin/Functions/getLambertDiffuse.glsl @@ -0,0 +1,22 @@ +/** + * Calculates the intensity of diffusely reflected light. + * + * @name czm_getLambertDiffuse + * @glslFunction + * + * @param {vec3} lightDirectionEC Unit vector pointing to the light source in eye coordinates. + * @param {vec3} normalEC The surface normal in eye coordinates. + * + * @returns {float} The intensity of the diffuse reflection. + * + * @see czm_phong + * + * @example + * float diffuseIntensity = czm_getLambertDiffuse(lightDirectionEC, normalEC); + * float specularIntensity = czm_getSpecular(lightDirectionEC, toEyeEC, normalEC, 200); + * vec3 color = (diffuseColor * diffuseIntensity) + (specularColor * specularIntensity); + */ +float czm_getLambertDiffuse(vec3 lightDirectionEC, vec3 normalEC) +{ + return max(dot(lightDirectionEC, normalEC), 0.0); +} \ No newline at end of file diff --git a/Source/Shaders/Builtin/Functions/getSpecular.glsl b/Source/Shaders/Builtin/Functions/getSpecular.glsl new file mode 100644 index 000000000000..57998b693cd9 --- /dev/null +++ b/Source/Shaders/Builtin/Functions/getSpecular.glsl @@ -0,0 +1,26 @@ +/** + * Calculates the specular intensity of reflected light. + * + * @name czm_getSpecular + * @glslFunction + * + * @param {vec3} lightDirectionEC Unit vector pointing to the light source in eye coordinates. + * @param {vec3} toEyeEC Unit vector pointing to the eye position in eye coordinates. + * @param {vec3} normalEC The surface normal in eye coordinates. + * @param {float} shininess The sharpness of the specular reflection. Higher values create a smaller, more focused specular highlight. + * + * @returns {float} The intensity of the specular highlight. + * + * @see czm_phong + * + * @example + * float diffuseIntensity = czm_getLambertDiffuse(lightDirectionEC, normalEC); + * float specularIntensity = czm_getSpecular(lightDirectionEC, toEyeEC, normalEC, 200); + * vec3 color = (diffuseColor * diffuseIntensity) + (specularColor * specularIntensity); + */ +float czm_getSpecular(vec3 lightDirectionEC, vec3 toEyeEC, vec3 normalEC, float shininess) +{ + vec3 toReflectedLight = reflect(-lightDirectionEC, normalEC); + float specular = max(dot(toReflectedLight, toEyeEC), 0.0); + return pow(specular, shininess); +} \ No newline at end of file diff --git a/Source/Shaders/Builtin/Functions/phong.glsl b/Source/Shaders/Builtin/Functions/phong.glsl index e6982c20c589..04512ce85e5a 100644 --- a/Source/Shaders/Builtin/Functions/phong.glsl +++ b/Source/Shaders/Builtin/Functions/phong.glsl @@ -1,23 +1,11 @@ -float getLambertDiffuse(vec3 lightDirectionEC, vec3 normalEC) +float czm_private_getLambertDiffuseOfMaterial(vec3 lightDirectionEC, czm_material material) { - return max(dot(lightDirectionEC, normalEC), 0.0); + return czm_getLambertDiffuse(lightDirectionEC, material.normal); } -float getLambertDiffuseOfMaterial(vec3 lightDirectionEC, czm_material material) +float czm_private_getSpecularOfMaterial(vec3 lightDirectionEC, vec3 toEyeEC, czm_material material) { - return getLambertDiffuse(lightDirectionEC, material.normal); -} - -float getSpecular(vec3 lightDirectionEC, vec3 toEyeEC, vec3 normalEC, float shininess) -{ - vec3 toReflectedLight = reflect(-lightDirectionEC, normalEC); - float specular = max(dot(toReflectedLight, toEyeEC), 0.0); - return pow(specular, shininess); -} - -float getSpecularOfMaterial(vec3 lightDirectionEC, vec3 toEyeEC, czm_material material) -{ - return getSpecular(lightDirectionEC, toEyeEC, material.normal, material.shininess); + return czm_getSpecular(lightDirectionEC, toEyeEC, material.normal, material.shininess); } /** @@ -41,10 +29,10 @@ float getSpecularOfMaterial(vec3 lightDirectionEC, vec3 toEyeEC, czm_material ma vec4 czm_phong(vec3 toEye, czm_material material) { // Diffuse from directional light sources at eye (for top-down and horizon views) - float diffuse = getLambertDiffuseOfMaterial(vec3(0.0, 0.0, 1.0), material) + getLambertDiffuseOfMaterial(vec3(0.0, 1.0, 0.0), material); + float diffuse = czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 0.0, 1.0), material) + czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 1.0, 0.0), material); // Specular from sun and pseudo-moon - float specular = getSpecularOfMaterial(czm_sunDirectionEC, toEye, material) + getSpecularOfMaterial(czm_moonDirectionEC, toEye, material); + float specular = czm_private_getSpecularOfMaterial(czm_sunDirectionEC, toEye, material) + czm_private_getSpecularOfMaterial(czm_moonDirectionEC, toEye, material); vec3 ambient = vec3(0.0); vec3 color = ambient + material.emission; diff --git a/Source/Shaders/Builtin/Structs.glsl b/Source/Shaders/Builtin/Structs.glsl deleted file mode 100644 index d401e41b90c0..000000000000 --- a/Source/Shaders/Builtin/Structs.glsl +++ /dev/null @@ -1,99 +0,0 @@ -/** - * Used as input to every material's czm_getMaterial function. - * - * @name czm_materialInput - * @glslStruct - * - * @property {float} s 1D texture coordinates. - * @property {vec2} st 2D texture coordinates. - * @property {vec3} str 3D texture coordinates. - * @property {vec3} normalEC Unperturbed surface normal in eye coordinates. - * @property {mat3} tangentToEyeMatrix Matrix for converting a tangent space normal to eye space. - * @property {vec3} positionToEyeEC Vector from the fragment to the eye in eye coordinates. The magnitude is the distance in meters from the fragment to the eye. - */ -struct czm_materialInput -{ - float s; - vec2 st; - vec3 str; - vec3 normalEC; - mat3 tangentToEyeMatrix; - vec3 positionToEyeEC; -}; - -/** - * Holds material information that can be used for lighting. Returned by all czm_getMaterial functions. - * - * @name czm_material - * @glslStruct - * - * @property {vec3} diffuse Incoming light that scatters evenly in all directions. - * @property {float} specular Intensity of incoming light reflecting in a single direction. - * @property {float} shininess The sharpness of the specular reflection. Higher values create a smaller, more focused specular highlight. - * @property {vec3} normal Surface's normal in eye coordinates. It is used for effects such as normal mapping. The default is the surface's unmodified normal. - * @property {vec3} emission Light emitted by the material equally in all directions. The default is vec3(0.0), which emits no light. - * @property {float} alpha Opacity of this material. 0.0 is completely transparent; 1.0 is completely opaque. - */ -struct czm_material -{ - vec3 diffuse; - float specular; - float shininess; - vec3 normal; - vec3 emission; - float alpha; -}; - -/** - * DOC_TBA - * - * @name czm_ray - * @glslStruct - */ -struct czm_ray -{ - vec3 origin; - vec3 direction; -}; - -/** - * DOC_TBA - * - * @name czm_raySegment - * @glslStruct - */ -struct czm_raySegment -{ - float start; - float stop; -}; - -/** - * DOC_TBA - * - * @name czm_emptyRaySegment - * @glslConstant - */ -const czm_raySegment czm_emptyRaySegment = czm_raySegment(-czm_infinity, -czm_infinity); - -/** - * DOC_TBA - * - * @name czm_fullRaySegment - * @glslConstant - */ -const czm_raySegment czm_fullRaySegment = czm_raySegment(0.0, czm_infinity); - -/** - * DOC_TBA - * - * @name czm_ellipsoid - * @glslStruct - */ -struct czm_ellipsoid -{ - vec3 center; - vec3 radii; - vec3 inverseRadii; - vec3 inverseRadiiSquared; -}; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Structs/ellipsoid.glsl b/Source/Shaders/Builtin/Structs/ellipsoid.glsl new file mode 100644 index 000000000000..47d69db876a2 --- /dev/null +++ b/Source/Shaders/Builtin/Structs/ellipsoid.glsl @@ -0,0 +1,12 @@ +/** DOC_TBA + * + * @name czm_ellipsoid + * @glslStruct + */ +struct czm_ellipsoid +{ + vec3 center; + vec3 radii; + vec3 inverseRadii; + vec3 inverseRadiiSquared; +}; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Structs/material.glsl b/Source/Shaders/Builtin/Structs/material.glsl new file mode 100644 index 000000000000..6d6679879ee6 --- /dev/null +++ b/Source/Shaders/Builtin/Structs/material.glsl @@ -0,0 +1,22 @@ +/** + * Holds material information that can be used for lighting. Returned by all czm_getMaterial functions. + * + * @name czm_material + * @glslStruct + * + * @property {vec3} diffuse Incoming light that scatters evenly in all directions. + * @property {float} specular Intensity of incoming light reflecting in a single direction. + * @property {float} shininess The sharpness of the specular reflection. Higher values create a smaller, more focused specular highlight. + * @property {vec3} normal Surface's normal in eye coordinates. It is used for effects such as normal mapping. The default is the surface's unmodified normal. + * @property {vec3} emission Light emitted by the material equally in all directions. The default is vec3(0.0), which emits no light. + * @property {float} alpha Opacity of this material. 0.0 is completely transparent; 1.0 is completely opaque. + */ +struct czm_material +{ + vec3 diffuse; + float specular; + float shininess; + vec3 normal; + vec3 emission; + float alpha; +}; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Structs/materialInput.glsl b/Source/Shaders/Builtin/Structs/materialInput.glsl new file mode 100644 index 000000000000..b9c82f02952e --- /dev/null +++ b/Source/Shaders/Builtin/Structs/materialInput.glsl @@ -0,0 +1,22 @@ +/** + * Used as input to every material's czm_getMaterial function. + * + * @name czm_materialInput + * @glslStruct + * + * @property {float} s 1D texture coordinates. + * @property {vec2} st 2D texture coordinates. + * @property {vec3} str 3D texture coordinates. + * @property {vec3} normalEC Unperturbed surface normal in eye coordinates. + * @property {mat3} tangentToEyeMatrix Matrix for converting a tangent space normal to eye space. + * @property {vec3} positionToEyeEC Vector from the fragment to the eye in eye coordinates. The magnitude is the distance in meters from the fragment to the eye. + */ +struct czm_materialInput +{ + float s; + vec2 st; + vec3 str; + vec3 normalEC; + mat3 tangentToEyeMatrix; + vec3 positionToEyeEC; +}; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Structs/ray.glsl b/Source/Shaders/Builtin/Structs/ray.glsl new file mode 100644 index 000000000000..dbc233ad9e56 --- /dev/null +++ b/Source/Shaders/Builtin/Structs/ray.glsl @@ -0,0 +1,11 @@ +/** + * DOC_TBA + * + * @name czm_ray + * @glslStruct + */ +struct czm_ray +{ + vec3 origin; + vec3 direction; +}; \ No newline at end of file diff --git a/Source/Shaders/Builtin/Structs/raySegment.glsl b/Source/Shaders/Builtin/Structs/raySegment.glsl new file mode 100644 index 000000000000..e08505789665 --- /dev/null +++ b/Source/Shaders/Builtin/Structs/raySegment.glsl @@ -0,0 +1,27 @@ +/** + * DOC_TBA + * + * @name czm_raySegment + * @glslStruct + */ +struct czm_raySegment +{ + float start; + float stop; +}; + +/** + * DOC_TBA + * + * @name czm_emptyRaySegment + * @glslConstant + */ +const czm_raySegment czm_emptyRaySegment = czm_raySegment(-czm_infinity, -czm_infinity); + +/** + * DOC_TBA + * + * @name czm_fullRaySegment + * @glslConstant + */ +const czm_raySegment czm_fullRaySegment = czm_raySegment(0.0, czm_infinity); diff --git a/Source/Shaders/CentralBodyFS.glsl b/Source/Shaders/CentralBodyFS.glsl index bc97a4151027..1ff383c33470 100644 --- a/Source/Shaders/CentralBodyFS.glsl +++ b/Source/Shaders/CentralBodyFS.glsl @@ -26,20 +26,6 @@ varying vec3 v_positionMC; varying vec3 v_positionEC; varying vec2 v_textureCoordinates; -// TODO: use built-in function when shader pipeline is ready -float getLambertDiffuse(vec3 lightDirectionEC, vec3 normalEC) -{ - return max(dot(lightDirectionEC, normalEC), 0.0); -} - -// TODO: use built-in function when shader pipeline is ready -float getSpecular(vec3 lightDirectionEC, vec3 toEyeEC, vec3 normalEC, float shininess) -{ - vec3 toReflectedLight = reflect(-lightDirectionEC, normalEC); - float specular = max(dot(toReflectedLight, toEyeEC), 0.0); - return pow(specular, shininess); -} - vec3 sampleAndBlend( vec3 previousColor, sampler2D texture, @@ -188,7 +174,7 @@ vec4 computeWaterColor(vec3 positionEyeCoordinates, vec2 textureCoordinates, mat const vec3 waveHighlightColor = vec3(0.3, 0.45, 0.6); // Use diffuse light to highlight the waves - float diffuseIntensity = getLambertDiffuse(czm_sunDirectionEC, normalEC); + float diffuseIntensity = czm_getLambertDiffuse(czm_sunDirectionEC, normalEC); vec3 diffuseHighlight = waveHighlightColor * diffuseIntensity; #ifdef SHOW_OCEAN_WAVES @@ -201,7 +187,7 @@ vec4 computeWaterColor(vec3 positionEyeCoordinates, vec2 textureCoordinates, mat #endif // Add specular highlights in 3D, and in all modes when zoomed in. - float specularIntensity = getSpecular(czm_sunDirectionEC, normalizedpositionToEyeEC, normalEC, 10.0) + 0.25 * getSpecular(czm_moonDirectionEC, normalizedpositionToEyeEC, normalEC, 10.0); + float specularIntensity = czm_getSpecular(czm_sunDirectionEC, normalizedpositionToEyeEC, normalEC, 10.0) + 0.25 * czm_getSpecular(czm_moonDirectionEC, normalizedpositionToEyeEC, normalEC, 10.0); float surfaceReflectance = mix(0.0, mix(u_zoomedOutOceanSpecularIntensity, oceanSpecularIntensity, waveIntensity), specularMapValue); float specular = specularIntensity * surfaceReflectance; diff --git a/Specs/Renderer/ShaderProgramSpec.js b/Specs/Renderer/ShaderProgramSpec.js index e88040e06ce3..79720e46bbab 100644 --- a/Specs/Renderer/ShaderProgramSpec.js +++ b/Specs/Renderer/ShaderProgramSpec.js @@ -1,5 +1,6 @@ /*global defineSuite*/ defineSuite([ + 'Renderer/ShaderProgram', 'Specs/createContext', 'Specs/destroyContext', 'Core/Cartesian2', @@ -11,8 +12,11 @@ defineSuite([ 'Core/PrimitiveType', 'Renderer/BufferUsage', 'Renderer/ClearCommand', - 'Renderer/UniformDatatype' - ], 'Renderer/ShaderProgram', function( + 'Renderer/UniformDatatype', + 'Shaders/Builtin/CzmBuiltins', + 'Renderer/ShaderProgram' + ], function( + ShaderProgram, createContext, destroyContext, Cartesian2, @@ -24,7 +28,8 @@ defineSuite([ PrimitiveType, BufferUsage, ClearCommand, - UniformDatatype) { + UniformDatatype, + CzmBuiltins) { "use strict"; /*global jasmine,describe,xdescribe,it,xit,expect,beforeEach,afterEach,beforeAll,afterAll,spyOn,runs,waits,waitsFor*/ @@ -32,14 +37,62 @@ defineSuite([ var sp; var va; + var injectedTestFunctions = { + czm_circularDependency1 : 'void czm_circularDependency1() { czm_circularDependency2(); }', + czm_circularDependency2 : 'void czm_circularDependency2() { czm_circularDependency1(); }', + czm_testFunction3 : 'void czm_testFunction3(vec4 color) { czm_testFunction2(color); }', + czm_testFunction2 : 'void czm_testFunction2(vec4 color) { czm_testFunction1(color); }', + czm_testFunction1 : 'void czm_testFunction1(vec4 color) { gl_FragColor = color; }', + czm_testDiamondDependency1 : 'vec4 czm_testDiamondDependency1(vec4 color) { return czm_testAddAlpha(color); }', + czm_testDiamondDependency2 : 'vec4 czm_testDiamondDependency2(vec4 color) { return czm_testAddAlpha(color); }', + czm_testAddAlpha : 'vec4 czm_testAddAlpha(vec4 color) { color.a = clamp(color.a + 0.1, 0.0, 1.0); return color; }', + czm_testAddRed : 'vec4 czm_testAddRed(vec4 color) { color.r = clamp(color.r + 0.1, 0.0, 1.0); return color; }', + czm_testAddGreen : 'vec4 czm_testAddGreen(vec4 color) { color.g = clamp(color.g + 0.1, 0.0, 1.0); return color; }', + czm_testAddRedGreenAlpha : 'vec4 czm_testAddRedGreenAlpha(vec4 color) { color = czm_testAddRed(color); color = czm_testAddGreen(color); color = czm_testAddAlpha(color); return color; }', + czm_testFunction4 : 'void czm_testFunction4(vec4 color) { color = czm_testAddAlpha(color); color = czm_testAddRedGreenAlpha(color); czm_testFunction3(color); }', + czm_testFunctionWithComment : '/**\n czm_circularDependency1() \n*/\nvoid czm_testFunctionWithComment(vec4 color) { czm_testFunction1(color); }' + }; + beforeAll(function() { context = createContext(); + + for(var functionName in injectedTestFunctions) { + if(injectedTestFunctions.hasOwnProperty(functionName)) { + ShaderProgram._czmBuiltinsAndUniforms[functionName] = injectedTestFunctions[functionName]; + } + } + }); afterAll(function() { destroyContext(context); + + for ( var functionName in injectedTestFunctions) { + if (injectedTestFunctions.hasOwnProperty(functionName)) { + delete ShaderProgram._czmBuiltinsAndUniforms[functionName]; + } + } }); + function renderFragment(context, shaderProgram) { + va = context.createVertexArray([{ + index : shaderProgram.getVertexAttributes().position.index, + vertexBuffer : context.createVertexBuffer(new Float32Array([0, 0, 0, 1]), BufferUsage.STATIC_DRAW), + componentsPerAttribute : 4 + }]); + + ClearCommand.ALL.execute(context); + expect(context.readPixels()).toEqual([0, 0, 0, 0]); + + context.draw({ + primitiveType : PrimitiveType.POINTS, + shaderProgram : shaderProgram, + vertexArray : va + }); + + return context.readPixels(); + } + it('has vertex and fragment shader source', function() { var vs = 'void main() { gl_Position = vec4(1.0); }'; var fs = 'void main() { gl_FragColor = vec4(1.0); }'; @@ -437,21 +490,110 @@ defineSuite([ '}'; sp = context.createShaderProgram(vs, fs); - va = context.createVertexArray([{ - index : sp.getVertexAttributes().position.index, - vertexBuffer : context.createVertexBuffer(new Float32Array([0, 0, 0, 1]), BufferUsage.STATIC_DRAW), - componentsPerAttribute : 4 - }]); + expect(renderFragment(context, sp)).toEqual([255, 255, 255, 255]); + }); - ClearCommand.ALL.execute(context); - expect(context.readPixels()).toEqual([0, 0, 0, 0]); + it('has built-in constant, structs, and functions', function() { + var vs = 'attribute vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }'; + var fs = + 'void main() { \n' + + ' czm_materialInput materialInput; \n' + + ' czm_material material = czm_getDefaultMaterial(materialInput); \n' + + ' material.diffuse = vec3(1.0, 1.0, 1.0); \n' + + ' material.alpha = 1.0; \n' + + ' material.diffuse = czm_hue(material.diffuse, czm_twoPi); \n' + + ' gl_FragColor = vec4(material.diffuse, material.alpha); \n' + + '}'; + sp = context.createShaderProgram(vs, fs); - context.draw({ - primitiveType : PrimitiveType.POINTS, - shaderProgram : sp, - vertexArray : va - }); - expect(context.readPixels()).toEqual([255, 255, 255, 255]); + expect(renderFragment(context, sp)).toEqual([255, 255, 255, 255]); + }); + + it('1 level function dependency', function() { + var vs = 'attribute vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }'; + var fs = + 'void main() { \n' + + ' czm_testFunction1(vec4(1.0)); \n' + + '}'; + sp = context.createShaderProgram(vs, fs); + + expect(renderFragment(context, sp)).toEqual([255, 255, 255, 255]); + }); + + it('2 level function dependency', function() { + var vs = 'attribute vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }'; + var fs = + 'void main() { \n' + + ' czm_testFunction2(vec4(1.0)); \n' + + '}'; + sp = context.createShaderProgram(vs, fs); + + expect(renderFragment(context, sp)).toEqual([255, 255, 255, 255]); + }); + + it('3 level function dependency', function() { + var vs = 'attribute vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }'; + var fs = + 'void main() { \n' + + ' czm_testFunction3(vec4(1.0)); \n' + + '}'; + sp = context.createShaderProgram(vs, fs); + + expect(renderFragment(context, sp)).toEqual([255, 255, 255, 255]); + }); + + it('diamond dependency', function() { + var vs = 'attribute vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }'; + var fs = + 'void main() { \n' + + ' vec4 color = vec4(1.0, 1.0, 1.0, 0.8); \n' + + ' color = czm_testDiamondDependency1(color); \n' + + ' color = czm_testDiamondDependency2(color); \n' + + ' gl_FragColor = color; \n' + + '}'; + sp = context.createShaderProgram(vs, fs); + + expect(renderFragment(context, sp)).toEqual([255, 255, 255, 255]); + }); + + it('diamond plus 3 level function dependency', function() { + var vs = 'attribute vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }'; + var fs = + 'void main() { \n' + + ' vec4 color = vec4(1.0, 1.0, 1.0, 0.8); \n' + + ' color = czm_testDiamondDependency1(color); \n' + + ' color = czm_testDiamondDependency2(color); \n' + + ' czm_testFunction3(color); \n' + + '}'; + sp = context.createShaderProgram(vs, fs); + + expect(renderFragment(context, sp)).toEqual([255, 255, 255, 255]); + }); + + it('big mess of function dependencies', function() { + var vs = 'attribute vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }'; + var fs = + 'void main() { \n' + + ' vec4 color = vec4(0.9, 0.9, 1.0, 0.6); \n' + + ' color = czm_testDiamondDependency1(color); \n' + + ' color = czm_testDiamondDependency2(color); \n' + + ' czm_testFunction4(color); \n' + + '}'; + sp = context.createShaderProgram(vs, fs); + + expect(renderFragment(context, sp)).toEqual([255, 255, 255, 255]); + }); + + it('doc comment with reference to another function', function() { + var vs = 'attribute vec4 position; void main() { gl_PointSize = 1.0; gl_Position = position; }'; + var fs = + 'void main() { \n' + + ' vec4 color = vec4(1.0, 1.0, 1.0, 1.0); \n' + + ' czm_testFunctionWithComment(color); \n' + + '}'; + sp = context.createShaderProgram(vs, fs); + + expect(renderFragment(context, sp)).toEqual([255, 255, 255, 255]); }); it('compiles with #version at the top', function() { @@ -523,4 +665,12 @@ defineSuite([ s.destroy(); }).toThrow(); }); + + it('fails with built-in function circular dependency', function() { + var vs = 'void main() { gl_Position = vec4(0.0); }'; + var fs = 'void main() { czm_circularDependency1(); gl_FragColor = vec4(1.0); }'; + expect(function() { + sp = context.createShaderProgram(vs, fs); + }).toThrow(); + }); }, 'WebGL'); \ No newline at end of file diff --git a/Tools/buildTasks/glslToJavaScript.js b/Tools/buildTasks/glslToJavaScript.js index d0f2cf183aa5..333034eb4990 100644 --- a/Tools/buildTasks/glslToJavaScript.js +++ b/Tools/buildTasks/glslToJavaScript.js @@ -25,11 +25,26 @@ forEachFile('existingjsfiles', function(relativePath, file) { leftOverJsFiles.add(file.getAbsolutePath()); }); +var builtinFunctions = []; +var builtinConstants = []; +var builtinStructs = []; + forEachFile('glslfiles', function(relativePath, file) { "use strict"; var glslFile = file; var jsFile = new File(file.getParent(), file.getName().replace('.glsl', '.js')); + // identify built in functions, structs, and constants + if(glslFile.getPath().indexOf('Builtin' + File.separator + 'Functions') !== -1) { + builtinFunctions.push(file.getName().replace('.glsl', '')); + } + else if(glslFile.getPath().indexOf('Builtin' + File.separator + 'Constants') !== -1) { + builtinConstants.push(file.getName().replace('.glsl', '')); + } + else if(glslFile.getPath().indexOf('Builtin' + File.separator + 'Structs') !== -1) { + builtinStructs.push(file.getName().replace('.glsl', '')); + } + leftOverJsFiles.remove(jsFile.getAbsolutePath()); if (jsFile.exists() && jsFile.lastModified() > glslFile.lastModified() && jsFile.lastModified() > minifyStateFileLastModified) { @@ -53,11 +68,11 @@ forEachFile('glslfiles', function(relativePath, file) { contents = contents.split('"').join('\\"').replace(/\n/gm, '\\n\\\n'); contents = copyrightComments + '\ -//This file is automatically rebuilt by the Cesium build process.\n\ -/*global define*/\n\ -define(function() {\n\ -"use strict";\n\ -return "' + contents + '";\n\ + //This file is automatically rebuilt by the Cesium build process.\n\ + /*global define*/\n\ + define(function() {\n\ + "use strict";\n\ + return "' + contents + '";\n\ });'; writeFileContents(jsFile.getAbsolutePath(), contents, true); @@ -66,4 +81,45 @@ return "' + contents + '";\n\ // delete any left over JS files from old shaders for ( var it = leftOverJsFiles.iterator(); it.hasNext();) { new File(it.next())['delete'](); -} \ No newline at end of file +} + +var generateBuiltinContents = function(contents, builtins, path){ + "use strict"; + var amdPath = contents.amdPath; + var amdClassName = contents.amdClassName; + var builtinLookup = contents.builtinLookup; + for (var i = 0; i < builtins.length; i++) { + var builtin = builtins[i]; + amdPath = amdPath + ',\n \'./' + path + '/' + builtin + '\''; + amdClassName = amdClassName + ',\n ' + 'czm_' + builtin; + builtinLookup = builtinLookup + ',\n ' + 'czm_' + builtin + ' : ' + 'czm_' + builtin; + } + contents.amdPath = amdPath; + contents.amdClassName = amdClassName; + contents.builtinLookup = builtinLookup; +}; + +//generate the JS file for Built-in GLSL Functions, Structs, and Constants +var contents = {amdPath:'', amdClassName:'', builtinLookup:''}; +generateBuiltinContents(contents, builtinConstants, 'Constants'); +generateBuiltinContents(contents, builtinStructs, 'Structs'); +generateBuiltinContents(contents, builtinFunctions, 'Functions'); + +contents.amdPath = contents.amdPath.replace(',\n', ''); +contents.amdClassName = contents.amdClassName.replace(',\n', ''); +contents.builtinLookup = contents.builtinLookup.replace(',\n', ''); + +var fileContents = '\ +//This file is automatically rebuilt by the Cesium build process.\n\ +/*global define*/\n\ +define([\n' + +contents.amdPath + +'\n ], function(\n' + +contents.amdClassName + +') {\n\ + "use strict";\n\ + return {\n' + contents.builtinLookup + '};\n\ +});'; + +var builtinFile = new File(project.getProperty('shadersDirectory') + '/Builtin', 'CzmBuiltins.js'); +writeFileContents(builtinFile.getAbsolutePath(), fileContents, true);