Project scenario:
I am implementing a visual domain analysis based on cesium version 106. Because I did it in version 1.96 before, I just moved the code directly.
Problem description
When I executed the function module, an error occurred varying: only allowed at global scope
Part of the code
const postStage = new Cesium.PostProcessStage({<!-- --> fragmentShader: ` #define USE_CUBE_MAP_SHADOW true uniform sampler2D colorTexture; uniform sampler2D depthTexture; varying vec2 v_textureCoordinates; uniform mat4 camera_projection_matrix; uniform mat4 camera_view_matrix; uniform samplerCube shadowMap_textureCube; uniform mat4 shadowMap_matrix; uniform vec4 shadowMap_lightPositionEC; uniform vec4 shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness; uniform vec4 shadowMap_texelSizeDepthBiasAndNormalShadingSmooth; uniform float helsing_viewDistance; uniform vec4 helsing_visibleAreaColor; uniform vec4 helsing_invisibleAreaColor; struct zx_shadowParameters { vec3 texCoords; float depthBias; float depth; float nDotL; vec2 texelStepSize; float normalShadingSmooth; float darkness; }; float czm_shadowVisibility(samplerCube shadowMap, zx_shadowParameters shadowParameters) { float depthBias = shadowParameters.depthBias; float depth = shadowParameters.depth; float nDotL = shadowParameters.nDotL; float normalShadingSmooth = shadowParameters.normalShadingSmooth; float darkness = shadowParameters.darkness; vec3 uvw = shadowParameters.texCoords; depth -= depthBias; float visibility = czm_shadowDepthCompare(shadowMap, uvw, depth); return czm_private_shadowVisibility(visibility, nDotL, normalShadingSmooth, darkness); } vec4 getPositionEC(){ return czm_windowToEyeCoordinates(gl_FragCoord); } vec3 getNormalEC(){ return vec3(1.); } vec4 toEye(in vec2 uv,in float depth){ vec2 xy=vec2((uv.x*2.-1.),(uv.y*2.-1.)); vec4 posInCamera=czm_inverseProjection*vec4(xy,depth,1.); posInCamera=posInCamera/posInCamera.w; return posInCamera; } vec3 pointProjectOnPlane(in vec3 planeNormal,in vec3 planeOrigin,in vec3 point){ vec3 v01=point-planeOrigin; float d=dot(planeNormal,v01); return(point-planeNormal*d); } float getDepth(in vec4 depth){ float z_window=czm_unpackDepth(depth); z_window=czm_reverseLogDepth(z_window); float n_range=czm_depthRange.near; float f_range=czm_depthRange.far; return(2.*z_window-n_range-f_range)/(f_range-n_range); } float shadow(in vec4 positionEC){ vec3 normalEC=getNormalEC(); zx_shadowParameters shadowParameters; shadowParameters.texelStepSize=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.xy; shadowParameters.depthBias=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.z; shadowParameters.normalShadingSmooth=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.w; shadowParameters.darkness=shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness.w; vec3 directionEC=positionEC.xyz-shadowMap_lightPositionEC.xyz; float distance=length(directionEC); directionEC=normalize(directionEC); float radius=shadowMap_lightPositionEC.w; if(distance>radius) { return 2.0; } vec3 directionWC=czm_inverseViewRotation*directionEC; shadowParameters.depth=distance/radius-0.0003; shadowParameters.nDotL=clamp(dot(normalEC,-directionEC),0.,1.); shadowParameters.texCoords=directionWC; float visibility=czm_shadowVisibility(shadowMap_textureCube,shadowParameters); return visibility; } bool visible(in vec4 result) { result.x/=result.w; result.y/=result.w; result.z/=result.w; return result.x>=-1. & amp; & amp;result.x<=1. & amp; & amp;result.y>=-1. & amp; & amp;result.y<=1. & amp; & amp;result.z>=-1. & amp; & amp;result.z<=1.; } void main(){ // Glaze color = structure 2D (color texture, texture coordinates) gl_FragColor = texture2D(colorTexture, v_textureCoordinates); //Depth = get depth(structure 2D(depth texture, texture coordinates)) float depth = getDepth(texture2D(depthTexture, v_textureCoordinates)); //View = (texture coordinates, depth) vec4 viewPos = toEye(v_textureCoordinates, depth); // world coordinates vec4 wordPos = czm_inverseView * viewPos; //Coordinates in the virtual camera vec4 vcPos = camera_view_matrix * wordPos; float near = .001 * helsing_viewDistance; float dis = length(vcPos.xyz); if(dis > near & amp; & amp; dis < helsing_viewDistance){ //Perspective projection vec4 posInEye = camera_projection_matrix * vcPos; //Visual area color // vec4 helsing_visibleAreaColor=vec4(0.,1.,0.,.5); // vec4 helsing_invisibleAreaColor=vec4(1.,0.,0.,.5); if(visible(posInEye)){ float vis = shadow(viewPos); if(vis > 0.3){ gl_FragColor = mix(gl_FragColor,helsing_visibleAreaColor,.5); } else{ gl_FragColor = mix(gl_FragColor,helsing_invisibleAreaColor,.5); } } } }`, uniforms: {<!-- -->} });
Way to try:
(1) Enable webgl1 when creating a container
viewer=new Cesium.Viewer(containerId,{<!-- --> infoBox: false, contextOptions: {<!-- --> requestWebgl1: true } })
(2) Modify varying to in/out, but it never works;
(3) Try to remove the “in” in the parameters of some methods, and then it will be fine. I have not studied GLSL very much, and I don’t know the specific reason.
export default ` #define USE_CUBE_MAP_SHADOW true; uniform sampler2D colorTexture; uniform sampler2D depthTexture; varying vec2 v_textureCoordinates; uniform mat4 camera_projection_matrix; uniform mat4 camera_view_matrix; uniform samplerCube shadowMap_textureCube; uniform mat4 shadowMap_matrix; uniform vec4 shadowMap_lightPositionEC; uniform vec4 shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness; uniform vec4 shadowMap_texelSizeDepthBiasAndNormalShadingSmooth; uniform float helsing_viewDistance; uniform vec4 helsing_visibleAreaColor; uniform vec4 helsing_invisibleAreaColor; struct zx_shadowParameters { vec3 texCoords; float depthBias; float depth; float nDotL; vec2 texelStepSize; float normalShadingSmooth; float darkness; }; float czm_shadowVisibility(samplerCube shadowMap, zx_shadowParameters shadowParameters) { float depthBias = shadowParameters.depthBias; float depth = shadowParameters.depth; float nDotL = shadowParameters.nDotL; float normalShadingSmooth = shadowParameters.normalShadingSmooth; float darkness = shadowParameters.darkness; vec3 uvw = shadowParameters.texCoords; depth -= depthBias; float visibility = czm_shadowDepthCompare(shadowMap, uvw, depth); return czm_private_shadowVisibility(visibility, nDotL, normalShadingSmooth, darkness); } vec4 getPositionEC(){ return czm_windowToEyeCoordinates(gl_FragCoord); } vec3 getNormalEC(){ return vec3(1.); } vec4 toEye(vec2 uv, float depth){ vec2 xy=vec2((uv.x*2.-1.),(uv.y*2.-1.)); vec4 posInCamera=czm_inverseProjection*vec4(xy,depth,1.); posInCamera=posInCamera/posInCamera.w; return posInCamera; } vec3 pointProjectOnPlane(vec3 planeNormal, vec3 planeOrigin, vec3 point){ vec3 v01=point-planeOrigin; float d=dot(planeNormal,v01); return(point-planeNormal*d); } float getDepth(vec4 depth){ float z_window=czm_unpackDepth(depth); z_window=czm_reverseLogDepth(z_window); float n_range=czm_depthRange.near; float f_range=czm_depthRange.far; return(2.*z_window-n_range-f_range)/(f_range-n_range); } float shadow(vec4 positionEC){ vec3 normalEC=getNormalEC(); zx_shadowParameters shadowParameters; shadowParameters.texelStepSize=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.xy; shadowParameters.depthBias=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.z; shadowParameters.normalShadingSmooth=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.w; shadowParameters.darkness=shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness.w; vec3 directionEC=positionEC.xyz-shadowMap_lightPositionEC.xyz; float distance=length(directionEC); directionEC=normalize(directionEC); float radius=shadowMap_lightPositionEC.w; if(distance>radius) { return 2.0; } vec3 directionWC=czm_inverseViewRotation*directionEC; shadowParameters.depth=distance/radius-0.0003; shadowParameters.nDotL=clamp(dot(normalEC,-directionEC),0.,1.); shadowParameters.texCoords=directionWC; float visibility=czm_shadowVisibility(shadowMap_textureCube,shadowParameters); return visibility; } bool visible(vec4 result) { result.x/=result.w; result.y/=result.w; result.z/=result.w; return result.x>=-1. & amp; & amp;result.x<=1. & amp; & amp;result.y>=-1. & amp; & amp;result.y<=1. & amp; & amp;result.z>=-1. & amp; & amp;result.z<=1.; } void main(){ // Glaze color = structure 2D (color texture, texture coordinates) gl_FragColor = texture2D(colorTexture, v_textureCoordinates); //Depth = get depth(structure 2D(depth texture, texture coordinates)) float depth = getDepth(texture2D(depthTexture, v_textureCoordinates)); //View = (texture coordinates, depth) vec4 viewPos = toEye(v_textureCoordinates, depth); // world coordinates vec4 wordPos = czm_inverseView * viewPos; //Coordinates in the virtual camera vec4 vcPos = camera_view_matrix * wordPos; float near = .001 * helsing_viewDistance; float dis = length(vcPos.xyz); if(dis > near & amp; & amp; dis < helsing_viewDistance){ // perspective projection vec4 posInEye = camera_projection_matrix * vcPos; //Visible area color // vec4 helsing_visibleAreaColor=vec4(0.,1.,0.,.5); // vec4 helsing_invisibleAreaColor=vec4(1.,0.,0.,.5); if(visible(posInEye)){ float vis = shadow(viewPos); if(vis > 0.3){ gl_FragColor = mix(gl_FragColor,helsing_visibleAreaColor,.5); } else{ gl_FragColor = mix(gl_FragColor,helsing_invisibleAreaColor,.5); } } } } `;
If anyone feels that there is any infringement, please contact me, thank you