Skip to content

Commit 9dfcdc8

Browse files
committed
Add a section about coordinate spaces to spatial.rst
1 parent 1228ab4 commit 9dfcdc8

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

tutorials/shaders/shader_reference/spatial_shader.rst

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,3 +507,89 @@ If you want the lights to add together, add the light contribution to ``DIFFUSE_
507507
materials from appearing in screen-space reflections or refraction.
508508
:ref:`SDFGI <doc_using_sdfgi>` sharp reflections are not visible on transparent
509509
materials (only rough reflections are visible on transparent materials).
510+
511+
512+
513+
Coordinate spaces
514+
^^^^^^^^^^^^^^^^^
515+
516+
Spatial shaders do their work across multiple coordinate spaces. Some built-in variables with the same name are in different
517+
coordinate spaces in different processor functions.
518+
519+
+-------------------------------+----------------------------------------------------------------------------------------+
520+
| Coordinate space | Description |
521+
+===============================+========================================================================================+
522+
| Model space | Also called "local space" or "local coordinates". Called "object space" in |
523+
| | other renderers. Equivalent to the local coordinates used by Transform3D. |
524+
+-------------------------------+----------------------------------------------------------------------------------------+
525+
| World space | Equivalent to the global coordinates used by Transform3D. |
526+
| | |
527+
+-------------------------------+----------------------------------------------------------------------------------------+
528+
| View space | Coordinate space relative to the camera. |
529+
| | Called "camera space" or "eye space" in other renderers. |
530+
+-------------------------------+----------------------------------------------------------------------------------------+
531+
| Clip space | TODO |
532+
+-------------------------------+----------------------------------------------------------------------------------------+
533+
| Screen space | TODO |
534+
+-------------------------------+----------------------------------------------------------------------------------------+
535+
| Normalized device coordinates | Final coordinates expected by the renderer. Usually not used directly. Values |
536+
| | range from |
537+
| | ``-1.0`` to ``1.0``, with ``(-1.0,-1.0)`` being the upper-left corner of the screen, |
538+
| | and ``(1.0,1.0)`` the lower right. |
539+
+-------------------------------+----------------------------------------------------------------------------------------+
540+
541+
.. note::
542+
543+
Godot 4.3 introduced a change to clip space. If you are using a shader that uses clips space directly and was
544+
originally written for an earlier version, see `<https://godotengine.org/article/introducing-reverse-z/>`.
545+
546+
547+
548+
You don't need to know much matrix math in order to use coordinate spaces. You can convert vectors between different coordinate spaces by using the built-in transform matrices.
549+
550+
To transform a *position* vector ``VERTEX`` from view space to world space, multiply it by
551+
the view-to-world matrix ``INV_VIEW_MATRIX``:
552+
553+
.. code-block:: glsl
554+
555+
void fragment() {
556+
vec3 position_world = (INV_VIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
557+
}
558+
559+
To transform a *direction* vector, such as ``NORMAL``, change the ``z`` field of the vec4 to ``0.0`` :
560+
561+
.. code-block:: glsl
562+
563+
void fragment() {
564+
vec3 normal_world = (INV_VIEW_MATRIX * vec4(NORMAL, 0.0)).xyz;
565+
}
566+
567+
You can also compose multiple transformations together. To transform ``VERTEX`` from model space to view space, first
568+
transform it to world space, then to view space. Note the order of the matrices:
569+
570+
.. code-block:: glsl
571+
572+
void vertex() {
573+
vec3 position_view = (VIEW_MATRIX * MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
574+
}
575+
576+
You can transform to one coordinate space, do some operations, and transform back. This snippet scales ``VERTEX`` in model space,
577+
translates it in world space, then transforms back into model space, so it can be passed to ``fragment()``.
578+
579+
.. code-block:: glsl
580+
581+
void vertex() {
582+
VERTEX *= 2.0;
583+
vec3 position_world = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
584+
position_world += vec3(1.0);
585+
VERTEX = (inverse(MODEL_MATRIX) * vec4(position_world, 1.0)).xyz;
586+
}
587+
588+
TODO: add a sentence about screen space and NDC.
589+
590+
.. code-block:: glsl
591+
592+
void convert_to_screenspace() {
593+
TODO: add the to and from NDC snippets here
594+
}
595+

0 commit comments

Comments
 (0)