Skip to content

Commit 7d9df6b

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

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tutorials/shaders/shader_reference/spatial_shader.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,51 @@ Render modes
8383
| **fog_disabled** | Disable receiving depth-based or volumetric fog. Useful for blend_add materials like particles. |
8484
+-------------------------------+------------------------------------------------------------------------------------------------------+
8585

86+
Coordinate spaces
87+
^^^^^^^^^^^^^^^^^
88+
89+
Spatial shaders do their work across multiple coordinate spaces. Some built-in variables with the same name are in different
90+
coordinate spaces in different processor functions.
91+
92+
+-------------------------------+----------------------------------------------------------------------------------------+
93+
| Coordinate space | Description |
94+
+===============================+========================================================================================+
95+
| Model space | Also called "local space" or "local coordinates". |
96+
| | Same as local coordinates of Transform3D. |
97+
+-------------------------------+----------------------------------------------------------------------------------------+
98+
| World space | Same as global coordinates of Transform3D. |
99+
| | |
100+
+-------------------------------+----------------------------------------------------------------------------------------+
101+
| View space | |
102+
+-------------------------------+----------------------------------------------------------------------------------------+
103+
| Clip space | |
104+
+-------------------------------+----------------------------------------------------------------------------------------+
105+
| Screen space | |
106+
+-------------------------------+----------------------------------------------------------------------------------------+
107+
| Normalized device coordinates | |
108+
+-------------------------------+----------------------------------------------------------------------------------------+
109+
110+
You can convert between different coordinate spaces by using the built-in transform matrices. For example, in the fragment shader,
111+
both ``VERTEX`` and ``NORMAL`` are in view space by default. You can convert them to world space by multiplying them by
112+
``INV_VIEW_MATRIX``. When transforming a *position* vector, the `z` value should be `1.0`. When transforming a direction
113+
vector, the `z` value should be `0.0`.
114+
115+
.. code-block:: glsl
116+
117+
void fragment() {
118+
vec3 position_world = (INV_VIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
119+
vec3 normal_world = (INV_VIEW_MATRIX * vec4(NORMAL, 0.0)).xyz;
120+
}
121+
122+
TODO add a sentence about screen space and NDC.
123+
124+
.. code-block:: glsl
125+
126+
void convert_to_screenspace() {
127+
TODO add the to and from NDC snippets here
128+
}
129+
130+
86131
Built-ins
87132
^^^^^^^^^
88133

0 commit comments

Comments
 (0)