xr_full_screen_effects.rst 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. .. _doc_xr_full_screen_effects:
  2. XR full screen effects
  3. ======================
  4. When adding custom full screen effects to your XR application, one approach is
  5. using a full screen quad and applying effects to that quad's shader.
  6. Add a :ref:`MeshInstance3D <class_MeshInstance3D>` node
  7. to your scene as a child of your :ref:`XRCamera3D <class_XRCamera3D>`,
  8. and set the ``mesh`` property to a :ref:`QuadMesh <class_QuadMesh>`.
  9. Set the width and height of the quad to ``2``.
  10. .. image:: img/xr_full_screen_effects_starting_quad.webp
  11. You can then add a shader to your quad to make it cover the screen. This is done by setting the
  12. vertex shader's ``POSITION`` built-in to ``vec4(VERTEX.xy, 1.0, 1.0)``.
  13. However, when creating an effect that is centered straight ahead in the user's view
  14. (such as a vignette effect), the end result may look incorrect in XR.
  15. Below shows captures of the right-eye view with a vignette shader, both from the headset and the render target itself.
  16. The left captures are an unmodified shader; the right captures adjust the full screen quad using the projection matrix.
  17. While the capture on the left is centered in the render target, it is off-center in the headset view.
  18. But, after applying the projection matrix, we see that the effect is centered in the headset itself.
  19. .. image:: img/xr_full_screen_effects_vignette_before_after.webp
  20. Applying the projection matrix
  21. ------------------------------
  22. To properly center the effect, the ``POSITION`` of the full screen quad
  23. needs to take the asymmetric field of view into account. To do this while also ensuring the quad
  24. has full coverage of the entire render target, we can subdivide the quad and apply the projection matrix
  25. to the inner vertices. Let's increase the subdivide width and depth of the quad.
  26. .. image:: img/xr_full_screen_effects_ending_quad.webp
  27. Then, in the vertex function of our shader, we apply an offset from the projection matrix to
  28. the inner vertices. Here's an example of how you might do this with the above simple vignette shader:
  29. .. code-block:: glsl
  30. shader_type spatial;
  31. render_mode depth_test_disabled, skip_vertex_transform, unshaded, cull_disabled;
  32. // Modify VERTEX.xy using the projection matrix to correctly center the effect.
  33. void vertex() {
  34. vec2 vert_pos = VERTEX.xy;
  35. if (length(vert_pos) < 0.99) {
  36. vec4 offset = PROJECTION_MATRIX * vec4(0.0, 0.0, 1.0, 1.0);
  37. vert_pos += (offset.xy / offset.w);
  38. }
  39. POSITION = vec4(vert_pos, 1.0, 1.0);
  40. }
  41. void fragment() {
  42. ALBEDO = vec3(0.0);
  43. ALPHA = dot(UV * 2.0 - 1.0, UV * 2.0 - 1.0) * 2.0;
  44. }
  45. .. note:: For more info on asymmetric FOV and its purpose, see this
  46. `Meta Asymmetric Field of View FAQ <https://developers.meta.com/horizon/documentation/unity/unity-asymmetric-fov-faq/>`_.
  47. Limitations
  48. -----------
  49. This full screen effect method has no performance concerns for per-pixel effects such as the above vignette shader.
  50. However, it is not recommended to read from the screen texture when using this technique.
  51. Full screen effects that require reading from the screen texture effectively disable all rendering performance optimizations in XR.
  52. This is because, when reading from the screen texture, Godot makes a full copy of the render buffer;
  53. this drastically increases the workload for the GPU and can create performance concerns.