effect-fog-of-war-2d.effect 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - passes:
  5. - vert: sprite-vs:vert
  6. frag: sprite-fs:frag
  7. depthStencilState:
  8. depthTest: false
  9. depthWrite: false
  10. blendState:
  11. targets:
  12. - blend: true
  13. blendSrc: src_alpha
  14. blendDst: one_minus_src_alpha
  15. blendDstAlpha: one_minus_src_alpha
  16. rasterizerState:
  17. cullMode: none
  18. properties:
  19. alphaThreshold: { value: 0.5 }
  20. }%
  21. CCProgram sprite-vs %{
  22. precision highp float;
  23. #include <builtin/uniforms/cc-global>
  24. #if USE_LOCAL
  25. #include <builtin/uniforms/cc-local>
  26. #endif
  27. #if SAMPLE_FROM_RT
  28. #include <common/common-define>
  29. #endif
  30. in vec3 a_position;
  31. in vec2 a_texCoord;
  32. in vec4 a_color;
  33. out vec4 color;
  34. out vec2 uv0;
  35. out vec4 worldPos;
  36. vec4 vert () {
  37. vec4 pos = vec4(a_position, 1);
  38. #if USE_LOCAL
  39. pos = cc_matWorld * pos;
  40. #endif
  41. worldPos = pos;
  42. #if USE_PIXEL_ALIGNMENT
  43. pos = cc_matView * pos;
  44. pos.xyz = floor(pos.xyz);
  45. pos = cc_matProj * pos;
  46. #else
  47. pos = cc_matViewProj * pos;
  48. #endif
  49. uv0 = a_texCoord;
  50. #if SAMPLE_FROM_RT
  51. CC_HANDLE_RT_SAMPLE_FLIP(uv0);
  52. #endif
  53. color = a_color;
  54. return pos;
  55. }
  56. }%
  57. CCProgram sprite-fs %{
  58. precision highp float;
  59. #include <builtin/internal/embedded-alpha>
  60. #include <builtin/internal/alpha-test>
  61. #include <cc-global>
  62. in vec4 color;
  63. in vec4 worldPos;
  64. uniform Constants{
  65. vec4 fogOfWarParams;
  66. };
  67. #if USE_TEXTURE
  68. in vec2 uv0;
  69. #pragma builtin(local)
  70. layout(set = 2, binding = 12) uniform sampler2D cc_spriteTexture;
  71. #endif
  72. vec4 frag () {
  73. vec4 o = vec4(1, 1, 1, 1);
  74. #if USE_TEXTURE
  75. o *= CCSampleWithAlphaSeparated(cc_spriteTexture, uv0);
  76. #if IS_GRAY
  77. float gray = 0.2126 * o.r + 0.7152 * o.g + 0.0722 * o.b;
  78. o.r = o.g = o.b = gray;
  79. #endif
  80. #endif
  81. o *= color;
  82. float halfWidth = fogOfWarParams.x/2.0;
  83. float halfHeight = fogOfWarParams.y/2.0;
  84. float centerRadius = fogOfWarParams.z;
  85. float fadeOutRange = fogOfWarParams.w;
  86. vec3 diff = worldPos.xyz - vec3(halfWidth,halfHeight,0.0);
  87. o.a = clamp((sqrt(dot(diff,diff)) - centerRadius)/fadeOutRange,0.0,1.0);
  88. o.a = pow(o.a,2.0);
  89. ALPHA_TEST(o);
  90. return o;
  91. }
  92. }%