fs_denoise_gbuffer.sc 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. $input v_normal, v_texcoord0, v_texcoord1, v_texcoord2, v_texcoord3
  2. #include "../common/common.sh"
  3. #include "parameters.sh"
  4. #include "normal_encoding.sh"
  5. SAMPLER2D(s_albedo, 0);
  6. SAMPLER2D(s_normal, 1);
  7. // http://www.thetenthplanet.de/archives/1180
  8. // "followup: normal mapping without precomputed tangents"
  9. mat3 cotangentFrame(vec3 N, vec3 p, vec2 uv)
  10. {
  11. // get edge vectors of the pixel triangle
  12. vec3 dp1 = dFdx(p);
  13. vec3 dp2 = dFdy(p);
  14. vec2 duv1 = dFdx(uv);
  15. vec2 duv2 = dFdy(uv);
  16. // solve the linear system
  17. vec3 dp2perp = cross(dp2, N);
  18. vec3 dp1perp = cross(N, dp1);
  19. vec3 T = dp2perp * duv1.x + dp1perp * duv2.x;
  20. vec3 B = dp2perp * duv1.y + dp1perp * duv2.y;
  21. // construct a scale-invariant frame
  22. float invMax = inversesqrt(max(dot(T,T), dot(B,B)));
  23. return mat3(T*invMax, B*invMax, N);
  24. }
  25. void main()
  26. {
  27. vec3 albedo = toLinear(texture2D(s_albedo, v_texcoord0).xyz);
  28. // get vertex normal
  29. vec3 normal = normalize(v_normal);
  30. // get normal map normal, unpack, and calculate z
  31. vec3 normalMap;
  32. normalMap.xy = texture2D(s_normal, v_texcoord0).xy;
  33. normalMap.xy = normalMap.xy * 2.0 - 1.0;
  34. normalMap.z = sqrt(1.0 - dot(normalMap.xy, normalMap.xy));
  35. // swap x and y, because the brick texture looks flipped, don't copy this...
  36. normalMap.xy = normalMap.yx;
  37. // perturb geometry normal by normal map
  38. vec3 pos = v_texcoord2.xyz; // contains world space pos
  39. mat3 TBN = cotangentFrame(normal, pos, v_texcoord0);
  40. vec3 bumpedNormal = normalize(instMul(TBN, normalMap));
  41. // need some proxy for roughness value w/o roughness texture
  42. // assume horizontal (blue) normal map is smooth, and then
  43. // modulate with albedo for some higher frequency detail
  44. float roughness = normalMap.z * mix(0.9, 1.0, albedo.y);
  45. roughness = roughness * 0.6 + 0.2;
  46. // Calculate velocity as delta position from previous frame to this
  47. vec2 previousNDC = v_texcoord1.xy * (1.0/v_texcoord1.w);
  48. previousNDC.y *= -1.0;
  49. previousNDC = previousNDC * 0.5 + 0.5;
  50. vec2 velocity = gl_FragCoord.xy*u_viewTexel.xy - previousNDC;
  51. vec3 bufferNormal = NormalEncode(bumpedNormal);
  52. gl_FragData[0] = vec4(toGamma(albedo), 1.0);
  53. gl_FragData[1] = vec4(bufferNormal, roughness); // Todo, better packing
  54. gl_FragData[2] = vec4(velocity, 0.0, 0.0);
  55. }