bloom-mipmap-blur.effect 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
  2. // Based on Mipmap Filter Blur
  3. // https://github.com/gamemcu/interstellar-next
  4. CCEffect %{
  5. techniques:
  6. - passes:
  7. - vert: base-vs:vert
  8. frag: prefilter-fs:frag
  9. pass: cc-bloom-mipmap-prefilter
  10. depthStencilState:
  11. depthTest: false
  12. depthWrite: false
  13. - vert: downsample-vs:vert
  14. frag: downsample-fs:frag
  15. pass: cc-bloom-mipmap-downsample
  16. depthStencilState:
  17. depthTest: false
  18. depthWrite: false
  19. - vert: upsample-vs:vert
  20. frag: upsample-fs:frag
  21. pass: cc-bloom-mipmap-upsample
  22. depthStencilState:
  23. depthTest: false
  24. depthWrite: false
  25. - vert: base-vs:vert
  26. frag: combine-fs:frag
  27. pass: cc-bloom-mipmap-combine
  28. depthStencilState:
  29. depthTest: false
  30. depthWrite: false
  31. blendState:
  32. targets:
  33. - blend: true
  34. blendSrc: one
  35. blendDst: one
  36. blendSrcAlpha: zero
  37. blendDstAlpha: one
  38. }%
  39. CCProgram base-vs %{
  40. precision mediump float;
  41. #include <legacy/decode-standard>
  42. #include <post-process/pipeline>
  43. out vec2 vUv;
  44. out vec2 vUv00;
  45. out vec2 vUv01;
  46. out vec2 vUv02;
  47. out vec2 vUv03;
  48. out vec2 vUv04;
  49. vec4 vert() {
  50. vUv = a_texCoord;
  51. StandardVertInput In;
  52. CCDecode(In);
  53. FLIP_VULKAN_NDC(In.position);
  54. return In.position;
  55. }
  56. }%
  57. CCProgram prefilter-fs %{
  58. precision mediump float;
  59. #pragma rate BloomUBO pass
  60. uniform BloomUBO {
  61. mediump vec4 bloomParams; // x: useHDRIlluminance z: threshold, w: enableAlphaMask
  62. };
  63. in vec2 vUv;
  64. #pragma rate mainTexture pass
  65. uniform sampler2D mainTexture;
  66. float luminance(vec3 color) {
  67. return dot(color, vec3(0.2126729, 0.7151522, 0.0721750));
  68. }
  69. vec4 frag() {
  70. vec4 pixel = texture(mainTexture, vUv);
  71. float l = smoothstep(bloomParams.z, bloomParams.z + 0.025, luminance(pixel.rgb));
  72. return vec4(pixel.rgb * l, l);
  73. }
  74. }%
  75. CCProgram downsample-vs %{
  76. precision mediump float;
  77. #include <legacy/decode-standard>
  78. #include <post-process/pipeline>
  79. #pragma rate BloomUBO pass
  80. uniform BloomUBO {
  81. mediump vec4 bloomParams; // x: useHDRIlluminance z: threshold, w: enableAlphaMask
  82. };
  83. out vec2 vUv;
  84. out vec2 vUv00;
  85. out vec2 vUv01;
  86. out vec2 vUv02;
  87. out vec2 vUv03;
  88. out vec2 vUv04;
  89. out vec2 vUv05;
  90. out vec2 vUv06;
  91. out vec2 vUv07;
  92. out vec2 vUv08;
  93. out vec2 vUv09;
  94. out vec2 vUv10;
  95. out vec2 vUv11;
  96. vec4 vert() {
  97. vUv = a_texCoord;
  98. // CC_HANDLE_RT_SAMPLE_FLIP(vUv);
  99. vUv00 = vUv + bloomParams.xy * vec2(-1.0, 1.0);
  100. vUv01 = vUv + bloomParams.xy * vec2(1.0, 1.0);
  101. vUv02 = vUv + bloomParams.xy * vec2(-1.0, - 1.0);
  102. vUv03 = vUv + bloomParams.xy * vec2(1.0, - 1.0);
  103. vUv04 = vUv + bloomParams.xy * vec2(-2.0, 2.0);
  104. vUv05 = vUv + bloomParams.xy * vec2(0.0, 2.0);
  105. vUv06 = vUv + bloomParams.xy * vec2(2.0, 2.0);
  106. vUv07 = vUv + bloomParams.xy * vec2(-2.0, 0.0);
  107. vUv08 = vUv + bloomParams.xy * vec2(2.0, 0.0);
  108. vUv09 = vUv + bloomParams.xy * vec2(-2.0, - 2.0);
  109. vUv10 = vUv + bloomParams.xy * vec2(0.0, - 2.0);
  110. vUv11 = vUv + bloomParams.xy * vec2(2.0, - 2.0);
  111. StandardVertInput In;
  112. CCDecode(In);
  113. FLIP_VULKAN_NDC(In.position);
  114. return In.position;
  115. }
  116. }%
  117. CCProgram downsample-fs %{
  118. precision mediump float;
  119. // (1 / 4) * 0.5 = 0.125
  120. #define WEIGHT_INNER 0.125
  121. // (1 / 9) * 0.5 = 0.0555555
  122. #define WEIGHT_OUTER 0.0555555
  123. in vec2 vUv;
  124. in vec2 vUv00;
  125. in vec2 vUv01;
  126. in vec2 vUv02;
  127. in vec2 vUv03;
  128. in vec2 vUv04;
  129. in vec2 vUv05;
  130. in vec2 vUv06;
  131. in vec2 vUv07;
  132. in vec2 vUv08;
  133. in vec2 vUv09;
  134. in vec2 vUv10;
  135. in vec2 vUv11;
  136. #pragma rate mainTexture pass
  137. uniform sampler2D mainTexture;
  138. float clampToBorder(const in vec2 uv) {
  139. return float(uv.s >= 0.0 && uv.s <= 1.0 && uv.t >= 0.0 && uv.t <= 1.0);
  140. }
  141. vec4 frag() {
  142. vec4 c = vec4(0.0);
  143. vec4 w = WEIGHT_INNER * vec4(
  144. clampToBorder(vUv00),
  145. clampToBorder(vUv01),
  146. clampToBorder(vUv02),
  147. clampToBorder(vUv03)
  148. );
  149. c += w.x * texture(mainTexture, vUv00);
  150. c += w.y * texture(mainTexture, vUv01);
  151. c += w.z * texture(mainTexture, vUv02);
  152. c += w.w * texture(mainTexture, vUv03);
  153. w = WEIGHT_OUTER * vec4(
  154. clampToBorder(vUv04),
  155. clampToBorder(vUv05),
  156. clampToBorder(vUv06),
  157. clampToBorder(vUv07)
  158. );
  159. c += w.x * texture(mainTexture, vUv04);
  160. c += w.y * texture(mainTexture, vUv05);
  161. c += w.z * texture(mainTexture, vUv06);
  162. c += w.w * texture(mainTexture, vUv07);
  163. w = WEIGHT_OUTER * vec4(
  164. clampToBorder(vUv08),
  165. clampToBorder(vUv09),
  166. clampToBorder(vUv10),
  167. clampToBorder(vUv11)
  168. );
  169. c += w.x * texture(mainTexture, vUv08);
  170. c += w.y * texture(mainTexture, vUv09);
  171. c += w.z * texture(mainTexture, vUv10);
  172. c += w.w * texture(mainTexture, vUv11);
  173. c += WEIGHT_OUTER * texture(mainTexture, vUv);
  174. return c;
  175. }
  176. }%
  177. CCProgram upsample-vs %{
  178. precision mediump float;
  179. #include <legacy/decode-standard>
  180. #include <post-process/pipeline>
  181. #pragma rate BloomUBO pass
  182. uniform BloomUBO {
  183. mediump vec4 bloomParams; // x: useHDRIlluminance z: threshold, w: enableAlphaMask
  184. };
  185. out vec2 vUv;
  186. out vec2 vUv0;
  187. out vec2 vUv1;
  188. out vec2 vUv2;
  189. out vec2 vUv3;
  190. out vec2 vUv4;
  191. out vec2 vUv5;
  192. out vec2 vUv6;
  193. out vec2 vUv7;
  194. vec4 vert() {
  195. vUv = a_texCoord;
  196. // CC_HANDLE_RT_SAMPLE_FLIP(vUv);
  197. vUv0 = vUv + bloomParams.xy * vec2(-1.0, 1.0);
  198. vUv1 = vUv + bloomParams.xy * vec2(0.0, 1.0);
  199. vUv2 = vUv + bloomParams.xy * vec2(1.0, 1.0);
  200. vUv3 = vUv + bloomParams.xy * vec2(-1.0, 0.0);
  201. vUv4 = vUv + bloomParams.xy * vec2(1.0, 0.0);
  202. vUv5 = vUv + bloomParams.xy * vec2(-1.0, - 1.0);
  203. vUv6 = vUv + bloomParams.xy * vec2(0.0, - 1.0);
  204. vUv7 = vUv + bloomParams.xy * vec2(1.0, - 1.0);
  205. StandardVertInput In;
  206. CCDecode(In);
  207. FLIP_VULKAN_NDC(In.position);
  208. return In.position;
  209. }
  210. }%
  211. CCProgram upsample-fs %{
  212. precision mediump float;
  213. in vec2 vUv;
  214. in vec2 vUv0;
  215. in vec2 vUv1;
  216. in vec2 vUv2;
  217. in vec2 vUv3;
  218. in vec2 vUv4;
  219. in vec2 vUv5;
  220. in vec2 vUv6;
  221. in vec2 vUv7;
  222. #pragma rate mainTexture pass
  223. uniform lowp sampler2D mainTexture;
  224. #pragma rate downsampleTexture pass
  225. uniform lowp sampler2D downsampleTexture;
  226. vec4 frag() {
  227. vec4 c = vec4(0.0);
  228. c += texture(mainTexture, vUv0) * 0.0625;
  229. c += texture(mainTexture, vUv1) * 0.125;
  230. c += texture(mainTexture, vUv2) * 0.0625;
  231. c += texture(mainTexture, vUv3) * 0.125;
  232. c += texture(mainTexture, vUv) * 0.25;
  233. c += texture(mainTexture, vUv4) * 0.125;
  234. c += texture(mainTexture, vUv5) * 0.0625;
  235. c += texture(mainTexture, vUv6) * 0.125;
  236. c += texture(mainTexture, vUv7) * 0.0625;
  237. vec4 baseColor = texture(downsampleTexture, vUv);
  238. return mix(baseColor, c, 0.85);
  239. }
  240. }%
  241. CCProgram combine-fs %{
  242. precision mediump float;
  243. #pragma rate BloomUBO pass
  244. uniform BloomUBO {
  245. mediump vec4 bloomParams; // x: useHDRIlluminance z: threshold, w: enableAlphaMask
  246. };
  247. in vec2 vUv;
  248. #pragma rate bloomTexture pass
  249. uniform sampler2D bloomTexture;
  250. vec4 frag() {
  251. vec4 bloomColor = texture(bloomTexture, vUv);
  252. vec3 color = bloomColor.rgb * bloomParams.w;
  253. return vec4(color, 1);
  254. }
  255. }%