Skip to content

Commit 9377e35

Browse files
CalinouQbieShay
andcommitted
Document Depth Test and Stencil features in Standard Material 3D and Spatial shader
Co-authored-by: Qbieshay <[email protected]>
1 parent b61cd5d commit 9377e35

File tree

4 files changed

+104
-1
lines changed

4 files changed

+104
-1
lines changed
154 KB
Loading
228 KB
Loading

tutorials/3d/standard_material_3d.rst

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,16 @@ and works very well with the *Render Priority* property of Material
241241

242242
.. image:: img/spatial_material3.png
243243

244+
Depth Test
245+
~~~~~~~~~~
246+
247+
This can be used to invert the standard depth test. When set to **Inverted**,
248+
the object will only appear when occluded, and will be hidden otherwise.
249+
250+
This has no effect if **No Depth Test** is enabled.
251+
252+
.. image:: img/material_depth_test.webp
253+
244254
Shading
245255
-------
246256

@@ -272,7 +282,7 @@ You can also use per-vertex lighting to achieve a retro look.
272282
.. figure:: img/standard_material_shading_modes_textured.webp
273283
:align: center
274284
:alt: Two cubes with a brick texture, one shaded and one unshaded.
275-
285+
276286
Texture from `AmbientCG <https://ambientcg.com/view?id=Bricks051>`__
277287

278288
The **Unshaded** shading mode does not calculate lighting at all. Instead, the
@@ -703,6 +713,8 @@ Billboard Keep Scale
703713

704714
Enables scaling a mesh in billboard mode.
705715

716+
.. _ref_standard_material_3d_grow:
717+
706718
Grow
707719
----
708720

@@ -721,6 +733,10 @@ make it black and unshaded, reverse culling (Cull Front), and add some grow:
721733
vertices, or "smooth shading". If the mesh has disconnected faces with unique
722734
vertices, or "flat shading", the mesh will appear to have gaps when using Grow.
723735

736+
Note that in Godot 4.5 onwards, stencil buffer-based outlines are available
737+
using the **Outline** :ref:`stencil mode <doc_standard_material_3d_stencil>`.
738+
This can be used as an alternative to Grow for outlines.
739+
724740
Transform
725741
---------
726742

@@ -812,6 +828,36 @@ is the same across the entire object's surface.
812828

813829
.. image:: img/standart_material_distance_fade_object_dither_mode.webp
814830

831+
.. _doc_standard_material_3d_stencil:
832+
833+
Stencil
834+
-------
835+
836+
Since Godot 4.5, Godot allows materials to make use of the stencil buffer.
837+
This feature is commonly used to create outlines and X-ray effects,
838+
which can be useful to highlight objects, especially behind walls.
839+
840+
The **Outline** and **X-Ray** modes assign a preconfigured stencil material
841+
in the material's **Next Pass** property. The **Custom** mode can be used for
842+
advanced effects.
843+
844+
.. image:: img/material_stencil.webp
845+
846+
Materials that write to the stencil buffer are always drawn in the transparent pass,
847+
so they are subject to the usual
848+
:ref:`transparency limitations <doc_3d_rendering_limitations_transparency_sorting>`.
849+
850+
.. note::
851+
852+
Like with the :ref:`Grow property <ref_standard_material_3d_grow>`, for the
853+
stencil outline to work as expected, the mesh must have connected faces with
854+
shared vertices, or "smooth shading". If the mesh has disconnected faces with
855+
unique vertices, or "flat shading", the mesh will appear to have gaps when using
856+
a stencil outline.
857+
858+
Stencil outlines render similarly to the Grow property, but won't look identical
859+
in every scenario, especially when intersections with opaque surfaces are involved.
860+
815861
Material Settings
816862
-----------------
817863

