├── ShaderSource ├── Builtin │ ├── Constants │ │ ├── epsilon1.glsl │ │ ├── epsilon2.glsl │ │ ├── epsilon3.glsl │ │ ├── epsilon4.glsl │ │ ├── epsilon5.glsl │ │ ├── epsilon6.glsl │ │ ├── epsilon7.glsl │ │ ├── passGlobe.glsl │ │ ├── passOpaque.glsl │ │ ├── passCompute.glsl │ │ ├── passOverlay.glsl │ │ ├── passEnvironment.glsl │ │ ├── passTranslucent.glsl │ │ ├── infinity.glsl │ │ ├── passCesium3DTile.glsl │ │ ├── passClassification.glsl │ │ ├── passTerrainClassification.glsl │ │ ├── passCesium3DTileClassification.glsl │ │ ├── sceneMode2D.glsl │ │ ├── sceneMode3D.glsl │ │ ├── passCesium3DTileClassificationIgnoreShow.glsl │ │ ├── sceneModeMorphing.glsl │ │ ├── sceneModeColumbusView.glsl │ │ ├── solarRadius.glsl │ │ ├── pi.glsl │ │ ├── twoPi.glsl │ │ ├── oneOverPi.glsl │ │ ├── piOverSix.glsl │ │ ├── piOverTwo.glsl │ │ ├── piOverFour.glsl │ │ ├── piOverThree.glsl │ │ ├── oneOverTwoPi.glsl │ │ ├── threePiOver2.glsl │ │ ├── degreesPerRadian.glsl │ │ ├── radiansPerDegree.glsl │ │ ├── depthRange.glsl │ │ └── webMercatorMaxLatitude.glsl │ ├── Functions │ │ ├── readDepth.glsl │ │ ├── cascadeDistance.glsl │ │ ├── cascadeColor.glsl │ │ ├── columbusViewMorph.glsl │ │ ├── ellipsoidWgs84TextureCoordinates.glsl │ │ ├── inverseGamma.glsl │ │ ├── cascadeWeights.glsl │ │ ├── cascadeMatrix.glsl │ │ ├── ellipsoidContainsPoint.glsl │ │ ├── reverseLogDepth.glsl │ │ ├── acesTonemapping.glsl │ │ ├── alphaWeight.glsl │ │ ├── backFacing.glsl │ │ ├── eyeOffset.glsl │ │ ├── luminance.glsl │ │ ├── gammaCorrect.glsl │ │ ├── geodeticSurfaceNormal.glsl │ │ ├── transformPlane.glsl │ │ ├── unpackDepth.glsl │ │ ├── defaultPbrMaterial.glsl │ │ ├── multiplyWithColorBalance.glsl │ │ ├── decompressTextureCoordinates.glsl │ │ ├── lineDistance.glsl │ │ ├── isEmpty.glsl │ │ ├── packDepth.glsl │ │ ├── isFull.glsl │ │ ├── pointAlongRay.glsl │ │ ├── shadowDepthCompare.glsl │ │ ├── saturation.glsl │ │ ├── HSBToRGB.glsl │ │ ├── writeDepthClamp.glsl │ │ ├── computePosition.glsl │ │ ├── HSLToRGB.glsl │ │ ├── getLambertDiffuse.glsl │ │ ├── approximateSphericalCoordinates.glsl │ │ ├── RGBToHSB.glsl │ │ ├── getDefaultMaterial.glsl │ │ ├── RGBToXYZ.glsl │ │ ├── unpackFloat.glsl │ │ ├── XYZToRGB.glsl │ │ ├── signNotZero.glsl │ │ ├── planeDistance.glsl │ │ ├── translucentPhong.glsl │ │ ├── RGBToHSL.glsl │ │ ├── nearFarScalar.glsl │ │ ├── tangentToEyeSpaceMatrix.glsl │ │ ├── hue.glsl │ │ ├── pbrSpecularGlossinessMaterial.glsl │ │ ├── latitudeToWebMercatorFraction.glsl │ │ ├── readNonPerspective.glsl │ │ ├── writeNonPerspective.glsl │ │ ├── sphericalHarmonics.glsl │ │ ├── getSpecular.glsl │ │ ├── eyeToWindowCoordinates.glsl │ │ ├── fog.glsl │ │ ├── transpose.glsl │ │ ├── pbrMetallicRoughnessMaterial.glsl │ │ ├── getWaterNoise.glsl │ │ ├── equalsEpsilon.glsl │ │ ├── translateRelativeToEye.glsl │ │ ├── modelToWindowCoordinates.glsl │ │ ├── antialias.glsl │ │ ├── eastNorthUpToEyeCoordinates.glsl │ │ ├── depthClamp.glsl │ │ ├── metersPerPixel.glsl │ │ ├── vertexLogDepth.glsl │ │ ├── writeLogDepth.glsl │ │ ├── phong.glsl │ │ ├── fastApproximateAtan.glsl │ │ ├── branchFreeTernary.glsl │ │ ├── shadowVisibility.glsl │ │ ├── rayEllipsoidIntersectionInterval.glsl │ │ ├── sampleOctahedralProjection.glsl │ │ ├── octDecode.glsl │ │ └── pbrLighting.glsl │ └── Structs │ │ ├── ray.glsl │ │ ├── depthRangeStruct.glsl │ │ ├── shadowParameters.glsl │ │ ├── raySegment.glsl │ │ ├── pbrParameters.glsl │ │ ├── material.glsl │ │ └── materialInput.glsl ├── Appearances │ ├── PerInstanceFlatColorAppearanceFS.glsl │ ├── PerInstanceFlatColorAppearanceVS.glsl │ ├── BasicMaterialAppearanceVS.glsl │ ├── EllipsoidSurfaceAppearanceVS.glsl │ ├── TexturedMaterialAppearanceVS.glsl │ ├── PerInstanceColorAppearanceVS.glsl │ ├── BasicMaterialAppearanceFS.glsl │ ├── PerInstanceColorAppearanceFS.glsl │ ├── TexturedMaterialAppearanceFS.glsl │ ├── PolylineColorAppearanceVS.glsl │ ├── AllMaterialAppearanceVS.glsl │ ├── AllMaterialAppearanceFS.glsl │ ├── PolylineMaterialAppearanceVS.glsl │ └── EllipsoidSurfaceAppearanceFS.glsl ├── ReprojectWebMercatorFS.glsl ├── PostProcessStages │ ├── PassThrough.glsl │ ├── PassThroughDepth.glsl │ ├── DepthView.glsl │ ├── Brightness.glsl │ ├── Silhouette.glsl │ ├── AmbientOcclusionModulate.glsl │ ├── NightVision.glsl │ ├── AdditiveBlend.glsl │ ├── BloomComposite.glsl │ ├── DepthViewPacked.glsl │ ├── AcesTonemappingStage.glsl │ ├── BlackAndWhite.glsl │ ├── ContrastBias.glsl │ ├── ReinhardTonemapping.glsl │ ├── FXAA.glsl │ ├── ModifiedReinhardTonemapping.glsl │ ├── CompositeTranslucentClassification.glsl │ ├── BrightPass.glsl │ ├── FilmicTonemapping.glsl │ ├── DepthOfField.glsl │ ├── GaussianBlur1D.glsl │ ├── EdgeDetection.glsl │ └── PointCloudEyeDomeLighting.glsl ├── SunFS.glsl ├── DepthPlaneVS.glsl ├── ViewportQuadVS.glsl ├── VectorTileVS.glsl ├── OctahedralProjectionVS.glsl ├── SkyBoxFS.glsl ├── SkyBoxVS.glsl ├── ReprojectWebMercatorVS.glsl ├── ShadowVolumeFS.glsl ├── OctahedralProjectionFS.glsl ├── Materials │ ├── AspectRampMaterial.glsl │ ├── SlopeRampMaterial.glsl │ ├── ElevationRampMaterial.glsl │ ├── DotMaterial.glsl │ ├── NormalMapMaterial.glsl │ ├── RimLightingMaterial.glsl │ ├── PolylineGlowMaterial.glsl │ ├── StripeMaterial.glsl │ ├── ElevationContourMaterial.glsl │ ├── PolylineOutlineMaterial.glsl │ ├── BumpMapMaterial.glsl │ ├── FadeMaterial.glsl │ ├── CheckerboardMaterial.glsl │ ├── PolylineDashMaterial.glsl │ ├── PolylineArrowMaterial.glsl │ ├── GridMaterial.glsl │ ├── ElevationBandMaterial.glsl │ └── Water.glsl ├── ViewportQuadFS.glsl ├── SkyAtmosphereVS.glsl ├── CompareAndPackTranslucentDepth.glsl ├── AdjustTranslucentFS.glsl ├── PolylineFS.glsl ├── SkyAtmosphereFS.glsl ├── SunVS.glsl ├── Vector3DTilePolylinesVS.glsl ├── CompositeOITFS.glsl ├── DepthPlaneFS.glsl ├── EllipsoidVS.glsl ├── PointPrimitiveCollectionFS.glsl ├── PolylineShadowVolumeMorphFS.glsl ├── Vector3DTileClampedPolylinesFS.glsl ├── SunTextureFS.glsl ├── OctahedralProjectionAtlasFS.glsl ├── BrdfLutGeneratorFS.glsl └── Vector3DTileClampedPolylinesVS.glsl ├── Docs └── Builtin │ ├── Functions │ ├── planeDistance.md │ ├── readDepth.md │ ├── reverseLogDepth.md │ ├── saturation.md │ └── windowToEyeCoordinates.md │ └── Structs │ ├── ray.md │ ├── depthRangeStruct.md │ ├── raySegment.md │ ├── pbrParameters.md │ ├── shadowParameters.md │ ├── material.md │ └── materialInput.md └── README.md /ShaderSource/Builtin/Constants/epsilon1.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * 0.1 3 | * 4 | * @name czm_epsilon1 5 | * @glslConstant 6 | */ 7 | const float czm_epsilon1 = 0.1; 8 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/epsilon2.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * 0.01 3 | * 4 | * @name czm_epsilon2 5 | * @glslConstant 6 | */ 7 | const float czm_epsilon2 = 0.01; 8 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/epsilon3.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * 0.001 3 | * 4 | * @name czm_epsilon3 5 | * @glslConstant 6 | */ 7 | const float czm_epsilon3 = 0.001; 8 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/epsilon4.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * 0.0001 3 | * 4 | * @name czm_epsilon4 5 | * @glslConstant 6 | */ 7 | const float czm_epsilon4 = 0.0001; 8 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/PerInstanceFlatColorAppearanceFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec4 v_color; 2 | 3 | void main() 4 | { 5 | gl_FragColor = czm_gammaCorrect(v_color); 6 | } 7 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/epsilon5.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * 0.00001 3 | * 4 | * @name czm_epsilon5 5 | * @glslConstant 6 | */ 7 | const float czm_epsilon5 = 0.00001; 8 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/epsilon6.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * 0.000001 3 | * 4 | * @name czm_epsilon6 5 | * @glslConstant 6 | */ 7 | const float czm_epsilon6 = 0.000001; 8 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/epsilon7.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * 0.0000001 3 | * 4 | * @name czm_epsilon7 5 | * @glslConstant 6 | */ 7 | const float czm_epsilon7 = 0.0000001; 8 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/readDepth.glsl: -------------------------------------------------------------------------------- 1 | float czm_readDepth(sampler2D depthTexture, vec2 texCoords) 2 | { 3 | return czm_reverseLogDepth(texture2D(depthTexture, texCoords).r); 4 | } 5 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Structs/ray.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_ray 5 | * @glslStruct 6 | */ 7 | struct czm_ray 8 | { 9 | vec3 origin; 10 | vec3 direction; 11 | }; 12 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Structs/depthRangeStruct.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * @name czm_depthRangeStruct 3 | * @glslStruct 4 | */ 5 | struct czm_depthRangeStruct 6 | { 7 | float near; 8 | float far; 9 | }; 10 | -------------------------------------------------------------------------------- /ShaderSource/ReprojectWebMercatorFS.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D u_texture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | void main() 6 | { 7 | gl_FragColor = texture2D(u_texture, v_textureCoordinates); 8 | } 9 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/cascadeDistance.glsl: -------------------------------------------------------------------------------- 1 | 2 | uniform vec4 shadowMap_cascadeDistances; 3 | 4 | float czm_cascadeDistance(vec4 weights) 5 | { 6 | return dot(shadowMap_cascadeDistances, weights); 7 | } 8 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/PassThrough.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | void main() 6 | { 7 | gl_FragColor = texture2D(colorTexture, v_textureCoordinates); 8 | } 9 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passGlobe.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#GLOBE} 3 | * 4 | * @name czm_passGlobe 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passGlobe = 2.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passOpaque.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#OPAQUE} 3 | * 4 | * @name czm_passOpaque 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passOpaque = 7.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passCompute.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#COMPUTE} 3 | * 4 | * @name czm_passCompute 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passCompute = 1.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passOverlay.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#OVERLAY} 3 | * 4 | * @name czm_passOverlay 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passOverlay = 9.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/SunFS.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D u_texture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | void main() 6 | { 7 | vec4 color = texture2D(u_texture, v_textureCoordinates); 8 | gl_FragColor = czm_gammaCorrect(color); 9 | } 10 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/PassThroughDepth.glsl: -------------------------------------------------------------------------------- 1 | uniform highp sampler2D u_depthTexture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | void main() 6 | { 7 | gl_FragColor = czm_packDepth(texture2D(u_depthTexture, v_textureCoordinates).r); 8 | } 9 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passEnvironment.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#ENVIRONMENT} 3 | * 4 | * @name czm_passEnvironment 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passEnvironment = 0.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passTranslucent.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#TRANSLUCENT} 3 | * 4 | * @name czm_passTranslucent 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passTranslucent = 8.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/DepthPlaneVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec4 position; 2 | 3 | varying vec4 positionEC; 4 | 5 | void main() 6 | { 7 | positionEC = czm_modelView * position; 8 | gl_Position = czm_projection * positionEC; 9 | 10 | czm_vertexLogDepth(); 11 | } 12 | -------------------------------------------------------------------------------- /ShaderSource/ViewportQuadVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec4 position; 2 | attribute vec2 textureCoordinates; 3 | 4 | varying vec2 v_textureCoordinates; 5 | 6 | void main() 7 | { 8 | gl_Position = position; 9 | v_textureCoordinates = textureCoordinates; 10 | } 11 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/infinity.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_infinity 5 | * @glslConstant 6 | */ 7 | const float czm_infinity = 5906376272000.0; // Distance from the Sun to Pluto in meters. TODO: What is best given lowp, mediump, and highp? 8 | -------------------------------------------------------------------------------- /ShaderSource/VectorTileVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position; 2 | attribute float a_batchId; 3 | 4 | uniform mat4 u_modifiedModelViewProjection; 5 | 6 | void main() 7 | { 8 | gl_Position = czm_depthClamp(u_modifiedModelViewProjection * vec4(position, 1.0)); 9 | } 10 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passCesium3DTile.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#CESIUM_3D_TILE} 3 | * 4 | * @name czm_passCesium3DTile 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passCesium3DTile = 4.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/OctahedralProjectionVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec4 position; 2 | attribute vec3 cubeMapCoordinates; 3 | 4 | varying vec3 v_cubeMapCoordinates; 5 | 6 | void main() 7 | { 8 | gl_Position = position; 9 | v_cubeMapCoordinates = cubeMapCoordinates; 10 | } 11 | -------------------------------------------------------------------------------- /ShaderSource/SkyBoxFS.glsl: -------------------------------------------------------------------------------- 1 | uniform samplerCube u_cubeMap; 2 | 3 | varying vec3 v_texCoord; 4 | 5 | void main() 6 | { 7 | vec4 color = textureCube(u_cubeMap, normalize(v_texCoord)); 8 | gl_FragColor = vec4(czm_gammaCorrect(color).rgb, czm_morphTime); 9 | } 10 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passClassification.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#CLASSIFICATION} 3 | * 4 | * @name czm_passClassification 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passClassification = 7.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/DepthView.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D depthTexture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | void main(void) 6 | { 7 | float depth = czm_readDepth(depthTexture, v_textureCoordinates); 8 | gl_FragColor = vec4(vec3(depth), 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passTerrainClassification.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#TERRAIN_CLASSIFICATION} 3 | * 4 | * @name czm_passTerrainClassification 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passTerrainClassification = 3.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/SkyBoxVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position; 2 | 3 | varying vec3 v_texCoord; 4 | 5 | void main() 6 | { 7 | vec3 p = czm_viewRotation * (czm_temeToPseudoFixed * (czm_entireFrustum.y * position)); 8 | gl_Position = czm_projection * vec4(p, 1.0); 9 | v_texCoord = position.xyz; 10 | } 11 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/cascadeColor.glsl: -------------------------------------------------------------------------------- 1 | 2 | vec4 czm_cascadeColor(vec4 weights) 3 | { 4 | return vec4(1.0, 0.0, 0.0, 1.0) * weights.x + 5 | vec4(0.0, 1.0, 0.0, 1.0) * weights.y + 6 | vec4(0.0, 0.0, 1.0, 1.0) * weights.z + 7 | vec4(1.0, 0.0, 1.0, 1.0) * weights.w; 8 | } 9 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passCesium3DTileClassification.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#CESIUM_3D_TILE_CLASSIFICATION} 3 | * 4 | * @name czm_passCesium3DTileClassification 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passCesium3DTileClassification = 5.0; 10 | -------------------------------------------------------------------------------- /Docs/Builtin/Functions/planeDistance.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ``` glsl 4 | float czm_planeDistance(vec4 plane, vec3 point) { 5 | return (dot(plane.xyz, point) + plane.w); 6 | } 7 | 8 | float czm_planeDistance(vec3 planeNormal, float planeDistance, vec3 point) { 9 | return (dot(planeNormal, point) + planeDistance); 10 | } 11 | ``` 12 | 13 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/sceneMode2D.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The constant identifier for the 2D {@link SceneMode} 3 | * 4 | * @name czm_sceneMode2D 5 | * @glslConstant 6 | * @see czm_sceneMode 7 | * @see czm_sceneModeColumbusView 8 | * @see czm_sceneMode3D 9 | * @see czm_sceneModeMorphing 10 | */ 11 | const float czm_sceneMode2D = 2.0; 12 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/sceneMode3D.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The constant identifier for the 3D {@link SceneMode} 3 | * 4 | * @name czm_sceneMode3D 5 | * @glslConstant 6 | * @see czm_sceneMode 7 | * @see czm_sceneMode2D 8 | * @see czm_sceneModeColumbusView 9 | * @see czm_sceneModeMorphing 10 | */ 11 | const float czm_sceneMode3D = 3.0; 12 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/columbusViewMorph.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_columbusViewMorph 5 | * @glslFunction 6 | */ 7 | vec4 czm_columbusViewMorph(vec4 position2D, vec4 position3D, float time) 8 | { 9 | // Just linear for now. 10 | vec3 p = mix(position2D.xyz, position3D.xyz, time); 11 | return vec4(p, 1.0); 12 | } 13 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/ellipsoidWgs84TextureCoordinates.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_ellipsoidWgs84TextureCoordinates 5 | * @glslFunction 6 | */ 7 | vec2 czm_ellipsoidWgs84TextureCoordinates(vec3 normal) 8 | { 9 | return vec2(atan(normal.y, normal.x) * czm_oneOverTwoPi + 0.5, asin(normal.z) * czm_oneOverPi + 0.5); 10 | } 11 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Structs/shadowParameters.glsl: -------------------------------------------------------------------------------- 1 | struct czm_shadowParameters 2 | { 3 | #ifdef USE_CUBE_MAP_SHADOW 4 | vec3 texCoords; 5 | #else 6 | vec2 texCoords; 7 | #endif 8 | 9 | float depthBias; 10 | float depth; 11 | float nDotL; 12 | vec2 texelStepSize; 13 | float normalShadingSmooth; 14 | float darkness; 15 | }; 16 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/Brightness.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | uniform float brightness; 3 | 4 | varying vec2 v_textureCoordinates; 5 | 6 | void main(void) 7 | { 8 | vec3 rgb = texture2D(colorTexture, v_textureCoordinates).rgb; 9 | vec3 target = vec3(0.0); 10 | gl_FragColor = vec4(mix(target, rgb, brightness), 1.0); 11 | } 12 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/passCesium3DTileClassificationIgnoreShow.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The automatic GLSL constant for {@link Pass#CESIUM_3D_TILE_CLASSIFICATION_IGNORE_SHOW} 3 | * 4 | * @name czm_passCesium3DTileClassificationIgnoreShow 5 | * @glslConstant 6 | * 7 | * @see czm_pass 8 | */ 9 | const float czm_passCesium3DTileClassificationIgnoreShow = 6.0; 10 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/sceneModeMorphing.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The constant identifier for the Morphing {@link SceneMode} 3 | * 4 | * @name czm_sceneModeMorphing 5 | * @glslConstant 6 | * @see czm_sceneMode 7 | * @see czm_sceneMode2D 8 | * @see czm_sceneModeColumbusView 9 | * @see czm_sceneMode3D 10 | */ 11 | const float czm_sceneModeMorphing = 0.0; 12 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/sceneModeColumbusView.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The constant identifier for the Columbus View {@link SceneMode} 3 | * 4 | * @name czm_sceneModeColumbusView 5 | * @glslConstant 6 | * @see czm_sceneMode 7 | * @see czm_sceneMode2D 8 | * @see czm_sceneMode3D 9 | * @see czm_sceneModeMorphing 10 | */ 11 | const float czm_sceneModeColumbusView = 1.0; 12 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/solarRadius.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for one solar radius. 3 | * 4 | * @alias czm_solarRadius 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.SOLAR_RADIUS 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_solarRadius = ...; 12 | */ 13 | const float czm_solarRadius = 695500000.0; 14 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/inverseGamma.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Converts a color in linear space to RGB space. 3 | * 4 | * @name czm_inverseGamma 5 | * @glslFunction 6 | * 7 | * @param {vec3} color The color in linear space. 8 | * @returns {vec3} The color in RGB space. 9 | */ 10 | vec3 czm_inverseGamma(vec3 color) { 11 | return pow(color, vec3(1.0 / czm_gamma)); 12 | } 13 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/cascadeWeights.glsl: -------------------------------------------------------------------------------- 1 | 2 | uniform vec4 shadowMap_cascadeSplits[2]; 3 | 4 | vec4 czm_cascadeWeights(float depthEye) 5 | { 6 | // One component is set to 1.0 and all others set to 0.0. 7 | vec4 near = step(shadowMap_cascadeSplits[0], vec4(depthEye)); 8 | vec4 far = step(depthEye, shadowMap_cascadeSplits[1]); 9 | return near * far; 10 | } 11 | -------------------------------------------------------------------------------- /ShaderSource/ReprojectWebMercatorVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec4 position; 2 | attribute float webMercatorT; 3 | 4 | uniform vec2 u_textureDimensions; 5 | 6 | varying vec2 v_textureCoordinates; 7 | 8 | void main() 9 | { 10 | v_textureCoordinates = vec2(position.x, webMercatorT); 11 | gl_Position = czm_viewportOrthographic * (position * vec4(u_textureDimensions, 1.0, 1.0)); 12 | } 13 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/cascadeMatrix.glsl: -------------------------------------------------------------------------------- 1 | 2 | uniform mat4 shadowMap_cascadeMatrices[4]; 3 | 4 | mat4 czm_cascadeMatrix(vec4 weights) 5 | { 6 | return shadowMap_cascadeMatrices[0] * weights.x + 7 | shadowMap_cascadeMatrices[1] * weights.y + 8 | shadowMap_cascadeMatrices[2] * weights.z + 9 | shadowMap_cascadeMatrices[3] * weights.w; 10 | } 11 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/PerInstanceFlatColorAppearanceVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position3DHigh; 2 | attribute vec3 position3DLow; 3 | attribute vec4 color; 4 | attribute float batchId; 5 | 6 | varying vec4 v_color; 7 | 8 | void main() 9 | { 10 | vec4 p = czm_computePosition(); 11 | 12 | v_color = color; 13 | 14 | gl_Position = czm_modelViewProjectionRelativeToEye * p; 15 | } 16 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/ellipsoidContainsPoint.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_ellipsoidContainsPoint 5 | * @glslFunction 6 | * 7 | */ 8 | bool czm_ellipsoidContainsPoint(vec3 ellipsoid_inverseRadii, vec3 point) 9 | { 10 | vec3 scaled = ellipsoid_inverseRadii * (czm_inverseModelView * vec4(point, 1.0)).xyz; 11 | return (dot(scaled, scaled) <= 1.0); 12 | } 13 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/pi.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for Math.PI. 3 | * 4 | * @alias czm_pi 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.PI 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_pi = ...; 12 | * 13 | * // Example 14 | * float twoPi = 2.0 * czm_pi; 15 | */ 16 | const float czm_pi = 3.141592653589793; 17 | -------------------------------------------------------------------------------- /ShaderSource/ShadowVolumeFS.glsl: -------------------------------------------------------------------------------- 1 | #ifdef GL_EXT_frag_depth 2 | #extension GL_EXT_frag_depth : enable 3 | #endif 4 | 5 | #ifdef VECTOR_TILE 6 | uniform vec4 u_highlightColor; 7 | #endif 8 | 9 | void main(void) 10 | { 11 | #ifdef VECTOR_TILE 12 | gl_FragColor = czm_gammaCorrect(u_highlightColor); 13 | #else 14 | gl_FragColor = vec4(1.0); 15 | #endif 16 | czm_writeDepthClamp(); 17 | } 18 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/twoPi.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for 2pi. 3 | * 4 | * @alias czm_twoPi 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.TWO_PI 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_twoPi = ...; 12 | * 13 | * // Example 14 | * float pi = czm_twoPi / 2.0; 15 | */ 16 | const float czm_twoPi = 6.283185307179586; 17 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/Silhouette.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | uniform sampler2D silhouetteTexture; 3 | 4 | varying vec2 v_textureCoordinates; 5 | 6 | void main(void) 7 | { 8 | vec4 silhouetteColor = texture2D(silhouetteTexture, v_textureCoordinates); 9 | vec4 color = texture2D(colorTexture, v_textureCoordinates); 10 | gl_FragColor = mix(color, silhouetteColor, silhouetteColor.a); 11 | } 12 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/oneOverPi.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for 1/pi. 3 | * 4 | * @alias czm_oneOverPi 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.ONE_OVER_PI 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_oneOverPi = ...; 12 | * 13 | * // Example 14 | * float pi = 1.0 / czm_oneOverPi; 15 | */ 16 | const float czm_oneOverPi = 0.3183098861837907; 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/piOverSix.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for pi/6. 3 | * 4 | * @alias czm_piOverSix 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.PI_OVER_SIX 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_piOverSix = ...; 12 | * 13 | * // Example 14 | * float pi = 6.0 * czm_piOverSix; 15 | */ 16 | const float czm_piOverSix = 0.5235987755982988; 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/piOverTwo.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for pi/2. 3 | * 4 | * @alias czm_piOverTwo 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.PI_OVER_TWO 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_piOverTwo = ...; 12 | * 13 | * // Example 14 | * float pi = 2.0 * czm_piOverTwo; 15 | */ 16 | const float czm_piOverTwo = 1.5707963267948966; 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/reverseLogDepth.glsl: -------------------------------------------------------------------------------- 1 | float czm_reverseLogDepth(float logZ) 2 | { 3 | #ifdef LOG_DEPTH 4 | float near = czm_currentFrustum.x; 5 | float far = czm_currentFrustum.y; 6 | float log2Depth = logZ * czm_log2FarDepthFromNearPlusOne; 7 | float depthFromNear = pow(2.0, log2Depth) - 1.0; 8 | return far * (1.0 - near / (depthFromNear + near)) / (far - near); 9 | #endif 10 | return logZ; 11 | } 12 | -------------------------------------------------------------------------------- /ShaderSource/OctahedralProjectionFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec3 v_cubeMapCoordinates; 2 | uniform samplerCube cubeMap; 3 | 4 | void main() 5 | { 6 | vec4 rgba = textureCube(cubeMap, v_cubeMapCoordinates); 7 | #ifdef RGBA_NORMALIZED 8 | gl_FragColor = vec4(rgba.rgb, 1.0); 9 | #else 10 | float m = rgba.a * 16.0; 11 | vec3 r = rgba.rgb * m; 12 | gl_FragColor = vec4(r * r, 1.0); 13 | #endif 14 | } 15 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/piOverFour.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for pi/4. 3 | * 4 | * @alias czm_piOverFour 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.PI_OVER_FOUR 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_piOverFour = ...; 12 | * 13 | * // Example 14 | * float pi = 4.0 * czm_piOverFour; 15 | */ 16 | const float czm_piOverFour = 0.7853981633974483; 17 | -------------------------------------------------------------------------------- /Docs/Builtin/Structs/ray.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ``` glsl 4 | /** 5 | * DOC_TBA 6 | * 7 | * @name czm_ray 8 | * @glslStruct 9 | */ 10 | struct czm_ray 11 | { 12 | vec3 origin; 13 | vec3 direction; 14 | }; 15 | ``` 16 | 17 | # 文档 18 | 19 | 一个结构体。 20 | 21 | ## 1. 成员 `origin` 22 | 23 | ### 类型 24 | 25 | `vec3` 26 | 27 | ### 含义 28 | 29 | 射线起点。 30 | 31 | 32 | ## 2. 成员 `direction` 33 | ### 类型 34 | 35 | `vec3` 36 | 37 | ### 含义 38 | 39 | 射线朝向。 -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/piOverThree.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for pi/3. 3 | * 4 | * @alias czm_piOverThree 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.PI_OVER_THREE 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_piOverThree = ...; 12 | * 13 | * // Example 14 | * float pi = 3.0 * czm_piOverThree; 15 | */ 16 | const float czm_piOverThree = 1.0471975511965976; 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/oneOverTwoPi.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for 1/2pi. 3 | * 4 | * @alias czm_oneOverTwoPi 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.ONE_OVER_TWO_PI 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_oneOverTwoPi = ...; 12 | * 13 | * // Example 14 | * float pi = 2.0 * czm_oneOverTwoPi; 15 | */ 16 | const float czm_oneOverTwoPi = 0.15915494309189535; 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/threePiOver2.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for 3pi/2. 3 | * 4 | * @alias czm_threePiOver2 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.THREE_PI_OVER_TWO 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_threePiOver2 = ...; 12 | * 13 | * // Example 14 | * float pi = (2.0 / 3.0) * czm_threePiOver2; 15 | */ 16 | const float czm_threePiOver2 = 4.71238898038469; 17 | -------------------------------------------------------------------------------- /ShaderSource/Materials/AspectRampMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D image; 2 | 3 | czm_material czm_getMaterial(czm_materialInput materialInput) 4 | { 5 | czm_material material = czm_getDefaultMaterial(materialInput); 6 | vec4 rampColor = texture2D(image, vec2(materialInput.aspect / (2.0 * czm_pi), 0.5)); 7 | rampColor = czm_gammaCorrect(rampColor); 8 | material.diffuse = rampColor.rgb; 9 | material.alpha = rampColor.a; 10 | return material; 11 | } 12 | -------------------------------------------------------------------------------- /ShaderSource/Materials/SlopeRampMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D image; 2 | 3 | czm_material czm_getMaterial(czm_materialInput materialInput) 4 | { 5 | czm_material material = czm_getDefaultMaterial(materialInput); 6 | vec4 rampColor = texture2D(image, vec2(materialInput.slope / (czm_pi / 2.0), 0.5)); 7 | rampColor = czm_gammaCorrect(rampColor); 8 | material.diffuse = rampColor.rgb; 9 | material.alpha = rampColor.a; 10 | return material; 11 | } 12 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/AmbientOcclusionModulate.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | uniform sampler2D ambientOcclusionTexture; 3 | uniform bool ambientOcclusionOnly; 4 | varying vec2 v_textureCoordinates; 5 | 6 | void main(void) 7 | { 8 | vec3 color = texture2D(colorTexture, v_textureCoordinates).rgb; 9 | vec3 ao = texture2D(ambientOcclusionTexture, v_textureCoordinates).rgb; 10 | gl_FragColor.rgb = ambientOcclusionOnly ? ao : ao * color; 11 | } 12 | -------------------------------------------------------------------------------- /Docs/Builtin/Structs/depthRangeStruct.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ``` glsl 4 | /** 5 | * @name czm_depthRangeStruct 6 | * @glslStruct 7 | */ 8 | struct czm_depthRangeStruct 9 | { 10 | float near; 11 | float far; 12 | }; 13 | ``` 14 | 15 | # 文档 16 | 17 | 该结构体表示近处和远处的深度值范围(最大最小值)。 18 | 19 | ## 1. 成员 near 20 | 21 | ### 类型 22 | 23 | `float` 24 | 25 | ### 含义 26 | 27 | 近值。 28 | 29 | 30 | 31 | ## 2. 成员 far 32 | 33 | ### 类型 34 | 35 | `float` 36 | 37 | ### 含义 38 | 39 | 远值。 40 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/acesTonemapping.glsl: -------------------------------------------------------------------------------- 1 | // See: 2 | // https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/ 3 | 4 | vec3 czm_acesTonemapping(vec3 color) { 5 | float g = 0.985; 6 | float a = 0.065; 7 | float b = 0.0001; 8 | float c = 0.433; 9 | float d = 0.238; 10 | 11 | color = (color * (color + a) - b) / (color * (g * color + c) + d); 12 | 13 | color = clamp(color, 0.0, 1.0); 14 | 15 | return color; 16 | } 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/degreesPerRadian.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for converting radians to degrees. 3 | * 4 | * @alias czm_degreesPerRadian 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.DEGREES_PER_RADIAN 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_degreesPerRadian = ...; 12 | * 13 | * // Example 14 | * float deg = czm_degreesPerRadian * rad; 15 | */ 16 | const float czm_degreesPerRadian = 57.29577951308232; 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/radiansPerDegree.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL floating-point constant for converting degrees to radians. 3 | * 4 | * @alias czm_radiansPerDegree 5 | * @glslConstant 6 | * 7 | * @see CesiumMath.RADIANS_PER_DEGREE 8 | * 9 | * @example 10 | * // GLSL declaration 11 | * const float czm_radiansPerDegree = ...; 12 | * 13 | * // Example 14 | * float rad = czm_radiansPerDegree * deg; 15 | */ 16 | const float czm_radiansPerDegree = 0.017453292519943295; 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/alphaWeight.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * @private 3 | */ 4 | float czm_alphaWeight(float a) 5 | { 6 | float z = (gl_FragCoord.z - czm_viewportTransformation[3][2]) / czm_viewportTransformation[2][2]; 7 | 8 | // See Weighted Blended Order-Independent Transparency for examples of different weighting functions: 9 | // http://jcgt.org/published/0002/02/09/ 10 | return pow(a + 0.01, 4.0) + max(1e-2, min(3.0 * 1e3, 0.003 / (1e-5 + pow(abs(z) / 200.0, 4.0)))); 11 | } 12 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/depthRange.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * A built-in GLSL vec2 constant for defining the depth range. 3 | * This is a workaround to a bug where IE11 does not implement gl_DepthRange. 4 | * 5 | * @alias czm_depthRange 6 | * @glslConstant 7 | * 8 | * @example 9 | * // GLSL declaration 10 | * float depthRangeNear = czm_depthRange.near; 11 | * float depthRangeFar = czm_depthRange.far; 12 | * 13 | */ 14 | const czm_depthRangeStruct czm_depthRange = czm_depthRangeStruct(0.0, 1.0); 15 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/backFacing.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Determines if the fragment is back facing 3 | * 4 | * @name czm_backFacing 5 | * @glslFunction 6 | * 7 | * @returns {bool} true if the fragment is back facing; otherwise, false. 8 | */ 9 | bool czm_backFacing() 10 | { 11 | // !gl_FrontFacing doesn't work as expected on Mac/Intel so use the more verbose form instead. See https://github.com/CesiumGS/cesium/pull/8494. 12 | return gl_FrontFacing == false; 13 | } 14 | -------------------------------------------------------------------------------- /ShaderSource/ViewportQuadFS.glsl: -------------------------------------------------------------------------------- 1 | 2 | varying vec2 v_textureCoordinates; 3 | 4 | void main() 5 | { 6 | czm_materialInput materialInput; 7 | 8 | materialInput.s = v_textureCoordinates.s; 9 | materialInput.st = v_textureCoordinates; 10 | materialInput.str = vec3(v_textureCoordinates, 0.0); 11 | materialInput.normalEC = vec3(0.0, 0.0, -1.0); 12 | 13 | czm_material material = czm_getMaterial(materialInput); 14 | 15 | gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); 16 | } 17 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/NightVision.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | float rand(vec2 co) 6 | { 7 | return fract(sin(dot(co.xy ,vec2(12.9898, 78.233))) * 43758.5453); 8 | } 9 | 10 | void main(void) 11 | { 12 | float noiseValue = rand(v_textureCoordinates + sin(czm_frameNumber)) * 0.1; 13 | vec3 rgb = texture2D(colorTexture, v_textureCoordinates).rgb; 14 | vec3 green = vec3(0.0, 1.0, 0.0); 15 | gl_FragColor = vec4((noiseValue + rgb) * green, 1.0); 16 | } 17 | -------------------------------------------------------------------------------- /ShaderSource/SkyAtmosphereVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec4 position; 2 | 3 | varying vec3 v_outerPositionWC; 4 | 5 | #ifndef PER_FRAGMENT_ATMOSPHERE 6 | varying vec3 v_mieColor; 7 | varying vec3 v_rayleighColor; 8 | #endif 9 | 10 | void main(void) 11 | { 12 | vec4 positionWC = czm_model * position; 13 | 14 | #ifndef PER_FRAGMENT_ATMOSPHERE 15 | calculateMieColorAndRayleighColor(positionWC.xyz, v_mieColor, v_rayleighColor); 16 | #endif 17 | v_outerPositionWC = positionWC.xyz; 18 | gl_Position = czm_modelViewProjection * position; 19 | } 20 | -------------------------------------------------------------------------------- /ShaderSource/CompareAndPackTranslucentDepth.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D u_opaqueDepthTexture; 2 | uniform sampler2D u_translucentDepthTexture; 3 | 4 | varying vec2 v_textureCoordinates; 5 | 6 | void main() 7 | { 8 | float opaqueDepth = texture2D(u_opaqueDepthTexture, v_textureCoordinates).r; 9 | float translucentDepth = texture2D(u_translucentDepthTexture, v_textureCoordinates).r; 10 | translucentDepth = czm_branchFreeTernary(translucentDepth > opaqueDepth, 1.0, translucentDepth); 11 | gl_FragColor = czm_packDepth(translucentDepth); 12 | } 13 | -------------------------------------------------------------------------------- /ShaderSource/AdjustTranslucentFS.glsl: -------------------------------------------------------------------------------- 1 | #ifdef MRT 2 | #extension GL_EXT_draw_buffers : enable 3 | #endif 4 | 5 | uniform vec4 u_bgColor; 6 | uniform sampler2D u_depthTexture; 7 | 8 | varying vec2 v_textureCoordinates; 9 | 10 | void main() 11 | { 12 | if (texture2D(u_depthTexture, v_textureCoordinates).r < 1.0) 13 | { 14 | #ifdef MRT 15 | gl_FragData[0] = u_bgColor; 16 | gl_FragData[1] = vec4(u_bgColor.a); 17 | #else 18 | gl_FragColor = u_bgColor; 19 | #endif 20 | return; 21 | } 22 | 23 | discard; 24 | } 25 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/eyeOffset.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_eyeOffset 5 | * @glslFunction 6 | * 7 | * @param {vec4} positionEC DOC_TBA. 8 | * @param {vec3} eyeOffset DOC_TBA. 9 | * 10 | * @returns {vec4} DOC_TBA. 11 | */ 12 | vec4 czm_eyeOffset(vec4 positionEC, vec3 eyeOffset) 13 | { 14 | // This equation is approximate in x and y. 15 | vec4 p = positionEC; 16 | vec4 zEyeOffset = normalize(p) * eyeOffset.z; 17 | p.xy += eyeOffset.xy + zEyeOffset.xy; 18 | p.z += zEyeOffset.z; 19 | return p; 20 | } 21 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/AdditiveBlend.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | uniform sampler2D colorTexture2; 3 | 4 | uniform vec2 center; 5 | uniform float radius; 6 | 7 | varying vec2 v_textureCoordinates; 8 | 9 | void main() 10 | { 11 | vec4 color0 = texture2D(colorTexture, v_textureCoordinates); 12 | vec4 color1 = texture2D(colorTexture2, v_textureCoordinates); 13 | 14 | float x = length(gl_FragCoord.xy - center) / radius; 15 | float t = smoothstep(0.5, 0.8, x); 16 | gl_FragColor = mix(color0 + color1, color1, t); 17 | } 18 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/luminance.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Computes the luminance of a color. 3 | * 4 | * @name czm_luminance 5 | * @glslFunction 6 | * 7 | * @param {vec3} rgb The color. 8 | * 9 | * @returns {float} The luminance. 10 | * 11 | * @example 12 | * float light = czm_luminance(vec3(0.0)); // 0.0 13 | * float dark = czm_luminance(vec3(1.0)); // ~1.0 14 | */ 15 | float czm_luminance(vec3 rgb) 16 | { 17 | // Algorithm from Chapter 10 of Graphics Shaders. 18 | const vec3 W = vec3(0.2125, 0.7154, 0.0721); 19 | return dot(rgb, W); 20 | } 21 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/BloomComposite.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | uniform sampler2D bloomTexture; 3 | uniform bool glowOnly; 4 | 5 | varying vec2 v_textureCoordinates; 6 | 7 | void main(void) 8 | { 9 | vec4 color = texture2D(colorTexture, v_textureCoordinates); 10 | 11 | #ifdef CZM_SELECTED_FEATURE 12 | if (czm_selected()) { 13 | gl_FragColor = color; 14 | return; 15 | } 16 | #endif 17 | 18 | vec4 bloom = texture2D(bloomTexture, v_textureCoordinates); 19 | gl_FragColor = glowOnly ? bloom : bloom + color; 20 | } 21 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/BasicMaterialAppearanceVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position3DHigh; 2 | attribute vec3 position3DLow; 3 | attribute vec3 normal; 4 | attribute float batchId; 5 | 6 | varying vec3 v_positionEC; 7 | varying vec3 v_normalEC; 8 | 9 | void main() 10 | { 11 | vec4 p = czm_computePosition(); 12 | 13 | v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates 14 | v_normalEC = czm_normal * normal; // normal in eye coordinates 15 | 16 | gl_Position = czm_modelViewProjectionRelativeToEye * p; 17 | } 18 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/gammaCorrect.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Converts a color from RGB space to linear space. 3 | * 4 | * @name czm_gammaCorrect 5 | * @glslFunction 6 | * 7 | * @param {vec3} color The color in RGB space. 8 | * @returns {vec3} The color in linear space. 9 | */ 10 | vec3 czm_gammaCorrect(vec3 color) { 11 | #ifdef HDR 12 | color = pow(color, vec3(czm_gamma)); 13 | #endif 14 | return color; 15 | } 16 | 17 | vec4 czm_gammaCorrect(vec4 color) { 18 | #ifdef HDR 19 | color.rgb = pow(color.rgb, vec3(czm_gamma)); 20 | #endif 21 | return color; 22 | } 23 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/geodeticSurfaceNormal.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_geodeticSurfaceNormal 5 | * @glslFunction 6 | * 7 | * @param {vec3} positionOnEllipsoid DOC_TBA 8 | * @param {vec3} ellipsoidCenter DOC_TBA 9 | * @param {vec3} oneOverEllipsoidRadiiSquared DOC_TBA 10 | * 11 | * @returns {vec3} DOC_TBA. 12 | */ 13 | vec3 czm_geodeticSurfaceNormal(vec3 positionOnEllipsoid, vec3 ellipsoidCenter, vec3 oneOverEllipsoidRadiiSquared) 14 | { 15 | return normalize((positionOnEllipsoid - ellipsoidCenter) * oneOverEllipsoidRadiiSquared); 16 | } 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Structs/raySegment.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_raySegment 5 | * @glslStruct 6 | */ 7 | struct czm_raySegment 8 | { 9 | float start; 10 | float stop; 11 | }; 12 | 13 | /** 14 | * DOC_TBA 15 | * 16 | * @name czm_emptyRaySegment 17 | * @glslConstant 18 | */ 19 | const czm_raySegment czm_emptyRaySegment = czm_raySegment(-czm_infinity, -czm_infinity); 20 | 21 | /** 22 | * DOC_TBA 23 | * 24 | * @name czm_fullRaySegment 25 | * @glslConstant 26 | */ 27 | const czm_raySegment czm_fullRaySegment = czm_raySegment(0.0, czm_infinity); 28 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/transformPlane.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Transforms a plane. 3 | * 4 | * @name czm_transformPlane 5 | * @glslFunction 6 | * 7 | * @param {vec4} plane The plane in Hessian Normal Form. 8 | * @param {mat4} transform The inverse-transpose of a transformation matrix. 9 | */ 10 | vec4 czm_transformPlane(vec4 plane, mat4 transform) { 11 | vec4 transformedPlane = transform * plane; 12 | // Convert the transformed plane to Hessian Normal Form 13 | float normalMagnitude = length(transformedPlane.xyz); 14 | return transformedPlane / normalMagnitude; 15 | } 16 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Structs/pbrParameters.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Parameters for {@link czm_pbrLighting} 3 | * 4 | * @name czm_material 5 | * @glslStruct 6 | * 7 | * @property {vec3} diffuseColor the diffuse color of the material for the lambert term of the rendering equation 8 | * @property {float} roughness a value from 0.0 to 1.0 that indicates how rough the surface of the material is. 9 | * @property {vec3} f0 The reflectance of the material at normal incidence 10 | */ 11 | struct czm_pbrParameters 12 | { 13 | vec3 diffuseColor; 14 | float roughness; 15 | vec3 f0; 16 | }; 17 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/DepthViewPacked.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D u_depthTexture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | void main() 6 | { 7 | float z_window = czm_unpackDepth(texture2D(u_depthTexture, v_textureCoordinates)); 8 | z_window = czm_reverseLogDepth(z_window); 9 | float n_range = czm_depthRange.near; 10 | float f_range = czm_depthRange.far; 11 | float z_ndc = (2.0 * z_window - n_range - f_range) / (f_range - n_range); 12 | float scale = pow(z_ndc * 0.5 + 0.5, 8.0); 13 | gl_FragColor = vec4(mix(vec3(0.0), vec3(1.0), scale), 1.0); 14 | } 15 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/AcesTonemappingStage.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | #ifdef AUTO_EXPOSURE 6 | uniform sampler2D autoExposure; 7 | #endif 8 | 9 | void main() 10 | { 11 | vec4 fragmentColor = texture2D(colorTexture, v_textureCoordinates); 12 | vec3 color = fragmentColor.rgb; 13 | 14 | #ifdef AUTO_EXPOSURE 15 | color /= texture2D(autoExposure, vec2(0.5)).r; 16 | #endif 17 | color = czm_acesTonemapping(color); 18 | color = czm_inverseGamma(color); 19 | 20 | gl_FragColor = vec4(color, fragmentColor.a); 21 | } 22 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/unpackDepth.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Unpacks a vec4 depth value to a float in [0, 1) range. 3 | * 4 | * @name czm_unpackDepth 5 | * @glslFunction 6 | * 7 | * @param {vec4} packedDepth The packed depth. 8 | * 9 | * @returns {float} The floating-point depth in [0, 1) range. 10 | */ 11 | float czm_unpackDepth(vec4 packedDepth) 12 | { 13 | // See Aras Pranckevičius' post Encoding Floats to RGBA 14 | // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/ 15 | return dot(packedDepth, vec4(1.0, 1.0 / 255.0, 1.0 / 65025.0, 1.0 / 16581375.0)); 16 | } 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/defaultPbrMaterial.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Get default parameters for physically based rendering. These defaults 3 | * describe a rough dielectric (non-metal) surface (e.g. rough plastic). 4 | * 5 | * @return {czm_pbrParameters} Default parameters for {@link czm_pbrLighting} 6 | */ 7 | czm_pbrParameters czm_defaultPbrMaterial() 8 | { 9 | czm_pbrParameters results; 10 | results.diffuseColor = vec3(1.0); 11 | results.roughness = 1.0; 12 | 13 | const vec3 REFLECTANCE_DIELECTRIC = vec3(0.04); 14 | results.f0 = REFLECTANCE_DIELECTRIC; 15 | return results; 16 | } 17 | -------------------------------------------------------------------------------- /Docs/Builtin/Structs/raySegment.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ```glsl 4 | /** 5 | * DOC_TBA 6 | * 7 | * @name czm_raySegment 8 | * @glslStruct 9 | */ 10 | struct czm_raySegment 11 | { 12 | float start; 13 | float stop; 14 | }; 15 | 16 | /** 17 | * DOC_TBA 18 | * 19 | * @name czm_emptyRaySegment 20 | * @glslConstant 21 | */ 22 | const czm_raySegment czm_emptyRaySegment = czm_raySegment(-czm_infinity, -czm_infinity); 23 | 24 | /** 25 | * DOC_TBA 26 | * 27 | * @name czm_fullRaySegment 28 | * @glslConstant 29 | */ 30 | const czm_raySegment czm_fullRaySegment = czm_raySegment(0.0, czm_infinity); 31 | ``` 32 | 33 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/multiplyWithColorBalance.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_multiplyWithColorBalance 5 | * @glslFunction 6 | */ 7 | vec3 czm_multiplyWithColorBalance(vec3 left, vec3 right) 8 | { 9 | // Algorithm from Chapter 10 of Graphics Shaders. 10 | const vec3 W = vec3(0.2125, 0.7154, 0.0721); 11 | 12 | vec3 target = left * right; 13 | float leftLuminance = dot(left, W); 14 | float rightLuminance = dot(right, W); 15 | float targetLuminance = dot(target, W); 16 | 17 | return ((leftLuminance + rightLuminance) / (2.0 * targetLuminance)) * target; 18 | } 19 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/BlackAndWhite.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | uniform float gradations; 3 | 4 | varying vec2 v_textureCoordinates; 5 | 6 | void main(void) 7 | { 8 | vec3 rgb = texture2D(colorTexture, v_textureCoordinates).rgb; 9 | #ifdef CZM_SELECTED_FEATURE 10 | if (czm_selected()) { 11 | gl_FragColor = vec4(rgb, 1.0); 12 | return; 13 | } 14 | #endif 15 | float luminance = czm_luminance(rgb); 16 | float darkness = luminance * gradations; 17 | darkness = (darkness - fract(darkness)) / gradations; 18 | gl_FragColor = vec4(vec3(darkness), 1.0); 19 | } 20 | -------------------------------------------------------------------------------- /Docs/Builtin/Structs/pbrParameters.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ```glsl 4 | /** 5 | * Parameters for {@link czm_pbrLighting} 6 | * 7 | * @name czm_material 8 | * @glslStruct 9 | * 10 | * @property {vec3} diffuseColor the diffuse color of the material for the lambert term of the rendering equation 11 | * @property {float} roughness a value from 0.0 to 1.0 that indicates how rough the surface of the material is. 12 | * @property {vec3} f0 The reflectance of the material at normal incidence 13 | */ 14 | struct czm_pbrParameters 15 | { 16 | vec3 diffuseColor; 17 | float roughness; 18 | vec3 f0; 19 | }; 20 | ``` 21 | 22 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/EllipsoidSurfaceAppearanceVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position3DHigh; 2 | attribute vec3 position3DLow; 3 | attribute vec2 st; 4 | attribute float batchId; 5 | 6 | varying vec3 v_positionMC; 7 | varying vec3 v_positionEC; 8 | varying vec2 v_st; 9 | 10 | void main() 11 | { 12 | vec4 p = czm_computePosition(); 13 | 14 | v_positionMC = position3DHigh + position3DLow; // position in model coordinates 15 | v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates 16 | v_st = st; 17 | 18 | gl_Position = czm_modelViewProjectionRelativeToEye * p; 19 | } 20 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/ContrastBias.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | uniform float contrast; 3 | uniform float brightness; 4 | 5 | varying vec2 v_textureCoordinates; 6 | 7 | void main(void) 8 | { 9 | vec3 sceneColor = texture2D(colorTexture, v_textureCoordinates).xyz; 10 | sceneColor = czm_RGBToHSB(sceneColor); 11 | sceneColor.z += brightness; 12 | sceneColor = czm_HSBToRGB(sceneColor); 13 | 14 | float factor = (259.0 * (contrast + 255.0)) / (255.0 * (259.0 - contrast)); 15 | sceneColor = factor * (sceneColor - vec3(0.5)) + vec3(0.5); 16 | gl_FragColor = vec4(sceneColor, 1.0); 17 | } 18 | -------------------------------------------------------------------------------- /ShaderSource/Materials/ElevationRampMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D image; 2 | uniform float minimumHeight; 3 | uniform float maximumHeight; 4 | 5 | czm_material czm_getMaterial(czm_materialInput materialInput) 6 | { 7 | czm_material material = czm_getDefaultMaterial(materialInput); 8 | float scaledHeight = clamp((materialInput.height - minimumHeight) / (maximumHeight - minimumHeight), 0.0, 1.0); 9 | vec4 rampColor = texture2D(image, vec2(scaledHeight, 0.5)); 10 | rampColor = czm_gammaCorrect(rampColor); 11 | material.diffuse = rampColor.rgb; 12 | material.alpha = rampColor.a; 13 | return material; 14 | } 15 | -------------------------------------------------------------------------------- /Docs/Builtin/Functions/readDepth.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ``` glsl 4 | float czm_readDepth(sampler2D depthTexture, vec2 texCoords) 5 | { 6 | return czm_reverseLogDepth(texture2D(depthTexture, texCoords).r); 7 | } 8 | ``` 9 | 10 | # 文档 11 | 12 | 一个函数,从深度纹理中根据纹理坐标,取深度值。 13 | 14 | ## 1. 参数 15 | 16 | ### ① depthTexture 17 | 18 | 参数类型:`sampler2D` 19 | 20 | 深度纹理。 21 | 22 | ### ② texCoords 23 | 24 | 参数类型:`vec2` 25 | 26 | 纹理坐标。 27 | 28 | ## 2. 返回值 29 | 30 | 返回值类型:`float` 31 | 32 | 33 | 34 | # 注意事项 35 | 36 | `depthTexture` 可以是对数深度纹理,也可以是非对数深度纹理。 37 | 38 | 39 | 40 | # 参考 41 | 42 | 函数:[czm_reverseLogDepth](./reverseLogDepth.md) 43 | 44 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/TexturedMaterialAppearanceVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position3DHigh; 2 | attribute vec3 position3DLow; 3 | attribute vec3 normal; 4 | attribute vec2 st; 5 | attribute float batchId; 6 | 7 | varying vec3 v_positionEC; 8 | varying vec3 v_normalEC; 9 | varying vec2 v_st; 10 | 11 | void main() 12 | { 13 | vec4 p = czm_computePosition(); 14 | 15 | v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates 16 | v_normalEC = czm_normal * normal; // normal in eye coordinates 17 | v_st = st; 18 | 19 | gl_Position = czm_modelViewProjectionRelativeToEye * p; 20 | } 21 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/decompressTextureCoordinates.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Decompresses texture coordinates that were packed into a single float. 3 | * 4 | * @name czm_decompressTextureCoordinates 5 | * @glslFunction 6 | * 7 | * @param {float} encoded The compressed texture coordinates. 8 | * @returns {vec2} The decompressed texture coordinates. 9 | */ 10 | vec2 czm_decompressTextureCoordinates(float encoded) 11 | { 12 | float temp = encoded / 4096.0; 13 | float xZeroTo4095 = floor(temp); 14 | float stx = xZeroTo4095 / 4095.0; 15 | float sty = (encoded - xZeroTo4095 * 4096.0) / 4095.0; 16 | return vec2(stx, sty); 17 | } 18 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/lineDistance.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Computes distance from an point in 2D to a line in 2D. 3 | * 4 | * @name czm_lineDistance 5 | * @glslFunction 6 | * 7 | * param {vec2} point1 A point along the line. 8 | * param {vec2} point2 A point along the line. 9 | * param {vec2} point A point that may or may not be on the line. 10 | * returns {float} The distance from the point to the line. 11 | */ 12 | float czm_lineDistance(vec2 point1, vec2 point2, vec2 point) { 13 | return abs((point2.y - point1.y) * point.x - (point2.x - point1.x) * point.y + point2.x * point1.y - point2.y * point1.x) / distance(point2, point1); 14 | } 15 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/PerInstanceColorAppearanceVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position3DHigh; 2 | attribute vec3 position3DLow; 3 | attribute vec3 normal; 4 | attribute vec4 color; 5 | attribute float batchId; 6 | 7 | varying vec3 v_positionEC; 8 | varying vec3 v_normalEC; 9 | varying vec4 v_color; 10 | 11 | void main() 12 | { 13 | vec4 p = czm_computePosition(); 14 | 15 | v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates 16 | v_normalEC = czm_normal * normal; // normal in eye coordinates 17 | v_color = color; 18 | 19 | gl_Position = czm_modelViewProjectionRelativeToEye * p; 20 | } 21 | -------------------------------------------------------------------------------- /ShaderSource/Materials/DotMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform vec4 lightColor; 2 | uniform vec4 darkColor; 3 | uniform vec2 repeat; 4 | 5 | czm_material czm_getMaterial(czm_materialInput materialInput) 6 | { 7 | czm_material material = czm_getDefaultMaterial(materialInput); 8 | 9 | // From Stefan Gustavson's Procedural Textures in GLSL in OpenGL Insights 10 | float b = smoothstep(0.3, 0.32, length(fract(repeat * materialInput.st) - 0.5)); // 0.0 or 1.0 11 | 12 | vec4 color = mix(lightColor, darkColor, b); 13 | color = czm_gammaCorrect(color); 14 | material.diffuse = color.rgb; 15 | material.alpha = color.a; 16 | 17 | return material; 18 | } 19 | -------------------------------------------------------------------------------- /ShaderSource/PolylineFS.glsl: -------------------------------------------------------------------------------- 1 | #ifdef VECTOR_TILE 2 | uniform vec4 u_highlightColor; 3 | #endif 4 | 5 | varying vec2 v_st; 6 | 7 | void main() 8 | { 9 | czm_materialInput materialInput; 10 | 11 | vec2 st = v_st; 12 | st.t = czm_readNonPerspective(st.t, gl_FragCoord.w); 13 | 14 | materialInput.s = st.s; 15 | materialInput.st = st; 16 | materialInput.str = vec3(st, 0.0); 17 | 18 | czm_material material = czm_getMaterial(materialInput); 19 | gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); 20 | #ifdef VECTOR_TILE 21 | gl_FragColor *= u_highlightColor; 22 | #endif 23 | 24 | czm_writeLogDepth(); 25 | } 26 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/isEmpty.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Determines if a time interval is empty. 3 | * 4 | * @name czm_isEmpty 5 | * @glslFunction 6 | * 7 | * @param {czm_raySegment} interval The interval to test. 8 | * 9 | * @returns {bool} true if the time interval is empty; otherwise, false. 10 | * 11 | * @example 12 | * bool b0 = czm_isEmpty(czm_emptyRaySegment); // true 13 | * bool b1 = czm_isEmpty(czm_raySegment(0.0, 1.0)); // false 14 | * bool b2 = czm_isEmpty(czm_raySegment(1.0, 1.0)); // false, contains 1.0. 15 | */ 16 | bool czm_isEmpty(czm_raySegment interval) 17 | { 18 | return (interval.stop < 0.0); 19 | } 20 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/packDepth.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Packs a depth value into a vec3 that can be represented by unsigned bytes. 3 | * 4 | * @name czm_packDepth 5 | * @glslFunction 6 | * 7 | * @param {float} depth The floating-point depth. 8 | * @returns {vec3} The packed depth. 9 | */ 10 | vec4 czm_packDepth(float depth) 11 | { 12 | // See Aras Pranckevičius' post Encoding Floats to RGBA 13 | // http://aras-p.info/blog/2009/07/30/encoding-floats-to-rgba-the-final/ 14 | vec4 enc = vec4(1.0, 255.0, 65025.0, 16581375.0) * depth; 15 | enc = fract(enc); 16 | enc -= enc.yzww * vec4(1.0 / 255.0, 1.0 / 255.0, 1.0 / 255.0, 0.0); 17 | return enc; 18 | } 19 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/isFull.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Determines if a time interval is empty. 3 | * 4 | * @name czm_isFull 5 | * @glslFunction 6 | * 7 | * @param {czm_raySegment} interval The interval to test. 8 | * 9 | * @returns {bool} true if the time interval is empty; otherwise, false. 10 | * 11 | * @example 12 | * bool b0 = czm_isEmpty(czm_emptyRaySegment); // true 13 | * bool b1 = czm_isEmpty(czm_raySegment(0.0, 1.0)); // false 14 | * bool b2 = czm_isEmpty(czm_raySegment(1.0, 1.0)); // false, contains 1.0. 15 | */ 16 | bool czm_isFull(czm_raySegment interval) 17 | { 18 | return (interval.start == 0.0 && interval.stop == czm_infinity); 19 | } 20 | -------------------------------------------------------------------------------- /Docs/Builtin/Functions/reverseLogDepth.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ``` glsl 4 | float czm_reverseLogDepth(float logZ) 5 | { 6 | #ifdef LOG_DEPTH 7 | float near = czm_currentFrustum.x; 8 | float far = czm_currentFrustum.y; 9 | float log2Depth = logZ * czm_log2FarDepthFromNearPlusOne; 10 | float depthFromNear = pow(2.0, log2Depth) - 1.0; 11 | return far * (1.0 - near / (depthFromNear + near)) / (far - near); 12 | #endif 13 | return logZ; 14 | } 15 | ``` 16 | 17 | # 文档 18 | 19 | 一个函数,将对数深度值解算到普通的深度值。 20 | 21 | ## 1. 参数 22 | 23 | ### ① logZ 24 | 25 | 参数类型:`float` 26 | 27 | 对数深度值。 28 | 29 | ## 2. 返回值 30 | 31 | 返回值类型:`float` 32 | 33 | 普通深度值。 34 | 35 | 36 | 37 | # 原理 38 | 39 | TODO. 40 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/ReinhardTonemapping.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | #ifdef AUTO_EXPOSURE 6 | uniform sampler2D autoExposure; 7 | #endif 8 | 9 | // See equation 3: 10 | // http://www.cs.utah.edu/~reinhard/cdrom/tonemap.pdf 11 | 12 | void main() 13 | { 14 | vec4 fragmentColor = texture2D(colorTexture, v_textureCoordinates); 15 | vec3 color = fragmentColor.rgb; 16 | #ifdef AUTO_EXPOSURE 17 | float exposure = texture2D(autoExposure, vec2(0.5)).r; 18 | color /= exposure; 19 | #endif 20 | color = color / (1.0 + color); 21 | color = czm_inverseGamma(color); 22 | gl_FragColor = vec4(color, fragmentColor.a); 23 | } 24 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/pointAlongRay.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Computes the point along a ray at the given time. time can be positive, negative, or zero. 3 | * 4 | * @name czm_pointAlongRay 5 | * @glslFunction 6 | * 7 | * @param {czm_ray} ray The ray to compute the point along. 8 | * @param {float} time The time along the ray. 9 | * 10 | * @returns {vec3} The point along the ray at the given time. 11 | * 12 | * @example 13 | * czm_ray ray = czm_ray(vec3(0.0), vec3(1.0, 0.0, 0.0)); // origin, direction 14 | * vec3 v = czm_pointAlongRay(ray, 2.0); // (2.0, 0.0, 0.0) 15 | */ 16 | vec3 czm_pointAlongRay(czm_ray ray, float time) 17 | { 18 | return ray.origin + (time * ray.direction); 19 | } 20 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/FXAA.glsl: -------------------------------------------------------------------------------- 1 | varying vec2 v_textureCoordinates; 2 | 3 | uniform sampler2D colorTexture; 4 | 5 | const float fxaaQualitySubpix = 0.5; 6 | const float fxaaQualityEdgeThreshold = 0.125; 7 | const float fxaaQualityEdgeThresholdMin = 0.0833; 8 | 9 | void main() 10 | { 11 | vec2 fxaaQualityRcpFrame = vec2(1.0) / czm_viewport.zw; 12 | vec4 color = FxaaPixelShader( 13 | v_textureCoordinates, 14 | colorTexture, 15 | fxaaQualityRcpFrame, 16 | fxaaQualitySubpix, 17 | fxaaQualityEdgeThreshold, 18 | fxaaQualityEdgeThresholdMin); 19 | float alpha = texture2D(colorTexture, v_textureCoordinates).a; 20 | gl_FragColor = vec4(color.rgb, alpha); 21 | } 22 | -------------------------------------------------------------------------------- /ShaderSource/SkyAtmosphereFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec3 v_outerPositionWC; 2 | 3 | #ifndef PER_FRAGMENT_ATMOSPHERE 4 | varying vec3 v_mieColor; 5 | varying vec3 v_rayleighColor; 6 | #endif 7 | 8 | void main (void) 9 | { 10 | vec3 toCamera = czm_viewerPositionWC - v_outerPositionWC; 11 | vec3 lightDirection = getLightDirection(czm_viewerPositionWC); 12 | vec3 mieColor; 13 | vec3 rayleighColor; 14 | 15 | #ifdef PER_FRAGMENT_ATMOSPHERE 16 | calculateMieColorAndRayleighColor(v_outerPositionWC, mieColor, rayleighColor); 17 | #else 18 | mieColor = v_mieColor; 19 | rayleighColor = v_rayleighColor; 20 | #endif 21 | 22 | gl_FragColor = calculateFinalColor(czm_viewerPositionWC, toCamera, lightDirection, mieColor, rayleighColor); 23 | } 24 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/shadowDepthCompare.glsl: -------------------------------------------------------------------------------- 1 | 2 | float czm_sampleShadowMap(highp samplerCube shadowMap, vec3 d) 3 | { 4 | return czm_unpackDepth(textureCube(shadowMap, d)); 5 | } 6 | 7 | float czm_sampleShadowMap(highp sampler2D shadowMap, vec2 uv) 8 | { 9 | #ifdef USE_SHADOW_DEPTH_TEXTURE 10 | return texture2D(shadowMap, uv).r; 11 | #else 12 | return czm_unpackDepth(texture2D(shadowMap, uv)); 13 | #endif 14 | } 15 | 16 | float czm_shadowDepthCompare(samplerCube shadowMap, vec3 uv, float depth) 17 | { 18 | return step(depth, czm_sampleShadowMap(shadowMap, uv)); 19 | } 20 | 21 | float czm_shadowDepthCompare(sampler2D shadowMap, vec2 uv, float depth) 22 | { 23 | return step(depth, czm_sampleShadowMap(shadowMap, uv)); 24 | } 25 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/saturation.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Adjusts the saturation of a color. 3 | * 4 | * @name czm_saturation 5 | * @glslFunction 6 | * 7 | * @param {vec3} rgb The color. 8 | * @param {float} adjustment The amount to adjust the saturation of the color. 9 | * 10 | * @returns {float} The color with the saturation adjusted. 11 | * 12 | * @example 13 | * vec3 greyScale = czm_saturation(color, 0.0); 14 | * vec3 doubleSaturation = czm_saturation(color, 2.0); 15 | */ 16 | vec3 czm_saturation(vec3 rgb, float adjustment) 17 | { 18 | // Algorithm from Chapter 16 of OpenGL Shading Language 19 | const vec3 W = vec3(0.2125, 0.7154, 0.0721); 20 | vec3 intensity = vec3(dot(rgb, W)); 21 | return mix(intensity, rgb, adjustment); 22 | } 23 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/ModifiedReinhardTonemapping.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | uniform vec3 white; 3 | 4 | varying vec2 v_textureCoordinates; 5 | 6 | #ifdef AUTO_EXPOSURE 7 | uniform sampler2D autoExposure; 8 | #endif 9 | 10 | // See equation 4: 11 | // http://www.cs.utah.edu/~reinhard/cdrom/tonemap.pdf 12 | 13 | void main() 14 | { 15 | vec4 fragmentColor = texture2D(colorTexture, v_textureCoordinates); 16 | vec3 color = fragmentColor.rgb; 17 | #ifdef AUTO_EXPOSURE 18 | float exposure = texture2D(autoExposure, vec2(0.5)).r; 19 | color /= exposure; 20 | #endif 21 | color = (color * (1.0 + color / white)) / (1.0 + color); 22 | color = czm_inverseGamma(color); 23 | gl_FragColor = vec4(color, fragmentColor.a); 24 | } 25 | -------------------------------------------------------------------------------- /ShaderSource/Materials/NormalMapMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D image; 2 | uniform float strength; 3 | uniform vec2 repeat; 4 | 5 | czm_material czm_getMaterial(czm_materialInput materialInput) 6 | { 7 | czm_material material = czm_getDefaultMaterial(materialInput); 8 | 9 | vec4 textureValue = texture2D(image, fract(repeat * materialInput.st)); 10 | vec3 normalTangentSpace = textureValue.channels; 11 | normalTangentSpace.xy = normalTangentSpace.xy * 2.0 - 1.0; 12 | normalTangentSpace.z = clamp(1.0 - strength, 0.1, 1.0); 13 | normalTangentSpace = normalize(normalTangentSpace); 14 | vec3 normalEC = materialInput.tangentToEyeMatrix * normalTangentSpace; 15 | 16 | material.normal = normalEC; 17 | 18 | return material; 19 | } 20 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/BasicMaterialAppearanceFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec3 v_positionEC; 2 | varying vec3 v_normalEC; 3 | 4 | void main() 5 | { 6 | vec3 positionToEyeEC = -v_positionEC; 7 | 8 | vec3 normalEC = normalize(v_normalEC); 9 | #ifdef FACE_FORWARD 10 | normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC); 11 | #endif 12 | 13 | czm_materialInput materialInput; 14 | materialInput.normalEC = normalEC; 15 | materialInput.positionToEyeEC = positionToEyeEC; 16 | czm_material material = czm_getMaterial(materialInput); 17 | 18 | #ifdef FLAT 19 | gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); 20 | #else 21 | gl_FragColor = czm_phong(normalize(positionToEyeEC), material, czm_lightDirectionEC); 22 | #endif 23 | } 24 | -------------------------------------------------------------------------------- /ShaderSource/SunVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec2 direction; 2 | 3 | uniform float u_size; 4 | 5 | varying vec2 v_textureCoordinates; 6 | 7 | void main() 8 | { 9 | vec4 position; 10 | if (czm_morphTime == 1.0) 11 | { 12 | position = vec4(czm_sunPositionWC, 1.0); 13 | } 14 | else 15 | { 16 | position = vec4(czm_sunPositionColumbusView.zxy, 1.0); 17 | } 18 | 19 | vec4 positionEC = czm_view * position; 20 | vec4 positionWC = czm_eyeToWindowCoordinates(positionEC); 21 | 22 | vec2 halfSize = vec2(u_size * 0.5); 23 | halfSize *= ((direction * 2.0) - 1.0); 24 | 25 | gl_Position = czm_viewportOrthographic * vec4(positionWC.xy + halfSize, -positionWC.z, 1.0); 26 | 27 | v_textureCoordinates = direction; 28 | } 29 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/HSBToRGB.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Converts an HSB color (hue, saturation, brightness) to RGB 3 | * HSB <-> RGB conversion with minimal branching: {@link http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl} 4 | * 5 | * @name czm_HSBToRGB 6 | * @glslFunction 7 | * 8 | * @param {vec3} hsb The color in HSB. 9 | * 10 | * @returns {vec3} The color in RGB. 11 | * 12 | * @example 13 | * vec3 hsb = czm_RGBToHSB(rgb); 14 | * hsb.z *= 0.1; 15 | * rgb = czm_HSBToRGB(hsb); 16 | */ 17 | 18 | const vec4 K_HSB2RGB = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); 19 | 20 | vec3 czm_HSBToRGB(vec3 hsb) 21 | { 22 | vec3 p = abs(fract(hsb.xxx + K_HSB2RGB.xyz) * 6.0 - K_HSB2RGB.www); 23 | return hsb.z * mix(K_HSB2RGB.xxx, clamp(p - K_HSB2RGB.xxx, 0.0, 1.0), hsb.y); 24 | } 25 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/writeDepthClamp.glsl: -------------------------------------------------------------------------------- 1 | // emulated noperspective 2 | #if defined(GL_EXT_frag_depth) && !defined(LOG_DEPTH) 3 | varying float v_WindowZ; 4 | #endif 5 | 6 | /** 7 | * Emulates GL_DEPTH_CLAMP. Clamps a fragment to the near and far plane 8 | * by writing the fragment's depth. See czm_depthClamp for more details. 9 | *

