view.glsl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /* view.glsl -- Contains everything you need to manage transformations
  2. *
  3. * Copyright (c) 2025 Le Juez Victor
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * For conditions of distribution and use, see accompanying LICENSE file.
  7. */
  8. #include "../math.glsl"
  9. struct View {
  10. vec3 position;
  11. mat4 view;
  12. mat4 invView;
  13. mat4 proj;
  14. mat4 invProj;
  15. mat4 viewProj;
  16. float aspect;
  17. float near;
  18. float far;
  19. };
  20. layout(std140) uniform ViewBlock {
  21. View uView;
  22. };
  23. vec3 V_GetViewPosition(vec2 texCoord, float linearDepth)
  24. {
  25. vec2 ndc = texCoord * 2.0 - 1.0;
  26. float tanHalfFov = 1.0 / uView.proj[1][1];
  27. vec3 viewRay = vec3(
  28. ndc.x * tanHalfFov * uView.aspect,
  29. ndc.y * tanHalfFov,
  30. -1.0
  31. );
  32. return viewRay * linearDepth;
  33. }
  34. vec3 V_GetViewPosition(sampler2D texLinearDepth, vec2 texCoord)
  35. {
  36. float linearDepth = texture(texLinearDepth, texCoord).r;
  37. return V_GetViewPosition(texCoord, linearDepth);
  38. }
  39. vec3 V_GetViewPosition(sampler2D texLinearDepth, ivec2 pixCoord)
  40. {
  41. vec2 texCoord = (vec2(pixCoord) + 0.5) / vec2(textureSize(texLinearDepth, 0));
  42. float linearDepth = texelFetch(texLinearDepth, pixCoord, 0).r;
  43. return V_GetViewPosition(texCoord, linearDepth);
  44. }
  45. vec3 V_GetWorldPosition(vec3 viewPosition)
  46. {
  47. return (uView.invView * vec4(viewPosition, 1.0)).xyz;
  48. }
  49. vec3 V_GetWorldPosition(vec2 texCoord, float linearDepth)
  50. {
  51. vec3 viewPosition = V_GetViewPosition(texCoord, linearDepth);
  52. return V_GetWorldPosition(viewPosition);
  53. }
  54. vec3 V_GetWorldPosition(sampler2D texLinearDepth, vec2 texCoord)
  55. {
  56. float linearDepth = texture(texLinearDepth, texCoord).r;
  57. return V_GetWorldPosition(texCoord, linearDepth);
  58. }
  59. vec3 V_GetWorldPosition(sampler2D texLinearDepth, ivec2 pixCoord)
  60. {
  61. vec2 texCoord = (vec2(pixCoord) + 0.5) / vec2(textureSize(texLinearDepth, 0));
  62. float linearDepth = texelFetch(texLinearDepth, pixCoord, 0).r;
  63. return V_GetWorldPosition(texCoord, linearDepth);
  64. }
  65. vec3 V_GetViewNormal(vec3 worldNormal)
  66. {
  67. return normalize(mat3(uView.view) * worldNormal);
  68. }
  69. vec3 V_GetViewNormal(vec2 encWorldNormal)
  70. {
  71. vec3 worldNormal = M_DecodeOctahedral(encWorldNormal);
  72. return V_GetViewNormal(worldNormal);
  73. }
  74. vec3 V_GetViewNormal(sampler2D texNormal, vec2 texCoord)
  75. {
  76. vec2 encWorldNormal = texture(texNormal, texCoord).rg;
  77. return V_GetViewNormal(encWorldNormal);
  78. }
  79. vec3 V_GetViewNormal(sampler2D texNormal, ivec2 pixCoord)
  80. {
  81. vec2 encWorldNormal = texelFetch(texNormal, pixCoord, 0).rg;
  82. return V_GetViewNormal(encWorldNormal);
  83. }
  84. vec3 V_GetWorldNormal(sampler2D texNormal, vec2 texCoord)
  85. {
  86. vec2 encWorldNormal = texture(texNormal, texCoord).rg;
  87. return M_DecodeOctahedral(encWorldNormal);
  88. }
  89. vec3 V_GetWorldNormal(sampler2D texNormal, ivec2 pixCoord)
  90. {
  91. vec2 encWorldNormal = texelFetch(texNormal, pixCoord, 0).rg;
  92. return M_DecodeOctahedral(encWorldNormal);
  93. }
  94. vec2 V_ViewToScreen(vec3 viewPosition)
  95. {
  96. vec4 clipPos = uView.proj * vec4(viewPosition, 1.0);
  97. return (clipPos.xy / clipPos.w) * 0.5 + 0.5;
  98. }
  99. vec2 V_WorldToScreen(vec3 worldPosition)
  100. {
  101. vec4 projPos = uView.viewProj * vec4(worldPosition, 1.0);
  102. return (projPos.xy / projPos.w) * 0.5 + 0.5;
  103. }
  104. bool V_OffScreen(vec2 texCoord)
  105. {
  106. return any(lessThan(texCoord, vec2(0.0))) ||
  107. any(greaterThan(texCoord, vec2(1.0)));
  108. }
  109. float V_LinearizeDepth(float depth)
  110. {
  111. float near = uView.near;
  112. float far = uView.far;
  113. return (2.0 * near * far) / (far + near - (depth * 2.0 - 1.0) * (far - near));
  114. }
  115. float V_LinearizeDepth01(float depth)
  116. {
  117. float near = uView.near;
  118. float far = uView.far;
  119. float z = (2.0 * near * far) / (far + near - (depth * 2.0 - 1.0) * (far - near));
  120. return (z - near) / (far - near);
  121. }