tutorials/shaders/shader_reference/spatial_shader.rst

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ For visual examples of these render modes, see :ref:`Standard Material 3D and OR
3636
+-------------------------------+------------------------------------------------------------------------------------------------------+
3737
| **depth_test_disabled** | Disable depth testing. |
3838
+-------------------------------+------------------------------------------------------------------------------------------------------+
39+
| **depth_test_default** | Depth test will discard the pixel if it is behind other pixels. |
40+
| | In Forward+ only, the pixel is also discarded if it's at the exact same depth as another pixel. |
41+
+-------------------------------+------------------------------------------------------------------------------------------------------+
42+
| **depth_test_inverted** | Depth test will discard the pixel if it is in front of other pixels. Useful for stencil effects. |
43+
+-------------------------------+------------------------------------------------------------------------------------------------------+
3944
| **sss_mode_skin** | Subsurface Scattering mode for skin (optimizes visuals for human skin, e.g. boosted red channel). |
4045
+-------------------------------+------------------------------------------------------------------------------------------------------+
4146
| **cull_back** | Cull back-faces (default). |
@@ -91,6 +96,58 @@ For visual examples of these render modes, see :ref:`Standard Material 3D and OR
9196
| **fog_disabled** | Disable receiving depth-based or volumetric fog. Useful for ``blend_add`` materials like particles. |
9297
+-------------------------------+------------------------------------------------------------------------------------------------------+
9398

99+
Stencil modes
100+
-------------
101+
102+
.. note::
103+
104+
Stencil support is experimental, use at your own risk.
105+
We will try to not break compatibility as much as possible,
106+
but if significant flaws are found in the API, it may change
107+
in the next minor version.
108+
109+
Stencil operations are a set of operations that allow writing to
110+
an efficient buffer in an hardware-accelerated manner.
111+
This is generally used to mask in or out parts of the scene.
112+
113+
Some of the most well-known uses are:
114+
115+
- Outlines: Mask out the inner mesh that is being outlined to avoid inner outlines.
116+
- X-Ray: Display a mesh behind other objects.
117+
- Portals: Draw geometry that is normally "impossible" (non-Euclidian) by masking objects.
118+
119+
.. note::
120+
121+
You can only read from the stencil buffer in the transparent pass.
122+
Any attempt to read in the opaque pass will fail, as it's currently not supported behavior.
123+
124+
Note that for compositor effects, the main renderer's stencil buffer can't be copied
125+
to a custom texture.
126+
127+
+-------------------------------+------------------------------------------------------------------------------------------------------+
128+
| Stencil mode | Description |
129+
+===============================+======================================================================================================+
130+
| **read** | Read from the stencil buffer. |
131+
+-------------------------------+------------------------------------------------------------------------------------------------------+
132+
| **write** | Write reference value to the stencil buffer. |
133+
+-------------------------------+------------------------------------------------------------------------------------------------------+
134+
| **write_if_depth_fail** | Write reference value to the stencil buffer if the depth test fails. |
135+
+-------------------------------+------------------------------------------------------------------------------------------------------+
136+
| **compare_always** | Always pass stencil test. |
137+
+-------------------------------+------------------------------------------------------------------------------------------------------+
138+
| **compare_equal** | Pass stencil test if the reference value is equal to the stencil buffer value. |
139+
+-------------------------------+------------------------------------------------------------------------------------------------------+
140+
| **compare_not_equal** | Pass stencil test if the reference value is not equal to the stencil buffer value. |
141+
+-------------------------------+------------------------------------------------------------------------------------------------------+
142+
| **compare_less** | Pass stencil test if the reference value is less than the stencil buffer value. |
143+
+-------------------------------+------------------------------------------------------------------------------------------------------+
144+
| **compare_less_or_equal** | Pass stencil test if the reference value is less than or equal to the stencil buffer value. |
145+
+-------------------------------+------------------------------------------------------------------------------------------------------+
146+
| **compare_greater** | Pass stencil test if the reference value is greater than the stencil buffer value. |
147+
+-------------------------------+------------------------------------------------------------------------------------------------------+
148+
| **compare_greater_or_equal** | Pass stencil test if the reference value is greater than or equal to the stencil buffer value. |
149+
+-------------------------------+------------------------------------------------------------------------------------------------------+
150+
94151
Built-ins
95152
---------
96153

0 commit comments

Comments
 (0)