10 | * The shader must enable the GL_EXT_frag_depth extension. 11 | *

12 | * 13 | * @name czm_writeDepthClamp 14 | * @glslFunction 15 | * 16 | * @example 17 | * gl_FragColor = color; 18 | * czm_writeDepthClamp(); 19 | * 20 | * @see czm_depthClamp 21 | */ 22 | void czm_writeDepthClamp() 23 | { 24 | #if defined(GL_EXT_frag_depth) && !defined(LOG_DEPTH) 25 | gl_FragDepthEXT = clamp(v_WindowZ * gl_FragCoord.w, 0.0, 1.0); 26 | #endif 27 | } 28 | -------------------------------------------------------------------------------- /ShaderSource/Materials/RimLightingMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform vec4 color; 2 | uniform vec4 rimColor; 3 | uniform float width; 4 | 5 | czm_material czm_getMaterial(czm_materialInput materialInput) 6 | { 7 | czm_material material = czm_getDefaultMaterial(materialInput); 8 | 9 | // See http://www.fundza.com/rman_shaders/surface/fake_rim/fake_rim1.html 10 | float d = 1.0 - dot(materialInput.normalEC, normalize(materialInput.positionToEyeEC)); 11 | float s = smoothstep(1.0 - width, 1.0, d); 12 | 13 | vec4 outColor = czm_gammaCorrect(color); 14 | vec4 outRimColor = czm_gammaCorrect(rimColor); 15 | 16 | material.diffuse = outColor.rgb; 17 | material.emission = outRimColor.rgb * s; 18 | material.alpha = mix(outColor.a, outRimColor.a, s); 19 | 20 | return material; 21 | } 22 | -------------------------------------------------------------------------------- /ShaderSource/Vector3DTilePolylinesVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec4 currentPosition; 2 | attribute vec4 previousPosition; 3 | attribute vec4 nextPosition; 4 | attribute vec2 expandAndWidth; 5 | attribute float a_batchId; 6 | 7 | uniform mat4 u_modifiedModelView; 8 | 9 | void main() 10 | { 11 | float expandDir = expandAndWidth.x; 12 | float width = abs(expandAndWidth.y) + 0.5; 13 | bool usePrev = expandAndWidth.y < 0.0; 14 | 15 | vec4 p = u_modifiedModelView * currentPosition; 16 | vec4 prev = u_modifiedModelView * previousPosition; 17 | vec4 next = u_modifiedModelView * nextPosition; 18 | 19 | float angle; 20 | vec4 positionWC = getPolylineWindowCoordinatesEC(p, prev, next, expandDir, width, usePrev, angle); 21 | gl_Position = czm_viewportOrthographic * positionWC; 22 | } 23 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/PerInstanceColorAppearanceFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec3 v_positionEC; 2 | varying vec3 v_normalEC; 3 | varying vec4 v_color; 4 | 5 | void main() 6 | { 7 | vec3 positionToEyeEC = -v_positionEC; 8 | 9 | vec3 normalEC = normalize(v_normalEC); 10 | #ifdef FACE_FORWARD 11 | normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC); 12 | #endif 13 | 14 | vec4 color = czm_gammaCorrect(v_color); 15 | 16 | czm_materialInput materialInput; 17 | materialInput.normalEC = normalEC; 18 | materialInput.positionToEyeEC = positionToEyeEC; 19 | czm_material material = czm_getDefaultMaterial(materialInput); 20 | material.diffuse = color.rgb; 21 | material.alpha = color.a; 22 | 23 | gl_FragColor = czm_phong(normalize(positionToEyeEC), material, czm_lightDirectionEC); 24 | } 25 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/TexturedMaterialAppearanceFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec3 v_positionEC; 2 | varying vec3 v_normalEC; 3 | varying vec2 v_st; 4 | 5 | void main() 6 | { 7 | vec3 positionToEyeEC = -v_positionEC; 8 | 9 | vec3 normalEC = normalize(v_normalEC); 10 | #ifdef FACE_FORWARD 11 | normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC); 12 | #endif 13 | 14 | czm_materialInput materialInput; 15 | materialInput.normalEC = normalEC; 16 | materialInput.positionToEyeEC = positionToEyeEC; 17 | materialInput.st = v_st; 18 | czm_material material = czm_getMaterial(materialInput); 19 | 20 | #ifdef FLAT 21 | gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); 22 | #else 23 | gl_FragColor = czm_phong(normalize(positionToEyeEC), material, czm_lightDirectionEC); 24 | #endif 25 | } 26 | -------------------------------------------------------------------------------- /ShaderSource/Materials/PolylineGlowMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform vec4 color; 2 | uniform float glowPower; 3 | uniform float taperPower; 4 | 5 | czm_material czm_getMaterial(czm_materialInput materialInput) 6 | { 7 | czm_material material = czm_getDefaultMaterial(materialInput); 8 | 9 | vec2 st = materialInput.st; 10 | float glow = glowPower / abs(st.t - 0.5) - (glowPower / 0.5); 11 | 12 | if (taperPower <= 0.99999) { 13 | glow *= min(1.0, taperPower / (0.5 - st.s * 0.5) - (taperPower / 0.5)); 14 | } 15 | 16 | vec4 fragColor; 17 | fragColor.rgb = max(vec3(glow - 1.0 + color.rgb), color.rgb); 18 | fragColor.a = clamp(0.0, 1.0, glow) * color.a; 19 | fragColor = czm_gammaCorrect(fragColor); 20 | 21 | material.emission = fragColor.rgb; 22 | material.alpha = fragColor.a; 23 | 24 | return material; 25 | } 26 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/computePosition.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Returns a position in model coordinates relative to eye taking into 3 | * account the current scene mode: 3D, 2D, or Columbus view. 4 | *

5 | * This uses standard position attributes, position3DHigh, 6 | * position3DLow, position2DHigh, and position2DLow, 7 | * and should be used when writing a vertex shader for an {@link Appearance}. 8 | *

9 | * 10 | * @name czm_computePosition 11 | * @glslFunction 12 | * 13 | * @returns {vec4} The position relative to eye. 14 | * 15 | * @example 16 | * vec4 p = czm_computePosition(); 17 | * v_positionEC = (czm_modelViewRelativeToEye * p).xyz; 18 | * gl_Position = czm_modelViewProjectionRelativeToEye * p; 19 | * 20 | * @see czm_translateRelativeToEye 21 | */ 22 | vec4 czm_computePosition(); 23 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Constants/webMercatorMaxLatitude.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * The maximum latitude, in radians, both North and South, supported by a Web Mercator 3 | * (EPSG:3857) projection. Technically, the Mercator projection is defined 4 | * for any latitude up to (but not including) 90 degrees, but it makes sense 5 | * to cut it off sooner because it grows exponentially with increasing latitude. 6 | * The logic behind this particular cutoff value, which is the one used by 7 | * Google Maps, Bing Maps, and Esri, is that it makes the projection 8 | * square. That is, the rectangle is equal in the X and Y directions. 9 | * 10 | * The constant value is computed as follows: 11 | * czm_pi * 0.5 - (2.0 * atan(exp(-czm_pi))) 12 | * 13 | * @name czm_webMercatorMaxLatitude 14 | * @glslConstant 15 | */ 16 | const float czm_webMercatorMaxLatitude = 1.4844222297453324; 17 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/HSLToRGB.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Converts an HSL color (hue, saturation, lightness) to RGB 3 | * HSL <-> RGB conversion: {@link http://www.chilliant.com/rgb2hsv.html} 4 | * 5 | * @name czm_HSLToRGB 6 | * @glslFunction 7 | * 8 | * @param {vec3} rgb The color in HSL. 9 | * 10 | * @returns {vec3} The color in RGB. 11 | * 12 | * @example 13 | * vec3 hsl = czm_RGBToHSL(rgb); 14 | * hsl.z *= 0.1; 15 | * rgb = czm_HSLToRGB(hsl); 16 | */ 17 | 18 | vec3 hueToRGB(float hue) 19 | { 20 | float r = abs(hue * 6.0 - 3.0) - 1.0; 21 | float g = 2.0 - abs(hue * 6.0 - 2.0); 22 | float b = 2.0 - abs(hue * 6.0 - 4.0); 23 | return clamp(vec3(r, g, b), 0.0, 1.0); 24 | } 25 | 26 | vec3 czm_HSLToRGB(vec3 hsl) 27 | { 28 | vec3 rgb = hueToRGB(hsl.x); 29 | float c = (1.0 - abs(2.0 * hsl.z - 1.0)) * hsl.y; 30 | return (rgb - 0.5) * c + hsl.z; 31 | } 32 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/CompositeTranslucentClassification.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | 3 | #ifdef DEBUG_SHOW_DEPTH 4 | uniform sampler2D u_packedTranslucentDepth; 5 | #endif 6 | 7 | varying vec2 v_textureCoordinates; 8 | 9 | void main() 10 | { 11 | #ifdef DEBUG_SHOW_DEPTH 12 | if (v_textureCoordinates.x < 0.5) 13 | { 14 | gl_FragColor.rgb = vec3(czm_unpackDepth(texture2D(u_packedTranslucentDepth, v_textureCoordinates))); 15 | gl_FragColor.a = 1.0; 16 | } 17 | #else 18 | vec4 color = texture2D(colorTexture, v_textureCoordinates); 19 | 20 | #ifdef PICK 21 | if (color == vec4(0.0)) 22 | { 23 | discard; 24 | } 25 | #else 26 | // Reverse premultiplication process to get the correct composited result of the classification primitives 27 | color.rgb /= color.a; 28 | #endif 29 | gl_FragColor = color; 30 | #endif 31 | } 32 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/getLambertDiffuse.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Calculates the intensity of diffusely reflected light. 3 | * 4 | * @name czm_getLambertDiffuse 5 | * @glslFunction 6 | * 7 | * @param {vec3} lightDirectionEC Unit vector pointing to the light source in eye coordinates. 8 | * @param {vec3} normalEC The surface normal in eye coordinates. 9 | * 10 | * @returns {float} The intensity of the diffuse reflection. 11 | * 12 | * @see czm_phong 13 | * 14 | * @example 15 | * float diffuseIntensity = czm_getLambertDiffuse(lightDirectionEC, normalEC); 16 | * float specularIntensity = czm_getSpecular(lightDirectionEC, toEyeEC, normalEC, 200); 17 | * vec3 color = (diffuseColor * diffuseIntensity) + (specularColor * specularIntensity); 18 | */ 19 | float czm_getLambertDiffuse(vec3 lightDirectionEC, vec3 normalEC) 20 | { 21 | return max(dot(lightDirectionEC, normalEC), 0.0); 22 | } 23 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/approximateSphericalCoordinates.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Approximately computes spherical coordinates given a normal. 3 | * Uses approximate inverse trigonometry for speed and consistency, 4 | * since inverse trigonometry can differ from vendor-to-vendor and when compared with the CPU. 5 | * 6 | * @name czm_approximateSphericalCoordinates 7 | * @glslFunction 8 | * 9 | * @param {vec3} normal arbitrary-length normal. 10 | * 11 | * @returns {vec2} Approximate latitude and longitude spherical coordinates. 12 | */ 13 | vec2 czm_approximateSphericalCoordinates(vec3 normal) { 14 | // Project into plane with vertical for latitude 15 | float latitudeApproximation = czm_fastApproximateAtan(sqrt(normal.x * normal.x + normal.y * normal.y), normal.z); 16 | float longitudeApproximation = czm_fastApproximateAtan(normal.x, normal.y); 17 | return vec2(latitudeApproximation, longitudeApproximation); 18 | } 19 | -------------------------------------------------------------------------------- /ShaderSource/Materials/StripeMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform vec4 evenColor; 2 | uniform vec4 oddColor; 3 | uniform float offset; 4 | uniform float repeat; 5 | uniform bool horizontal; 6 | 7 | czm_material czm_getMaterial(czm_materialInput materialInput) 8 | { 9 | czm_material material = czm_getDefaultMaterial(materialInput); 10 | 11 | // Based on the Stripes Fragment Shader in the Orange Book (11.1.2) 12 | float coord = mix(materialInput.st.s, materialInput.st.t, float(horizontal)); 13 | float value = fract((coord - offset) * (repeat * 0.5)); 14 | float dist = min(value, min(abs(value - 0.5), 1.0 - value)); 15 | 16 | vec4 currentColor = mix(evenColor, oddColor, step(0.5, value)); 17 | vec4 color = czm_antialias(evenColor, oddColor, currentColor, dist); 18 | color = czm_gammaCorrect(color); 19 | 20 | material.diffuse = color.rgb; 21 | material.alpha = color.a; 22 | 23 | return material; 24 | } 25 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/PolylineColorAppearanceVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position3DHigh; 2 | attribute vec3 position3DLow; 3 | attribute vec3 prevPosition3DHigh; 4 | attribute vec3 prevPosition3DLow; 5 | attribute vec3 nextPosition3DHigh; 6 | attribute vec3 nextPosition3DLow; 7 | attribute vec2 expandAndWidth; 8 | attribute vec4 color; 9 | attribute float batchId; 10 | 11 | varying vec4 v_color; 12 | 13 | void main() 14 | { 15 | float expandDir = expandAndWidth.x; 16 | float width = abs(expandAndWidth.y) + 0.5; 17 | bool usePrev = expandAndWidth.y < 0.0; 18 | 19 | vec4 p = czm_computePosition(); 20 | vec4 prev = czm_computePrevPosition(); 21 | vec4 next = czm_computeNextPosition(); 22 | 23 | float angle; 24 | vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, angle); 25 | gl_Position = czm_viewportOrthographic * positionWC; 26 | 27 | v_color = color; 28 | } 29 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/AllMaterialAppearanceVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position3DHigh; 2 | attribute vec3 position3DLow; 3 | attribute vec3 normal; 4 | attribute vec3 tangent; 5 | attribute vec3 bitangent; 6 | attribute vec2 st; 7 | attribute float batchId; 8 | 9 | varying vec3 v_positionEC; 10 | varying vec3 v_normalEC; 11 | varying vec3 v_tangentEC; 12 | varying vec3 v_bitangentEC; 13 | varying vec2 v_st; 14 | 15 | void main() 16 | { 17 | vec4 p = czm_computePosition(); 18 | 19 | v_positionEC = (czm_modelViewRelativeToEye * p).xyz; // position in eye coordinates 20 | v_normalEC = czm_normal * normal; // normal in eye coordinates 21 | v_tangentEC = czm_normal * tangent; // tangent in eye coordinates 22 | v_bitangentEC = czm_normal * bitangent; // bitangent in eye coordinates 23 | v_st = st; 24 | 25 | gl_Position = czm_modelViewProjectionRelativeToEye * p; 26 | } 27 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/RGBToHSB.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Converts an RGB color to HSB (hue, saturation, brightness) 3 | * HSB <-> RGB conversion with minimal branching: {@link http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl} 4 | * 5 | * @name czm_RGBToHSB 6 | * @glslFunction 7 | * 8 | * @param {vec3} rgb The color in RGB. 9 | * 10 | * @returns {vec3} The color in HSB. 11 | * 12 | * @example 13 | * vec3 hsb = czm_RGBToHSB(rgb); 14 | * hsb.z *= 0.1; 15 | * rgb = czm_HSBToRGB(hsb); 16 | */ 17 | 18 | const vec4 K_RGB2HSB = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); 19 | 20 | vec3 czm_RGBToHSB(vec3 rgb) 21 | { 22 | vec4 p = mix(vec4(rgb.bg, K_RGB2HSB.wz), vec4(rgb.gb, K_RGB2HSB.xy), step(rgb.b, rgb.g)); 23 | vec4 q = mix(vec4(p.xyw, rgb.r), vec4(rgb.r, p.yzx), step(p.x, rgb.r)); 24 | 25 | float d = q.x - min(q.w, q.y); 26 | return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + czm_epsilon7)), d / (q.x + czm_epsilon7), q.x); 27 | } 28 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/getDefaultMaterial.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * An czm_material with default values. Every material's czm_getMaterial 3 | * should use this default material as a base for the material it returns. 4 | * The default normal value is given by materialInput.normalEC. 5 | * 6 | * @name czm_getDefaultMaterial 7 | * @glslFunction 8 | * 9 | * @param {czm_materialInput} input The input used to construct the default material. 10 | * 11 | * @returns {czm_material} The default material. 12 | * 13 | * @see czm_materialInput 14 | * @see czm_material 15 | * @see czm_getMaterial 16 | */ 17 | czm_material czm_getDefaultMaterial(czm_materialInput materialInput) 18 | { 19 | czm_material material; 20 | material.diffuse = vec3(0.0); 21 | material.specular = 0.0; 22 | material.shininess = 1.0; 23 | material.normal = materialInput.normalEC; 24 | material.emission = vec3(0.0); 25 | material.alpha = 1.0; 26 | return material; 27 | } 28 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/RGBToXYZ.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Converts an RGB color to CIE Yxy. 3 | *

The conversion is described in 4 | * {@link http://content.gpwiki.org/index.php/D3DBook:High-Dynamic_Range_Rendering#Luminance_Transform|Luminance Transform} 5 | *

6 | * 7 | * @name czm_RGBToXYZ 8 | * @glslFunction 9 | * 10 | * @param {vec3} rgb The color in RGB. 11 | * 12 | * @returns {vec3} The color in CIE Yxy. 13 | * 14 | * @example 15 | * vec3 xyz = czm_RGBToXYZ(rgb); 16 | * xyz.x = max(xyz.x - luminanceThreshold, 0.0); 17 | * rgb = czm_XYZToRGB(xyz); 18 | */ 19 | vec3 czm_RGBToXYZ(vec3 rgb) 20 | { 21 | const mat3 RGB2XYZ = mat3(0.4124, 0.2126, 0.0193, 22 | 0.3576, 0.7152, 0.1192, 23 | 0.1805, 0.0722, 0.9505); 24 | vec3 xyz = RGB2XYZ * rgb; 25 | vec3 Yxy; 26 | Yxy.r = xyz.g; 27 | float temp = dot(vec3(1.0), xyz); 28 | Yxy.gb = xyz.rg / temp; 29 | return Yxy; 30 | } 31 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/unpackFloat.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Unpack an IEEE 754 single-precision float that is packed as a little-endian unsigned normalized vec4. 3 | * 4 | * @name czm_unpackFloat 5 | * @glslFunction 6 | * 7 | * @param {vec4} packedFloat The packed float. 8 | * 9 | * @returns {float} The floating-point depth in arbitrary range. 10 | */ 11 | float czm_unpackFloat(vec4 packedFloat) 12 | { 13 | // Convert to [0.0, 255.0] and round to integer 14 | packedFloat = floor(packedFloat * 255.0 + 0.5); 15 | float sign = 1.0 - step(128.0, packedFloat[3]) * 2.0; 16 | float exponent = 2.0 * mod(packedFloat[3], 128.0) + step(128.0, packedFloat[2]) - 127.0; 17 | if (exponent == -127.0) 18 | { 19 | return 0.0; 20 | } 21 | float mantissa = mod(packedFloat[2], 128.0) * 65536.0 + packedFloat[1] * 256.0 + packedFloat[0] + float(0x800000); 22 | float result = sign * exp2(exponent - 23.0) * mantissa; 23 | return result; 24 | } 25 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/XYZToRGB.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Converts a CIE Yxy color to RGB. 3 | *

The conversion is described in 4 | * {@link http://content.gpwiki.org/index.php/D3DBook:High-Dynamic_Range_Rendering#Luminance_Transform|Luminance Transform} 5 | *

6 | * 7 | * @name czm_XYZToRGB 8 | * @glslFunction 9 | * 10 | * @param {vec3} Yxy The color in CIE Yxy. 11 | * 12 | * @returns {vec3} The color in RGB. 13 | * 14 | * @example 15 | * vec3 xyz = czm_RGBToXYZ(rgb); 16 | * xyz.x = max(xyz.x - luminanceThreshold, 0.0); 17 | * rgb = czm_XYZToRGB(xyz); 18 | */ 19 | vec3 czm_XYZToRGB(vec3 Yxy) 20 | { 21 | const mat3 XYZ2RGB = mat3( 3.2405, -0.9693, 0.0556, 22 | -1.5371, 1.8760, -0.2040, 23 | -0.4985, 0.0416, 1.0572); 24 | vec3 xyz; 25 | xyz.r = Yxy.r * Yxy.g / Yxy.b; 26 | xyz.g = Yxy.r; 27 | xyz.b = Yxy.r * (1.0 - Yxy.g - Yxy.b) / Yxy.b; 28 | 29 | return XYZ2RGB * xyz; 30 | } 31 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/BrightPass.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | 3 | uniform float avgLuminance; 4 | uniform float threshold; 5 | uniform float offset; 6 | 7 | varying vec2 v_textureCoordinates; 8 | 9 | float key(float avg) 10 | { 11 | float guess = 1.5 - (1.5 / (avg * 0.1 + 1.0)); 12 | return max(0.0, guess) + 0.1; 13 | } 14 | 15 | // See section 9. "The bright-pass filter" of Realtime HDR Rendering 16 | // http://www.cg.tuwien.ac.at/research/publications/2007/Luksch_2007_RHR/Luksch_2007_RHR-RealtimeHDR%20.pdf 17 | 18 | void main() 19 | { 20 | vec4 color = texture2D(colorTexture, v_textureCoordinates); 21 | vec3 xyz = czm_RGBToXYZ(color.rgb); 22 | float luminance = xyz.r; 23 | 24 | float scaledLum = key(avgLuminance) * luminance / avgLuminance; 25 | float brightLum = max(scaledLum - threshold, 0.0); 26 | float brightness = brightLum / (offset + brightLum); 27 | 28 | xyz.r = brightness; 29 | gl_FragColor = vec4(czm_XYZToRGB(xyz), 1.0); 30 | } 31 | -------------------------------------------------------------------------------- /Docs/Builtin/Structs/shadowParameters.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ``` glsl 4 | struct czm_shadowParameters 5 | { 6 | #ifdef USE_CUBE_MAP_SHADOW 7 | vec3 texCoords; 8 | #else 9 | vec2 texCoords; 10 | #endif 11 | 12 | float depthBias; 13 | float depth; 14 | float nDotL; 15 | vec2 texelStepSize; 16 | float normalShadingSmooth; 17 | float darkness; 18 | }; 19 | ``` 20 | 21 | # 文档 22 | 23 | ## 1. 成员 `texCoords` 24 | 25 | ### 类型 26 | 27 | 当宏 `USE_CUBE_MAP_SHADOW` 定义时,为 `vec3`; 28 | 29 | 否则为 `vec2`. 30 | 31 | ### 说明 32 | 33 | TODO. 34 | 35 | 36 | 37 | ## 2. 成员 `depthBias` 38 | 39 | ### 类型 40 | 41 | `float` 42 | 43 | 44 | 45 | ## 3. 成员 `depth` 46 | 47 | ### 类型 48 | 49 | `float` 50 | 51 | 52 | 53 | ## 4. 成员 `nDotL` 54 | 55 | ### 类型 56 | 57 | `float` 58 | 59 | 60 | 61 | ## 5. 成员 `texelStepSize` 62 | 63 | ### 类型 64 | 65 | `vec2` 66 | 67 | 68 | 69 | ## 6. 成员 `normalShadingSmooth` 70 | 71 | ### 类型 72 | 73 | `float` 74 | 75 | 76 | 77 | ## 7. 成员 `darkness` 78 | 79 | ### 类型 80 | 81 | `float` -------------------------------------------------------------------------------- /ShaderSource/Materials/ElevationContourMaterial.glsl: -------------------------------------------------------------------------------- 1 | #ifdef GL_OES_standard_derivatives 2 | #extension GL_OES_standard_derivatives : enable 3 | #endif 4 | 5 | uniform vec4 color; 6 | uniform float spacing; 7 | uniform float width; 8 | 9 | czm_material czm_getMaterial(czm_materialInput materialInput) 10 | { 11 | czm_material material = czm_getDefaultMaterial(materialInput); 12 | 13 | float distanceToContour = mod(materialInput.height, spacing); 14 | 15 | #ifdef GL_OES_standard_derivatives 16 | float dxc = abs(dFdx(materialInput.height)); 17 | float dyc = abs(dFdy(materialInput.height)); 18 | float dF = max(dxc, dyc) * czm_pixelRatio * width; 19 | float alpha = (distanceToContour < dF) ? 1.0 : 0.0; 20 | #else 21 | float alpha = (distanceToContour < (czm_pixelRatio * width)) ? 1.0 : 0.0; 22 | #endif 23 | 24 | vec4 outColor = czm_gammaCorrect(vec4(color.rgb, alpha * color.a)); 25 | material.diffuse = outColor.rgb; 26 | material.alpha = outColor.a; 27 | 28 | return material; 29 | } 30 | -------------------------------------------------------------------------------- /ShaderSource/CompositeOITFS.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Compositing for Weighted Blended Order-Independent Transparency. See: 3 | * - http://jcgt.org/published/0002/02/09/ 4 | * - http://casual-effects.blogspot.com/2014/03/weighted-blended-order-independent.html 5 | */ 6 | 7 | uniform sampler2D u_opaque; 8 | uniform sampler2D u_accumulation; 9 | uniform sampler2D u_revealage; 10 | 11 | varying vec2 v_textureCoordinates; 12 | 13 | void main() 14 | { 15 | vec4 opaque = texture2D(u_opaque, v_textureCoordinates); 16 | vec4 accum = texture2D(u_accumulation, v_textureCoordinates); 17 | float r = texture2D(u_revealage, v_textureCoordinates).r; 18 | 19 | #ifdef MRT 20 | vec4 transparent = vec4(accum.rgb / clamp(r, 1e-4, 5e4), accum.a); 21 | #else 22 | vec4 transparent = vec4(accum.rgb / clamp(accum.a, 1e-4, 5e4), r); 23 | #endif 24 | 25 | gl_FragColor = (1.0 - transparent.a) * transparent + transparent.a * opaque; 26 | 27 | if (opaque != czm_backgroundColor) 28 | { 29 | gl_FragColor.a = 1.0; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/signNotZero.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Returns 1.0 if the given value is positive or zero, and -1.0 if it is negative. This is similar to the GLSL 3 | * built-in function sign except that returns 1.0 instead of 0.0 when the input value is 0.0. 4 | * 5 | * @name czm_signNotZero 6 | * @glslFunction 7 | * 8 | * @param {} value The value for which to determine the sign. 9 | * @returns {} 1.0 if the value is positive or zero, -1.0 if the value is negative. 10 | */ 11 | float czm_signNotZero(float value) 12 | { 13 | return value >= 0.0 ? 1.0 : -1.0; 14 | } 15 | 16 | vec2 czm_signNotZero(vec2 value) 17 | { 18 | return vec2(czm_signNotZero(value.x), czm_signNotZero(value.y)); 19 | } 20 | 21 | vec3 czm_signNotZero(vec3 value) 22 | { 23 | return vec3(czm_signNotZero(value.x), czm_signNotZero(value.y), czm_signNotZero(value.z)); 24 | } 25 | 26 | vec4 czm_signNotZero(vec4 value) 27 | { 28 | return vec4(czm_signNotZero(value.x), czm_signNotZero(value.y), czm_signNotZero(value.z), czm_signNotZero(value.w)); 29 | } 30 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/AllMaterialAppearanceFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec3 v_positionEC; 2 | varying vec3 v_normalEC; 3 | varying vec3 v_tangentEC; 4 | varying vec3 v_bitangentEC; 5 | varying vec2 v_st; 6 | 7 | void main() 8 | { 9 | vec3 positionToEyeEC = -v_positionEC; 10 | mat3 tangentToEyeMatrix = czm_tangentToEyeSpaceMatrix(v_normalEC, v_tangentEC, v_bitangentEC); 11 | 12 | vec3 normalEC = normalize(v_normalEC); 13 | #ifdef FACE_FORWARD 14 | normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC); 15 | #endif 16 | 17 | czm_materialInput materialInput; 18 | materialInput.normalEC = normalEC; 19 | materialInput.tangentToEyeMatrix = tangentToEyeMatrix; 20 | materialInput.positionToEyeEC = positionToEyeEC; 21 | materialInput.st = v_st; 22 | czm_material material = czm_getMaterial(materialInput); 23 | 24 | #ifdef FLAT 25 | gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); 26 | #else 27 | gl_FragColor = czm_phong(normalize(positionToEyeEC), material, czm_lightDirectionEC); 28 | #endif 29 | } 30 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/planeDistance.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Computes distance from a point to a plane. 3 | * 4 | * @name czm_planeDistance 5 | * @glslFunction 6 | * 7 | * param {vec4} plane A Plane in Hessian Normal Form. See Plane.js 8 | * param {vec3} point A point in the same space as the plane. 9 | * returns {float} The distance from the point to the plane. 10 | */ 11 | float czm_planeDistance(vec4 plane, vec3 point) { 12 | return (dot(plane.xyz, point) + plane.w); 13 | } 14 | 15 | /** 16 | * Computes distance from a point to a plane. 17 | * 18 | * @name czm_planeDistance 19 | * @glslFunction 20 | * 21 | * param {vec3} planeNormal Normal for a plane in Hessian Normal Form. See Plane.js 22 | * param {float} planeDistance Distance for a plane in Hessian Normal form. See Plane.js 23 | * param {vec3} point A point in the same space as the plane. 24 | * returns {float} The distance from the point to the plane. 25 | */ 26 | float czm_planeDistance(vec3 planeNormal, float planeDistance, vec3 point) { 27 | return (dot(planeNormal, point) + planeDistance); 28 | } 29 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/translucentPhong.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * @private 3 | */ 4 | vec4 czm_translucentPhong(vec3 toEye, czm_material material, vec3 lightDirectionEC) 5 | { 6 | // Diffuse from directional light sources at eye (for top-down and horizon views) 7 | float diffuse = czm_getLambertDiffuse(vec3(0.0, 0.0, 1.0), material.normal); 8 | 9 | if (czm_sceneMode == czm_sceneMode3D) { 10 | // (and horizon views in 3D) 11 | diffuse += czm_getLambertDiffuse(vec3(0.0, 1.0, 0.0), material.normal); 12 | } 13 | 14 | diffuse = clamp(diffuse, 0.0, 1.0); 15 | 16 | float specular = czm_getSpecular(lightDirectionEC, toEye, material.normal, material.shininess); 17 | 18 | // Temporary workaround for adding ambient. 19 | vec3 materialDiffuse = material.diffuse * 0.5; 20 | 21 | vec3 ambient = materialDiffuse; 22 | vec3 color = ambient + material.emission; 23 | color += materialDiffuse * diffuse * czm_lightColor; 24 | color += material.specular * specular * czm_lightColor; 25 | 26 | return vec4(color, material.alpha); 27 | } 28 | -------------------------------------------------------------------------------- /ShaderSource/Materials/PolylineOutlineMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform vec4 color; 2 | uniform vec4 outlineColor; 3 | uniform float outlineWidth; 4 | 5 | varying float v_width; 6 | 7 | czm_material czm_getMaterial(czm_materialInput materialInput) 8 | { 9 | czm_material material = czm_getDefaultMaterial(materialInput); 10 | 11 | vec2 st = materialInput.st; 12 | float halfInteriorWidth = 0.5 * (v_width - outlineWidth) / v_width; 13 | float b = step(0.5 - halfInteriorWidth, st.t); 14 | b *= 1.0 - step(0.5 + halfInteriorWidth, st.t); 15 | 16 | // Find the distance from the closest separator (region between two colors) 17 | float d1 = abs(st.t - (0.5 - halfInteriorWidth)); 18 | float d2 = abs(st.t - (0.5 + halfInteriorWidth)); 19 | float dist = min(d1, d2); 20 | 21 | vec4 currentColor = mix(outlineColor, color, b); 22 | vec4 outColor = czm_antialias(outlineColor, color, currentColor, dist); 23 | outColor = czm_gammaCorrect(outColor); 24 | 25 | material.diffuse = outColor.rgb; 26 | material.alpha = outColor.a; 27 | 28 | return material; 29 | } 30 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/RGBToHSL.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Converts an RGB color to HSL (hue, saturation, lightness) 3 | * HSL <-> RGB conversion: {@link http://www.chilliant.com/rgb2hsv.html} 4 | * 5 | * @name czm_RGBToHSL 6 | * @glslFunction 7 | * 8 | * @param {vec3} rgb The color in RGB. 9 | * 10 | * @returns {vec3} The color in HSL. 11 | * 12 | * @example 13 | * vec3 hsl = czm_RGBToHSL(rgb); 14 | * hsl.z *= 0.1; 15 | * rgb = czm_HSLToRGB(hsl); 16 | */ 17 | 18 | vec3 RGBtoHCV(vec3 rgb) 19 | { 20 | // Based on work by Sam Hocevar and Emil Persson 21 | vec4 p = (rgb.g < rgb.b) ? vec4(rgb.bg, -1.0, 2.0 / 3.0) : vec4(rgb.gb, 0.0, -1.0 / 3.0); 22 | vec4 q = (rgb.r < p.x) ? vec4(p.xyw, rgb.r) : vec4(rgb.r, p.yzx); 23 | float c = q.x - min(q.w, q.y); 24 | float h = abs((q.w - q.y) / (6.0 * c + czm_epsilon7) + q.z); 25 | return vec3(h, c, q.x); 26 | } 27 | 28 | vec3 czm_RGBToHSL(vec3 rgb) 29 | { 30 | vec3 hcv = RGBtoHCV(rgb); 31 | float l = hcv.z - hcv.y * 0.5; 32 | float s = hcv.y / (1.0 - abs(l * 2.0 - 1.0) + czm_epsilon7); 33 | return vec3(hcv.x, s, l); 34 | } 35 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/nearFarScalar.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Computes a value that scales with distance. The scaling is clamped at the near and 3 | * far distances, and does not extrapolate. This function works with the 4 | * {@link NearFarScalar} JavaScript class. 5 | * 6 | * @name czm_nearFarScalar 7 | * @glslFunction 8 | * 9 | * @param {vec4} nearFarScalar A vector with 4 components: Near distance (x), Near value (y), Far distance (z), Far value (w). 10 | * @param {float} cameraDistSq The square of the current distance from the camera. 11 | * 12 | * @returns {float} The value at this distance. 13 | */ 14 | float czm_nearFarScalar(vec4 nearFarScalar, float cameraDistSq) 15 | { 16 | float valueAtMin = nearFarScalar.y; 17 | float valueAtMax = nearFarScalar.w; 18 | float nearDistanceSq = nearFarScalar.x * nearFarScalar.x; 19 | float farDistanceSq = nearFarScalar.z * nearFarScalar.z; 20 | 21 | float t = (cameraDistSq - nearDistanceSq) / (farDistanceSq - nearDistanceSq); 22 | 23 | t = pow(clamp(t, 0.0, 1.0), 0.2); 24 | 25 | return mix(valueAtMin, valueAtMax, t); 26 | } 27 | -------------------------------------------------------------------------------- /Docs/Builtin/Functions/saturation.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ```glsl 4 | /** 5 | * Adjusts the saturation of a color. 6 | * 7 | * @name czm_saturation 8 | * @glslFunction 9 | * 10 | * @param {vec3} rgb The color. 11 | * @param {float} adjustment The amount to adjust the saturation of the color. 12 | * 13 | * @returns {float} The color with the saturation adjusted. 14 | * 15 | * @example 16 | * vec3 greyScale = czm_saturation(color, 0.0); 17 | * vec3 doubleSaturation = czm_saturation(color, 2.0); 18 | */ 19 | vec3 czm_saturation(vec3 rgb, float adjustment) 20 | { 21 | // Algorithm from Chapter 16 of OpenGL Shading Language 22 | const vec3 W = vec3(0.2125, 0.7154, 0.0721); 23 | vec3 intensity = vec3(dot(rgb, W)); 24 | return mix(intensity, rgb, adjustment); 25 | } 26 | ``` 27 | 28 | # 文档 29 | 30 | 调整颜色值(RGB格式)的饱和度,使用《OpenGL Shading Language》第 16 章中提及的饱和度调整算法: 31 | 32 | - 使用权重向量 `vec3(0.2125, 0.7154, 0.0721)` 点乘 RGB 颜色向量,得到一个灰度值 33 | 34 | - 将此灰度值与原来的 RGB 颜色进行 `mix` 混合,混合插值系数是 `adjustment` 35 | 36 | - 返回一个调整饱和度后的颜色值 `vec3` 37 | 38 | 可以简单理解为入参 `adjustment` 就是调整饱和度的系数,值的范围是 `[0, 1]` 39 | 40 | 41 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/PolylineMaterialAppearanceVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position3DHigh; 2 | attribute vec3 position3DLow; 3 | attribute vec3 prevPosition3DHigh; 4 | attribute vec3 prevPosition3DLow; 5 | attribute vec3 nextPosition3DHigh; 6 | attribute vec3 nextPosition3DLow; 7 | attribute vec2 expandAndWidth; 8 | attribute vec2 st; 9 | attribute float batchId; 10 | 11 | varying float v_width; 12 | varying vec2 v_st; 13 | varying float v_polylineAngle; 14 | 15 | void main() 16 | { 17 | float expandDir = expandAndWidth.x; 18 | float width = abs(expandAndWidth.y) + 0.5; 19 | bool usePrev = expandAndWidth.y < 0.0; 20 | 21 | vec4 p = czm_computePosition(); 22 | vec4 prev = czm_computePrevPosition(); 23 | vec4 next = czm_computeNextPosition(); 24 | 25 | float angle; 26 | vec4 positionWC = getPolylineWindowCoordinates(p, prev, next, expandDir, width, usePrev, angle); 27 | gl_Position = czm_viewportOrthographic * positionWC; 28 | 29 | v_width = width; 30 | v_st.s = st.s; 31 | v_st.t = czm_writeNonPerspective(st.t, gl_Position.w); 32 | v_polylineAngle = angle; 33 | } 34 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/tangentToEyeSpaceMatrix.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Creates a matrix that transforms vectors from tangent space to eye space. 3 | * 4 | * @name czm_tangentToEyeSpaceMatrix 5 | * @glslFunction 6 | * 7 | * @param {vec3} normalEC The normal vector in eye coordinates. 8 | * @param {vec3} tangentEC The tangent vector in eye coordinates. 9 | * @param {vec3} bitangentEC The bitangent vector in eye coordinates. 10 | * 11 | * @returns {mat3} The matrix that transforms from tangent space to eye space. 12 | * 13 | * @example 14 | * mat3 tangentToEye = czm_tangentToEyeSpaceMatrix(normalEC, tangentEC, bitangentEC); 15 | * vec3 normal = tangentToEye * texture2D(normalMap, st).xyz; 16 | */ 17 | mat3 czm_tangentToEyeSpaceMatrix(vec3 normalEC, vec3 tangentEC, vec3 bitangentEC) 18 | { 19 | vec3 normal = normalize(normalEC); 20 | vec3 tangent = normalize(tangentEC); 21 | vec3 bitangent = normalize(bitangentEC); 22 | return mat3(tangent.x , tangent.y , tangent.z, 23 | bitangent.x, bitangent.y, bitangent.z, 24 | normal.x , normal.y , normal.z); 25 | } 26 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/hue.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Adjusts the hue of a color. 3 | * 4 | * @name czm_hue 5 | * @glslFunction 6 | * 7 | * @param {vec3} rgb The color. 8 | * @param {float} adjustment The amount to adjust the hue of the color in radians. 9 | * 10 | * @returns {float} The color with the hue adjusted. 11 | * 12 | * @example 13 | * vec3 adjustHue = czm_hue(color, czm_pi); // The same as czm_hue(color, -czm_pi) 14 | */ 15 | vec3 czm_hue(vec3 rgb, float adjustment) 16 | { 17 | const mat3 toYIQ = mat3(0.299, 0.587, 0.114, 18 | 0.595716, -0.274453, -0.321263, 19 | 0.211456, -0.522591, 0.311135); 20 | const mat3 toRGB = mat3(1.0, 0.9563, 0.6210, 21 | 1.0, -0.2721, -0.6474, 22 | 1.0, -1.107, 1.7046); 23 | 24 | vec3 yiq = toYIQ * rgb; 25 | float hue = atan(yiq.z, yiq.y) + adjustment; 26 | float chroma = sqrt(yiq.z * yiq.z + yiq.y * yiq.y); 27 | 28 | vec3 color = vec3(yiq.x, chroma * cos(hue), chroma * sin(hue)); 29 | return toRGB * color; 30 | } 31 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Structs/material.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Holds material information that can be used for lighting. Returned by all czm_getMaterial functions. 3 | * 4 | * @name czm_material 5 | * @glslStruct 6 | * 7 | * @property {vec3} diffuse Incoming light that scatters evenly in all directions. 8 | * @property {float} specular Intensity of incoming light reflecting in a single direction. 9 | * @property {float} shininess The sharpness of the specular reflection. Higher values create a smaller, more focused specular highlight. 10 | * @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. 11 | * @property {vec3} emission Light emitted by the material equally in all directions. The default is vec3(0.0), which emits no light. 12 | * @property {float} alpha Opacity of this material. 0.0 is completely transparent; 1.0 is completely opaque. 13 | */ 14 | struct czm_material 15 | { 16 | vec3 diffuse; 17 | float specular; 18 | float shininess; 19 | vec3 normal; 20 | vec3 emission; 21 | float alpha; 22 | }; 23 | -------------------------------------------------------------------------------- /ShaderSource/DepthPlaneFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec4 positionEC; 2 | 3 | void main() 4 | { 5 | vec3 position; 6 | vec3 direction; 7 | if (czm_orthographicIn3D == 1.0) 8 | { 9 | vec2 uv = (gl_FragCoord.xy - czm_viewport.xy) / czm_viewport.zw; 10 | vec2 minPlane = vec2(czm_frustumPlanes.z, czm_frustumPlanes.y); // left, bottom 11 | vec2 maxPlane = vec2(czm_frustumPlanes.w, czm_frustumPlanes.x); // right, top 12 | position = vec3(mix(minPlane, maxPlane, uv), 0.0); 13 | direction = vec3(0.0, 0.0, -1.0); 14 | } 15 | else 16 | { 17 | position = vec3(0.0); 18 | direction = normalize(positionEC.xyz); 19 | } 20 | 21 | czm_ray ray = czm_ray(position, direction); 22 | 23 | vec3 ellipsoid_center = czm_view[3].xyz; 24 | 25 | czm_raySegment intersection = czm_rayEllipsoidIntersectionInterval(ray, ellipsoid_center, czm_ellipsoidInverseRadii); 26 | if (!czm_isEmpty(intersection)) 27 | { 28 | gl_FragColor = vec4(1.0, 1.0, 0.0, 1.0); 29 | } 30 | else 31 | { 32 | discard; 33 | } 34 | 35 | czm_writeLogDepth(); 36 | } 37 | -------------------------------------------------------------------------------- /ShaderSource/Materials/BumpMapMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D image; 2 | uniform float strength; 3 | uniform vec2 repeat; 4 | 5 | czm_material czm_getMaterial(czm_materialInput materialInput) 6 | { 7 | czm_material material = czm_getDefaultMaterial(materialInput); 8 | 9 | vec2 st = materialInput.st; 10 | 11 | vec2 centerPixel = fract(repeat * st); 12 | float centerBump = texture2D(image, centerPixel).channel; 13 | 14 | float imageWidth = float(imageDimensions.x); 15 | vec2 rightPixel = fract(repeat * (st + vec2(1.0 / imageWidth, 0.0))); 16 | float rightBump = texture2D(image, rightPixel).channel; 17 | 18 | float imageHeight = float(imageDimensions.y); 19 | vec2 leftPixel = fract(repeat * (st + vec2(0.0, 1.0 / imageHeight))); 20 | float topBump = texture2D(image, leftPixel).channel; 21 | 22 | vec3 normalTangentSpace = normalize(vec3(centerBump - rightBump, centerBump - topBump, clamp(1.0 - strength, 0.1, 1.0))); 23 | vec3 normalEC = materialInput.tangentToEyeMatrix * normalTangentSpace; 24 | 25 | material.normal = normalEC; 26 | material.diffuse = vec3(0.01); 27 | 28 | return material; 29 | } 30 | -------------------------------------------------------------------------------- /ShaderSource/Materials/FadeMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform vec4 fadeInColor; 2 | uniform vec4 fadeOutColor; 3 | uniform float maximumDistance; 4 | uniform bool repeat; 5 | uniform vec2 fadeDirection; 6 | uniform vec2 time; 7 | 8 | float getTime(float t, float coord) 9 | { 10 | float scalar = 1.0 / maximumDistance; 11 | float q = distance(t, coord) * scalar; 12 | if (repeat) 13 | { 14 | float r = distance(t, coord + 1.0) * scalar; 15 | float s = distance(t, coord - 1.0) * scalar; 16 | q = min(min(r, s), q); 17 | } 18 | return clamp(q, 0.0, 1.0); 19 | } 20 | 21 | czm_material czm_getMaterial(czm_materialInput materialInput) 22 | { 23 | czm_material material = czm_getDefaultMaterial(materialInput); 24 | 25 | vec2 st = materialInput.st; 26 | float s = getTime(time.x, st.s) * fadeDirection.s; 27 | float t = getTime(time.y, st.t) * fadeDirection.t; 28 | 29 | float u = length(vec2(s, t)); 30 | vec4 color = mix(fadeInColor, fadeOutColor, u); 31 | 32 | color = czm_gammaCorrect(color); 33 | material.emission = color.rgb; 34 | material.alpha = color.a; 35 | 36 | return material; 37 | } 38 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/pbrSpecularGlossinessMaterial.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Compute parameters for physically based rendering using the 3 | * specular/glossy workflow. All inputs are linear; sRGB texture values must 4 | * be decoded beforehand 5 | * 6 | * @name czm_pbrSpecularGlossinessMaterial 7 | * @glslFunction 8 | * 9 | * @param {vec3} diffuse The diffuse color for dielectrics (non-metals) 10 | * @param {vec3} specular The reflectance at normal incidence (f0) 11 | * @param {float} glossiness A number from 0.0 to 1.0 indicating how smooth the surface is. 12 | * @return {czm_pbrParameters} parameters to pass into {@link czm_pbrLighting} 13 | */ 14 | czm_pbrParameters czm_pbrSpecularGlossinessMaterial( 15 | vec3 diffuse, 16 | vec3 specular, 17 | float glossiness 18 | ) { 19 | czm_pbrParameters results; 20 | 21 | // glossiness is the opposite of roughness, but easier for artists to use. 22 | float roughness = 1.0 - glossiness; 23 | results.roughness = roughness * roughness; 24 | 25 | results.diffuseColor = diffuse * (1.0 - max(max(specular.r, specular.g), specular.b)); 26 | results.f0 = specular; 27 | 28 | return results; 29 | } 30 | -------------------------------------------------------------------------------- /ShaderSource/Materials/CheckerboardMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform vec4 lightColor; 2 | uniform vec4 darkColor; 3 | uniform vec2 repeat; 4 | 5 | czm_material czm_getMaterial(czm_materialInput materialInput) 6 | { 7 | czm_material material = czm_getDefaultMaterial(materialInput); 8 | 9 | vec2 st = materialInput.st; 10 | 11 | // From Stefan Gustavson's Procedural Textures in GLSL in OpenGL Insights 12 | float b = mod(floor(repeat.s * st.s) + floor(repeat.t * st.t), 2.0); // 0.0 or 1.0 13 | 14 | // Find the distance from the closest separator (region between two colors) 15 | float scaledWidth = fract(repeat.s * st.s); 16 | scaledWidth = abs(scaledWidth - floor(scaledWidth + 0.5)); 17 | float scaledHeight = fract(repeat.t * st.t); 18 | scaledHeight = abs(scaledHeight - floor(scaledHeight + 0.5)); 19 | float value = min(scaledWidth, scaledHeight); 20 | 21 | vec4 currentColor = mix(lightColor, darkColor, b); 22 | vec4 color = czm_antialias(lightColor, darkColor, currentColor, value, 0.03); 23 | 24 | color = czm_gammaCorrect(color); 25 | material.diffuse = color.rgb; 26 | material.alpha = color.a; 27 | 28 | return material; 29 | } 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 当前版本:1.98 2 | 3 | --- 4 | 5 | # 指南 6 | 7 | 这是一个翻译 `Cesium.js` 中内置着色器的仓库,如果有兴趣可以参与贡献。 8 | 9 | # 结构 10 | 11 | 与官方仓库中 `Source/Shaders` 目录基本一致。每个 glsl 写入同一个 markdown 文档。 12 | 13 | 此项目根目录下的 [ShaderSource](./ShaderSource) 目录是着色器源代码,[Docs](./Docs) 目录下的文件才是翻译后的文档。 14 | 15 | # 如何贡献 16 | 17 | 目前分文档翻译和原理翻译两部分。 18 | 19 | ## step1:复刻一份仓库到你的账户下 20 | 21 | 使用 `fork` 功能。 22 | 23 | ## step2:开一个新分支 24 | 25 | 请自行查找 git 文档。 26 | 27 | 例如,你想修改/增加 `Builtin/Structs/ray.glsl` 的文档,开的分支名可为:`[add] builtin/structs/ray` / `[branch] builtin/structs/ray` 28 | 29 | ## step3:书写 markdown 文档 30 | 31 | 参考结构说明以及 `Docs/README.md` 中的预制目录表格。 32 | 33 | ### 翻译核心着色器的格式要求 34 | 35 | todo 36 | 37 | ### 翻译 Builtin/Constants 的格式要求 38 | 39 | todo 40 | 41 | ### 翻译 Builtin/Functions 的格式要求 42 | 43 | todo 44 | 45 | ### 翻译 Builtin/Structs 的格式要求 46 | 47 | todo 48 | 49 | ### 翻译 Appearances 的格式要求 50 | 51 | todo 52 | 53 | ### 翻译 Materials 的格式要求 54 | 55 | todo 56 | 57 | ### 翻译 PostProcessStages 的格式要求 58 | 59 | ## step4:在贡献者列表写上你的名字 60 | 61 | 不一定非要是 github 链接,你随意,只占一行文本超链接即可,禁止打广告。 62 | 63 | ## step5:发起推送请求(PullRequest) 64 | 65 | 然后我来审查合并。 66 | 67 | # 贡献者 68 | 69 | - [@onsummer ](https://github.com/onsummer) 70 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/latitudeToWebMercatorFraction.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Computes the fraction of a Web Wercator rectangle at which a given geodetic latitude is located. 3 | * 4 | * @name czm_latitudeToWebMercatorFraction 5 | * @glslFunction 6 | * 7 | * @param {float} latitude The geodetic latitude, in radians. 8 | * @param {float} southMercatorY The Web Mercator coordinate of the southern boundary of the rectangle. 9 | * @param {float} oneOverMercatorHeight The total height of the rectangle in Web Mercator coordinates. 10 | * 11 | * @returns {float} The fraction of the rectangle at which the latitude occurs. If the latitude is the southern 12 | * boundary of the rectangle, the return value will be zero. If it is the northern boundary, the return 13 | * value will be 1.0. Latitudes in between are mapped according to the Web Mercator projection. 14 | */ 15 | float czm_latitudeToWebMercatorFraction(float latitude, float southMercatorY, float oneOverMercatorHeight) 16 | { 17 | float sinLatitude = sin(latitude); 18 | float mercatorY = 0.5 * log((1.0 + sinLatitude) / (1.0 - sinLatitude)); 19 | 20 | return (mercatorY - southMercatorY) * oneOverMercatorHeight; 21 | } 22 | -------------------------------------------------------------------------------- /ShaderSource/Appearances/EllipsoidSurfaceAppearanceFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec3 v_positionMC; 2 | varying vec3 v_positionEC; 3 | varying vec2 v_st; 4 | 5 | void main() 6 | { 7 | czm_materialInput materialInput; 8 | 9 | vec3 normalEC = normalize(czm_normal3D * czm_geodeticSurfaceNormal(v_positionMC, vec3(0.0), vec3(1.0))); 10 | #ifdef FACE_FORWARD 11 | normalEC = faceforward(normalEC, vec3(0.0, 0.0, 1.0), -normalEC); 12 | #endif 13 | 14 | materialInput.s = v_st.s; 15 | materialInput.st = v_st; 16 | materialInput.str = vec3(v_st, 0.0); 17 | 18 | // Convert tangent space material normal to eye space 19 | materialInput.normalEC = normalEC; 20 | materialInput.tangentToEyeMatrix = czm_eastNorthUpToEyeCoordinates(v_positionMC, materialInput.normalEC); 21 | 22 | // Convert view vector to world space 23 | vec3 positionToEyeEC = -v_positionEC; 24 | materialInput.positionToEyeEC = positionToEyeEC; 25 | 26 | czm_material material = czm_getMaterial(materialInput); 27 | 28 | #ifdef FLAT 29 | gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); 30 | #else 31 | gl_FragColor = czm_phong(normalize(positionToEyeEC), material, czm_lightDirectionEC); 32 | #endif 33 | } 34 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/FilmicTonemapping.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | #ifdef AUTO_EXPOSURE 6 | uniform sampler2D autoExposure; 7 | #endif 8 | 9 | // See slides 142 and 143: 10 | // http://www.gdcvault.com/play/1012459/Uncharted_2__HDR_Lighting 11 | 12 | void main() 13 | { 14 | vec4 fragmentColor = texture2D(colorTexture, v_textureCoordinates); 15 | vec3 color = fragmentColor.rgb; 16 | 17 | #ifdef AUTO_EXPOSURE 18 | float exposure = texture2D(autoExposure, vec2(0.5)).r; 19 | color /= exposure; 20 | #endif 21 | 22 | const float A = 0.22; // shoulder strength 23 | const float B = 0.30; // linear strength 24 | const float C = 0.10; // linear angle 25 | const float D = 0.20; // toe strength 26 | const float E = 0.01; // toe numerator 27 | const float F = 0.30; // toe denominator 28 | 29 | const float white = 11.2; // linear white point value 30 | 31 | vec3 c = ((color * (A * color + C * B) + D * E) / (color * ( A * color + B) + D * F)) - E / F; 32 | float w = ((white * (A * white + C * B) + D * E) / (white * ( A * white + B) + D * F)) - E / F; 33 | 34 | c = czm_inverseGamma(c / w); 35 | gl_FragColor = vec4(c, fragmentColor.a); 36 | } 37 | -------------------------------------------------------------------------------- /ShaderSource/Materials/PolylineDashMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform vec4 color; 2 | uniform vec4 gapColor; 3 | uniform float dashLength; 4 | uniform float dashPattern; 5 | varying float v_polylineAngle; 6 | 7 | const float maskLength = 16.0; 8 | 9 | mat2 rotate(float rad) { 10 | float c = cos(rad); 11 | float s = sin(rad); 12 | return mat2( 13 | c, s, 14 | -s, c 15 | ); 16 | } 17 | 18 | czm_material czm_getMaterial(czm_materialInput materialInput) 19 | { 20 | czm_material material = czm_getDefaultMaterial(materialInput); 21 | 22 | vec2 pos = rotate(v_polylineAngle) * gl_FragCoord.xy; 23 | 24 | // Get the relative position within the dash from 0 to 1 25 | float dashPosition = fract(pos.x / (dashLength * czm_pixelRatio)); 26 | // Figure out the mask index. 27 | float maskIndex = floor(dashPosition * maskLength); 28 | // Test the bit mask. 29 | float maskTest = floor(dashPattern / pow(2.0, maskIndex)); 30 | vec4 fragColor = (mod(maskTest, 2.0) < 1.0) ? gapColor : color; 31 | if (fragColor.a < 0.005) { // matches 0/255 and 1/255 32 | discard; 33 | } 34 | 35 | fragColor = czm_gammaCorrect(fragColor); 36 | material.emission = fragColor.rgb; 37 | material.alpha = fragColor.a; 38 | return material; 39 | } 40 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/DepthOfField.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D colorTexture; 2 | uniform sampler2D blurTexture; 3 | uniform sampler2D depthTexture; 4 | uniform float focalDistance; 5 | 6 | varying vec2 v_textureCoordinates; 7 | 8 | vec4 toEye(vec2 uv, float depth) 9 | { 10 | vec2 xy = vec2((uv.x * 2.0 - 1.0), ((1.0 - uv.y) * 2.0 - 1.0)); 11 | vec4 posInCamera = czm_inverseProjection * vec4(xy, depth, 1.0); 12 | posInCamera = posInCamera / posInCamera.w; 13 | return posInCamera; 14 | } 15 | 16 | float computeDepthBlur(float depth) 17 | { 18 | float f; 19 | if (depth < focalDistance) 20 | { 21 | f = (focalDistance - depth) / (focalDistance - czm_currentFrustum.x); 22 | } 23 | else 24 | { 25 | f = (depth - focalDistance) / (czm_currentFrustum.y - focalDistance); 26 | f = pow(f, 0.1); 27 | } 28 | f *= f; 29 | f = clamp(f, 0.0, 1.0); 30 | return pow(f, 0.5); 31 | } 32 | 33 | void main(void) 34 | { 35 | float depth = czm_readDepth(depthTexture, v_textureCoordinates); 36 | vec4 posInCamera = toEye(v_textureCoordinates, depth); 37 | float d = computeDepthBlur(-posInCamera.z); 38 | gl_FragColor = mix(texture2D(colorTexture, v_textureCoordinates), texture2D(blurTexture, v_textureCoordinates), d); 39 | } 40 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/readNonPerspective.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Reads a value previously transformed with {@link czm_writeNonPerspective} 3 | * by dividing it by `w`, the value used in the perspective divide. 4 | * This function is intended to be called in a fragment shader to access a 5 | * `varying` that should not be subject to perspective interpolation. 6 | * For example, screen-space texture coordinates. The value should have been 7 | * previously written in the vertex shader with a call to 8 | * {@link czm_writeNonPerspective}. 9 | * 10 | * @name czm_readNonPerspective 11 | * @glslFunction 12 | * 13 | * @param {float|vec2|vec3|vec4} value The non-perspective value to be read. 14 | * @param {float} oneOverW One over the perspective divide value, `w`. Usually this is simply `gl_FragCoord.w`. 15 | * @returns {float|vec2|vec3|vec4} The usable value. 16 | */ 17 | float czm_readNonPerspective(float value, float oneOverW) { 18 | return value * oneOverW; 19 | } 20 | 21 | vec2 czm_readNonPerspective(vec2 value, float oneOverW) { 22 | return value * oneOverW; 23 | } 24 | 25 | vec3 czm_readNonPerspective(vec3 value, float oneOverW) { 26 | return value * oneOverW; 27 | } 28 | 29 | vec4 czm_readNonPerspective(vec4 value, float oneOverW) { 30 | return value * oneOverW; 31 | } 32 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/GaussianBlur1D.glsl: -------------------------------------------------------------------------------- 1 | #define SAMPLES 8 2 | 3 | uniform float delta; 4 | uniform float sigma; 5 | uniform float direction; // 0.0 for x direction, 1.0 for y direction 6 | 7 | uniform sampler2D colorTexture; 8 | 9 | #ifdef USE_STEP_SIZE 10 | uniform float stepSize; 11 | #else 12 | uniform vec2 step; 13 | #endif 14 | 15 | varying vec2 v_textureCoordinates; 16 | 17 | // Incremental Computation of the Gaussian: 18 | // https://developer.nvidia.com/gpugems/GPUGems3/gpugems3_ch40.html 19 | 20 | void main() 21 | { 22 | vec2 st = v_textureCoordinates; 23 | vec2 dir = vec2(1.0 - direction, direction); 24 | 25 | #ifdef USE_STEP_SIZE 26 | vec2 step = vec2(stepSize * (czm_pixelRatio / czm_viewport.zw)); 27 | #else 28 | vec2 step = step; 29 | #endif 30 | 31 | vec3 g; 32 | g.x = 1.0 / (sqrt(czm_twoPi) * sigma); 33 | g.y = exp((-0.5 * delta * delta) / (sigma * sigma)); 34 | g.z = g.y * g.y; 35 | 36 | vec4 result = texture2D(colorTexture, st) * g.x; 37 | for (int i = 1; i < SAMPLES; ++i) 38 | { 39 | g.xy *= g.yz; 40 | 41 | vec2 offset = float(i) * dir * step; 42 | result += texture2D(colorTexture, st - offset) * g.x; 43 | result += texture2D(colorTexture, st + offset) * g.x; 44 | } 45 | 46 | gl_FragColor = result; 47 | } 48 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/writeNonPerspective.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Transforms a value for non-perspective interpolation by multiplying 3 | * it by w, the value used in the perspective divide. This function is 4 | * intended to be called in a vertex shader to compute the value of a 5 | * `varying` that should not be subject to perspective interpolation. 6 | * For example, screen-space texture coordinates. The fragment shader 7 | * must call {@link czm_readNonPerspective} to retrieve the final 8 | * non-perspective value. 9 | * 10 | * @name czm_writeNonPerspective 11 | * @glslFunction 12 | * 13 | * @param {float|vec2|vec3|vec4} value The value to be interpolated without accounting for perspective. 14 | * @param {float} w The perspective divide value. Usually this is the computed `gl_Position.w`. 15 | * @returns {float|vec2|vec3|vec4} The transformed value, intended to be stored in a `varying` and read in the 16 | * fragment shader with {@link czm_readNonPerspective}. 17 | */ 18 | float czm_writeNonPerspective(float value, float w) { 19 | return value * w; 20 | } 21 | 22 | vec2 czm_writeNonPerspective(vec2 value, float w) { 23 | return value * w; 24 | } 25 | 26 | vec3 czm_writeNonPerspective(vec3 value, float w) { 27 | return value * w; 28 | } 29 | 30 | vec4 czm_writeNonPerspective(vec4 value, float w) { 31 | return value * w; 32 | } 33 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Structs/materialInput.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Used as input to every material's czm_getMaterial function. 3 | * 4 | * @name czm_materialInput 5 | * @glslStruct 6 | * 7 | * @property {float} s 1D texture coordinates. 8 | * @property {vec2} st 2D texture coordinates. 9 | * @property {vec3} str 3D texture coordinates. 10 | * @property {vec3} normalEC Unperturbed surface normal in eye coordinates. 11 | * @property {mat3} tangentToEyeMatrix Matrix for converting a tangent space normal to eye space. 12 | * @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. 13 | * @property {float} height The height of the terrain in meters above or below the WGS84 ellipsoid. Only available for globe materials. 14 | * @property {float} slope The slope of the terrain in radians. 0 is flat; pi/2 is vertical. Only available for globe materials. 15 | * @property {float} aspect The aspect of the terrain in radians. 0 is East, pi/2 is North, pi is West, 3pi/2 is South. Only available for globe materials. 16 | */ 17 | struct czm_materialInput 18 | { 19 | float s; 20 | vec2 st; 21 | vec3 str; 22 | vec3 normalEC; 23 | mat3 tangentToEyeMatrix; 24 | vec3 positionToEyeEC; 25 | float height; 26 | float slope; 27 | float aspect; 28 | }; 29 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/sphericalHarmonics.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Computes a color from the third order spherical harmonic coefficients and a normalized direction vector. 3 | *

4 | * The order of the coefficients is [L00, L1_1, L10, L11, L2_2, L2_1, L20, L21, L22]. 5 | *

6 | * 7 | * @name czm_sphericalHarmonics 8 | * @glslFunction 9 | * 10 | * @param {vec3} normal The normalized direction. 11 | * @param {vec3[9]} coefficients The third order spherical harmonic coefficients. 12 | * @returns {vec3} The color at the direction. 13 | * 14 | * @see https://graphics.stanford.edu/papers/envmap/envmap.pdf 15 | */ 16 | vec3 czm_sphericalHarmonics(vec3 normal, vec3 coefficients[9]) 17 | { 18 | vec3 L00 = coefficients[0]; 19 | vec3 L1_1 = coefficients[1]; 20 | vec3 L10 = coefficients[2]; 21 | vec3 L11 = coefficients[3]; 22 | vec3 L2_2 = coefficients[4]; 23 | vec3 L2_1 = coefficients[5]; 24 | vec3 L20 = coefficients[6]; 25 | vec3 L21 = coefficients[7]; 26 | vec3 L22 = coefficients[8]; 27 | 28 | float x = normal.x; 29 | float y = normal.y; 30 | float z = normal.z; 31 | 32 | return 33 | L00 34 | + L1_1 * y 35 | + L10 * z 36 | + L11 * x 37 | + L2_2 * (y * x) 38 | + L2_1 * (y * z) 39 | + L20 * (3.0 * z * z - 1.0) 40 | + L21 * (z * x) 41 | + L22 * (x * x - y * y); 42 | } 43 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/getSpecular.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Calculates the specular intensity of reflected light. 3 | * 4 | * @name czm_getSpecular 5 | * @glslFunction 6 | * 7 | * @param {vec3} lightDirectionEC Unit vector pointing to the light source in eye coordinates. 8 | * @param {vec3} toEyeEC Unit vector pointing to the eye position in eye coordinates. 9 | * @param {vec3} normalEC The surface normal in eye coordinates. 10 | * @param {float} shininess The sharpness of the specular reflection. Higher values create a smaller, more focused specular highlight. 11 | * 12 | * @returns {float} The intensity of the specular highlight. 13 | * 14 | * @see czm_phong 15 | * 16 | * @example 17 | * float diffuseIntensity = czm_getLambertDiffuse(lightDirectionEC, normalEC); 18 | * float specularIntensity = czm_getSpecular(lightDirectionEC, toEyeEC, normalEC, 200); 19 | * vec3 color = (diffuseColor * diffuseIntensity) + (specularColor * specularIntensity); 20 | */ 21 | float czm_getSpecular(vec3 lightDirectionEC, vec3 toEyeEC, vec3 normalEC, float shininess) 22 | { 23 | vec3 toReflectedLight = reflect(-lightDirectionEC, normalEC); 24 | float specular = max(dot(toReflectedLight, toEyeEC), 0.0); 25 | 26 | // pow has undefined behavior if both parameters <= 0. 27 | // Prevent this by making sure shininess is at least czm_epsilon2. 28 | return pow(specular, max(shininess, czm_epsilon2)); 29 | } 30 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/eyeToWindowCoordinates.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Transforms a position from eye to window coordinates. The transformation 3 | * from eye to clip coordinates is done using {@link czm_projection}. 4 | * The transform from normalized device coordinates to window coordinates is 5 | * done using {@link czm_viewportTransformation}, which assumes a depth range 6 | * of near = 0 and far = 1. 7 | *

8 | * This transform is useful when there is a need to manipulate window coordinates 9 | * in a vertex shader as done by {@link BillboardCollection}. 10 | * 11 | * @name czm_eyeToWindowCoordinates 12 | * @glslFunction 13 | * 14 | * @param {vec4} position The position in eye coordinates to transform. 15 | * 16 | * @returns {vec4} The transformed position in window coordinates. 17 | * 18 | * @see czm_modelToWindowCoordinates 19 | * @see czm_projection 20 | * @see czm_viewportTransformation 21 | * @see BillboardCollection 22 | * 23 | * @example 24 | * vec4 positionWC = czm_eyeToWindowCoordinates(positionEC); 25 | */ 26 | vec4 czm_eyeToWindowCoordinates(vec4 positionEC) 27 | { 28 | vec4 q = czm_projection * positionEC; // clip coordinates 29 | q.xyz /= q.w; // normalized device coordinates 30 | q.xyz = (czm_viewportTransformation * vec4(q.xyz, 1.0)).xyz; // window coordinates 31 | return q; 32 | } 33 | -------------------------------------------------------------------------------- /ShaderSource/EllipsoidVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 position; 2 | 3 | uniform vec3 u_radii; 4 | 5 | varying vec3 v_positionEC; 6 | 7 | void main() 8 | { 9 | // In the vertex data, the cube goes from (-1.0, -1.0, -1.0) to (1.0, 1.0, 1.0) in model coordinates. 10 | // Scale to consider the radii. We could also do this once on the CPU when using the BoxGeometry, 11 | // but doing it here allows us to change the radii without rewriting the vertex data, and 12 | // allows all ellipsoids to reuse the same vertex data. 13 | vec4 p = vec4(u_radii * position, 1.0); 14 | 15 | v_positionEC = (czm_modelView * p).xyz; // position in eye coordinates 16 | gl_Position = czm_modelViewProjection * p; // position in clip coordinates 17 | 18 | // With multi-frustum, when the ellipsoid primitive is positioned on the intersection of two frustums 19 | // and close to terrain, the terrain (writes depth) in the closest frustum can overwrite part of the 20 | // ellipsoid (does not write depth) that was rendered in the farther frustum. 21 | // 22 | // Here, we clamp the depth in the vertex shader to avoid being overwritten; however, this creates 23 | // artifacts since some fragments can be alpha blended twice. This is solved by only rendering 24 | // the ellipsoid in the closest frustum to the viewer. 25 | gl_Position.z = clamp(gl_Position.z, czm_depthRange.near, czm_depthRange.far); 26 | 27 | czm_vertexLogDepth(); 28 | } 29 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/fog.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Gets the color with fog at a distance from the camera. 3 | * 4 | * @name czm_fog 5 | * @glslFunction 6 | * 7 | * @param {float} distanceToCamera The distance to the camera in meters. 8 | * @param {vec3} color The original color. 9 | * @param {vec3} fogColor The color of the fog. 10 | * 11 | * @returns {vec3} The color adjusted for fog at the distance from the camera. 12 | */ 13 | vec3 czm_fog(float distanceToCamera, vec3 color, vec3 fogColor) 14 | { 15 | float scalar = distanceToCamera * czm_fogDensity; 16 | float fog = 1.0 - exp(-(scalar * scalar)); 17 | return mix(color, fogColor, fog); 18 | } 19 | 20 | /** 21 | * Gets the color with fog at a distance from the camera. 22 | * 23 | * @name czm_fog 24 | * @glslFunction 25 | * 26 | * @param {float} distanceToCamera The distance to the camera in meters. 27 | * @param {vec3} color The original color. 28 | * @param {vec3} fogColor The color of the fog. 29 | * @param {float} fogModifierConstant A constant to modify the appearance of fog. 30 | * 31 | * @returns {vec3} The color adjusted for fog at the distance from the camera. 32 | */ 33 | vec3 czm_fog(float distanceToCamera, vec3 color, vec3 fogColor, float fogModifierConstant) 34 | { 35 | float scalar = distanceToCamera * czm_fogDensity; 36 | float fog = 1.0 - exp(-((fogModifierConstant * scalar + fogModifierConstant) * (scalar * (1.0 + fogModifierConstant)))); 37 | return mix(color, fogColor, fog); 38 | } 39 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/transpose.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Returns the transpose of the matrix. The input matrix can be 3 | * a mat2, mat3, or mat4. 4 | * 5 | * @name czm_transpose 6 | * @glslFunction 7 | * 8 | * @param {} matrix The matrix to transpose. 9 | * 10 | * @returns {} The transposed matrix. 11 | * 12 | * @example 13 | * // GLSL declarations 14 | * mat2 czm_transpose(mat2 matrix); 15 | * mat3 czm_transpose(mat3 matrix); 16 | * mat4 czm_transpose(mat4 matrix); 17 | * 18 | * // Transpose a 3x3 rotation matrix to find its inverse. 19 | * mat3 eastNorthUpToEye = czm_eastNorthUpToEyeCoordinates( 20 | * positionMC, normalEC); 21 | * mat3 eyeToEastNorthUp = czm_transpose(eastNorthUpToEye); 22 | */ 23 | mat2 czm_transpose(mat2 matrix) 24 | { 25 | return mat2( 26 | matrix[0][0], matrix[1][0], 27 | matrix[0][1], matrix[1][1]); 28 | } 29 | 30 | mat3 czm_transpose(mat3 matrix) 31 | { 32 | return mat3( 33 | matrix[0][0], matrix[1][0], matrix[2][0], 34 | matrix[0][1], matrix[1][1], matrix[2][1], 35 | matrix[0][2], matrix[1][2], matrix[2][2]); 36 | } 37 | 38 | mat4 czm_transpose(mat4 matrix) 39 | { 40 | return mat4( 41 | matrix[0][0], matrix[1][0], matrix[2][0], matrix[3][0], 42 | matrix[0][1], matrix[1][1], matrix[2][1], matrix[3][1], 43 | matrix[0][2], matrix[1][2], matrix[2][2], matrix[3][2], 44 | matrix[0][3], matrix[1][3], matrix[2][3], matrix[3][3]); 45 | } 46 | -------------------------------------------------------------------------------- /ShaderSource/PointPrimitiveCollectionFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec4 v_color; 2 | varying vec4 v_outlineColor; 3 | varying float v_innerPercent; 4 | varying float v_pixelDistance; 5 | varying vec4 v_pickColor; 6 | 7 | void main() 8 | { 9 | // The distance in UV space from this fragment to the center of the point, at most 0.5. 10 | float distanceToCenter = length(gl_PointCoord - vec2(0.5)); 11 | // The max distance stops one pixel shy of the edge to leave space for anti-aliasing. 12 | float maxDistance = max(0.0, 0.5 - v_pixelDistance); 13 | float wholeAlpha = 1.0 - smoothstep(maxDistance, 0.5, distanceToCenter); 14 | float innerAlpha = 1.0 - smoothstep(maxDistance * v_innerPercent, 0.5 * v_innerPercent, distanceToCenter); 15 | 16 | vec4 color = mix(v_outlineColor, v_color, innerAlpha); 17 | color.a *= wholeAlpha; 18 | 19 | // Fully transparent parts of the billboard are not pickable. 20 | #if !defined(OPAQUE) && !defined(TRANSLUCENT) 21 | if (color.a < 0.005) // matches 0/255 and 1/255 22 | { 23 | discard; 24 | } 25 | #else 26 | // The billboard is rendered twice. The opaque pass discards translucent fragments 27 | // and the translucent pass discards opaque fragments. 28 | #ifdef OPAQUE 29 | if (color.a < 0.995) // matches < 254/255 30 | { 31 | discard; 32 | } 33 | #else 34 | if (color.a >= 0.995) // matches 254/255 and 255/255 35 | { 36 | discard; 37 | } 38 | #endif 39 | #endif 40 | 41 | gl_FragColor = czm_gammaCorrect(color); 42 | czm_writeLogDepth(); 43 | } 44 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/pbrMetallicRoughnessMaterial.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Compute parameters for physically based rendering using the 3 | * metallic/roughness workflow. All inputs are linear; sRGB texture values must 4 | * be decoded beforehand 5 | * 6 | * @name czm_pbrMetallicRoughnessMaterial 7 | * @glslFunction 8 | * 9 | * @param {vec3} baseColor For dielectrics, this is the base color. For metals, this is the f0 value (reflectance at normal incidence) 10 | * @param {float} metallic 0.0 indicates dielectric. 1.0 indicates metal. Values in between are allowed (e.g. to model rust or dirt); 11 | * @param {float} roughness A value between 0.0 and 1.0 12 | * @return {czm_pbrParameters} parameters to pass into {@link czm_pbrLighting} 13 | */ 14 | czm_pbrParameters czm_pbrMetallicRoughnessMaterial( 15 | vec3 baseColor, 16 | float metallic, 17 | float roughness 18 | ) { 19 | czm_pbrParameters results; 20 | 21 | // roughness is authored as perceptual roughness 22 | // square it to get material roughness 23 | roughness = clamp(roughness, 0.0, 1.0); 24 | results.roughness = roughness * roughness; 25 | 26 | // dielectrics us f0 = 0.04, metals use albedo as f0 27 | metallic = clamp(metallic, 0.0, 1.0); 28 | const vec3 REFLECTANCE_DIELECTRIC = vec3(0.04); 29 | vec3 f0 = mix(REFLECTANCE_DIELECTRIC, baseColor, metallic); 30 | results.f0 = f0; 31 | 32 | // diffuse only applies to dielectrics. 33 | results.diffuseColor = baseColor * (1.0 - f0) * (1.0 - metallic); 34 | 35 | return results; 36 | } 37 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/getWaterNoise.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * @private 3 | */ 4 | vec4 czm_getWaterNoise(sampler2D normalMap, vec2 uv, float time, float angleInRadians) 5 | { 6 | float cosAngle = cos(angleInRadians); 7 | float sinAngle = sin(angleInRadians); 8 | 9 | // time dependent sampling directions 10 | vec2 s0 = vec2(1.0/17.0, 0.0); 11 | vec2 s1 = vec2(-1.0/29.0, 0.0); 12 | vec2 s2 = vec2(1.0/101.0, 1.0/59.0); 13 | vec2 s3 = vec2(-1.0/109.0, -1.0/57.0); 14 | 15 | // rotate sampling direction by specified angle 16 | s0 = vec2((cosAngle * s0.x) - (sinAngle * s0.y), (sinAngle * s0.x) + (cosAngle * s0.y)); 17 | s1 = vec2((cosAngle * s1.x) - (sinAngle * s1.y), (sinAngle * s1.x) + (cosAngle * s1.y)); 18 | s2 = vec2((cosAngle * s2.x) - (sinAngle * s2.y), (sinAngle * s2.x) + (cosAngle * s2.y)); 19 | s3 = vec2((cosAngle * s3.x) - (sinAngle * s3.y), (sinAngle * s3.x) + (cosAngle * s3.y)); 20 | 21 | vec2 uv0 = (uv/103.0) + (time * s0); 22 | vec2 uv1 = uv/107.0 + (time * s1) + vec2(0.23); 23 | vec2 uv2 = uv/vec2(897.0, 983.0) + (time * s2) + vec2(0.51); 24 | vec2 uv3 = uv/vec2(991.0, 877.0) + (time * s3) + vec2(0.71); 25 | 26 | uv0 = fract(uv0); 27 | uv1 = fract(uv1); 28 | uv2 = fract(uv2); 29 | uv3 = fract(uv3); 30 | vec4 noise = (texture2D(normalMap, uv0)) + 31 | (texture2D(normalMap, uv1)) + 32 | (texture2D(normalMap, uv2)) + 33 | (texture2D(normalMap, uv3)); 34 | 35 | // average and scale to between -1 and 1 36 | return ((noise / 4.0) - 0.5) * 2.0; 37 | } 38 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/equalsEpsilon.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Compares left and right componentwise. Returns true 3 | * if they are within epsilon and false otherwise. The inputs 4 | * left and right can be floats, vec2s, 5 | * vec3s, or vec4s. 6 | * 7 | * @name czm_equalsEpsilon 8 | * @glslFunction 9 | * 10 | * @param {} left The first vector. 11 | * @param {} right The second vector. 12 | * @param {float} epsilon The epsilon to use for equality testing. 13 | * @returns {bool} true if the components are within epsilon and false otherwise. 14 | * 15 | * @example 16 | * // GLSL declarations 17 | * bool czm_equalsEpsilon(float left, float right, float epsilon); 18 | * bool czm_equalsEpsilon(vec2 left, vec2 right, float epsilon); 19 | * bool czm_equalsEpsilon(vec3 left, vec3 right, float epsilon); 20 | * bool czm_equalsEpsilon(vec4 left, vec4 right, float epsilon); 21 | */ 22 | bool czm_equalsEpsilon(vec4 left, vec4 right, float epsilon) { 23 | return all(lessThanEqual(abs(left - right), vec4(epsilon))); 24 | } 25 | 26 | bool czm_equalsEpsilon(vec3 left, vec3 right, float epsilon) { 27 | return all(lessThanEqual(abs(left - right), vec3(epsilon))); 28 | } 29 | 30 | bool czm_equalsEpsilon(vec2 left, vec2 right, float epsilon) { 31 | return all(lessThanEqual(abs(left - right), vec2(epsilon))); 32 | } 33 | 34 | bool czm_equalsEpsilon(float left, float right, float epsilon) { 35 | return (abs(left - right) <= epsilon); 36 | } 37 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/translateRelativeToEye.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Translates a position (or any vec3) that was encoded with {@link EncodedCartesian3}, 3 | * and then provided to the shader as separate high and low bits to 4 | * be relative to the eye. As shown in the example, the position can then be transformed in eye 5 | * or clip coordinates using {@link czm_modelViewRelativeToEye} or {@link czm_modelViewProjectionRelativeToEye}, 6 | * respectively. 7 | *

8 | * This technique, called GPU RTE, eliminates jittering artifacts when using large coordinates as 9 | * described in {@link http://help.agi.com/AGIComponents/html/BlogPrecisionsPrecisions.htm|Precisions, Precisions}. 10 | *

11 | * 12 | * @name czm_translateRelativeToEye 13 | * @glslFunction 14 | * 15 | * @param {vec3} high The position's high bits. 16 | * @param {vec3} low The position's low bits. 17 | * @returns {vec3} The position translated to be relative to the camera's position. 18 | * 19 | * @example 20 | * attribute vec3 positionHigh; 21 | * attribute vec3 positionLow; 22 | * 23 | * void main() 24 | * { 25 | * vec4 p = czm_translateRelativeToEye(positionHigh, positionLow); 26 | * gl_Position = czm_modelViewProjectionRelativeToEye * p; 27 | * } 28 | * 29 | * @see czm_modelViewRelativeToEye 30 | * @see czm_modelViewProjectionRelativeToEye 31 | * @see czm_computePosition 32 | * @see EncodedCartesian3 33 | */ 34 | vec4 czm_translateRelativeToEye(vec3 high, vec3 low) 35 | { 36 | vec3 highDifference = high - czm_encodedCameraPositionMCHigh; 37 | vec3 lowDifference = low - czm_encodedCameraPositionMCLow; 38 | 39 | return vec4(highDifference + lowDifference, 1.0); 40 | } 41 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/modelToWindowCoordinates.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Transforms a position from model to window coordinates. The transformation 3 | * from model to clip coordinates is done using {@link czm_modelViewProjection}. 4 | * The transform from normalized device coordinates to window coordinates is 5 | * done using {@link czm_viewportTransformation}, which assumes a depth range 6 | * of near = 0 and far = 1. 7 | *

8 | * This transform is useful when there is a need to manipulate window coordinates 9 | * in a vertex shader as done by {@link BillboardCollection}. 10 | *

11 | * This function should not be confused with {@link czm_viewportOrthographic}, 12 | * which is an orthographic projection matrix that transforms from window 13 | * coordinates to clip coordinates. 14 | * 15 | * @name czm_modelToWindowCoordinates 16 | * @glslFunction 17 | * 18 | * @param {vec4} position The position in model coordinates to transform. 19 | * 20 | * @returns {vec4} The transformed position in window coordinates. 21 | * 22 | * @see czm_eyeToWindowCoordinates 23 | * @see czm_modelViewProjection 24 | * @see czm_viewportTransformation 25 | * @see czm_viewportOrthographic 26 | * @see BillboardCollection 27 | * 28 | * @example 29 | * vec4 positionWC = czm_modelToWindowCoordinates(positionMC); 30 | */ 31 | vec4 czm_modelToWindowCoordinates(vec4 position) 32 | { 33 | vec4 q = czm_modelViewProjection * position; // clip coordinates 34 | q.xyz /= q.w; // normalized device coordinates 35 | q.xyz = (czm_viewportTransformation * vec4(q.xyz, 1.0)).xyz; // window coordinates 36 | return q; 37 | } 38 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/antialias.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Procedural anti-aliasing by blurring two colors that meet at a sharp edge. 3 | * 4 | * @name czm_antialias 5 | * @glslFunction 6 | * 7 | * @param {vec4} color1 The color on one side of the edge. 8 | * @param {vec4} color2 The color on the other side of the edge. 9 | * @param {vec4} currentcolor The current color, either color1 or color2. 10 | * @param {float} dist The distance to the edge in texture coordinates. 11 | * @param {float} [fuzzFactor=0.1] Controls the blurriness between the two colors. 12 | * @returns {vec4} The anti-aliased color. 13 | * 14 | * @example 15 | * // GLSL declarations 16 | * vec4 czm_antialias(vec4 color1, vec4 color2, vec4 currentColor, float dist, float fuzzFactor); 17 | * vec4 czm_antialias(vec4 color1, vec4 color2, vec4 currentColor, float dist); 18 | * 19 | * // get the color for a material that has a sharp edge at the line y = 0.5 in texture space 20 | * float dist = abs(textureCoordinates.t - 0.5); 21 | * vec4 currentColor = mix(bottomColor, topColor, step(0.5, textureCoordinates.t)); 22 | * vec4 color = czm_antialias(bottomColor, topColor, currentColor, dist, 0.1); 23 | */ 24 | vec4 czm_antialias(vec4 color1, vec4 color2, vec4 currentColor, float dist, float fuzzFactor) 25 | { 26 | float val1 = clamp(dist / fuzzFactor, 0.0, 1.0); 27 | float val2 = clamp((dist - 0.5) / fuzzFactor, 0.0, 1.0); 28 | val1 = val1 * (1.0 - val2); 29 | val1 = val1 * val1 * (3.0 - (2.0 * val1)); 30 | val1 = pow(val1, 0.5); //makes the transition nicer 31 | 32 | vec4 midColor = (color1 + color2) * 0.5; 33 | return mix(midColor, currentColor, val1); 34 | } 35 | 36 | vec4 czm_antialias(vec4 color1, vec4 color2, vec4 currentColor, float dist) 37 | { 38 | return czm_antialias(color1, color2, currentColor, dist, 0.1); 39 | } 40 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/eastNorthUpToEyeCoordinates.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Computes a 3x3 rotation matrix that transforms vectors from an ellipsoid's east-north-up coordinate system 3 | * to eye coordinates. In east-north-up coordinates, x points east, y points north, and z points along the 4 | * surface normal. East-north-up can be used as an ellipsoid's tangent space for operations such as bump mapping. 5 | *

6 | * The ellipsoid is assumed to be centered at the model coordinate's origin. 7 | * 8 | * @name czm_eastNorthUpToEyeCoordinates 9 | * @glslFunction 10 | * 11 | * @param {vec3} positionMC The position on the ellipsoid in model coordinates. 12 | * @param {vec3} normalEC The normalized ellipsoid surface normal, at positionMC, in eye coordinates. 13 | * 14 | * @returns {mat3} A 3x3 rotation matrix that transforms vectors from the east-north-up coordinate system to eye coordinates. 15 | * 16 | * @example 17 | * // Transform a vector defined in the east-north-up coordinate 18 | * // system, (0, 0, 1) which is the surface normal, to eye 19 | * // coordinates. 20 | * mat3 m = czm_eastNorthUpToEyeCoordinates(positionMC, normalEC); 21 | * vec3 normalEC = m * vec3(0.0, 0.0, 1.0); 22 | */ 23 | mat3 czm_eastNorthUpToEyeCoordinates(vec3 positionMC, vec3 normalEC) 24 | { 25 | vec3 tangentMC = normalize(vec3(-positionMC.y, positionMC.x, 0.0)); // normalized surface tangent in model coordinates 26 | vec3 tangentEC = normalize(czm_normal3D * tangentMC); // normalized surface tangent in eye coordiantes 27 | vec3 bitangentEC = normalize(cross(normalEC, tangentEC)); // normalized surface bitangent in eye coordinates 28 | 29 | return mat3( 30 | tangentEC.x, tangentEC.y, tangentEC.z, 31 | bitangentEC.x, bitangentEC.y, bitangentEC.z, 32 | normalEC.x, normalEC.y, normalEC.z); 33 | } 34 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/depthClamp.glsl: -------------------------------------------------------------------------------- 1 | // emulated noperspective 2 | #if defined(GL_EXT_frag_depth) && !defined(LOG_DEPTH) 3 | varying float v_WindowZ; 4 | #endif 5 | 6 | /** 7 | * Emulates GL_DEPTH_CLAMP, which is not available in WebGL 1 or 2. 8 | * GL_DEPTH_CLAMP clamps geometry that is outside the near and far planes, 9 | * capping the shadow volume. More information here: 10 | * https://www.khronos.org/registry/OpenGL/extensions/ARB/ARB_depth_clamp.txt. 11 | * 12 | * When GL_EXT_frag_depth is available we emulate GL_DEPTH_CLAMP by ensuring 13 | * no geometry gets clipped by setting the clip space z value to 0.0 and then 14 | * sending the unaltered screen space z value (using emulated noperspective 15 | * interpolation) to the frag shader where it is clamped to [0,1] and then 16 | * written with gl_FragDepth (see czm_writeDepthClamp). This technique is based on: 17 | * https://stackoverflow.com/questions/5960757/how-to-emulate-gl-depth-clamp-nv. 18 | * 19 | * When GL_EXT_frag_depth is not available, which is the case on some mobile 20 | * devices, we must attempt to fix this only in the vertex shader. 21 | * The approach is to clamp the z value to the far plane, which closes the 22 | * shadow volume but also distorts the geometry, so there can still be artifacts 23 | * on frustum seams. 24 | * 25 | * @name czm_depthClamp 26 | * @glslFunction 27 | * 28 | * @param {vec4} coords The vertex in clip coordinates. 29 | * @returns {vec4} The modified vertex. 30 | * 31 | * @example 32 | * gl_Position = czm_depthClamp(czm_modelViewProjection * vec4(position, 1.0)); 33 | * 34 | * @see czm_writeDepthClamp 35 | */ 36 | vec4 czm_depthClamp(vec4 coords) 37 | { 38 | #ifndef LOG_DEPTH 39 | #ifdef GL_EXT_frag_depth 40 | v_WindowZ = (0.5 * (coords.z / coords.w) + 0.5) * coords.w; 41 | coords.z = 0.0; 42 | #else 43 | coords.z = min(coords.z, coords.w); 44 | #endif 45 | #endif 46 | return coords; 47 | } 48 | -------------------------------------------------------------------------------- /ShaderSource/PolylineShadowVolumeMorphFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec3 v_forwardDirectionEC; 2 | varying vec3 v_texcoordNormalizationAndHalfWidth; 3 | varying float v_batchId; 4 | 5 | #ifdef PER_INSTANCE_COLOR 6 | varying vec4 v_color; 7 | #else 8 | varying vec2 v_alignedPlaneDistances; 9 | varying float v_texcoordT; 10 | #endif 11 | 12 | float rayPlaneDistanceUnsafe(vec3 origin, vec3 direction, vec3 planeNormal, float planeDistance) { 13 | // We don't expect the ray to ever be parallel to the plane 14 | return (-planeDistance - dot(planeNormal, origin)) / dot(planeNormal, direction); 15 | } 16 | 17 | void main(void) 18 | { 19 | vec4 eyeCoordinate = gl_FragCoord; 20 | eyeCoordinate /= eyeCoordinate.w; 21 | 22 | #ifdef PER_INSTANCE_COLOR 23 | gl_FragColor = czm_gammaCorrect(v_color); 24 | #else // PER_INSTANCE_COLOR 25 | // Use distances for planes aligned with segment to prevent skew in dashing 26 | float distanceFromStart = rayPlaneDistanceUnsafe(eyeCoordinate.xyz, -v_forwardDirectionEC, v_forwardDirectionEC.xyz, v_alignedPlaneDistances.x); 27 | float distanceFromEnd = rayPlaneDistanceUnsafe(eyeCoordinate.xyz, v_forwardDirectionEC, -v_forwardDirectionEC.xyz, v_alignedPlaneDistances.y); 28 | 29 | // Clamp - distance to aligned planes may be negative due to mitering 30 | distanceFromStart = max(0.0, distanceFromStart); 31 | distanceFromEnd = max(0.0, distanceFromEnd); 32 | 33 | float s = distanceFromStart / (distanceFromStart + distanceFromEnd); 34 | s = (s * v_texcoordNormalizationAndHalfWidth.x) + v_texcoordNormalizationAndHalfWidth.y; 35 | 36 | czm_materialInput materialInput; 37 | 38 | materialInput.s = s; 39 | materialInput.st = vec2(s, v_texcoordT); 40 | materialInput.str = vec3(s, v_texcoordT, 0.0); 41 | 42 | czm_material material = czm_getMaterial(materialInput); 43 | gl_FragColor = vec4(material.diffuse + material.emission, material.alpha); 44 | #endif // PER_INSTANCE_COLOR 45 | } 46 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/EdgeDetection.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D depthTexture; 2 | uniform float length; 3 | uniform vec4 color; 4 | 5 | varying vec2 v_textureCoordinates; 6 | 7 | void main(void) 8 | { 9 | float directions[3]; 10 | directions[0] = -1.0; 11 | directions[1] = 0.0; 12 | directions[2] = 1.0; 13 | 14 | float scalars[3]; 15 | scalars[0] = 3.0; 16 | scalars[1] = 10.0; 17 | scalars[2] = 3.0; 18 | 19 | float padx = czm_pixelRatio / czm_viewport.z; 20 | float pady = czm_pixelRatio / czm_viewport.w; 21 | 22 | #ifdef CZM_SELECTED_FEATURE 23 | bool selected = false; 24 | for (int i = 0; i < 3; ++i) 25 | { 26 | float dir = directions[i]; 27 | selected = selected || czm_selected(vec2(-padx, dir * pady)); 28 | selected = selected || czm_selected(vec2(padx, dir * pady)); 29 | selected = selected || czm_selected(vec2(dir * padx, -pady)); 30 | selected = selected || czm_selected(vec2(dir * padx, pady)); 31 | if (selected) 32 | { 33 | break; 34 | } 35 | } 36 | if (!selected) 37 | { 38 | gl_FragColor = vec4(color.rgb, 0.0); 39 | return; 40 | } 41 | #endif 42 | 43 | float horizEdge = 0.0; 44 | float vertEdge = 0.0; 45 | 46 | for (int i = 0; i < 3; ++i) 47 | { 48 | float dir = directions[i]; 49 | float scale = scalars[i]; 50 | 51 | horizEdge -= texture2D(depthTexture, v_textureCoordinates + vec2(-padx, dir * pady)).x * scale; 52 | horizEdge += texture2D(depthTexture, v_textureCoordinates + vec2(padx, dir * pady)).x * scale; 53 | 54 | vertEdge -= texture2D(depthTexture, v_textureCoordinates + vec2(dir * padx, -pady)).x * scale; 55 | vertEdge += texture2D(depthTexture, v_textureCoordinates + vec2(dir * padx, pady)).x * scale; 56 | } 57 | 58 | float len = sqrt(horizEdge * horizEdge + vertEdge * vertEdge); 59 | gl_FragColor = vec4(color.rgb, len > length ? color.a : 0.0); 60 | } 61 | -------------------------------------------------------------------------------- /ShaderSource/Vector3DTileClampedPolylinesFS.glsl: -------------------------------------------------------------------------------- 1 | #ifdef GL_EXT_frag_depth 2 | #extension GL_EXT_frag_depth : enable 3 | #endif 4 | 5 | varying vec4 v_startPlaneEC; 6 | varying vec4 v_endPlaneEC; 7 | varying vec4 v_rightPlaneEC; 8 | varying float v_halfWidth; 9 | varying vec3 v_volumeUpEC; 10 | 11 | uniform vec4 u_highlightColor; 12 | void main() 13 | { 14 | float logDepthOrDepth = czm_branchFreeTernary(czm_sceneMode == czm_sceneMode2D, gl_FragCoord.z, czm_unpackDepth(texture2D(czm_globeDepthTexture, gl_FragCoord.xy / czm_viewport.zw))); 15 | 16 | // Discard for sky 17 | if (logDepthOrDepth == 0.0) { 18 | #ifdef DEBUG_SHOW_VOLUME 19 | gl_FragColor = vec4(0.0, 0.0, 1.0, 0.5); 20 | return; 21 | #else // DEBUG_SHOW_VOLUME 22 | discard; 23 | #endif // DEBUG_SHOW_VOLUME 24 | } 25 | 26 | vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, logDepthOrDepth); 27 | eyeCoordinate /= eyeCoordinate.w; 28 | 29 | float halfMaxWidth = v_halfWidth * czm_metersPerPixel(eyeCoordinate); 30 | 31 | // Expand halfMaxWidth if direction to camera is almost perpendicular with the volume's up direction 32 | halfMaxWidth += halfMaxWidth * (1.0 - dot(-normalize(eyeCoordinate.xyz), v_volumeUpEC)); 33 | 34 | // Check distance of the eye coordinate against the right-facing plane 35 | float widthwiseDistance = czm_planeDistance(v_rightPlaneEC, eyeCoordinate.xyz); 36 | 37 | // Check eye coordinate against the mitering planes 38 | float distanceFromStart = czm_planeDistance(v_startPlaneEC, eyeCoordinate.xyz); 39 | float distanceFromEnd = czm_planeDistance(v_endPlaneEC, eyeCoordinate.xyz); 40 | 41 | if (abs(widthwiseDistance) > halfMaxWidth || distanceFromStart < 0.0 || distanceFromEnd < 0.0) { 42 | #ifdef DEBUG_SHOW_VOLUME 43 | gl_FragColor = vec4(logDepthOrDepth, 0.0, 0.0, 0.5); 44 | return; 45 | #else // DEBUG_SHOW_VOLUME 46 | discard; 47 | #endif // DEBUG_SHOW_VOLUME 48 | } 49 | gl_FragColor = u_highlightColor; 50 | 51 | czm_writeDepthClamp(); 52 | } 53 | -------------------------------------------------------------------------------- /Docs/Builtin/Structs/material.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ``` glsl 4 | /** 5 | * Holds material information that can be used for lighting. Returned by all czm_getMaterial functions. 6 | * 7 | * @name czm_material 8 | * @glslStruct 9 | * 10 | * @property {vec3} diffuse Incoming light that scatters evenly in all directions. 11 | * @property {float} specular Intensity of incoming light reflecting in a single direction. 12 | * @property {float} shininess The sharpness of the specular reflection. Higher values create a smaller, more focused specular highlight. 13 | * @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. 14 | * @property {vec3} emission Light emitted by the material equally in all directions. The default is vec3(0.0), which emits no light. 15 | * @property {float} alpha Opacity of this material. 0.0 is completely transparent; 1.0 is completely opaque. 16 | */ 17 | struct czm_material 18 | { 19 | vec3 diffuse; 20 | float specular; 21 | float shininess; 22 | vec3 normal; 23 | vec3 emission; 24 | float alpha; 25 | }; 26 | ``` 27 | 28 | # 文档 29 | 30 | 材质信息,一般使用 `czm_getMaterial` 函数来创建。 31 | 32 | ## 1. 成员 `diffuse` 33 | 34 | ### 类型 35 | 36 | `vec3` 37 | 38 | ### 含义 39 | 40 | 漫反射入射光。 41 | 42 | 43 | 44 | ## 2. 成员 `specular` 45 | 46 | ### 类型 47 | 48 | `float` 49 | 50 | ### 含义 51 | 52 | 入射光强度。 53 | 54 | 55 | 56 | ## 3. 成员 `shininess` 57 | 58 | ### 类型 59 | 60 | `float` 61 | 62 | ### 含义 63 | 64 | 镜面反射强度。高值会产生更明显的镜面高光效果。 65 | 66 | 67 | 68 | ## 4. 成员 `normal` 69 | 70 | ### 类型 71 | 72 | `vec3` 73 | 74 | ### 含义 75 | 76 | 观察坐标系(eye-coordinate)中的法向量。 77 | 78 | 79 | 80 | ## 5. 成员 `emission` 81 | 82 | ### 类型 83 | 84 | `vec3` 85 | 86 | ### 含义 87 | 88 | 自发光色。默认值是 `vec3(0.0)`,即不发光。 89 | 90 | 91 | 92 | ## 6. 成员 `alpha` 93 | 94 | ### 类型 95 | 96 | `float` 97 | 98 | ### 含义 99 | 100 | 材质的透明度。0.0 是完全透明,1.0 是完全不透明。 101 | 102 | 103 | 104 | 105 | 106 | # 参考 107 | 108 | - `czm_getMaterial` 函数 -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/metersPerPixel.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Computes the size of a pixel in meters at a distance from the eye. 3 | *

4 | * Use this version when passing in a custom pixel ratio. For example, passing in 1.0 will return meters per native device pixel. 5 | *

6 | * @name czm_metersPerPixel 7 | * @glslFunction 8 | * 9 | * @param {vec3} positionEC The position to get the meters per pixel in eye coordinates. 10 | * @param {float} pixelRatio The scaling factor from pixel space to coordinate space 11 | * 12 | * @returns {float} The meters per pixel at positionEC. 13 | */ 14 | float czm_metersPerPixel(vec4 positionEC, float pixelRatio) 15 | { 16 | float width = czm_viewport.z; 17 | float height = czm_viewport.w; 18 | float pixelWidth; 19 | float pixelHeight; 20 | 21 | float top = czm_frustumPlanes.x; 22 | float bottom = czm_frustumPlanes.y; 23 | float left = czm_frustumPlanes.z; 24 | float right = czm_frustumPlanes.w; 25 | 26 | if (czm_sceneMode == czm_sceneMode2D || czm_orthographicIn3D == 1.0) 27 | { 28 | float frustumWidth = right - left; 29 | float frustumHeight = top - bottom; 30 | pixelWidth = frustumWidth / width; 31 | pixelHeight = frustumHeight / height; 32 | } 33 | else 34 | { 35 | float distanceToPixel = -positionEC.z; 36 | float inverseNear = 1.0 / czm_currentFrustum.x; 37 | float tanTheta = top * inverseNear; 38 | pixelHeight = 2.0 * distanceToPixel * tanTheta / height; 39 | tanTheta = right * inverseNear; 40 | pixelWidth = 2.0 * distanceToPixel * tanTheta / width; 41 | } 42 | 43 | return max(pixelWidth, pixelHeight) * pixelRatio; 44 | } 45 | 46 | /** 47 | * Computes the size of a pixel in meters at a distance from the eye. 48 | *

49 | * Use this version when scaling by pixel ratio. 50 | *

51 | * @name czm_metersPerPixel 52 | * @glslFunction 53 | * 54 | * @param {vec3} positionEC The position to get the meters per pixel in eye coordinates. 55 | * 56 | * @returns {float} The meters per pixel at positionEC. 57 | */ 58 | float czm_metersPerPixel(vec4 positionEC) 59 | { 60 | return czm_metersPerPixel(positionEC, czm_pixelRatio); 61 | } 62 | -------------------------------------------------------------------------------- /ShaderSource/Materials/PolylineArrowMaterial.glsl: -------------------------------------------------------------------------------- 1 | #ifdef GL_OES_standard_derivatives 2 | #extension GL_OES_standard_derivatives : enable 3 | #endif 4 | 5 | uniform vec4 color; 6 | 7 | float getPointOnLine(vec2 p0, vec2 p1, float x) 8 | { 9 | float slope = (p0.y - p1.y) / (p0.x - p1.x); 10 | return slope * (x - p0.x) + p0.y; 11 | } 12 | 13 | czm_material czm_getMaterial(czm_materialInput materialInput) 14 | { 15 | czm_material material = czm_getDefaultMaterial(materialInput); 16 | 17 | vec2 st = materialInput.st; 18 | 19 | #ifdef GL_OES_standard_derivatives 20 | float base = 1.0 - abs(fwidth(st.s)) * 10.0 * czm_pixelRatio; 21 | #else 22 | float base = 0.975; // 2.5% of the line will be the arrow head 23 | #endif 24 | 25 | vec2 center = vec2(1.0, 0.5); 26 | float ptOnUpperLine = getPointOnLine(vec2(base, 1.0), center, st.s); 27 | float ptOnLowerLine = getPointOnLine(vec2(base, 0.0), center, st.s); 28 | 29 | float halfWidth = 0.15; 30 | float s = step(0.5 - halfWidth, st.t); 31 | s *= 1.0 - step(0.5 + halfWidth, st.t); 32 | s *= 1.0 - step(base, st.s); 33 | 34 | float t = step(base, materialInput.st.s); 35 | t *= 1.0 - step(ptOnUpperLine, st.t); 36 | t *= step(ptOnLowerLine, st.t); 37 | 38 | // Find the distance from the closest separator (region between two colors) 39 | float dist; 40 | if (st.s < base) 41 | { 42 | float d1 = abs(st.t - (0.5 - halfWidth)); 43 | float d2 = abs(st.t - (0.5 + halfWidth)); 44 | dist = min(d1, d2); 45 | } 46 | else 47 | { 48 | float d1 = czm_infinity; 49 | if (st.t < 0.5 - halfWidth && st.t > 0.5 + halfWidth) 50 | { 51 | d1 = abs(st.s - base); 52 | } 53 | float d2 = abs(st.t - ptOnUpperLine); 54 | float d3 = abs(st.t - ptOnLowerLine); 55 | dist = min(min(d1, d2), d3); 56 | } 57 | 58 | vec4 outsideColor = vec4(0.0); 59 | vec4 currentColor = mix(outsideColor, color, clamp(s + t, 0.0, 1.0)); 60 | vec4 outColor = czm_antialias(outsideColor, color, currentColor, dist); 61 | 62 | outColor = czm_gammaCorrect(outColor); 63 | material.diffuse = outColor.rgb; 64 | material.alpha = outColor.a; 65 | return material; 66 | } 67 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/vertexLogDepth.glsl: -------------------------------------------------------------------------------- 1 | #ifdef LOG_DEPTH 2 | // 1.0 at the near plane, increasing linearly from there. 3 | varying float v_depthFromNearPlusOne; 4 | #ifdef SHADOW_MAP 5 | varying vec3 v_logPositionEC; 6 | #endif 7 | #endif 8 | 9 | vec4 czm_updatePositionDepth(vec4 coords) { 10 | #if defined(LOG_DEPTH) 11 | 12 | #ifdef SHADOW_MAP 13 | vec3 logPositionEC = (czm_inverseProjection * coords).xyz; 14 | v_logPositionEC = logPositionEC; 15 | #endif 16 | 17 | // With the very high far/near ratios used with the logarithmic depth 18 | // buffer, floating point rounding errors can cause linear depth values 19 | // to end up on the wrong side of the far plane, even for vertices that 20 | // are really nowhere near it. Since we always write a correct logarithmic 21 | // depth value in the fragment shader anyway, we just need to make sure 22 | // such errors don't cause the primitive to be clipped entirely before 23 | // we even get to the fragment shader. 24 | coords.z = clamp(coords.z / coords.w, -1.0, 1.0) * coords.w; 25 | #endif 26 | 27 | return coords; 28 | } 29 | 30 | /** 31 | * Writes the logarithmic depth to gl_Position using the already computed gl_Position. 32 | * 33 | * @name czm_vertexLogDepth 34 | * @glslFunction 35 | */ 36 | void czm_vertexLogDepth() 37 | { 38 | #ifdef LOG_DEPTH 39 | v_depthFromNearPlusOne = (gl_Position.w - czm_currentFrustum.x) + 1.0; 40 | gl_Position = czm_updatePositionDepth(gl_Position); 41 | #endif 42 | } 43 | 44 | /** 45 | * Writes the logarithmic depth to gl_Position using the provided clip coordinates. 46 | *

47 | * An example use case for this function would be moving the vertex in window coordinates 48 | * before converting back to clip coordinates. Use the original vertex clip coordinates. 49 | *

50 | * @name czm_vertexLogDepth 51 | * @glslFunction 52 | * 53 | * @param {vec4} clipCoords The vertex in clip coordinates. 54 | * 55 | * @example 56 | * czm_vertexLogDepth(czm_projection * vec4(positionEyeCoordinates, 1.0)); 57 | */ 58 | void czm_vertexLogDepth(vec4 clipCoords) 59 | { 60 | #ifdef LOG_DEPTH 61 | v_depthFromNearPlusOne = (clipCoords.w - czm_currentFrustum.x) + 1.0; 62 | czm_updatePositionDepth(clipCoords); 63 | #endif 64 | } 65 | -------------------------------------------------------------------------------- /ShaderSource/SunTextureFS.glsl: -------------------------------------------------------------------------------- 1 | uniform float u_radiusTS; 2 | 3 | varying vec2 v_textureCoordinates; 4 | 5 | vec2 rotate(vec2 p, vec2 direction) 6 | { 7 | return vec2(p.x * direction.x - p.y * direction.y, p.x * direction.y + p.y * direction.x); 8 | } 9 | 10 | vec4 addBurst(vec2 position, vec2 direction, float lengthScalar) 11 | { 12 | vec2 rotatedPosition = rotate(position, direction) * vec2(25.0, 0.75); 13 | float radius = length(rotatedPosition) * lengthScalar; 14 | float burst = 1.0 - smoothstep(0.0, 0.55, radius); 15 | return vec4(burst); 16 | } 17 | 18 | void main() 19 | { 20 | float lengthScalar = 2.0 / sqrt(2.0); 21 | vec2 position = v_textureCoordinates - vec2(0.5); 22 | float radius = length(position) * lengthScalar; 23 | float surface = step(radius, u_radiusTS); 24 | vec4 color = vec4(vec2(1.0), surface + 0.2, surface); 25 | 26 | float glow = 1.0 - smoothstep(0.0, 0.55, radius); 27 | color.ba += mix(vec2(0.0), vec2(1.0), glow) * 0.75; 28 | 29 | vec4 burst = vec4(0.0); 30 | 31 | // The following loop has been manually unrolled for speed, to 32 | // avoid sin() and cos(). 33 | // 34 | //for (float i = 0.4; i < 3.2; i += 1.047) { 35 | // vec2 direction = vec2(sin(i), cos(i)); 36 | // burst += 0.4 * addBurst(position, direction, lengthScalar); 37 | // 38 | // direction = vec2(sin(i - 0.08), cos(i - 0.08)); 39 | // burst += 0.3 * addBurst(position, direction, lengthScalar); 40 | //} 41 | 42 | burst += 0.4 * addBurst(position, vec2(0.38942, 0.92106), lengthScalar); // angle == 0.4 43 | burst += 0.4 * addBurst(position, vec2(0.99235, 0.12348), lengthScalar); // angle == 0.4 + 1.047 44 | burst += 0.4 * addBurst(position, vec2(0.60327, -0.79754), lengthScalar); // angle == 0.4 + 1.047 * 2.0 45 | 46 | burst += 0.3 * addBurst(position, vec2(0.31457, 0.94924), lengthScalar); // angle == 0.4 - 0.08 47 | burst += 0.3 * addBurst(position, vec2(0.97931, 0.20239), lengthScalar); // angle == 0.4 + 1.047 - 0.08 48 | burst += 0.3 * addBurst(position, vec2(0.66507, -0.74678), lengthScalar); // angle == 0.4 + 1.047 * 2.0 - 0.08 49 | 50 | // End of manual loop unrolling. 51 | 52 | color += clamp(burst, vec4(0.0), vec4(1.0)) * 0.15; 53 | 54 | gl_FragColor = clamp(color, vec4(0.0), vec4(1.0)); 55 | } 56 | -------------------------------------------------------------------------------- /Docs/Builtin/Structs/materialInput.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ``` glsl 4 | /** 5 | * Used as input to every material's czm_getMaterial function. 6 | * 7 | * @name czm_materialInput 8 | * @glslStruct 9 | * 10 | * @property {float} s 1D texture coordinates. 11 | * @property {vec2} st 2D texture coordinates. 12 | * @property {vec3} str 3D texture coordinates. 13 | * @property {vec3} normalEC Unperturbed surface normal in eye coordinates. 14 | * @property {mat3} tangentToEyeMatrix Matrix for converting a tangent space normal to eye space. 15 | * @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. 16 | * @property {float} height The height of the terrain in meters above or below the WGS84 ellipsoid. Only available for globe materials. 17 | * @property {float} slope The slope of the terrain in radians. 0 is flat; pi/2 is vertical. Only available for globe materials. 18 | * @property {float} aspect The aspect of the terrain in radians. 0 is East, pi/2 is North, pi is West, 3pi/2 is South. Only available for globe materials. 19 | */ 20 | struct czm_materialInput 21 | { 22 | float s; 23 | vec2 st; 24 | vec3 str; 25 | vec3 normalEC; 26 | mat3 tangentToEyeMatrix; 27 | vec3 positionToEyeEC; 28 | float height; 29 | float slope; 30 | float aspect; 31 | }; 32 | ``` 33 | 34 | # 文档 35 | 36 | ## 1. 成员 `s` 37 | 38 | ### 类型 39 | 40 | `float` 41 | 42 | ### 含义 43 | 44 | 一维纹理坐标 45 | 46 | 47 | 48 | ## 2. 成员 `st` 49 | 50 | ### 类型 51 | 52 | `vec2` 53 | 54 | ### 含义 55 | 56 | 二维纹理坐标 57 | 58 | 59 | 60 | ## 3. 成员 `str` 61 | 62 | ### 类型 63 | 64 | `vec3` 65 | 66 | ### 含义 67 | 68 | 三维纹理坐标 69 | 70 | 71 | 72 | ## 4. 成员 `normalEC` 73 | 74 | ### 类型 75 | 76 | `vec3` 77 | 78 | ### 含义 79 | 80 | 观察坐标(eye-coordinate,EC)中的法向量。 81 | 82 | 83 | 84 | ## 5. 成员 `tangentToEyeMatrix` 85 | 86 | ### 类型 87 | 88 | `mat3` 89 | 90 | 91 | 92 | ## 5. 成员 `positionToEyeEC` 93 | 94 | ### 类型 95 | 96 | `vec3` 97 | 98 | 99 | 100 | ## 6. 成员 `height` 101 | 102 | ### 类型 103 | 104 | `float` 105 | 106 | 107 | 108 | ## 7. 成员 `slope` 109 | 110 | ### 类型 111 | 112 | `float` 113 | 114 | 115 | 116 | ## 8. 成员 `aspect` 117 | 118 | ### 类型 119 | 120 | `float` 121 | -------------------------------------------------------------------------------- /ShaderSource/Materials/GridMaterial.glsl: -------------------------------------------------------------------------------- 1 | #ifdef GL_OES_standard_derivatives 2 | #extension GL_OES_standard_derivatives : enable 3 | #endif 4 | 5 | uniform vec4 color; 6 | uniform float cellAlpha; 7 | uniform vec2 lineCount; 8 | uniform vec2 lineThickness; 9 | uniform vec2 lineOffset; 10 | 11 | czm_material czm_getMaterial(czm_materialInput materialInput) 12 | { 13 | czm_material material = czm_getDefaultMaterial(materialInput); 14 | 15 | vec2 st = materialInput.st; 16 | 17 | float scaledWidth = fract(lineCount.s * st.s - lineOffset.s); 18 | scaledWidth = abs(scaledWidth - floor(scaledWidth + 0.5)); 19 | float scaledHeight = fract(lineCount.t * st.t - lineOffset.t); 20 | scaledHeight = abs(scaledHeight - floor(scaledHeight + 0.5)); 21 | 22 | float value; 23 | #ifdef GL_OES_standard_derivatives 24 | // Fuzz Factor - Controls blurriness of lines 25 | const float fuzz = 1.2; 26 | vec2 thickness = (lineThickness * czm_pixelRatio) - 1.0; 27 | 28 | // From "3D Engine Design for Virtual Globes" by Cozzi and Ring, Listing 4.13. 29 | vec2 dx = abs(dFdx(st)); 30 | vec2 dy = abs(dFdy(st)); 31 | vec2 dF = vec2(max(dx.s, dy.s), max(dx.t, dy.t)) * lineCount; 32 | value = min( 33 | smoothstep(dF.s * thickness.s, dF.s * (fuzz + thickness.s), scaledWidth), 34 | smoothstep(dF.t * thickness.t, dF.t * (fuzz + thickness.t), scaledHeight)); 35 | #else 36 | // Fuzz Factor - Controls blurriness of lines 37 | const float fuzz = 0.05; 38 | 39 | vec2 range = 0.5 - (lineThickness * 0.05); 40 | value = min( 41 | 1.0 - smoothstep(range.s, range.s + fuzz, scaledWidth), 42 | 1.0 - smoothstep(range.t, range.t + fuzz, scaledHeight)); 43 | #endif 44 | 45 | // Edges taken from RimLightingMaterial.glsl 46 | // See http://www.fundza.com/rman_shaders/surface/fake_rim/fake_rim1.html 47 | float dRim = 1.0 - abs(dot(materialInput.normalEC, normalize(materialInput.positionToEyeEC))); 48 | float sRim = smoothstep(0.8, 1.0, dRim); 49 | value *= (1.0 - sRim); 50 | 51 | vec4 halfColor; 52 | halfColor.rgb = color.rgb * 0.5; 53 | halfColor.a = color.a * (1.0 - ((1.0 - cellAlpha) * value)); 54 | halfColor = czm_gammaCorrect(halfColor); 55 | material.diffuse = halfColor.rgb; 56 | material.emission = halfColor.rgb; 57 | material.alpha = halfColor.a; 58 | 59 | return material; 60 | } 61 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/writeLogDepth.glsl: -------------------------------------------------------------------------------- 1 | #ifdef LOG_DEPTH 2 | varying float v_depthFromNearPlusOne; 3 | 4 | #ifdef POLYGON_OFFSET 5 | uniform vec2 u_polygonOffset; 6 | #endif 7 | 8 | #endif 9 | 10 | /** 11 | * Writes the fragment depth to the logarithmic depth buffer. 12 | *

13 | * Use this when the vertex shader does not call {@link czm_vertexlogDepth}, for example, when 14 | * ray-casting geometry using a full screen quad. 15 | *

16 | * @name czm_writeLogDepth 17 | * @glslFunction 18 | * 19 | * @param {float} depth The depth coordinate, where 1.0 is on the near plane and 20 | * depth increases in eye-space units from there 21 | * 22 | * @example 23 | * czm_writeLogDepth((czm_projection * v_positionEyeCoordinates).w + 1.0); 24 | */ 25 | void czm_writeLogDepth(float depth) 26 | { 27 | #if defined(GL_EXT_frag_depth) && defined(LOG_DEPTH) 28 | // Discard the vertex if it's not between the near and far planes. 29 | // We allow a bit of epsilon on the near plane comparison because a 1.0 30 | // from the vertex shader (indicating the vertex should be _on_ the near 31 | // plane) will not necessarily come here as exactly 1.0. 32 | if (depth <= 0.9999999 || depth > czm_farDepthFromNearPlusOne) { 33 | discard; 34 | } 35 | 36 | #ifdef POLYGON_OFFSET 37 | // Polygon offset: m * factor + r * units 38 | float factor = u_polygonOffset[0]; 39 | float units = u_polygonOffset[1]; 40 | 41 | // If we can't compute derivatives, just leave out the factor I guess? 42 | #ifdef GL_OES_standard_derivatives 43 | // m = sqrt(dZdX^2 + dZdY^2); 44 | float x = dFdx(depth); 45 | float y = dFdy(depth); 46 | float m = sqrt(x * x + y * y); 47 | 48 | // Apply the factor before computing the log depth. 49 | depth += m * factor; 50 | #endif 51 | 52 | #endif 53 | 54 | gl_FragDepthEXT = log2(depth) * czm_oneOverLog2FarDepthFromNearPlusOne; 55 | 56 | #ifdef POLYGON_OFFSET 57 | // Apply the units after the log depth. 58 | gl_FragDepthEXT += czm_epsilon7 * units; 59 | #endif 60 | 61 | #endif 62 | } 63 | 64 | /** 65 | * Writes the fragment depth to the logarithmic depth buffer. 66 | *

67 | * Use this when the vertex shader calls {@link czm_vertexlogDepth}. 68 | *

69 | * 70 | * @name czm_writeLogDepth 71 | * @glslFunction 72 | */ 73 | void czm_writeLogDepth() { 74 | #ifdef LOG_DEPTH 75 | czm_writeLogDepth(v_depthFromNearPlusOne); 76 | #endif 77 | } 78 | -------------------------------------------------------------------------------- /ShaderSource/Materials/ElevationBandMaterial.glsl: -------------------------------------------------------------------------------- 1 | uniform sampler2D heights; 2 | uniform sampler2D colors; 3 | 4 | // This material expects heights to be sorted from lowest to highest. 5 | 6 | float getHeight(int idx, float invTexSize) 7 | { 8 | vec2 uv = vec2((float(idx) + 0.5) * invTexSize, 0.5); 9 | #ifdef OES_texture_float 10 | return texture2D(heights, uv).x; 11 | #else 12 | return czm_unpackFloat(texture2D(heights, uv)); 13 | #endif 14 | } 15 | 16 | czm_material czm_getMaterial(czm_materialInput materialInput) 17 | { 18 | czm_material material = czm_getDefaultMaterial(materialInput); 19 | 20 | float height = materialInput.height; 21 | float invTexSize = 1.0 / float(heightsDimensions.x); 22 | 23 | float minHeight = getHeight(0, invTexSize); 24 | float maxHeight = getHeight(heightsDimensions.x - 1, invTexSize); 25 | 26 | // early-out when outside the height range 27 | if (height < minHeight || height > maxHeight) { 28 | material.diffuse = vec3(0.0); 29 | material.alpha = 0.0; 30 | return material; 31 | } 32 | 33 | // Binary search to find heights above and below. 34 | int idxBelow = 0; 35 | int idxAbove = heightsDimensions.x; 36 | float heightBelow = minHeight; 37 | float heightAbove = maxHeight; 38 | 39 | // while loop not allowed, so use for loop with max iterations. 40 | // maxIterations of 16 supports a texture size up to 65536 (2^16). 41 | const int maxIterations = 16; 42 | for (int i = 0; i < maxIterations; i++) { 43 | if (idxBelow >= idxAbove - 1) { 44 | break; 45 | } 46 | 47 | int idxMid = (idxBelow + idxAbove) / 2; 48 | float heightTex = getHeight(idxMid, invTexSize); 49 | 50 | if (height > heightTex) { 51 | idxBelow = idxMid; 52 | heightBelow = heightTex; 53 | } else { 54 | idxAbove = idxMid; 55 | heightAbove = heightTex; 56 | } 57 | } 58 | 59 | float lerper = heightBelow == heightAbove ? 1.0 : (height - heightBelow) / (heightAbove - heightBelow); 60 | vec2 colorUv = vec2(invTexSize * (float(idxBelow) + 0.5 + lerper), 0.5); 61 | vec4 color = texture2D(colors, colorUv); 62 | 63 | // undo preumultiplied alpha 64 | if (color.a > 0.0) 65 | { 66 | color.rgb /= color.a; 67 | } 68 | 69 | color.rgb = czm_gammaCorrect(color.rgb); 70 | 71 | material.diffuse = color.rgb; 72 | material.alpha = color.a; 73 | return material; 74 | } 75 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/phong.glsl: -------------------------------------------------------------------------------- 1 | float czm_private_getLambertDiffuseOfMaterial(vec3 lightDirectionEC, czm_material material) 2 | { 3 | return czm_getLambertDiffuse(lightDirectionEC, material.normal); 4 | } 5 | 6 | float czm_private_getSpecularOfMaterial(vec3 lightDirectionEC, vec3 toEyeEC, czm_material material) 7 | { 8 | return czm_getSpecular(lightDirectionEC, toEyeEC, material.normal, material.shininess); 9 | } 10 | 11 | /** 12 | * Computes a color using the Phong lighting model. 13 | * 14 | * @name czm_phong 15 | * @glslFunction 16 | * 17 | * @param {vec3} toEye A normalized vector from the fragment to the eye in eye coordinates. 18 | * @param {czm_material} material The fragment's material. 19 | * 20 | * @returns {vec4} The computed color. 21 | * 22 | * @example 23 | * vec3 positionToEyeEC = // ... 24 | * czm_material material = // ... 25 | * vec3 lightDirectionEC = // ... 26 | * gl_FragColor = czm_phong(normalize(positionToEyeEC), material, lightDirectionEC); 27 | * 28 | * @see czm_getMaterial 29 | */ 30 | vec4 czm_phong(vec3 toEye, czm_material material, vec3 lightDirectionEC) 31 | { 32 | // Diffuse from directional light sources at eye (for top-down) 33 | float diffuse = czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 0.0, 1.0), material); 34 | if (czm_sceneMode == czm_sceneMode3D) { 35 | // (and horizon views in 3D) 36 | diffuse += czm_private_getLambertDiffuseOfMaterial(vec3(0.0, 1.0, 0.0), material); 37 | } 38 | 39 | float specular = czm_private_getSpecularOfMaterial(lightDirectionEC, toEye, material); 40 | 41 | // Temporary workaround for adding ambient. 42 | vec3 materialDiffuse = material.diffuse * 0.5; 43 | 44 | vec3 ambient = materialDiffuse; 45 | vec3 color = ambient + material.emission; 46 | color += materialDiffuse * diffuse * czm_lightColor; 47 | color += material.specular * specular * czm_lightColor; 48 | 49 | return vec4(color, material.alpha); 50 | } 51 | 52 | vec4 czm_private_phong(vec3 toEye, czm_material material, vec3 lightDirectionEC) 53 | { 54 | float diffuse = czm_private_getLambertDiffuseOfMaterial(lightDirectionEC, material); 55 | float specular = czm_private_getSpecularOfMaterial(lightDirectionEC, toEye, material); 56 | 57 | vec3 ambient = vec3(0.0); 58 | vec3 color = ambient + material.emission; 59 | color += material.diffuse * diffuse * czm_lightColor; 60 | color += material.specular * specular * czm_lightColor; 61 | 62 | return vec4(color, material.alpha); 63 | } 64 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/fastApproximateAtan.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Approxiamtes atan over the range [0, 1]. Safe to flip output for negative input. 3 | * 4 | * Based on Michal Drobot's approximation from ShaderFastLibs, which in turn is based on 5 | * "Efficient approximations for the arctangent function," Rajan, S. Sichun Wang Inkol, R. Joyal, A., May 2006. 6 | * Adapted from ShaderFastLibs under MIT License. 7 | * 8 | * Chosen for the following characteristics over range [0, 1]: 9 | * - basically no error at 0 and 1, important for getting around range limit (naive atan2 via atan requires infinite range atan) 10 | * - no visible artifacts from first-derivative discontinuities, unlike latitude via range-reduced sqrt asin approximations (at equator) 11 | * 12 | * The original code is x * (-0.1784 * abs(x) - 0.0663 * x * x + 1.0301); 13 | * Removed the abs() in here because it isn't needed, the input range is guaranteed as [0, 1] by how we're approximating atan2. 14 | * 15 | * @name czm_fastApproximateAtan 16 | * @glslFunction 17 | * 18 | * @param {float} x Value between 0 and 1 inclusive. 19 | * 20 | * @returns {float} Approximation of atan(x) 21 | */ 22 | float czm_fastApproximateAtan(float x) { 23 | return x * (-0.1784 * x - 0.0663 * x * x + 1.0301); 24 | } 25 | 26 | /** 27 | * Approximation of atan2. 28 | * 29 | * Range reduction math based on nvidia's cg reference implementation for atan2: http://developer.download.nvidia.com/cg/atan2.html 30 | * However, we replaced their atan curve with Michael Drobot's (see above). 31 | * 32 | * @name czm_fastApproximateAtan 33 | * @glslFunction 34 | * 35 | * @param {float} x Value between -1 and 1 inclusive. 36 | * @param {float} y Value between -1 and 1 inclusive. 37 | * 38 | * @returns {float} Approximation of atan2(x, y) 39 | */ 40 | float czm_fastApproximateAtan(float x, float y) { 41 | // atan approximations are usually only reliable over [-1, 1], or, in our case, [0, 1] due to modifications. 42 | // So range-reduce using abs and by flipping whether x or y is on top. 43 | float t = abs(x); // t used as swap and atan result. 44 | float opposite = abs(y); 45 | float adjacent = max(t, opposite); 46 | opposite = min(t, opposite); 47 | 48 | t = czm_fastApproximateAtan(opposite / adjacent); 49 | 50 | // Undo range reduction 51 | t = czm_branchFreeTernary(abs(y) > abs(x), czm_piOverTwo - t, t); 52 | t = czm_branchFreeTernary(x < 0.0, czm_pi - t, t); 53 | t = czm_branchFreeTernary(y < 0.0, -t, t); 54 | return t; 55 | } 56 | -------------------------------------------------------------------------------- /ShaderSource/OctahedralProjectionAtlasFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec2 v_textureCoordinates; 2 | 3 | uniform float originalSize; 4 | uniform sampler2D texture0; 5 | uniform sampler2D texture1; 6 | uniform sampler2D texture2; 7 | uniform sampler2D texture3; 8 | uniform sampler2D texture4; 9 | uniform sampler2D texture5; 10 | 11 | const float yMipLevel1 = 1.0 - (1.0 / pow(2.0, 1.0)); 12 | const float yMipLevel2 = 1.0 - (1.0 / pow(2.0, 2.0)); 13 | const float yMipLevel3 = 1.0 - (1.0 / pow(2.0, 3.0)); 14 | const float yMipLevel4 = 1.0 - (1.0 / pow(2.0, 4.0)); 15 | 16 | void main() 17 | { 18 | vec2 uv = v_textureCoordinates; 19 | vec2 textureSize = vec2(originalSize * 1.5 + 2.0, originalSize); 20 | vec2 pixel = 1.0 / textureSize; 21 | 22 | float mipLevel = 0.0; 23 | 24 | if (uv.x - pixel.x > (textureSize.y / textureSize.x)) 25 | { 26 | mipLevel = 1.0; 27 | if (uv.y - pixel.y > yMipLevel1) 28 | { 29 | mipLevel = 2.0; 30 | if (uv.y - pixel.y * 3.0 > yMipLevel2) 31 | { 32 | mipLevel = 3.0; 33 | if (uv.y - pixel.y * 5.0 > yMipLevel3) 34 | { 35 | mipLevel = 4.0; 36 | if (uv.y - pixel.y * 7.0 > yMipLevel4) 37 | { 38 | mipLevel = 5.0; 39 | } 40 | } 41 | } 42 | } 43 | } 44 | 45 | if (mipLevel > 0.0) 46 | { 47 | float scale = pow(2.0, mipLevel); 48 | 49 | uv.y -= (pixel.y * (mipLevel - 1.0) * 2.0); 50 | uv.x *= ((textureSize.x - 2.0) / textureSize.y); 51 | 52 | uv.x -= 1.0 + pixel.x; 53 | uv.y -= (1.0 - (1.0 / pow(2.0, mipLevel - 1.0))); 54 | uv *= scale; 55 | } 56 | else 57 | { 58 | uv.x *= (textureSize.x / textureSize.y); 59 | } 60 | 61 | if(mipLevel == 0.0) 62 | { 63 | gl_FragColor = texture2D(texture0, uv); 64 | } 65 | else if(mipLevel == 1.0) 66 | { 67 | gl_FragColor = texture2D(texture1, uv); 68 | } 69 | else if(mipLevel == 2.0) 70 | { 71 | gl_FragColor = texture2D(texture2, uv); 72 | } 73 | else if(mipLevel == 3.0) 74 | { 75 | gl_FragColor = texture2D(texture3, uv); 76 | } 77 | else if(mipLevel == 4.0) 78 | { 79 | gl_FragColor = texture2D(texture4, uv); 80 | } 81 | else if(mipLevel == 5.0) 82 | { 83 | gl_FragColor = texture2D(texture5, uv); 84 | } 85 | else 86 | { 87 | gl_FragColor = vec4(0.0); 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/branchFreeTernary.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Branchless ternary operator to be used when it's inexpensive to explicitly 3 | * evaluate both possibilities for a float expression. 4 | * 5 | * @name czm_branchFreeTernary 6 | * @glslFunction 7 | * 8 | * @param {bool} comparison A comparison statement 9 | * @param {float} a Value to return if the comparison is true. 10 | * @param {float} b Value to return if the comparison is false. 11 | * 12 | * @returns {float} equivalent of comparison ? a : b 13 | */ 14 | float czm_branchFreeTernary(bool comparison, float a, float b) { 15 | float useA = float(comparison); 16 | return a * useA + b * (1.0 - useA); 17 | } 18 | 19 | /** 20 | * Branchless ternary operator to be used when it's inexpensive to explicitly 21 | * evaluate both possibilities for a vec2 expression. 22 | * 23 | * @name czm_branchFreeTernary 24 | * @glslFunction 25 | * 26 | * @param {bool} comparison A comparison statement 27 | * @param {vec2} a Value to return if the comparison is true. 28 | * @param {vec2} b Value to return if the comparison is false. 29 | * 30 | * @returns {vec2} equivalent of comparison ? a : b 31 | */ 32 | vec2 czm_branchFreeTernary(bool comparison, vec2 a, vec2 b) { 33 | float useA = float(comparison); 34 | return a * useA + b * (1.0 - useA); 35 | } 36 | 37 | /** 38 | * Branchless ternary operator to be used when it's inexpensive to explicitly 39 | * evaluate both possibilities for a vec3 expression. 40 | * 41 | * @name czm_branchFreeTernary 42 | * @glslFunction 43 | * 44 | * @param {bool} comparison A comparison statement 45 | * @param {vec3} a Value to return if the comparison is true. 46 | * @param {vec3} b Value to return if the comparison is false. 47 | * 48 | * @returns {vec3} equivalent of comparison ? a : b 49 | */ 50 | vec3 czm_branchFreeTernary(bool comparison, vec3 a, vec3 b) { 51 | float useA = float(comparison); 52 | return a * useA + b * (1.0 - useA); 53 | } 54 | 55 | /** 56 | * Branchless ternary operator to be used when it's inexpensive to explicitly 57 | * evaluate both possibilities for a vec4 expression. 58 | * 59 | * @name czm_branchFreeTernary 60 | * @glslFunction 61 | * 62 | * @param {bool} comparison A comparison statement 63 | * @param {vec3} a Value to return if the comparison is true. 64 | * @param {vec3} b Value to return if the comparison is false. 65 | * 66 | * @returns {vec3} equivalent of comparison ? a : b 67 | */ 68 | vec4 czm_branchFreeTernary(bool comparison, vec4 a, vec4 b) { 69 | float useA = float(comparison); 70 | return a * useA + b * (1.0 - useA); 71 | } 72 | -------------------------------------------------------------------------------- /ShaderSource/Materials/Water.glsl: -------------------------------------------------------------------------------- 1 | // Thanks for the contribution Jonas 2 | // http://29a.ch/2012/7/19/webgl-terrain-rendering-water-fog 3 | 4 | uniform sampler2D specularMap; 5 | uniform sampler2D normalMap; 6 | uniform vec4 baseWaterColor; 7 | uniform vec4 blendColor; 8 | uniform float frequency; 9 | uniform float animationSpeed; 10 | uniform float amplitude; 11 | uniform float specularIntensity; 12 | uniform float fadeFactor; 13 | 14 | czm_material czm_getMaterial(czm_materialInput materialInput) 15 | { 16 | czm_material material = czm_getDefaultMaterial(materialInput); 17 | 18 | float time = czm_frameNumber * animationSpeed; 19 | 20 | // fade is a function of the distance from the fragment and the frequency of the waves 21 | float fade = max(1.0, (length(materialInput.positionToEyeEC) / 10000000000.0) * frequency * fadeFactor); 22 | 23 | float specularMapValue = texture2D(specularMap, materialInput.st).r; 24 | 25 | // note: not using directional motion at this time, just set the angle to 0.0; 26 | vec4 noise = czm_getWaterNoise(normalMap, materialInput.st * frequency, time, 0.0); 27 | vec3 normalTangentSpace = noise.xyz * vec3(1.0, 1.0, (1.0 / amplitude)); 28 | 29 | // fade out the normal perturbation as we move further from the water surface 30 | normalTangentSpace.xy /= fade; 31 | 32 | // attempt to fade out the normal perturbation as we approach non water areas (low specular map value) 33 | normalTangentSpace = mix(vec3(0.0, 0.0, 50.0), normalTangentSpace, specularMapValue); 34 | 35 | normalTangentSpace = normalize(normalTangentSpace); 36 | 37 | // get ratios for alignment of the new normal vector with a vector perpendicular to the tangent plane 38 | float tsPerturbationRatio = clamp(dot(normalTangentSpace, vec3(0.0, 0.0, 1.0)), 0.0, 1.0); 39 | 40 | // fade out water effect as specular map value decreases 41 | material.alpha = mix(blendColor.a, baseWaterColor.a, specularMapValue) * specularMapValue; 42 | 43 | // base color is a blend of the water and non-water color based on the value from the specular map 44 | // may need a uniform blend factor to better control this 45 | material.diffuse = mix(blendColor.rgb, baseWaterColor.rgb, specularMapValue); 46 | 47 | // diffuse highlights are based on how perturbed the normal is 48 | material.diffuse += (0.1 * tsPerturbationRatio); 49 | 50 | material.diffuse = material.diffuse; 51 | 52 | material.normal = normalize(materialInput.tangentToEyeMatrix * normalTangentSpace); 53 | 54 | material.specular = specularIntensity; 55 | material.shininess = 10.0; 56 | 57 | return material; 58 | } 59 | -------------------------------------------------------------------------------- /Docs/Builtin/Functions/windowToEyeCoordinates.md: -------------------------------------------------------------------------------- 1 | # 源代码 2 | 3 | ``` glsl 4 | vec4 czm_windowToEyeCoordinates(vec4 fragmentCoordinate) 5 | { 6 | // 窗口坐标重建NDC坐标 7 | float x = 2.0 * (fragmentCoordinate.x - czm_viewport.x) / czm_viewport.z - 1.0; 8 | float y = 2.0 * (fragmentCoordinate.y - czm_viewport.y) / czm_viewport.w - 1.0; 9 | float z = (fragmentCoordinate.z - czm_viewportTransformation[3][2]) / czm_viewportTransformation[2][2]; 10 | vec4 q = vec4(x, y, z, 1.0); 11 | 12 | // NDC坐标转裁剪(投影)坐标 13 | q /= fragmentCoordinate.w; 14 | 15 | // 裁剪(投影)坐标转y(相机)坐标 16 | if (!(czm_inverseProjection == mat4(0.0))) // IE and Edge sometimes do something weird with != between mat4s 17 | { 18 | q = czm_inverseProjection * q; 19 | } 20 | else 21 | { 22 | float top = czm_frustumPlanes.x; 23 | float bottom = czm_frustumPlanes.y; 24 | float left = czm_frustumPlanes.z; 25 | float right = czm_frustumPlanes.w; 26 | 27 | float near = czm_currentFrustum.x; 28 | float far = czm_currentFrustum.y; 29 | 30 | q.x = (q.x * (right - left) + left + right) * 0.5; 31 | q.y = (q.y * (top - bottom) + bottom + top) * 0.5; 32 | q.z = (q.z * (near - far) - near - far) * 0.5; 33 | q.w = 1.0; 34 | } 35 | 36 | return q; 37 | } 38 | 39 | vec4 czm_windowToEyeCoordinates(vec2 fragmentCoordinateXY, float depthOrLogDepth) 40 | { 41 | // See reverseLogDepth.glsl. This is separate to re-use the pow. 42 | #ifdef LOG_DEPTH 43 | float near = czm_currentFrustum.x; 44 | float far = czm_currentFrustum.y; 45 | float log2Depth = depthOrLogDepth * czm_log2FarDepthFromNearPlusOne; 46 | float depthFromNear = pow(2.0, log2Depth) - 1.0; 47 | float depthFromCamera = depthFromNear + near; 48 | vec4 windowCoord = vec4(fragmentCoordinateXY, far * (1.0 - near / depthFromCamera) / (far - near), 1.0); 49 | vec4 eyeCoordinate = czm_windowToEyeCoordinates(windowCoord); 50 | eyeCoordinate.w = 1.0 / depthFromCamera; // Better precision 51 | return eyeCoordinate; 52 | #else 53 | vec4 windowCoord = vec4(fragmentCoordinateXY, depthOrLogDepth, 1.0); 54 | vec4 eyeCoordinate = czm_windowToEyeCoordinates(windowCoord); 55 | #endif 56 | return eyeCoordinate; 57 | } 58 | ``` 59 | 60 | # 文档 61 | 62 | 该函数能将窗口坐标计算至眼坐标(也即相机坐标、观察坐标)。有两个重载。 63 | 64 | ## 1. 参数 65 | 66 | ### 重载1 ① fragmentCoordinate 67 | 68 | 参数类型:`vec4` 69 | 70 | 片元的窗口坐标。 71 | 72 | ### 重载2 ① fragmentCoordinateXY 73 | 74 | 参数类型:`vec2` 75 | 76 | 片元的窗口坐标,二维。 77 | 78 | ### 重载2 ② depthOrLogDepth 79 | 80 | 参数类型:`float` 81 | 82 | (对数)深度值。 83 | 84 | 85 | 86 | ## 2. 返回值 87 | 88 | 返回值类型:`vec4` 89 | 90 | 91 | 92 | # 注意事项 93 | 94 | `depthTexture` 可以是对数深度纹理,也可以是非对数深度纹理。 95 | 96 | 97 | -------------------------------------------------------------------------------- /ShaderSource/PostProcessStages/PointCloudEyeDomeLighting.glsl: -------------------------------------------------------------------------------- 1 | #extension GL_EXT_frag_depth : enable 2 | 3 | uniform sampler2D u_pointCloud_colorGBuffer; 4 | uniform sampler2D u_pointCloud_depthGBuffer; 5 | uniform vec2 u_distanceAndEdlStrength; 6 | varying vec2 v_textureCoordinates; 7 | 8 | vec2 neighborContribution(float log2Depth, vec2 offset) 9 | { 10 | float dist = u_distanceAndEdlStrength.x; 11 | vec2 texCoordOrig = v_textureCoordinates + offset * dist; 12 | vec2 texCoord0 = v_textureCoordinates + offset * floor(dist); 13 | vec2 texCoord1 = v_textureCoordinates + offset * ceil(dist); 14 | 15 | float depthOrLogDepth0 = czm_unpackDepth(texture2D(u_pointCloud_depthGBuffer, texCoord0)); 16 | float depthOrLogDepth1 = czm_unpackDepth(texture2D(u_pointCloud_depthGBuffer, texCoord1)); 17 | 18 | // ignore depth values that are the clear depth 19 | if (depthOrLogDepth0 == 0.0 || depthOrLogDepth1 == 0.0) { 20 | return vec2(0.0); 21 | } 22 | 23 | // interpolate the two adjacent depth values 24 | float depthMix = mix(depthOrLogDepth0, depthOrLogDepth1, fract(dist)); 25 | vec4 eyeCoordinate = czm_windowToEyeCoordinates(texCoordOrig, depthMix); 26 | return vec2(max(0.0, log2Depth - log2(-eyeCoordinate.z / eyeCoordinate.w)), 1.0); 27 | } 28 | 29 | void main() 30 | { 31 | float depthOrLogDepth = czm_unpackDepth(texture2D(u_pointCloud_depthGBuffer, v_textureCoordinates)); 32 | 33 | vec4 eyeCoordinate = czm_windowToEyeCoordinates(gl_FragCoord.xy, depthOrLogDepth); 34 | eyeCoordinate /= eyeCoordinate.w; 35 | 36 | float log2Depth = log2(-eyeCoordinate.z); 37 | 38 | if (depthOrLogDepth == 0.0) // 0.0 is the clear value for the gbuffer 39 | { 40 | discard; 41 | } 42 | 43 | vec4 color = texture2D(u_pointCloud_colorGBuffer, v_textureCoordinates); 44 | 45 | // sample from neighbors left, right, down, up 46 | vec2 texelSize = 1.0 / czm_viewport.zw; 47 | 48 | vec2 responseAndCount = vec2(0.0); 49 | 50 | responseAndCount += neighborContribution(log2Depth, vec2(-texelSize.x, 0.0)); 51 | responseAndCount += neighborContribution(log2Depth, vec2(+texelSize.x, 0.0)); 52 | responseAndCount += neighborContribution(log2Depth, vec2(0.0, -texelSize.y)); 53 | responseAndCount += neighborContribution(log2Depth, vec2(0.0, +texelSize.y)); 54 | 55 | float response = responseAndCount.x / responseAndCount.y; 56 | float strength = u_distanceAndEdlStrength.y; 57 | float shade = exp(-response * 300.0 * strength); 58 | color.rgb *= shade; 59 | gl_FragColor = vec4(color); 60 | 61 | // Input and output depth are the same. 62 | gl_FragDepthEXT = depthOrLogDepth; 63 | } 64 | -------------------------------------------------------------------------------- /ShaderSource/BrdfLutGeneratorFS.glsl: -------------------------------------------------------------------------------- 1 | varying vec2 v_textureCoordinates; 2 | const float M_PI = 3.141592653589793; 3 | 4 | float vdcRadicalInverse(int i) 5 | { 6 | float r; 7 | float base = 2.0; 8 | float value = 0.0; 9 | float invBase = 1.0 / base; 10 | float invBi = invBase; 11 | for (int x = 0; x < 100; x++) 12 | { 13 | if (i <= 0) 14 | { 15 | break; 16 | } 17 | r = mod(float(i), base); 18 | value += r * invBi; 19 | invBi *= invBase; 20 | i = int(float(i) * invBase); 21 | } 22 | return value; 23 | } 24 | 25 | vec2 hammersley2D(int i, int N) 26 | { 27 | return vec2(float(i) / float(N), vdcRadicalInverse(i)); 28 | } 29 | 30 | vec3 importanceSampleGGX(vec2 xi, float roughness, vec3 N) 31 | { 32 | float a = roughness * roughness; 33 | float phi = 2.0 * M_PI * xi.x; 34 | float cosTheta = sqrt((1.0 - xi.y) / (1.0 + (a * a - 1.0) * xi.y)); 35 | float sinTheta = sqrt(1.0 - cosTheta * cosTheta); 36 | vec3 H = vec3(sinTheta * cos(phi), sinTheta * sin(phi), cosTheta); 37 | vec3 upVector = abs(N.z) < 0.999 ? vec3(0.0, 0.0, 1.0) : vec3(1.0, 0.0, 0.0); 38 | vec3 tangentX = normalize(cross(upVector, N)); 39 | vec3 tangentY = cross(N, tangentX); 40 | return tangentX * H.x + tangentY * H.y + N * H.z; 41 | } 42 | 43 | float G1_Smith(float NdotV, float k) 44 | { 45 | return NdotV / (NdotV * (1.0 - k) + k); 46 | } 47 | 48 | float G_Smith(float roughness, float NdotV, float NdotL) 49 | { 50 | float k = roughness * roughness / 2.0; 51 | return G1_Smith(NdotV, k) * G1_Smith(NdotL, k); 52 | } 53 | 54 | vec2 integrateBrdf(float roughness, float NdotV) 55 | { 56 | vec3 V = vec3(sqrt(1.0 - NdotV * NdotV), 0.0, NdotV); 57 | float A = 0.0; 58 | float B = 0.0; 59 | const int NumSamples = 1024; 60 | for (int i = 0; i < NumSamples; i++) 61 | { 62 | vec2 xi = hammersley2D(i, NumSamples); 63 | vec3 H = importanceSampleGGX(xi, roughness, vec3(0.0, 0.0, 1.0)); 64 | vec3 L = 2.0 * dot(V, H) * H - V; 65 | float NdotL = clamp(L.z, 0.0, 1.0); 66 | float NdotH = clamp(H.z, 0.0, 1.0); 67 | float VdotH = clamp(dot(V, H), 0.0, 1.0); 68 | if (NdotL > 0.0) 69 | { 70 | float G = G_Smith(roughness, NdotV, NdotL); 71 | float G_Vis = G * VdotH / (NdotH * NdotV); 72 | float Fc = pow(1.0 - VdotH, 5.0); 73 | A += (1.0 - Fc) * G_Vis; 74 | B += Fc * G_Vis; 75 | } 76 | } 77 | return vec2(A, B) / float(NumSamples); 78 | } 79 | 80 | void main() 81 | { 82 | gl_FragColor = vec4(integrateBrdf(v_textureCoordinates.y, v_textureCoordinates.x), 0.0, 1.0); 83 | } 84 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/shadowVisibility.glsl: -------------------------------------------------------------------------------- 1 | 2 | float czm_private_shadowVisibility(float visibility, float nDotL, float normalShadingSmooth, float darkness) 3 | { 4 | #ifdef USE_NORMAL_SHADING 5 | #ifdef USE_NORMAL_SHADING_SMOOTH 6 | float strength = clamp(nDotL / normalShadingSmooth, 0.0, 1.0); 7 | #else 8 | float strength = step(0.0, nDotL); 9 | #endif 10 | visibility *= strength; 11 | #endif 12 | 13 | visibility = max(visibility, darkness); 14 | return visibility; 15 | } 16 | 17 | #ifdef USE_CUBE_MAP_SHADOW 18 | float czm_shadowVisibility(samplerCube shadowMap, czm_shadowParameters shadowParameters) 19 | { 20 | float depthBias = shadowParameters.depthBias; 21 | float depth = shadowParameters.depth; 22 | float nDotL = shadowParameters.nDotL; 23 | float normalShadingSmooth = shadowParameters.normalShadingSmooth; 24 | float darkness = shadowParameters.darkness; 25 | vec3 uvw = shadowParameters.texCoords; 26 | 27 | depth -= depthBias; 28 | float visibility = czm_shadowDepthCompare(shadowMap, uvw, depth); 29 | return czm_private_shadowVisibility(visibility, nDotL, normalShadingSmooth, darkness); 30 | } 31 | #else 32 | float czm_shadowVisibility(sampler2D shadowMap, czm_shadowParameters shadowParameters) 33 | { 34 | float depthBias = shadowParameters.depthBias; 35 | float depth = shadowParameters.depth; 36 | float nDotL = shadowParameters.nDotL; 37 | float normalShadingSmooth = shadowParameters.normalShadingSmooth; 38 | float darkness = shadowParameters.darkness; 39 | vec2 uv = shadowParameters.texCoords; 40 | 41 | depth -= depthBias; 42 | #ifdef USE_SOFT_SHADOWS 43 | vec2 texelStepSize = shadowParameters.texelStepSize; 44 | float radius = 1.0; 45 | float dx0 = -texelStepSize.x * radius; 46 | float dy0 = -texelStepSize.y * radius; 47 | float dx1 = texelStepSize.x * radius; 48 | float dy1 = texelStepSize.y * radius; 49 | float visibility = ( 50 | czm_shadowDepthCompare(shadowMap, uv, depth) + 51 | czm_shadowDepthCompare(shadowMap, uv + vec2(dx0, dy0), depth) + 52 | czm_shadowDepthCompare(shadowMap, uv + vec2(0.0, dy0), depth) + 53 | czm_shadowDepthCompare(shadowMap, uv + vec2(dx1, dy0), depth) + 54 | czm_shadowDepthCompare(shadowMap, uv + vec2(dx0, 0.0), depth) + 55 | czm_shadowDepthCompare(shadowMap, uv + vec2(dx1, 0.0), depth) + 56 | czm_shadowDepthCompare(shadowMap, uv + vec2(dx0, dy1), depth) + 57 | czm_shadowDepthCompare(shadowMap, uv + vec2(0.0, dy1), depth) + 58 | czm_shadowDepthCompare(shadowMap, uv + vec2(dx1, dy1), depth) 59 | ) * (1.0 / 9.0); 60 | #else 61 | float visibility = czm_shadowDepthCompare(shadowMap, uv, depth); 62 | #endif 63 | 64 | return czm_private_shadowVisibility(visibility, nDotL, normalShadingSmooth, darkness); 65 | } 66 | #endif 67 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/rayEllipsoidIntersectionInterval.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * DOC_TBA 3 | * 4 | * @name czm_rayEllipsoidIntersectionInterval 5 | * @glslFunction 6 | */ 7 | czm_raySegment czm_rayEllipsoidIntersectionInterval(czm_ray ray, vec3 ellipsoid_center, vec3 ellipsoid_inverseRadii) 8 | { 9 | // ray and ellipsoid center in eye coordinates. radii in model coordinates. 10 | vec3 q = ellipsoid_inverseRadii * (czm_inverseModelView * vec4(ray.origin, 1.0)).xyz; 11 | vec3 w = ellipsoid_inverseRadii * (czm_inverseModelView * vec4(ray.direction, 0.0)).xyz; 12 | 13 | q = q - ellipsoid_inverseRadii * (czm_inverseModelView * vec4(ellipsoid_center, 1.0)).xyz; 14 | 15 | float q2 = dot(q, q); 16 | float qw = dot(q, w); 17 | 18 | if (q2 > 1.0) // Outside ellipsoid. 19 | { 20 | if (qw >= 0.0) // Looking outward or tangent (0 intersections). 21 | { 22 | return czm_emptyRaySegment; 23 | } 24 | else // qw < 0.0. 25 | { 26 | float qw2 = qw * qw; 27 | float difference = q2 - 1.0; // Positively valued. 28 | float w2 = dot(w, w); 29 | float product = w2 * difference; 30 | 31 | if (qw2 < product) // Imaginary roots (0 intersections). 32 | { 33 | return czm_emptyRaySegment; 34 | } 35 | else if (qw2 > product) // Distinct roots (2 intersections). 36 | { 37 | float discriminant = qw * qw - product; 38 | float temp = -qw + sqrt(discriminant); // Avoid cancellation. 39 | float root0 = temp / w2; 40 | float root1 = difference / temp; 41 | if (root0 < root1) 42 | { 43 | czm_raySegment i = czm_raySegment(root0, root1); 44 | return i; 45 | } 46 | else 47 | { 48 | czm_raySegment i = czm_raySegment(root1, root0); 49 | return i; 50 | } 51 | } 52 | else // qw2 == product. Repeated roots (2 intersections). 53 | { 54 | float root = sqrt(difference / w2); 55 | czm_raySegment i = czm_raySegment(root, root); 56 | return i; 57 | } 58 | } 59 | } 60 | else if (q2 < 1.0) // Inside ellipsoid (2 intersections). 61 | { 62 | float difference = q2 - 1.0; // Negatively valued. 63 | float w2 = dot(w, w); 64 | float product = w2 * difference; // Negatively valued. 65 | float discriminant = qw * qw - product; 66 | float temp = -qw + sqrt(discriminant); // Positively valued. 67 | czm_raySegment i = czm_raySegment(0.0, temp / w2); 68 | return i; 69 | } 70 | else // q2 == 1.0. On ellipsoid. 71 | { 72 | if (qw < 0.0) // Looking inward. 73 | { 74 | float w2 = dot(w, w); 75 | czm_raySegment i = czm_raySegment(0.0, -qw / w2); 76 | return i; 77 | } 78 | else // qw >= 0.0. Looking outward or tangent. 79 | { 80 | return czm_emptyRaySegment; 81 | } 82 | } 83 | } 84 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/sampleOctahedralProjection.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Samples the 4 neighboring pixels and return the weighted average. 3 | * 4 | * @private 5 | */ 6 | vec3 czm_sampleOctahedralProjectionWithFiltering(sampler2D projectedMap, vec2 textureSize, vec3 direction, float lod) 7 | { 8 | direction /= dot(vec3(1.0), abs(direction)); 9 | vec2 rev = abs(direction.zx) - vec2(1.0); 10 | vec2 neg = vec2(direction.x < 0.0 ? rev.x : -rev.x, 11 | direction.z < 0.0 ? rev.y : -rev.y); 12 | vec2 uv = direction.y < 0.0 ? neg : direction.xz; 13 | vec2 coord = 0.5 * uv + vec2(0.5); 14 | vec2 pixel = 1.0 / textureSize; 15 | 16 | if (lod > 0.0) 17 | { 18 | // Each subseqeuent mip level is half the size 19 | float scale = 1.0 / pow(2.0, lod); 20 | float offset = ((textureSize.y + 1.0) / textureSize.x); 21 | 22 | coord.x *= offset; 23 | coord *= scale; 24 | 25 | coord.x += offset + pixel.x; 26 | coord.y += (1.0 - (1.0 / pow(2.0, lod - 1.0))) + pixel.y * (lod - 1.0) * 2.0; 27 | } 28 | else 29 | { 30 | coord.x *= (textureSize.y / textureSize.x); 31 | } 32 | 33 | // Do bilinear filtering 34 | #ifndef OES_texture_float_linear 35 | vec3 color1 = texture2D(projectedMap, coord + vec2(0.0, pixel.y)).rgb; 36 | vec3 color2 = texture2D(projectedMap, coord + vec2(pixel.x, 0.0)).rgb; 37 | vec3 color3 = texture2D(projectedMap, coord + pixel).rgb; 38 | vec3 color4 = texture2D(projectedMap, coord).rgb; 39 | 40 | vec2 texturePosition = coord * textureSize; 41 | 42 | float fu = fract(texturePosition.x); 43 | float fv = fract(texturePosition.y); 44 | 45 | vec3 average1 = mix(color4, color2, fu); 46 | vec3 average2 = mix(color1, color3, fu); 47 | 48 | vec3 color = mix(average1, average2, fv); 49 | #else 50 | vec3 color = texture2D(projectedMap, coord).rgb; 51 | #endif 52 | 53 | return color; 54 | } 55 | 56 | 57 | /** 58 | * Samples from a cube map that has been projected using an octahedral projection from the given direction. 59 | * 60 | * @name czm_sampleOctahedralProjection 61 | * @glslFunction 62 | * 63 | * @param {sampler2D} projectedMap The texture with the octahedral projected cube map. 64 | * @param {vec2} textureSize The width and height dimensions in pixels of the projected map. 65 | * @param {vec3} direction The normalized direction used to sample the cube map. 66 | * @param {float} lod The level of detail to sample. 67 | * @param {float} maxLod The maximum level of detail. 68 | * @returns {vec3} The color of the cube map at the direction. 69 | */ 70 | vec3 czm_sampleOctahedralProjection(sampler2D projectedMap, vec2 textureSize, vec3 direction, float lod, float maxLod) { 71 | float currentLod = floor(lod + 0.5); 72 | float nextLod = min(currentLod + 1.0, maxLod); 73 | 74 | vec3 colorCurrentLod = czm_sampleOctahedralProjectionWithFiltering(projectedMap, textureSize, direction, currentLod); 75 | vec3 colorNextLod = czm_sampleOctahedralProjectionWithFiltering(projectedMap, textureSize, direction, nextLod); 76 | 77 | return mix(colorNextLod, colorCurrentLod, nextLod - lod); 78 | } 79 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/octDecode.glsl: -------------------------------------------------------------------------------- 1 | /** 2 | * Decodes a unit-length vector in 'oct' encoding to a normalized 3-component Cartesian vector. 3 | * The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors", 4 | * Cigolle et al 2014: http://jcgt.org/published/0003/02/01/ 5 | * 6 | * @name czm_octDecode 7 | * @param {vec2} encoded The oct-encoded, unit-length vector 8 | * @param {float} range The maximum value of the SNORM range. The encoded vector is stored in log2(rangeMax+1) bits. 9 | * @returns {vec3} The decoded and normalized vector 10 | */ 11 | vec3 czm_octDecode(vec2 encoded, float range) 12 | { 13 | if (encoded.x == 0.0 && encoded.y == 0.0) { 14 | return vec3(0.0, 0.0, 0.0); 15 | } 16 | 17 | encoded = encoded / range * 2.0 - 1.0; 18 | vec3 v = vec3(encoded.x, encoded.y, 1.0 - abs(encoded.x) - abs(encoded.y)); 19 | if (v.z < 0.0) 20 | { 21 | v.xy = (1.0 - abs(v.yx)) * czm_signNotZero(v.xy); 22 | } 23 | 24 | return normalize(v); 25 | } 26 | 27 | /** 28 | * Decodes a unit-length vector in 'oct' encoding to a normalized 3-component Cartesian vector. 29 | * The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors", 30 | * Cigolle et al 2014: http://jcgt.org/published/0003/02/01/ 31 | * 32 | * @name czm_octDecode 33 | * @param {vec2} encoded The oct-encoded, unit-length vector 34 | * @returns {vec3} The decoded and normalized vector 35 | */ 36 | vec3 czm_octDecode(vec2 encoded) 37 | { 38 | return czm_octDecode(encoded, 255.0); 39 | } 40 | 41 | /** 42 | * Decodes a unit-length vector in 'oct' encoding packed into a floating-point number to a normalized 3-component Cartesian vector. 43 | * The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors", 44 | * Cigolle et al 2014: http://jcgt.org/published/0003/02/01/ 45 | * 46 | * @name czm_octDecode 47 | * @param {float} encoded The oct-encoded, unit-length vector 48 | * @returns {vec3} The decoded and normalized vector 49 | */ 50 | vec3 czm_octDecode(float encoded) 51 | { 52 | float temp = encoded / 256.0; 53 | float x = floor(temp); 54 | float y = (temp - x) * 256.0; 55 | return czm_octDecode(vec2(x, y)); 56 | } 57 | 58 | /** 59 | * Decodes three unit-length vectors in 'oct' encoding packed into two floating-point numbers to normalized 3-component Cartesian vectors. 60 | * The 'oct' encoding is described in "A Survey of Efficient Representations of Independent Unit Vectors", 61 | * Cigolle et al 2014: http://jcgt.org/published/0003/02/01/ 62 | * 63 | * @name czm_octDecode 64 | * @param {vec2} encoded The packed oct-encoded, unit-length vectors. 65 | * @param {vec3} vector1 One decoded and normalized vector. 66 | * @param {vec3} vector2 One decoded and normalized vector. 67 | * @param {vec3} vector3 One decoded and normalized vector. 68 | */ 69 | void czm_octDecode(vec2 encoded, out vec3 vector1, out vec3 vector2, out vec3 vector3) 70 | { 71 | float temp = encoded.x / 65536.0; 72 | float x = floor(temp); 73 | float encodedFloat1 = (temp - x) * 65536.0; 74 | 75 | temp = encoded.y / 65536.0; 76 | float y = floor(temp); 77 | float encodedFloat2 = (temp - y) * 65536.0; 78 | 79 | vector1 = czm_octDecode(encodedFloat1); 80 | vector2 = czm_octDecode(encodedFloat2); 81 | vector3 = czm_octDecode(vec2(x, y)); 82 | } 83 | 84 | -------------------------------------------------------------------------------- /ShaderSource/Builtin/Functions/pbrLighting.glsl: -------------------------------------------------------------------------------- 1 | vec3 lambertianDiffuse(vec3 diffuseColor) 2 | { 3 | return diffuseColor / czm_pi; 4 | } 5 | 6 | vec3 fresnelSchlick2(vec3 f0, vec3 f90, float VdotH) 7 | { 8 | return f0 + (f90 - f0) * pow(clamp(1.0 - VdotH, 0.0, 1.0), 5.0); 9 | } 10 | 11 | float smithVisibilityG1(float NdotV, float roughness) 12 | { 13 | // this is the k value for direct lighting. 14 | // for image based lighting it will be roughness^2 / 2 15 | float k = (roughness + 1.0) * (roughness + 1.0) / 8.0; 16 | return NdotV / (NdotV * (1.0 - k) + k); 17 | } 18 | 19 | float smithVisibilityGGX(float roughness, float NdotL, float NdotV) 20 | { 21 | return ( 22 | smithVisibilityG1(NdotL, roughness) * 23 | smithVisibilityG1(NdotV, roughness) 24 | ); 25 | } 26 | 27 | float GGX(float roughness, float NdotH) 28 | { 29 | float roughnessSquared = roughness * roughness; 30 | float f = (NdotH * roughnessSquared - NdotH) * NdotH + 1.0; 31 | return roughnessSquared / (czm_pi * f * f); 32 | } 33 | 34 | /** 35 | * Compute the diffuse and specular contributions using physically based 36 | * rendering. This function only handles direct lighting. 37 | *

38 | * This function only handles the lighting calculations. Metallic/roughness 39 | * and specular/glossy must be handled separately. See {@czm_pbrMetallicRoughnessMaterial}, {@czm_pbrSpecularGlossinessMaterial} and {@czm_defaultPbrMaterial} 40 | *

41 | * 42 | * @name czm_pbrlighting 43 | * @glslFunction 44 | * 45 | * @param {vec3} positionEC The position of the fragment in eye coordinates 46 | * @param {vec3} normalEC The surface normal in eye coordinates 47 | * @param {vec3} lightDirectionEC Unit vector pointing to the light source in eye coordinates. 48 | * @param {vec3} lightColorHdr radiance of the light source. This is a HDR value. 49 | * @param {czm_pbrParameters} The computed PBR parameters. 50 | * @return {vec3} The computed HDR color 51 | * 52 | * @example 53 | * czm_pbrParameters pbrParameters = czm_pbrMetallicRoughnessMaterial( 54 | * baseColor, 55 | * metallic, 56 | * roughness 57 | * ); 58 | * vec3 color = czm_pbrlighting( 59 | * positionEC, 60 | * normalEC, 61 | * lightDirectionEC, 62 | * lightColorHdr, 63 | * pbrParameters); 64 | */ 65 | vec3 czm_pbrLighting( 66 | vec3 positionEC, 67 | vec3 normalEC, 68 | vec3 lightDirectionEC, 69 | vec3 lightColorHdr, 70 | czm_pbrParameters pbrParameters 71 | ) { 72 | vec3 v = -normalize(positionEC); 73 | vec3 l = normalize(lightDirectionEC); 74 | vec3 h = normalize(v + l); 75 | vec3 n = normalEC; 76 | float NdotL = clamp(dot(n, l), 0.001, 1.0); 77 | float NdotV = abs(dot(n, v)) + 0.001; 78 | float NdotH = clamp(dot(n, h), 0.0, 1.0); 79 | float LdotH = clamp(dot(l, h), 0.0, 1.0); 80 | float VdotH = clamp(dot(v, h), 0.0, 1.0); 81 | 82 | vec3 f0 = pbrParameters.f0; 83 | float reflectance = max(max(f0.r, f0.g), f0.b); 84 | vec3 f90 = vec3(clamp(reflectance * 25.0, 0.0, 1.0)); 85 | vec3 F = fresnelSchlick2(f0, f90, VdotH); 86 | 87 | float alpha = pbrParameters.roughness; 88 | float G = smithVisibilityGGX(alpha, NdotL, NdotV); 89 | float D = GGX(alpha, NdotH); 90 | vec3 specularContribution = F * G * D / (4.0 * NdotL * NdotV); 91 | 92 | vec3 diffuseColor = pbrParameters.diffuseColor; 93 | // F here represents the specular contribution 94 | vec3 diffuseContribution = (1.0 - F) * lambertianDiffuse(diffuseColor); 95 | 96 | // Lo = kD * albedo/pi + specular * Li * NdotL 97 | return (diffuseContribution + specularContribution) + NdotL * lightColorHdr; 98 | } 99 | -------------------------------------------------------------------------------- /ShaderSource/Vector3DTileClampedPolylinesVS.glsl: -------------------------------------------------------------------------------- 1 | attribute vec3 startEllipsoidNormal; 2 | attribute vec3 endEllipsoidNormal; 3 | attribute vec4 startPositionAndHeight; 4 | attribute vec4 endPositionAndHeight; 5 | attribute vec4 startFaceNormalAndVertexCorner; 6 | attribute vec4 endFaceNormalAndHalfWidth; 7 | attribute float a_batchId; 8 | 9 | uniform mat4 u_modifiedModelView; 10 | uniform vec2 u_minimumMaximumVectorHeights; 11 | 12 | varying vec4 v_startPlaneEC; 13 | varying vec4 v_endPlaneEC; 14 | varying vec4 v_rightPlaneEC; 15 | varying float v_halfWidth; 16 | varying vec3 v_volumeUpEC; 17 | 18 | void main() 19 | { 20 | // vertex corner IDs 21 | // 3-----------7 22 | // /| left /| 23 | // / | 1 / | 24 | // 2-----------6 5 end 25 | // | / | / 26 | // start |/ right |/ 27 | // 0-----------4 28 | // 29 | float isEnd = floor(startFaceNormalAndVertexCorner.w * 0.251); // 0 for front, 1 for end 30 | float isTop = floor(startFaceNormalAndVertexCorner.w * mix(0.51, 0.19, isEnd)); // 0 for bottom, 1 for top 31 | 32 | vec3 forward = endPositionAndHeight.xyz - startPositionAndHeight.xyz; 33 | vec3 right = normalize(cross(forward, startEllipsoidNormal)); 34 | 35 | vec4 position = vec4(startPositionAndHeight.xyz, 1.0); 36 | position.xyz += forward * isEnd; 37 | 38 | v_volumeUpEC = czm_normal * normalize(cross(right, forward)); 39 | 40 | // Push for volume height 41 | float offset; 42 | vec3 ellipsoidNormal = mix(startEllipsoidNormal, endEllipsoidNormal, isEnd); 43 | 44 | // offset height to create volume 45 | offset = mix(startPositionAndHeight.w, endPositionAndHeight.w, isEnd); 46 | offset = mix(u_minimumMaximumVectorHeights.y, u_minimumMaximumVectorHeights.x, isTop) - offset; 47 | position.xyz += offset * ellipsoidNormal; 48 | 49 | // move from RTC to EC 50 | position = u_modifiedModelView * position; 51 | right = czm_normal * right; 52 | 53 | // Push for width in a direction that is in the start or end plane and in a plane with right 54 | // N = normalEC ("right-facing" direction for push) 55 | // R = right 56 | // p = angle between N and R 57 | // w = distance to push along R if R == N 58 | // d = distance to push along N 59 | // 60 | // N R 61 | // { \ p| } * cos(p) = dot(N, R) = w / d 62 | // d\ \ | |w * d = w / dot(N, R) 63 | // { \| } 64 | // o---------- polyline segment ----> 65 | // 66 | vec3 scratchNormal = mix(-startFaceNormalAndVertexCorner.xyz, endFaceNormalAndHalfWidth.xyz, isEnd); 67 | scratchNormal = cross(scratchNormal, mix(startEllipsoidNormal, endEllipsoidNormal, isEnd)); 68 | vec3 miterPushNormal = czm_normal * normalize(scratchNormal); 69 | 70 | offset = 2.0 * endFaceNormalAndHalfWidth.w * max(0.0, czm_metersPerPixel(position)); // offset = widthEC 71 | offset = offset / dot(miterPushNormal, right); 72 | position.xyz += miterPushNormal * (offset * sign(0.5 - mod(startFaceNormalAndVertexCorner.w, 2.0))); 73 | 74 | gl_Position = czm_depthClamp(czm_projection * position); 75 | 76 | position = u_modifiedModelView * vec4(startPositionAndHeight.xyz, 1.0); 77 | vec3 startNormalEC = czm_normal * startFaceNormalAndVertexCorner.xyz; 78 | v_startPlaneEC = vec4(startNormalEC, -dot(startNormalEC, position.xyz)); 79 | v_rightPlaneEC = vec4(right, -dot(right, position.xyz)); 80 | 81 | position = u_modifiedModelView * vec4(endPositionAndHeight.xyz, 1.0); 82 | vec3 endNormalEC = czm_normal * endFaceNormalAndHalfWidth.xyz; 83 | v_endPlaneEC = vec4(endNormalEC, -dot(endNormalEC, position.xyz)); 84 | v_halfWidth = endFaceNormalAndHalfWidth.w; 85 | } 86 | --------------------------------------------------------------------------------