effect-shining.ts 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import XrFrame from 'XrFrame';
  2. const xrFrameSystem = wx.getXrFrameSystem();
  3. xrFrameSystem.registerEffect('shining', scene => scene.createEffect({
  4. name: "shining",
  5. properties: [
  6. {
  7. key: 'u_color',
  8. type: xrFrameSystem.EUniformType.FLOAT4,
  9. default: [1, 1, 1, 1]
  10. },
  11. {
  12. key: 'u_range',
  13. type: xrFrameSystem.EUniformType.FLOAT2,
  14. default: [0.5, 1]
  15. },
  16. {
  17. key: 'u_speed',
  18. type: xrFrameSystem.EUniformType.FLOAT,
  19. default: [1]
  20. },
  21. ],
  22. images: [
  23. {
  24. key: 'u_reflection',
  25. default: 'white',
  26. macro: 'WX_USE_REFLECTION'
  27. }
  28. ],
  29. defaultRenderQueue: 2000,
  30. passes: [{
  31. "renderStates": {
  32. cullOn: false,
  33. blendOn: false,
  34. depthWrite: true,
  35. },
  36. lightMode: "ForwardBase",
  37. useMaterialRenderStates: true,
  38. shaders: [0, 1]
  39. }],
  40. shaders: [`#version 100
  41. uniform mat4 u_world;
  42. uniform mat4 u_view;
  43. uniform mat4 u_projection;
  44. uniform mat4 u_viewInverse;
  45. attribute vec3 a_position;
  46. attribute vec3 a_normal;
  47. #ifdef WX_USE_NORMAL
  48. varying highp vec3 v_normal;
  49. varying highp vec3 v_viewDir;
  50. #endif
  51. void main()
  52. {
  53. vec4 worldP = u_world * vec4(a_position, 1.0);
  54. #ifdef WX_USE_NORMAL
  55. v_normal = normalize(mat3(u_world) * a_normal);
  56. v_viewDir = normalize(worldP.xyz - u_viewInverse[3].xyz);
  57. #endif
  58. gl_Position = u_projection * u_view * worldP;
  59. }
  60. `,
  61. `#version 100
  62. #define RECIPROCAL_PI 0.3183098861837907
  63. precision highp float;
  64. precision highp int;
  65. #ifdef WX_USE_NORMAL
  66. varying highp vec3 v_normal;
  67. varying highp vec3 v_viewDir;
  68. #endif
  69. uniform float u_gameTime;
  70. uniform vec4 u_color;
  71. uniform vec2 u_range;
  72. uniform float u_speed;
  73. uniform sampler2D u_reflection;
  74. vec4 textureBilinearEnvMap(sampler2D texture, vec3 direction){
  75. float uvX = (atan(direction.x, direction.z) ) * RECIPROCAL_PI * 0.5 + 0.5;
  76. float uvY = acos(direction.y) * RECIPROCAL_PI;
  77. vec2 uv = vec2(uvX, uvY);
  78. vec4 envmap = texture2D(texture, uv);
  79. return envmap;
  80. }
  81. void main()
  82. {
  83. vec4 color;
  84. #if defined(WX_USE_REFLECTION) && defined(WX_USE_NORMAL)
  85. vec3 reflectVec = reflect(-v_viewDir, v_normal);
  86. color = textureBilinearEnvMap(u_reflection, reflectVec) * u_color;
  87. #else
  88. color = u_color;
  89. #endif
  90. gl_FragData[0] = (u_range.x + sin(mod(u_gameTime * u_speed, 3.14)) * (u_range.y - u_range.x)) * color;
  91. }
  92. `],
  93. }));