glsl2.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. export default `
  2. #define USE_CUBE_MAP_SHADOW true
  3. uniform sampler2D colorTexture;
  4. uniform sampler2D depthTexture;
  5. varying 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. vec3 pointProjectOnPlane(in vec3 planeNormal,in vec3 planeOrigin,in vec3 point){
  51. vec3 v01=point-planeOrigin;
  52. float d=dot(planeNormal,v01);
  53. return(point-planeNormal*d);
  54. }
  55. float getDepth(in vec4 depth){
  56. float z_window=czm_unpackDepth(depth);
  57. z_window=czm_reverseLogDepth(z_window);
  58. float n_range=czm_depthRange.near;
  59. float f_range=czm_depthRange.far;
  60. return(2.*z_window-n_range-f_range)/(f_range-n_range);
  61. }
  62. float shadow(in vec4 positionEC){
  63. vec3 normalEC=getNormalEC();
  64. zx_shadowParameters shadowParameters;
  65. shadowParameters.texelStepSize=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.xy;
  66. shadowParameters.depthBias=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.z;
  67. shadowParameters.normalShadingSmooth=shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.w;
  68. shadowParameters.darkness=shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness.w;
  69. vec3 directionEC=positionEC.xyz-shadowMap_lightPositionEC.xyz;
  70. float distance=length(directionEC);
  71. directionEC=normalize(directionEC);
  72. float radius=shadowMap_lightPositionEC.w;
  73. if(distance>radius)
  74. {
  75. return 2.0;
  76. }
  77. vec3 directionWC=czm_inverseViewRotation*directionEC;
  78. shadowParameters.depth=distance/radius-0.0003;
  79. shadowParameters.nDotL=clamp(dot(normalEC,-directionEC),0.,1.);
  80. shadowParameters.texCoords=directionWC;
  81. float visibility=czm_shadowVisibility(shadowMap_textureCube,shadowParameters);
  82. return visibility;
  83. }
  84. bool visible(in vec4 result)
  85. {
  86. result.x/=result.w;
  87. result.y/=result.w;
  88. result.z/=result.w;
  89. return result.x>=-1.&&result.x<=1.
  90. &&result.y>=-1.&&result.y<=1.
  91. &&result.z>=-1.&&result.z<=1.;
  92. }
  93. void main(){
  94. // 釉色 = 结构二维(颜色纹理, 纹理坐标)
  95. gl_FragColor = texture2D(colorTexture, v_textureCoordinates);
  96. // 深度 = 获取深度(结构二维(深度纹理, 纹理坐标))
  97. float depth = getDepth(texture2D(depthTexture, v_textureCoordinates));
  98. // 视角 = (纹理坐标, 深度)
  99. vec4 viewPos = toEye(v_textureCoordinates, depth);
  100. // 世界坐标
  101. vec4 wordPos = czm_inverseView * viewPos;
  102. // 虚拟相机中坐标
  103. vec4 vcPos = camera_view_matrix * wordPos;
  104. float near = .001 * helsing_viewDistance;
  105. float dis = length(vcPos.xyz);
  106. if(dis > near && dis < helsing_viewDistance){
  107. // 透视投影
  108. vec4 posInEye = camera_projection_matrix * vcPos;
  109. // 可视区颜色
  110. // vec4 helsing_visibleAreaColor=vec4(0.,1.,0.,.5);
  111. // vec4 helsing_invisibleAreaColor=vec4(1.,0.,0.,.5);
  112. if(visible(posInEye)){
  113. float vis = shadow(viewPos);
  114. if(vis > 0.3){
  115. gl_FragColor = mix(gl_FragColor,helsing_visibleAreaColor,.5);
  116. } else{
  117. gl_FragColor = mix(gl_FragColor,helsing_invisibleAreaColor,.5);
  118. }
  119. }
  120. }
  121. }`;