spatial_shader.rst 63 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. .. _doc_spatial_shader:
  2. Spatial shaders
  3. ===============
  4. Spatial shaders are used for shading 3D objects. They are the most complex type of shader Godot offers.
  5. Spatial shaders are highly configurable with different render modes and different rendering options
  6. (e.g. Subsurface Scattering, Transmission, Ambient Occlusion, Rim lighting, etc.). Users can optionally
  7. write vertex, fragment, and light processor functions to affect how objects are drawn.
  8. Render modes
  9. ------------
  10. For visual examples of these render modes, see :ref:`Standard Material 3D and ORM Material 3D<doc_standard_material_3d>`.
  11. +-------------------------------+------------------------------------------------------------------------------------------------------+
  12. | Render mode | Description |
  13. +===============================+======================================================================================================+
  14. | **blend_mix** | Mix blend mode (alpha is transparency), default. |
  15. +-------------------------------+------------------------------------------------------------------------------------------------------+
  16. | **blend_add** | Additive blend mode. |
  17. +-------------------------------+------------------------------------------------------------------------------------------------------+
  18. | **blend_sub** | Subtractive blend mode. |
  19. +-------------------------------+------------------------------------------------------------------------------------------------------+
  20. | **blend_mul** | Multiplicative blend mode. |
  21. +-------------------------------+------------------------------------------------------------------------------------------------------+
  22. | **blend_premul_alpha** | Premultiplied alpha blend mode (fully transparent = add, fully opaque = mix). |
  23. +-------------------------------+------------------------------------------------------------------------------------------------------+
  24. | **depth_draw_opaque** | Only draw depth for opaque geometry (not transparent). |
  25. +-------------------------------+------------------------------------------------------------------------------------------------------+
  26. | **depth_draw_always** | Always draw depth (opaque and transparent). |
  27. +-------------------------------+------------------------------------------------------------------------------------------------------+
  28. | **depth_draw_never** | Never draw depth. |
  29. +-------------------------------+------------------------------------------------------------------------------------------------------+
  30. | **depth_prepass_alpha** | Do opaque depth pre-pass for transparent geometry. |
  31. +-------------------------------+------------------------------------------------------------------------------------------------------+
  32. | **depth_test_disabled** | Disable depth testing. |
  33. +-------------------------------+------------------------------------------------------------------------------------------------------+
  34. | **depth_test_default** | Depth test will discard the pixel if it is behind other pixels. |
  35. | | In Forward+ only, the pixel is also discarded if it's at the exact same depth as another pixel. |
  36. +-------------------------------+------------------------------------------------------------------------------------------------------+
  37. | **depth_test_inverted** | Depth test will discard the pixel if it is in front of other pixels. Useful for stencil effects. |
  38. +-------------------------------+------------------------------------------------------------------------------------------------------+
  39. | **sss_mode_skin** | Subsurface Scattering mode for skin (optimizes visuals for human skin, e.g. boosted red channel). |
  40. +-------------------------------+------------------------------------------------------------------------------------------------------+
  41. | **cull_back** | Cull back-faces (default). |
  42. +-------------------------------+------------------------------------------------------------------------------------------------------+
  43. | **cull_front** | Cull front-faces. |
  44. +-------------------------------+------------------------------------------------------------------------------------------------------+
  45. | **cull_disabled** | Culling disabled (double sided). |
  46. +-------------------------------+------------------------------------------------------------------------------------------------------+
  47. | **unshaded** | Result is just albedo. No lighting/shading happens in material, making it faster to render. |
  48. +-------------------------------+------------------------------------------------------------------------------------------------------+
  49. | **wireframe** | Geometry draws using lines (useful for troubleshooting). |
  50. +-------------------------------+------------------------------------------------------------------------------------------------------+
  51. | **debug_shadow_splits** | Directional shadows are drawn using different colors for each split (useful for troubleshooting). |
  52. +-------------------------------+------------------------------------------------------------------------------------------------------+
  53. | **diffuse_burley** | Burley (Disney PBS) for diffuse (default). |
  54. +-------------------------------+------------------------------------------------------------------------------------------------------+
  55. | **diffuse_lambert** | Lambert shading for diffuse. |
  56. +-------------------------------+------------------------------------------------------------------------------------------------------+
  57. | **diffuse_lambert_wrap** | Lambert-wrap shading (roughness-dependent) for diffuse. |
  58. +-------------------------------+------------------------------------------------------------------------------------------------------+
  59. | **diffuse_toon** | Toon shading for diffuse. |
  60. +-------------------------------+------------------------------------------------------------------------------------------------------+
  61. | **specular_schlick_ggx** | Schlick-GGX for direct light specular lobes (default). |
  62. +-------------------------------+------------------------------------------------------------------------------------------------------+
  63. | **specular_toon** | Toon for direct light specular lobes. |
  64. +-------------------------------+------------------------------------------------------------------------------------------------------+
  65. | **specular_disabled** | Disable direct light specular lobes. Doesn't affect reflected light (use ``SPECULAR = 0.0`` instead).|
  66. +-------------------------------+------------------------------------------------------------------------------------------------------+
  67. | **skip_vertex_transform** | ``VERTEX``, ``NORMAL``, ``TANGENT``, and ``BITANGENT`` |
  68. | | need to be transformed manually in the ``vertex()`` function. |
  69. +-------------------------------+------------------------------------------------------------------------------------------------------+
  70. | **world_vertex_coords** | ``VERTEX``, ``NORMAL``, ``TANGENT``, and ``BITANGENT`` |
  71. | | are modified in world space instead of model space. |
  72. +-------------------------------+------------------------------------------------------------------------------------------------------+
  73. | **ensure_correct_normals** | Use when non-uniform scale is applied to mesh *(note: currently unimplemented)*. |
  74. +-------------------------------+------------------------------------------------------------------------------------------------------+
  75. | **shadows_disabled** | Disable computing shadows in shader. The shader will not receive shadows, but can still cast them. |
  76. +-------------------------------+------------------------------------------------------------------------------------------------------+
  77. | **ambient_light_disabled** | Disable contribution from ambient light and radiance map. |
  78. +-------------------------------+------------------------------------------------------------------------------------------------------+
  79. | **shadow_to_opacity** | Lighting modifies the alpha so shadowed areas are opaque and |
  80. | | non-shadowed areas are transparent. Useful for overlaying shadows onto |
  81. | | a camera feed in AR. |
  82. +-------------------------------+------------------------------------------------------------------------------------------------------+
  83. | **vertex_lighting** | Use vertex-based lighting instead of per-pixel lighting. |
  84. +-------------------------------+------------------------------------------------------------------------------------------------------+
  85. | **particle_trails** | Enables the trails when used on particle geometry. |
  86. +-------------------------------+------------------------------------------------------------------------------------------------------+
  87. | **alpha_to_coverage** | Alpha antialiasing mode, see `here <https://github.com/godotengine/godot/pull/40364>`_ for more. |
  88. +-------------------------------+------------------------------------------------------------------------------------------------------+
  89. | **alpha_to_coverage_and_one** | Alpha antialiasing mode, see `here <https://github.com/godotengine/godot/pull/40364>`_ for more. |
  90. +-------------------------------+------------------------------------------------------------------------------------------------------+
  91. | **fog_disabled** | Disable receiving depth-based or volumetric fog. Useful for ``blend_add`` materials like particles. |
  92. +-------------------------------+------------------------------------------------------------------------------------------------------+
  93. Stencil modes
  94. -------------
  95. .. note::
  96. Stencil support is experimental, use at your own risk.
  97. We will try to not break compatibility as much as possible,
  98. but if significant flaws are found in the API, it may change
  99. in the next minor version.
  100. Stencil operations are a set of operations that allow writing to
  101. an efficient buffer in an hardware-accelerated manner.
  102. This is generally used to mask in or out parts of the scene.
  103. Some of the most well-known uses are:
  104. - Outlines: Mask out the inner mesh that is being outlined to avoid inner outlines.
  105. - X-Ray: Display a mesh behind other objects.
  106. - Portals: Draw geometry that is normally "impossible" (non-Euclidian) by masking objects.
  107. .. note::
  108. You can only read from the stencil buffer in the transparent pass.
  109. Any attempt to read in the opaque pass will fail, as it's currently not supported behavior.
  110. Note that for compositor effects, the main renderer's stencil buffer can't be copied
  111. to a custom texture.
  112. +-------------------------------+------------------------------------------------------------------------------------------------------+
  113. | Stencil mode | Description |
  114. +===============================+======================================================================================================+
  115. | **read** | Read from the stencil buffer. |
  116. +-------------------------------+------------------------------------------------------------------------------------------------------+
  117. | **write** | Write reference value to the stencil buffer. |
  118. +-------------------------------+------------------------------------------------------------------------------------------------------+
  119. | **write_if_depth_fail** | Write reference value to the stencil buffer if the depth test fails. |
  120. +-------------------------------+------------------------------------------------------------------------------------------------------+
  121. | **compare_always** | Always pass stencil test. |
  122. +-------------------------------+------------------------------------------------------------------------------------------------------+
  123. | **compare_equal** | Pass stencil test if the reference value is equal to the stencil buffer value. |
  124. +-------------------------------+------------------------------------------------------------------------------------------------------+
  125. | **compare_not_equal** | Pass stencil test if the reference value is not equal to the stencil buffer value. |
  126. +-------------------------------+------------------------------------------------------------------------------------------------------+
  127. | **compare_less** | Pass stencil test if the reference value is less than the stencil buffer value. |
  128. +-------------------------------+------------------------------------------------------------------------------------------------------+
  129. | **compare_less_or_equal** | Pass stencil test if the reference value is less than or equal to the stencil buffer value. |
  130. +-------------------------------+------------------------------------------------------------------------------------------------------+
  131. | **compare_greater** | Pass stencil test if the reference value is greater than the stencil buffer value. |
  132. +-------------------------------+------------------------------------------------------------------------------------------------------+
  133. | **compare_greater_or_equal** | Pass stencil test if the reference value is greater than or equal to the stencil buffer value. |
  134. +-------------------------------+------------------------------------------------------------------------------------------------------+
  135. Built-ins
  136. ---------
  137. Values marked as ``in`` are read-only. Values marked as ``out`` can optionally be written to and will
  138. not necessarily contain sensible values. Values marked as ``inout`` provide a sensible default
  139. value, and can optionally be written to. Samplers cannot be written to so they are not marked.
  140. Not all built-ins are available in all processing functions. To access a vertex
  141. built-in from the ``fragment()`` function, you can use a :ref:`varying <doc_shading_language_varyings>`.
  142. The same applies for accessing fragment built-ins from the ``light()`` function.
  143. Global built-ins
  144. ----------------
  145. Global built-ins are available everywhere, including custom functions.
  146. +-----------------------------+-----------------------------------------------------------------------------------------------------+
  147. | Built-in | Description |
  148. +=============================+=====================================================================================================+
  149. | in float **TIME** | Global time since the engine has started, in seconds. It repeats after every ``3,600`` |
  150. | | seconds (which can be changed with the |
  151. | | :ref:`rollover<class_ProjectSettings_property_rendering/limits/time/time_rollover_secs>` |
  152. | | setting). It's affected by :ref:`time_scale<class_Engine_property_time_scale>` but not by pausing. |
  153. | | If you need a ``TIME`` variable that is not affected by time scale, add your own |
  154. | | :ref:`global shader uniform<doc_shading_language_global_uniforms>` and update it each |
  155. | | frame. |
  156. +-----------------------------+-----------------------------------------------------------------------------------------------------+
  157. | in float **PI** | A ``PI`` constant (``3.141592``). |
  158. | | The ratio of a circle's circumference to its diameter and the number of radians in a half turn. |
  159. +-----------------------------+-----------------------------------------------------------------------------------------------------+
  160. | in float **TAU** | A ``TAU`` constant (``6.283185``). |
  161. | | Equivalent to ``PI * 2`` and the number of radians in a full turn. |
  162. +-----------------------------+-----------------------------------------------------------------------------------------------------+
  163. | in float **E** | An ``E`` constant (``2.718281``). Euler's number, the base of the natural logarithm. |
  164. +-----------------------------+-----------------------------------------------------------------------------------------------------+
  165. | in bool **OUTPUT_IS_SRGB** | ``true`` when output is in sRGB color space (this is ``true`` in the Compatibility |
  166. | | renderer, ``false`` in Forward+ and Mobile). |
  167. +-----------------------------+-----------------------------------------------------------------------------------------------------+
  168. | in float **CLIP_SPACE_FAR** | Clip space far ``z`` value. |
  169. | | In the Forward+ or Mobile renderers, it's ``0.0``. |
  170. | | In the Compatibility renderer, it's ``-1.0``. |
  171. +-----------------------------+-----------------------------------------------------------------------------------------------------+
  172. Vertex built-ins
  173. ----------------
  174. Vertex data (``VERTEX``, ``NORMAL``, ``TANGENT``, and ``BITANGENT``) are presented in model space
  175. (also called local space). If not written to, these values will not be modified and be
  176. passed through as they came, then transformed into view space to be used in ``fragment()``.
  177. They can optionally be presented in world space by using the ``world_vertex_coords`` render mode.
  178. Users can disable the built-in modelview transform (projection will still happen later) and do
  179. it manually with the following code:
  180. .. code-block:: glsl
  181. shader_type spatial;
  182. render_mode skip_vertex_transform;
  183. void vertex() {
  184. VERTEX = (MODELVIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
  185. NORMAL = normalize((MODELVIEW_MATRIX * vec4(NORMAL, 0.0)).xyz);
  186. BINORMAL = normalize((MODELVIEW_MATRIX * vec4(BINORMAL, 0.0)).xyz);
  187. TANGENT = normalize((MODELVIEW_MATRIX * vec4(TANGENT, 0.0)).xyz);
  188. }
  189. Other built-ins, such as ``UV``, ``UV2``, and ``COLOR``, are also passed through to the ``fragment()`` function if not modified.
  190. Users can override the modelview and projection transforms using the ``POSITION`` built-in. If ``POSITION`` is written
  191. to anywhere in the shader, it will always be used, so the user becomes responsible for ensuring that it always has
  192. an acceptable value. When ``POSITION`` is used, the value from ``VERTEX`` is ignored and projection does not happen.
  193. However, the value passed to the fragment shader still comes from ``VERTEX``.
  194. For instancing, the ``INSTANCE_CUSTOM`` variable contains the instance custom data. When using particles, this information
  195. is usually:
  196. * **x**: Rotation angle in radians.
  197. * **y**: Phase during lifetime (``0.0`` to ``1.0``).
  198. * **z**: Animation frame.
  199. This allows you to easily adjust the shader to a particle system using default particle material. When writing a custom particle
  200. shader, this value can be used as desired.
  201. +----------------------------------------+--------------------------------------------------------+
  202. | Built-in | Description |
  203. +========================================+========================================================+
  204. | in vec2 **VIEWPORT_SIZE** | Size of viewport (in pixels). |
  205. +----------------------------------------+--------------------------------------------------------+
  206. | in mat4 **VIEW_MATRIX** | World space to view space transform. |
  207. +----------------------------------------+--------------------------------------------------------+
  208. | in mat4 **INV_VIEW_MATRIX** | View space to world space transform. |
  209. +----------------------------------------+--------------------------------------------------------+
  210. | in mat4 **MAIN_CAM_INV_VIEW_MATRIX** | View space to world space transform of the camera used |
  211. | | to draw the current viewport. |
  212. +----------------------------------------+--------------------------------------------------------+
  213. | in mat4 **INV_PROJECTION_MATRIX** | Clip space to view space transform. |
  214. +----------------------------------------+--------------------------------------------------------+
  215. | in vec3 **NODE_POSITION_WORLD** | Node position, in world space. |
  216. +----------------------------------------+--------------------------------------------------------+
  217. | in vec3 **NODE_POSITION_VIEW** | Node position, in view space. |
  218. +----------------------------------------+--------------------------------------------------------+
  219. | in vec3 **CAMERA_POSITION_WORLD** | Camera position, in world space. Represents the |
  220. | | midpoint of the two eyes when in multiview/stereo |
  221. | | rendering. |
  222. +----------------------------------------+--------------------------------------------------------+
  223. | in vec3 **CAMERA_DIRECTION_WORLD** | Camera direction, in world space. |
  224. +----------------------------------------+--------------------------------------------------------+
  225. | in uint **CAMERA_VISIBLE_LAYERS** | Cull layers of the camera rendering the current pass. |
  226. +----------------------------------------+--------------------------------------------------------+
  227. | in int **INSTANCE_ID** | Instance ID for instancing. |
  228. +----------------------------------------+--------------------------------------------------------+
  229. | in vec4 **INSTANCE_CUSTOM** | Instance custom data (for particles, mostly). |
  230. +----------------------------------------+--------------------------------------------------------+
  231. | in int **VIEW_INDEX** | The view that we are rendering. |
  232. | | ``VIEW_MONO_LEFT`` (``0``) for Mono (not multiview) or |
  233. | | left eye, ``VIEW_RIGHT`` (``1``) for right eye. |
  234. +----------------------------------------+--------------------------------------------------------+
  235. | in int **VIEW_MONO_LEFT** | Constant for Mono or left eye, always ``0``. |
  236. +----------------------------------------+--------------------------------------------------------+
  237. | in int **VIEW_RIGHT** | Constant for right eye, always ``1``. |
  238. +----------------------------------------+--------------------------------------------------------+
  239. | in vec3 **EYE_OFFSET** | Position offset for the eye being rendered, in view |
  240. | | space. Only applicable for multiview rendering. |
  241. +----------------------------------------+--------------------------------------------------------+
  242. | inout vec3 **VERTEX** | Position of the vertex, in model space. |
  243. | | In world space if ``world_vertex_coords`` is used. |
  244. +----------------------------------------+--------------------------------------------------------+
  245. | in int **VERTEX_ID** | The index of the current vertex in the vertex buffer. |
  246. +----------------------------------------+--------------------------------------------------------+
  247. | inout vec3 **NORMAL** | Normal in model space. |
  248. | | In world space if ``world_vertex_coords`` is used. |
  249. +----------------------------------------+--------------------------------------------------------+
  250. | inout vec3 **TANGENT** | Tangent in model space. |
  251. | | In world space if ``world_vertex_coords`` is used. |
  252. +----------------------------------------+--------------------------------------------------------+
  253. | inout vec3 **BINORMAL** | Binormal in model space. |
  254. | | In world space if ``world_vertex_coords`` is used. |
  255. +----------------------------------------+--------------------------------------------------------+
  256. | out vec4 **POSITION** | If written to, overrides final vertex position in clip |
  257. | | space. |
  258. +----------------------------------------+--------------------------------------------------------+
  259. | inout vec2 **UV** | UV main channel. |
  260. +----------------------------------------+--------------------------------------------------------+
  261. | inout vec2 **UV2** | UV secondary channel. |
  262. +----------------------------------------+--------------------------------------------------------+
  263. | inout vec4 **COLOR** | Color from vertices. |
  264. +----------------------------------------+--------------------------------------------------------+
  265. | out float **ROUGHNESS** | Roughness for vertex lighting. |
  266. +----------------------------------------+--------------------------------------------------------+
  267. | inout float **POINT_SIZE** | Point size for point rendering. |
  268. +----------------------------------------+--------------------------------------------------------+
  269. | inout mat4 **MODELVIEW_MATRIX** | Model/local space to view space transform |
  270. | | (use if possible). |
  271. +----------------------------------------+--------------------------------------------------------+
  272. | inout mat3 **MODELVIEW_NORMAL_MATRIX** | |
  273. +----------------------------------------+--------------------------------------------------------+
  274. | in mat4 **MODEL_MATRIX** | Model/local space to world space transform. |
  275. +----------------------------------------+--------------------------------------------------------+
  276. | in mat3 **MODEL_NORMAL_MATRIX** | |
  277. +----------------------------------------+--------------------------------------------------------+
  278. | inout mat4 **PROJECTION_MATRIX** | View space to clip space transform. |
  279. +----------------------------------------+--------------------------------------------------------+
  280. | in uvec4 **BONE_INDICES** | |
  281. +----------------------------------------+--------------------------------------------------------+
  282. | in vec4 **BONE_WEIGHTS** | |
  283. +----------------------------------------+--------------------------------------------------------+
  284. | in vec4 **CUSTOM0** | Custom value from vertex primitive. When using extra |
  285. | | UVs, ``xy`` is UV3 and ``zw`` is UV4. |
  286. +----------------------------------------+--------------------------------------------------------+
  287. | in vec4 **CUSTOM1** | Custom value from vertex primitive. When using extra |
  288. | | UVs, ``xy`` is UV5 and ``zw`` is UV6. |
  289. +----------------------------------------+--------------------------------------------------------+
  290. | in vec4 **CUSTOM2** | Custom value from vertex primitive. When using extra |
  291. | | UVs, ``xy`` is UV7 and ``zw`` is UV8. |
  292. +----------------------------------------+--------------------------------------------------------+
  293. | in vec4 **CUSTOM3** | Custom value from vertex primitive. |
  294. +----------------------------------------+--------------------------------------------------------+
  295. | out float **Z_CLIP_SCALE** | If written to, scales the vertex towards the camera to |
  296. | | avoid clipping into things like walls. |
  297. | | Lighting and shadows will continue to work correctly |
  298. | | when this is written to, but screen-space effects like |
  299. | | SSAO and SSR may break with lower scales. Try to keep |
  300. | | this value as close to ``1.0`` as possible. |
  301. +----------------------------------------+--------------------------------------------------------+
  302. .. note::
  303. ``MODELVIEW_MATRIX`` combines both the ``MODEL_MATRIX`` and ``VIEW_MATRIX`` and is better suited when floating point issues may arise. For example, if the object is very far away from the world origin, you may run into floating point issues when using the separated ``MODEL_MATRIX`` and ``VIEW_MATRIX``.
  304. .. note::
  305. ``INV_VIEW_MATRIX`` is the matrix used for rendering the object in that pass, unlike ``MAIN_CAM_INV_VIEW_MATRIX``, which is the matrix of the camera in the scene. In the shadow pass, ``INV_VIEW_MATRIX``'s view is based on the camera that is located at the position of the light.
  306. Fragment built-ins
  307. ------------------
  308. The default use of a Godot fragment processor function is to set up the material properties of your object
  309. and to let the built-in renderer handle the final shading. However, you are not required to use all
  310. these properties, and if you don't write to them, Godot will optimize away the corresponding functionality.
  311. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  312. | Built-in | Description |
  313. +========================================+==================================================================================================+
  314. | in vec2 **VIEWPORT_SIZE** | Size of viewport (in pixels). |
  315. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  316. | in vec4 **FRAGCOORD** | Coordinate of pixel center in screen space. ``xy`` specifies position in window. Origin is lower |
  317. | | left. ``z`` specifies fragment depth. It is also used as the output value for the fragment depth |
  318. | | unless ``DEPTH`` is written to. |
  319. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  320. | in bool **FRONT_FACING** | ``true`` if current face is front facing, ``false`` otherwise. |
  321. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  322. | in vec3 **VIEW** | Normalized vector from fragment position to camera (in view space). This is the same for both |
  323. | | perspective and orthogonal cameras. |
  324. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  325. | in vec2 **UV** | UV that comes from the ``vertex()`` function. |
  326. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  327. | in vec2 **UV2** | UV2 that comes from the ``vertex()`` function. |
  328. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  329. | in vec4 **COLOR** | COLOR that comes from the ``vertex()`` function. |
  330. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  331. | in vec2 **POINT_COORD** | Point coordinate for drawing points with ``POINT_SIZE``. |
  332. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  333. | in mat4 **MODEL_MATRIX** | Model/local space to world space transform. |
  334. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  335. | in mat3 **MODEL_NORMAL_MATRIX** | Model/local space to world space transform for normals. This is the same as ``MODEL_MATRIX`` |
  336. | | by default unless the object is scaled non-uniformly, in which case this is set to |
  337. | | ``transpose(inverse(mat3(MODEL_MATRIX)))``. |
  338. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  339. | in mat4 **VIEW_MATRIX** | World space to view space transform. |
  340. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  341. | in mat4 **INV_VIEW_MATRIX** | View space to world space transform. |
  342. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  343. | in mat4 **PROJECTION_MATRIX** | View space to clip space transform. |
  344. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  345. | in mat4 **INV_PROJECTION_MATRIX** | Clip space to view space transform. |
  346. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  347. | in vec3 **NODE_POSITION_WORLD** | Node position, in world space. |
  348. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  349. | in vec3 **NODE_POSITION_VIEW** | Node position, in view space. |
  350. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  351. | in vec3 **CAMERA_POSITION_WORLD** | Camera position, in world space. Represents the midpoint of the two eyes when in |
  352. | | multiview/stereo rendering. |
  353. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  354. | in vec3 **CAMERA_DIRECTION_WORLD** | Camera direction, in world space. |
  355. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  356. | in uint **CAMERA_VISIBLE_LAYERS** | Cull layers of the camera rendering the current pass. |
  357. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  358. | in vec3 **VERTEX** | Position of the fragment (pixel), in view space. It is the ``VERTEX`` value from ``vertex()`` |
  359. | | interpolated between the face's vertices and transformed into view space. |
  360. | | If ``skip_vertex_transform`` is enabled, it may not be in view space. |
  361. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  362. | inout vec3 **LIGHT_VERTEX** | A writable version of ``VERTEX`` that can be used to alter light and shadows. Writing to this |
  363. | | will not change the position of the fragment. |
  364. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  365. | in int **VIEW_INDEX** | The view that we are rendering. Used to distinguish between views in multiview/stereo rendering. |
  366. | | ``VIEW_MONO_LEFT`` (``0``) for Mono (not multiview) or |
  367. | | left eye, ``VIEW_RIGHT`` (``1``) for right eye. |
  368. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  369. | in int **VIEW_MONO_LEFT** | Constant for Mono or left eye, always ``0``. |
  370. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  371. | in int **VIEW_RIGHT** | Constant for right eye, always ``1``. |
  372. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  373. | in vec3 **EYE_OFFSET** | Position offset for the eye being rendered, in view space. Only applicable for multiview |
  374. | | rendering. |
  375. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  376. | sampler2D **SCREEN_TEXTURE** | Removed in Godot 4. Use a ``sampler2D`` with ``hint_screen_texture`` instead. |
  377. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  378. | in vec2 **SCREEN_UV** | Screen UV coordinate for the current pixel. |
  379. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  380. | sampler2D **DEPTH_TEXTURE** | Removed in Godot 4. Use a ``sampler2D`` with ``hint_depth_texture`` instead. |
  381. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  382. | out float **DEPTH** | Custom depth value (range ``[0.0, 1.0]``). If ``DEPTH`` is written to in any shader branch, |
  383. | | then you are responsible for setting ``DEPTH`` for **all** other branches. |
  384. | | Otherwise, the graphics API will leave them uninitialized. |
  385. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  386. | inout vec3 **NORMAL** | Normal that comes from the ``vertex()`` function, in view space. |
  387. | | If ``skip_vertex_transform`` is enabled, it may not be in view space. |
  388. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  389. | inout vec3 **TANGENT** | Tangent that comes from the ``vertex()`` function, in view space. |
  390. | | If ``skip_vertex_transform`` is enabled, it may not be in view space. |
  391. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  392. | inout vec3 **BINORMAL** | Binormal that comes from the ``vertex()`` function, in view space. |
  393. | | If ``skip_vertex_transform`` is enabled, it may not be in view space. |
  394. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  395. | out vec3 **NORMAL_MAP** | Set normal here if reading normal from a texture instead of ``NORMAL``. |
  396. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  397. | out float **NORMAL_MAP_DEPTH** | Depth from ``NORMAL_MAP``. Defaults to ``1.0``. |
  398. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  399. | out vec3 **ALBEDO** | Albedo (default white). Base color. |
  400. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  401. | out float **ALPHA** | Alpha (range ``[0.0, 1.0]``). If read from or written to, the material will go to the |
  402. | | transparent pipeline. |
  403. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  404. | out float **ALPHA_SCISSOR_THRESHOLD** | If written to, values below a certain amount of alpha are discarded. |
  405. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  406. | out float **ALPHA_HASH_SCALE** | Alpha hash scale when using the alpha hash transparency mode. Defaults to ``1.0``. |
  407. | | Higher values result in more visible pixels in the dithering pattern. |
  408. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  409. | out float **ALPHA_ANTIALIASING_EDGE** | The threshold below which alpha to coverage antialiasing should be used. Defaults to ``0.0``. |
  410. | | Requires the ``alpha_to_coverage`` render mode. Should be set to a value lower than |
  411. | | ``ALPHA_SCISSOR_THRESHOLD`` to be effective. |
  412. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  413. | out vec2 **ALPHA_TEXTURE_COORDINATE** | The texture coordinate to use for alpha-to-coverge antialiasing. Requires the |
  414. | | ``alpha_to_coverage`` render mode. Typically set to ``UV * vec2(albedo_texture_size)`` where |
  415. | | ``albedo_texture_size`` is the size of the albedo texture in pixels. |
  416. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  417. | out float **PREMUL_ALPHA_FACTOR** | Premultiplied alpha factor. Only effective if ``render_mode blend_premul_alpha;`` is used. |
  418. | | This should be written to when using a *shaded* material with premultiplied alpha blending for |
  419. | | interaction with lighting. This is not required for unshaded materials. |
  420. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  421. | out float **METALLIC** | Metallic (range ``[0.0, 1.0]``). |
  422. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  423. | out float **SPECULAR** | Specular (not physically accurate to change). Defaults to ``0.5``. ``0.0`` disables reflections. |
  424. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  425. | out float **ROUGHNESS** | Roughness (range ``[0.0, 1.0]``). |
  426. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  427. | out float **RIM** | Rim (range ``[0.0, 1.0]``). If used, Godot calculates rim lighting. |
  428. | | Rim size depends on ``ROUGHNESS``. |
  429. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  430. | out float **RIM_TINT** | Rim Tint, range from ``0.0`` (white) to ``1.0`` (albedo). If used, Godot calculates rim lighting.|
  431. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  432. | out float **CLEARCOAT** | Small specular blob added on top of the existing one. If used, Godot calculates clearcoat. |
  433. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  434. | out float **CLEARCOAT_GLOSS** | Gloss of clearcoat. If used, Godot calculates clearcoat. |
  435. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  436. | out float **ANISOTROPY** | For distorting the specular blob according to tangent space. |
  437. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  438. | out vec2 **ANISOTROPY_FLOW** | Distortion direction, use with flowmaps. |
  439. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  440. | out float **SSS_STRENGTH** | Strength of subsurface scattering. If used, subsurface scattering will be applied to the object. |
  441. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  442. | out vec4 **SSS_TRANSMITTANCE_COLOR** | Color of subsurface scattering transmittance. If used, subsurface scattering transmittance |
  443. | | will be applied to the object. |
  444. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  445. | out float **SSS_TRANSMITTANCE_DEPTH** | Depth of subsurface scattering transmittance. Higher values allow the effect to reach deeper |
  446. | | into the object. |
  447. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  448. | out float **SSS_TRANSMITTANCE_BOOST** | Boosts the subsurface scattering transmittance if set above ``0.0``. This makes the effect |
  449. | | show up even on directly lit surfaces |
  450. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  451. | inout vec3 **BACKLIGHT** | Color of backlighting (works like direct light, but it's received even if the normal |
  452. | | is slightly facing away from the light). If used, backlighting will be applied to the object. |
  453. | | Can be used as a cheaper approximation of subsurface scattering. |
  454. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  455. | out float **AO** | Strength of ambient occlusion. For use with pre-baked AO. |
  456. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  457. | out float **AO_LIGHT_AFFECT** | How much ambient occlusion affects direct light (range ``[0.0, 1.0]``, default ``0.0``). |
  458. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  459. | out vec3 **EMISSION** | Emission color (can go over ``(1.0, 1.0, 1.0)`` for HDR). |
  460. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  461. | out vec4 **FOG** | If written to, blends final pixel color with ``FOG.rgb`` based on ``FOG.a``. |
  462. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  463. | out vec4 **RADIANCE** | If written to, blends environment map radiance with ``RADIANCE.rgb`` based on ``RADIANCE.a``. |
  464. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  465. | out vec4 **IRRADIANCE** | If written to, blends environment map irradiance with ``IRRADIANCE.rgb`` based on |
  466. | | ``IRRADIANCE.a``. |
  467. +----------------------------------------+--------------------------------------------------------------------------------------------------+
  468. .. note::
  469. Shaders going through the transparent pipeline when ``ALPHA`` is written to
  470. may exhibit transparency sorting issues. Read the
  471. :ref:`transparency sorting section in the 3D rendering limitations page <doc_3d_rendering_limitations_transparency_sorting>`
  472. for more information and ways to avoid issues.
  473. Light built-ins
  474. ---------------
  475. Writing light processor functions is completely optional. You can skip the ``light()`` function by using
  476. the ``unshaded`` render mode. If no light function is written, Godot will use the material properties
  477. written to in the ``fragment()`` function to calculate the lighting for you (subject to the render mode).
  478. The ``light()`` function is called for every light in every pixel. It is called within a loop for each light type.
  479. Below is an example of a custom ``light()`` function using a Lambertian lighting model:
  480. .. code-block:: glsl
  481. void light() {
  482. DIFFUSE_LIGHT += clamp(dot(NORMAL, LIGHT), 0.0, 1.0) * ATTENUATION * LIGHT_COLOR / PI;
  483. }
  484. If you want the lights to add together, add the light contribution to ``DIFFUSE_LIGHT`` using ``+=``, rather than overwriting it.
  485. .. warning::
  486. The ``light()`` function won't be run if the ``vertex_lighting`` render mode is enabled, or if
  487. :ref:`Rendering > Quality > Shading > Force Vertex Shading<class_ProjectSettings_property_rendering/shading/overrides/force_vertex_shading>`
  488. is enabled in the Project Settings. (It's enabled by default on mobile platforms.)
  489. +-----------------------------------+------------------------------------------------------------------------+
  490. | Built-in | Description |
  491. +===================================+========================================================================+
  492. | in vec2 **VIEWPORT_SIZE** | Size of viewport (in pixels). |
  493. +-----------------------------------+------------------------------------------------------------------------+
  494. | in vec4 **FRAGCOORD** | Coordinate of pixel center in screen space. |
  495. | | ``xy`` specifies position in window, ``z`` |
  496. | | specifies fragment depth if ``DEPTH`` is not used. |
  497. | | Origin is lower-left. |
  498. +-----------------------------------+------------------------------------------------------------------------+
  499. | in mat4 **MODEL_MATRIX** | Model/local space to world space transform. |
  500. +-----------------------------------+------------------------------------------------------------------------+
  501. | in mat4 **INV_VIEW_MATRIX** | View space to world space transform. |
  502. +-----------------------------------+------------------------------------------------------------------------+
  503. | in mat4 **VIEW_MATRIX** | World space to view space transform. |
  504. +-----------------------------------+------------------------------------------------------------------------+
  505. | in mat4 **PROJECTION_MATRIX** | View space to clip space transform. |
  506. +-----------------------------------+------------------------------------------------------------------------+
  507. | in mat4 **INV_PROJECTION_MATRIX** | Clip space to view space transform. |
  508. +-----------------------------------+------------------------------------------------------------------------+
  509. | in vec3 **NORMAL** | Normal vector, in view space. |
  510. +-----------------------------------+------------------------------------------------------------------------+
  511. | in vec2 **SCREEN_UV** | Screen UV coordinate for the current pixel. |
  512. +-----------------------------------+------------------------------------------------------------------------+
  513. | in vec2 **UV** | UV that comes from the ``vertex()`` function. |
  514. +-----------------------------------+------------------------------------------------------------------------+
  515. | in vec2 **UV2** | UV2 that comes from the ``vertex()`` function. |
  516. +-----------------------------------+------------------------------------------------------------------------+
  517. | in vec3 **VIEW** | View vector, in view space. |
  518. +-----------------------------------+------------------------------------------------------------------------+
  519. | in vec3 **LIGHT** | Light vector, in view space. |
  520. +-----------------------------------+------------------------------------------------------------------------+
  521. | in vec3 **LIGHT_COLOR** | :ref:`Light color<class_Light3D_property_light_color>` multiplied by |
  522. | | :ref:`light energy<class_Light3D_property_light_energy>` multiplied by |
  523. | | ``PI``. The ``PI`` multiplication is present because |
  524. | | physically-based lighting models include a division by ``PI``. |
  525. +-----------------------------------+------------------------------------------------------------------------+
  526. | in float **SPECULAR_AMOUNT** | For :ref:`class_OmniLight3D` and :ref:`class_SpotLight3D`, |
  527. | | ``2.0`` multiplied by |
  528. | | :ref:`light_specular<class_Light3D_property_light_specular>`. |
  529. | | For :ref:`class_DirectionalLight3D`, ``1.0``. |
  530. +-----------------------------------+------------------------------------------------------------------------+
  531. | in bool **LIGHT_IS_DIRECTIONAL** | ``true`` if this pass is a :ref:`class_DirectionalLight3D`. |
  532. +-----------------------------------+------------------------------------------------------------------------+
  533. | in float **ATTENUATION** | Attenuation based on distance or shadow. |
  534. +-----------------------------------+------------------------------------------------------------------------+
  535. | in vec3 **ALBEDO** | Base albedo. |
  536. +-----------------------------------+------------------------------------------------------------------------+
  537. | in vec3 **BACKLIGHT** | |
  538. +-----------------------------------+------------------------------------------------------------------------+
  539. | in float **METALLIC** | Metallic. |
  540. +-----------------------------------+------------------------------------------------------------------------+
  541. | in float **ROUGHNESS** | Roughness. |
  542. +-----------------------------------+------------------------------------------------------------------------+
  543. | out vec3 **DIFFUSE_LIGHT** | Diffuse light result. |
  544. +-----------------------------------+------------------------------------------------------------------------+
  545. | out vec3 **SPECULAR_LIGHT** | Specular light result. |
  546. +-----------------------------------+------------------------------------------------------------------------+
  547. | out float **ALPHA** | Alpha (range ``[0.0, 1.0]``). If written to, the material will go to |
  548. | | the transparent pipeline. |
  549. +-----------------------------------+------------------------------------------------------------------------+
  550. .. note::
  551. Shaders going through the transparent pipeline when ``ALPHA`` is written to
  552. may exhibit transparency sorting issues. Read the
  553. :ref:`transparency sorting section in the 3D rendering limitations page <doc_3d_rendering_limitations_transparency_sorting>`
  554. for more information and ways to avoid issues.
  555. Transparent materials also cannot cast shadows or appear in
  556. ``hint_screen_texture`` and ``hint_depth_texture`` uniforms. This in turn prevents those
  557. materials from appearing in screen-space reflections or refraction.
  558. :ref:`SDFGI <doc_using_sdfgi>` sharp reflections are not visible on transparent
  559. materials (only rough reflections are visible on transparent materials).