glsl.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. export default `
  2. varying vec2 v_textureCoordinates;
  3. uniform sampler2D colorTexture;
  4. uniform sampler2D depthTexture;
  5. uniform sampler2D shadowMap_texture;
  6. uniform mat4 shadowMap_matrix;
  7. uniform vec4 shadowMap_lightPositionEC;
  8. uniform vec4 shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness;
  9. uniform vec4 shadowMap_texelSizeDepthBiasAndNormalShadingSmooth;
  10. uniform int helsing_textureType;
  11. uniform sampler2D helsing_texture;
  12. uniform float helsing_alpha;
  13. uniform vec4 helsing_visibleAreaColor;
  14. uniform vec4 helsing_invisibleAreaColor;
  15. vec4 toEye(in vec2 uv, in float depth){
  16. vec2 xy = vec2((uv.x * 2.0 - 1.0),(uv.y * 2.0 - 1.0));
  17. vec4 posInCamera =czm_inverseProjection * vec4(xy, depth, 1.0);
  18. posInCamera =posInCamera / posInCamera.w;
  19. return posInCamera;
  20. }
  21. float getDepth(in vec4 depth){
  22. float z_window = czm_unpackDepth(depth);
  23. z_window = czm_reverseLogDepth(z_window);
  24. float n_range = czm_depthRange.near;
  25. float f_range = czm_depthRange.far;
  26. return (2.0 * z_window - n_range - f_range) / (f_range - n_range);
  27. }
  28. float _czm_sampleShadowMap(sampler2D shadowMap, vec2 uv){
  29. return texture2D(shadowMap, uv).r;
  30. }
  31. float _czm_shadowDepthCompare(sampler2D shadowMap, vec2 uv, float depth){
  32. return step(depth, _czm_sampleShadowMap(shadowMap, uv));
  33. }
  34. float _czm_shadowVisibility(sampler2D shadowMap, czm_shadowParameters shadowParameters){
  35. float depthBias = shadowParameters.depthBias;
  36. float depth = shadowParameters.depth;
  37. float nDotL = shadowParameters.nDotL;
  38. float normalShadingSmooth = shadowParameters.normalShadingSmooth;
  39. float darkness = shadowParameters.darkness;
  40. vec2 uv = shadowParameters.texCoords;
  41. depth -= depthBias;
  42. vec2 texelStepSize = shadowParameters.texelStepSize;
  43. float radius = 1.0;
  44. float dx0 = -texelStepSize.x * radius;
  45. float dy0 = -texelStepSize.y * radius;
  46. float dx1 = texelStepSize.x * radius;
  47. float dy1 = texelStepSize.y * radius;
  48. float visibility = (_czm_shadowDepthCompare(shadowMap, uv, depth)
  49. + _czm_shadowDepthCompare(shadowMap, uv + vec2(dx0, dy0), depth)
  50. + _czm_shadowDepthCompare(shadowMap, uv + vec2(0.0, dy0), depth)
  51. + _czm_shadowDepthCompare(shadowMap, uv + vec2(dx1, dy0), depth)
  52. + _czm_shadowDepthCompare(shadowMap, uv + vec2(dx0, 0.0), depth)
  53. + _czm_shadowDepthCompare(shadowMap, uv + vec2(dx1, 0.0), depth)
  54. + _czm_shadowDepthCompare(shadowMap, uv + vec2(dx0, dy1), depth)
  55. + _czm_shadowDepthCompare(shadowMap, uv + vec2(0.0, dy1), depth)
  56. + _czm_shadowDepthCompare(shadowMap, uv + vec2(dx1, dy1), depth)
  57. ) * (1.0 / 9.0);
  58. return visibility;
  59. }
  60. vec3 pointProjectOnPlane(in vec3 planeNormal, in vec3 planeOrigin, in vec3 point){
  61. vec3 v01 = point -planeOrigin;
  62. float d = dot(planeNormal, v01) ;
  63. return (point - planeNormal * d);
  64. }
  65. void main(){
  66. const float PI = 3.141592653589793;
  67. vec4 color = texture2D(colorTexture, v_textureCoordinates);
  68. vec4 currentDepth = texture2D(depthTexture, v_textureCoordinates);
  69. if(currentDepth.r >= 1.0){
  70. gl_FragColor = color;
  71. return;
  72. }
  73. float depth = getDepth(currentDepth);
  74. vec4 positionEC = toEye(v_textureCoordinates, depth);
  75. vec3 normalEC = vec3(1.0);
  76. czm_shadowParameters shadowParameters;
  77. shadowParameters.texelStepSize = shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.xy;
  78. shadowParameters.depthBias = shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.z;
  79. shadowParameters.normalShadingSmooth = shadowMap_texelSizeDepthBiasAndNormalShadingSmooth.w;
  80. shadowParameters.darkness = shadowMap_normalOffsetScaleDistanceMaxDistanceAndDarkness.w;
  81. shadowParameters.depthBias *= max(depth * 0.01, 1.0);
  82. vec3 directionEC = normalize(positionEC.xyz - shadowMap_lightPositionEC.xyz);
  83. float nDotL = clamp(dot(normalEC, -directionEC), 0.0, 1.0);
  84. vec4 shadowPosition = shadowMap_matrix * positionEC;
  85. shadowPosition /= shadowPosition.w;
  86. if (any(lessThan(shadowPosition.xyz, vec3(0.0))) || any(greaterThan(shadowPosition.xyz, vec3(1.0)))){
  87. gl_FragColor = color;
  88. return;
  89. }
  90. shadowParameters.texCoords = shadowPosition.xy;
  91. shadowParameters.depth = shadowPosition.z;
  92. shadowParameters.nDotL = nDotL;
  93. float visibility = _czm_shadowVisibility(shadowMap_texture, shadowParameters);
  94. if (helsing_textureType >1){ // 视频或图片模式
  95. vec4 videoColor = texture2D(helsing_texture, shadowPosition.xy);
  96. if (visibility == 1.0){
  97. gl_FragColor = mix(color, vec4(videoColor.xyz, 1.0), helsing_alpha * videoColor.a);
  98. }
  99. else{
  100. if(abs(shadowPosition.z - 0.0) < 0.01){
  101. return;
  102. }
  103. gl_FragColor = color;
  104. }
  105. }
  106. else{ // 可视域模式
  107. if (visibility == 1.0){
  108. gl_FragColor = mix(color, vec4(0.,1.,0.,.5), helsing_alpha);
  109. }
  110. else{
  111. if(abs(shadowPosition.z - 0.0) < 0.01){
  112. return;
  113. }
  114. gl_FragColor = mix(color, vec4(0.,1.,0.,.5), helsing_alpha);
  115. }
  116. }
  117. }`;