glsl.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. export default `
  2. #define USE_CUBE_MAP_SHADOW true
  3. uniform sampler2D colorTexture;
  4. uniform sampler2D depthTexture;
  5. in vec2 v_textureCoordinates;
  6. uniform mat4 camera_projection_matrix;
  7. uniform mat4 camera_view_matrix;
  8. uniform samplerCube shadowMap_textureCube;
  9. uniform mat4 shadowMap_matrix;
  10. uniform vec4 shadowMap_lightPositionEC;
  11. uniform vec4 shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness;
  12. uniform vec4 shadowMap_texelSizeDepthBiasAndNormalShadingSmooth;
  13. uniform float helsing_viewDistance;
  14. uniform vec4 helsing_visibleAreaColor;
  15. uniform vec4 helsing_invisibleAreaColor;
  16. struct zx_shadowParameters
  17. {
  18. vec3 texCoords;
  19. float depthBias;
  20. float depth;
  21. float nDotL;
  22. vec2 texelStepSize;
  23. float normalShadingSmooth;
  24. float darkness;
  25. };
  26. float czm_shadowVisibility(samplerCube shadowMap,zx_shadowParameters shadowParameters)
  27. {
  28. float depthBias = shadowParameters.depthBias;
  29. float depth = shadowParameters.depth;
  30. float nDotL = shadowParameters.nDotL;
  31. float normalShadingSmooth = shadowParameters.normalShadingSmooth;
  32. float darkness = shadowParameters.darkness;
  33. vec3 uvw = shadowParameters.texCoords;
  34. depth -= depthBias;
  35. float visibility = czm_shadowDepthCompare(shadowMap, uvw, depth);
  36. return czm_private_shadowVisibility(visibility, nDotL, normalShadingSmooth, darkness);
  37. }
  38. vec4 getPositionEC(){
  39. return czm_windowToEyeCoordinates(gl_FragCoord);
  40. }
  41. vec3 getNormalEC(){
  42. return vec3(1.);
  43. }
  44. vec4 toEye(in vec2 uv,in float depth){
  45. vec2 xy=vec2((uv.x*2.-1.),(uv.y*2.-1.));
  46. vec4 posInCamera=czm_inverseProjection*vec4(xy,depth,1.);
  47. posInCamera=posInCamera/posInCamera.w;
  48. return posInCamera;
  49. }
  50. float getDepth(in vec4 depth){
  51. float z_window=czm_unpackDepth(depth);
  52. z_window=czm_reverseLogDepth(z_window);
  53. float n_range=czm_depthRange.near;
  54. float f_range=czm_depthRange.far;
  55. return(2.*z_window-n_range-f_range)/(f_range-n_range);
  56. }
  57. float shadow(in vec4 positionEC,in float depth){
  58. vec3 normalEC=getNormalEC();
  59. zx_shadowParameters shadowParameters;
  60. shadowParameters.texelStepSize=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.xy;
  61. shadowParameters.depthBias=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.z;
  62. shadowParameters.normalShadingSmooth=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.w;
  63. shadowParameters.darkness=shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness.w;
  64. vec3 directionEC=positionEC.xyz-shadowMap_lightPositionEC.xyz;
  65. float distance=length(directionEC);
  66. directionEC=normalize(directionEC);
  67. float radius=shadowMap_lightPositionEC.w;
  68. if(distance>radius)
  69. {
  70. return 2.;
  71. }
  72. vec3 directionWC=czm_inverseViewRotation*directionEC;
  73. shadowParameters.depth=distance/radius-.0003;
  74. shadowParameters.nDotL=clamp(dot(normalEC,-directionEC),0.,1.);
  75. shadowParameters.texCoords=directionWC;
  76. float visibility=czm_shadowVisibility(shadowMap_textureCube,shadowParameters);
  77. return visibility;
  78. }
  79. bool visible(in vec4 result)
  80. {
  81. result.x/=result.w;
  82. result.y/=result.w;
  83. result.z/=result.w;
  84. return result.x>=-1.&&result.x<=1.
  85. &&result.y>=-1.&&result.y<=1.
  86. &&result.z>=-1.&&result.z<=1.;
  87. }
  88. void main(){
  89. out_FragColor=texture(colorTexture,v_textureCoordinates);
  90. float depth=getDepth(texture(depthTexture,v_textureCoordinates));
  91. vec4 viewPos=toEye(v_textureCoordinates,depth);
  92. // 世界坐标
  93. vec4 wordPos=czm_inverseView*viewPos;
  94. // 虚拟相机中坐标
  95. vec4 vcPos=camera_view_matrix*wordPos;
  96. float near=.001*helsing_viewDistance;
  97. float dis=length(vcPos.xyz);
  98. if(dis>near&&dis<helsing_viewDistance){
  99. // 透视投影
  100. vec4 posInEye=camera_projection_matrix*vcPos;
  101. // 可视区颜色
  102. // vec4 helsing_visibleAreaColor=vec4(0.,1.,0.,.5);
  103. // vec4 helsing_invisibleAreaColor=vec4(1.,0.,0.,.5);
  104. if(visible(posInEye)){
  105. float vis=shadow(viewPos,depth);
  106. if(vis>.3){
  107. out_FragColor=mix(out_FragColor,helsing_visibleAreaColor,.5);
  108. }else{
  109. out_FragColor=mix(out_FragColor,helsing_invisibleAreaColor,.5);
  110. }
  111. }
  112. }
  113. }`;