effect-planeShadow.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import XrFrame from 'XrFrame';
  2. const xrFrameSystem = wx.getXrFrameSystem();
  3. xrFrameSystem.registerEffect('plane-shadow', scene => scene.createEffect({
  4. name: "plane-shadow",
  5. properties: [
  6. // 需保证,至少存在一个properties用于创建effect共享内存
  7. {
  8. key: 'u_holder',
  9. type: xrFrameSystem.EUniformType.FLOAT4,
  10. default: [1, 1, 1, 1]
  11. }
  12. ],
  13. images: [],
  14. defaultRenderQueue: 3000,
  15. passes: [{
  16. "renderStates": {
  17. cullOn: false,
  18. blendOn: true,
  19. },
  20. lightMode: "ForwardBase",
  21. useMaterialRenderStates: true,
  22. shaders: [0, 1]
  23. }],
  24. shaders: [
  25. // Vertex
  26. `#version 100
  27. uniform highp mat4 u_view;
  28. uniform highp mat4 u_viewInverse;
  29. uniform highp mat4 u_vp;
  30. uniform highp mat4 u_projection;
  31. uniform highp mat4 u_world;
  32. attribute vec3 a_position;
  33. attribute highp vec2 a_texCoord;
  34. varying highp vec2 v_UV;
  35. #ifdef WX_RECEIVE_SHADOW
  36. varying highp vec3 v_WorldPosition;
  37. #endif
  38. void main()
  39. {
  40. v_UV = a_texCoord;
  41. vec4 worldPosition = u_world * vec4(a_position, 1.0);
  42. #ifdef WX_RECEIVE_SHADOW
  43. v_WorldPosition = worldPosition.xyz;
  44. #endif
  45. gl_Position = u_projection * u_view * worldPosition;
  46. }`,
  47. // Fragemenet
  48. `#version 100
  49. precision mediump float;
  50. precision highp int;
  51. varying highp vec2 v_UV;
  52. uniform vec4 u_ambientLightColorIns;
  53. uniform vec3 u_mainLightDir;
  54. uniform vec4 u_mainLightColorIns;
  55. #ifdef WX_RECEIVE_SHADOW
  56. varying highp vec3 v_WorldPosition;
  57. uniform float u_shadowStrength;
  58. uniform float u_shadowBias;
  59. uniform vec3 u_shadowColor;
  60. uniform sampler2D u_shadowMap;
  61. uniform mat4 u_csmLightSpaceMatrices[4];
  62. uniform vec4 u_csmFarBounds;
  63. uniform vec4 u_shadowTilingOffsets[4];
  64. #endif
  65. float unpackDepth(const in vec4 rgbaDepth)
  66. {
  67. vec4 bitShift = vec4(1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0);
  68. float depth = dot(rgbaDepth, bitShift);
  69. return depth;
  70. }
  71. float shadowCalculation(vec3 posWorld) {
  72. float srcShadow = 1.0;
  73. vec4 shadowCoord = u_csmLightSpaceMatrices[0] * vec4(posWorld, 1.0);
  74. shadowCoord.xyz = shadowCoord.xyz / shadowCoord.w;
  75. shadowCoord = shadowCoord * 0.5 + 0.5;
  76. shadowCoord.z = shadowCoord.z + step(shadowCoord.x, 0.001) + step(shadowCoord.y, 0.001) + step(0.999, shadowCoord.x) + step(0.999, shadowCoord.y);
  77. shadowCoord.xy = shadowCoord.xy * u_shadowTilingOffsets[0].xy + u_shadowTilingOffsets[0].zw;
  78. if (shadowCoord.z > 1.0)
  79. {
  80. shadowCoord.z = 1.0;
  81. }
  82. float currentDepth = shadowCoord.z;
  83. float bias = u_shadowBias;
  84. float zRef = currentDepth - bias;
  85. float sourceVal = float(zRef < unpackDepth(texture2D(u_shadowMap, shadowCoord.xy)));
  86. srcShadow = sourceVal;
  87. return srcShadow;
  88. }
  89. void main()
  90. {
  91. vec4 result = vec4(0.0);
  92. // MainLights
  93. #ifdef WX_USE_MAIN_DIR_LIGHT
  94. vec3 mainLightColor = u_mainLightColorIns.rgb * u_mainLightColorIns.a;
  95. #ifdef WX_RECEIVE_SHADOW
  96. float shadowScale = shadowCalculation(v_WorldPosition);
  97. result = vec4(mainLightColor * shadowScale, 1.0 - shadowScale);
  98. #endif
  99. #endif
  100. gl_FragData[0] = result;
  101. }
  102. `],
  103. }));