compositor_pass.kong 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #[set(everything)]
  2. const constants: {
  3. vignette_strength: float;
  4. grain_strength: float;
  5. };
  6. #[set(everything)]
  7. const sampler_linear: sampler;
  8. #[set(everything)]
  9. const tex: tex2d;
  10. // #[set(everything)]
  11. // const histogram: tex2d;
  12. struct vert_in {
  13. pos: float2;
  14. }
  15. struct vert_out {
  16. pos: float4;
  17. tex: float2;
  18. }
  19. fun compositor_pass_vert(input: vert_in): vert_out {
  20. var output: vert_out;
  21. output.tex = input.pos.xy * 0.5 + 0.5;
  22. output.tex.y = 1.0 - output.tex.y;
  23. output.pos = float4(input.pos.xy, 0.0, 1.0);
  24. return output;
  25. }
  26. fun tonemap_filmic(color: float3): float3 {
  27. // Based on Filmic Tonemapping Operators http://filmicgames.com/archives/75
  28. // var x: float3 = max(float3(0.0, 0.0, 0.0), color - 0.004);
  29. var x: float3;
  30. x.x = max(0.0, color.x - 0.004);
  31. x.y = max(0.0, color.y - 0.004);
  32. x.z = max(0.0, color.z - 0.004);
  33. return (x * (x * 6.2 + 0.5)) / (x * (x * 6.2 + 1.7) + 0.06);
  34. }
  35. fun compositor_pass_frag(input: vert_out): float4 {
  36. var color: float4 = sample_lod(tex, sampler_linear, input.tex, 0.0);
  37. // Static grain
  38. var x: float = (input.tex.x + 4.0) * (input.tex.y + 4.0) * 10.0;
  39. var g: float = (((x % 13.0) + 1.0) * ((x % 123.0) + 1.0) % 0.01) - 0.005;
  40. color.rgb = color.rgb + (float3(g, g, g) * constants.grain_strength);
  41. color.rgb = color.rgb * ((1.0 - constants.vignette_strength) + constants.vignette_strength * pow(16.0 * input.tex.x * input.tex.y * (1.0 - input.tex.x) * (1.0 - input.tex.y), 0.2));
  42. // Auto exposure
  43. // const auto_exposure_strength: float = 1.0;
  44. // var expo: float = 2.0 - clamp(length(sample_lod(histogram, sampler_linear, float2(0.5, 0.5), 0.0).rgb), 0.0, 1.0);
  45. // color.rgb *= pow(expo, auto_exposure_strength * 2.0);
  46. color.rgb = tonemap_filmic(color.rgb); // With gamma
  47. return color;
  48. }
  49. #[pipe]
  50. struct pipe {
  51. vertex = compositor_pass_vert;
  52. fragment = compositor_pass_frag;
  53. }