tonemap.effect 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2017-2020 Xiamen Yaji Software Co., Ltd.
  2. CCEffect %{
  3. temporaries:
  4. s1: &s1 {
  5. minFilter: linear,
  6. magFilter: linear,
  7. addressU: clamp,
  8. addressV: clamp,
  9. }
  10. techniques:
  11. - name: tonemap
  12. passes:
  13. - vert: tonemap-vs:vert
  14. frag: tonemap-fs:frag
  15. depthStencilState:
  16. depthTest: false
  17. depthWrite: false
  18. properties:
  19. u_texSampler: { sampler: *s1 }
  20. u_blendTexSampler: { sampler: *s1 }
  21. }%
  22. CCProgram tonemap-vs %{
  23. precision highp float;
  24. #include <common/common-define>
  25. #include <builtin/uniforms/cc-global>
  26. in vec2 a_position;
  27. in vec2 a_texCoord;
  28. out vec2 v_uv;
  29. out vec4 v_offset;
  30. vec4 vert () {
  31. vec4 pos = vec4(a_position, 0, 1);
  32. v_uv = a_texCoord * cc_screenScale.xy;
  33. v_offset = v_uv.xyxy + cc_nativeSize.zwzw * vec4(1.0, 0.0, 0.0, -1.0);
  34. return pos;
  35. }
  36. }%
  37. CCProgram tonemap-fs %{
  38. precision highp float;
  39. #include <builtin/uniforms/cc-global>
  40. #include <common/common-define>
  41. #include <common/color/gamma>
  42. #include <common/color/aces>
  43. in vec2 v_uv;
  44. in vec4 v_offset;
  45. uniform sampler2D u_texSampler;
  46. uniform sampler2D u_blendTexSampler;
  47. vec3 ToLDR(vec3 color) {
  48. #if CC_USE_HDR
  49. color *= cc_exposure.x * FP_SCALE_INV;
  50. color = ACESToneMap(color);
  51. color = LinearToSRGB(color);
  52. #endif
  53. return color;
  54. }
  55. vec4 frag () {
  56. #if CC_USE_SMAA
  57. // Fetch the blending weights for current pixel:
  58. vec4 a;
  59. a.rb = texture(u_blendTexSampler, v_uv).rb; // Bottom / Left
  60. a.g = texture(u_blendTexSampler, v_offset.zw).g; // Top
  61. a.a = texture(u_blendTexSampler, v_offset.xy).a; // Right
  62. // Is there any blending weight with a value greater than 0.0?
  63. if (dot(a, vec4(1.0)) < 1e-5) {
  64. vec4 o = texture(u_texSampler, v_uv);
  65. o.rgb = ToLDR(o.rgb);
  66. return o;
  67. } else {
  68. vec2 offset;
  69. offset.x = a.a > a.b ? a.a : -a.b;
  70. offset.y = a.g > a.r ? -a.g : a.r;
  71. if (abs(offset.x) > abs(offset.y)) {
  72. offset.y = 0.0;
  73. } else {
  74. offset.x = 0.0;
  75. }
  76. vec4 C = texture(u_texSampler, v_uv);
  77. C.rgb = ToLDR(C.rgb);
  78. vec2 uv = v_uv + sign(offset) * cc_nativeSize.zw;
  79. vec4 Cop = texture(u_texSampler, uv);
  80. Cop.rgb = ToLDR(Cop.rgb);
  81. float s = abs(offset.x) > abs(offset.y) ? abs(offset.x) : abs(offset.y);
  82. C.rgb = pow(C.rgb, vec3(2.2));
  83. Cop.rgb = pow(Cop.rgb, vec3(2.2));
  84. vec4 mixed = mix(C, Cop, s);
  85. mixed.rgb = pow(mixed.rgb, vec3(1.0 / 2.2));
  86. return mixed;
  87. }
  88. #else
  89. vec4 o = texture(u_texSampler, v_uv);
  90. //o = texture(u_blendTexSampler, v_uv);
  91. o.rgb = ToLDR(o.rgb);
  92. return o;
  93. #endif
  94. }
  95. }%