dof1.effect 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2017-2023 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. techniques:
  4. - passes:
  5. - vert: dof-vs
  6. frag: dof-blur-fs
  7. pass: cc-dof-blur
  8. depthStencilState:
  9. depthTest: false
  10. depthWrite: false
  11. - vert: dof-vs
  12. frag: dof-coc-fs
  13. pass: cc-dof-coc
  14. depthStencilState:
  15. depthTest: false
  16. depthWrite: false
  17. }%
  18. CCProgram ubo %{
  19. #include <common/common-define>
  20. #define focusDistance params.x
  21. #define focusRange params.y
  22. #define blurRadius params.z
  23. }%
  24. CCProgram dof-vs %{
  25. #include <./chunks/vs1>
  26. }%
  27. CCProgram dof-blur-fs %{
  28. precision highp float;
  29. #pragma rate DofUBO pass
  30. uniform DofUBO {
  31. vec4 blurParams;
  32. vec4 mainTexTexelSize;
  33. };
  34. layout(location = 0)out vec4 fragColor;
  35. #pragma rate screenTex pass
  36. uniform sampler2D screenTex;
  37. in vec2 v_uv;
  38. vec4 blur(sampler2D image, vec2 uv) {
  39. vec4 color = vec4(0.0);
  40. vec2 resolution = mainTexTexelSize.zw;
  41. vec2 off1 = vec2(1.411764705882353);
  42. vec2 off2 = vec2(3.2941176470588234);
  43. vec2 off3 = vec2(5.176470588235294);
  44. color += texture(image, uv) * 0.1964825501511404;
  45. color += texture(image, uv + (off1 / resolution)) * 0.2969069646728344;
  46. color += texture(image, uv - (off1 / resolution)) * 0.2969069646728344;
  47. color += texture(image, uv + (off2 / resolution)) * 0.09447039785044732;
  48. color += texture(image, uv - (off2 / resolution)) * 0.09447039785044732;
  49. color += texture(image, uv + (off3 / resolution)) * 0.010381362401148057;
  50. color += texture(image, uv - (off3 / resolution)) * 0.010381362401148057;
  51. return color;
  52. }
  53. void main() {
  54. vec4 pixelOffset = vec4(-1.0, - 1.0, 1.0, 1.0);
  55. vec4 offsets = mainTexTexelSize.xyxy * pixelOffset * blurParams.z;
  56. vec4 outColor = blur(screenTex, v_uv + offsets.xy);
  57. outColor += blur(screenTex, v_uv + offsets.zy);
  58. outColor += blur(screenTex, v_uv + offsets.xw);
  59. outColor += blur(screenTex, v_uv + offsets.zw);
  60. fragColor = outColor * blurParams.w * 0.25;
  61. }
  62. }%
  63. CCProgram dof-coc-fs %{
  64. precision highp float;
  65. #include <./chunks/depth>
  66. layout(location = 0)out vec4 fragColor;
  67. in vec2 v_uv;
  68. #pragma rate screenTex pass
  69. uniform sampler2D screenTex;
  70. #pragma rate colorTex pass
  71. uniform sampler2D colorTex;
  72. #pragma rate DofCocUBO pass
  73. uniform DofCocUBO {
  74. mat4 proj;
  75. mat4 invProj;
  76. mat4 viewMatInv;
  77. vec4 focus;
  78. vec4 cocParams;
  79. };
  80. void main() {
  81. vec4 blurPixel = texture(colorTex, v_uv);
  82. vec4 screenPixel = texture(screenTex, v_uv);
  83. float depth = GetDepthFromTex(v_uv);
  84. #if __VERSION__ < 450
  85. depth = 2.0 * depth - 1.0;
  86. #endif
  87. vec4 screenPos = vec4(v_uv.x * 2.0 - 1.0, v_uv.y * 2.0 - 1.0, depth, 1.0);
  88. vec4 viewPos = invProj * screenPos;
  89. viewPos /= viewPos.w;
  90. vec4 worldPos = viewMatInv * viewPos;
  91. float blur =
  92. smoothstep
  93. (cocParams.x
  94. , cocParams.y
  95. , length(worldPos - vec4(focus.xyz, 1.0))
  96. );
  97. fragColor = mix(screenPixel, blurPixel, blur);
  98. }
  99. }%