world_pass.kong 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #[set(everything)]
  2. const constants: {
  3. SMVP: float4x4;
  4. envmap_data_world: float4; // angle, empty, empty, strength
  5. };
  6. #[set(everything)]
  7. const sampler_linear: sampler;
  8. #[set(everything)]
  9. const envmap: tex2d;
  10. const PI: float = 3.1415926535;
  11. const PI2: float = 6.283185307;
  12. struct vert_in {
  13. pos: float3;
  14. nor: float3;
  15. }
  16. struct vert_out {
  17. pos: float4;
  18. nor: float3;
  19. }
  20. fun world_pass_vert(input: vert_in): vert_out {
  21. var output: vert_out;
  22. output.pos = constants.SMVP * float4(input.pos, 1.0);
  23. output.nor = input.nor;
  24. return output;
  25. }
  26. fun envmap_equirect(normal: float3, angle: float): float2 {
  27. var phi: float = acos(normal.z);
  28. var theta: float = atan2(normal.x, -normal.y) + PI + angle;
  29. return float2(theta / PI2, phi / PI);
  30. }
  31. fun world_pass_frag(input: vert_out): float4 {
  32. var n: float3 = normalize(input.nor);
  33. var color: float4;
  34. color.rgb = sample(envmap, sampler_linear, envmap_equirect(-n, constants.envmap_data_world.x)).rgb * constants.envmap_data_world.w;
  35. color.a = 0.0; // Mark as non-opaque
  36. return color;
  37. }
  38. #[pipe]
  39. struct pipe {
  40. vertex = world_pass_vert;
  41. fragment = world_pass_frag;
  42. }