canvas_item_shader.rst 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. .. _doc_canvas_item_shader:
  2. CanvasItem shaders
  3. ==================
  4. CanvasItem shaders are used to draw all 2D elements in Godot. These include
  5. all nodes that inherit from CanvasItems, and all GUI elements.
  6. CanvasItem shaders contain fewer built-in variables and functionality than
  7. :ref:`Spatial shaders<doc_spatial_shader>`, but they maintain the same basic structure
  8. with vertex, fragment, and light processor functions.
  9. Render modes
  10. ^^^^^^^^^^^^
  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** | Pre-multiplied alpha blend mode. |
  23. +---------------------------------+----------------------------------------------------------------------+
  24. | **blend_disabled** | Disable blending, values (including alpha) are written as-is. |
  25. +---------------------------------+----------------------------------------------------------------------+
  26. | **unshaded** | Result is just albedo. No lighting/shading happens in material. |
  27. +---------------------------------+----------------------------------------------------------------------+
  28. | **light_only** | Only draw on light pass. |
  29. +---------------------------------+----------------------------------------------------------------------+
  30. | **skip_vertex_transform** | ``VERTEX`` needs to be transformed manually in the ``vertex()`` |
  31. | | function. |
  32. +---------------------------------+----------------------------------------------------------------------+
  33. | **world_vertex_coords** | ``VERTEX`` is modified in world coordinates instead of local. |
  34. +---------------------------------+----------------------------------------------------------------------+
  35. Built-ins
  36. ^^^^^^^^^
  37. Values marked as ``in`` are read-only. Values marked as ``out`` can optionally be written to and will
  38. not necessarily contain sensible values. Values marked as ``inout`` provide a sensible default
  39. value, and can optionally be written to. Samplers cannot be written to so they are not marked.
  40. Global built-ins
  41. ^^^^^^^^^^^^^^^^
  42. Global built-ins are available everywhere, including custom functions.
  43. +-------------------+------------------------------------------------------------------------------------------+
  44. | Built-in | Description |
  45. +===================+==========================================================================================+
  46. | in float **TIME** | Global time since the engine has started, in seconds. It repeats after every ``3,600`` |
  47. | | seconds (which can be changed with the |
  48. | | :ref:`rollover<class_ProjectSettings_property_rendering/limits/time/time_rollover_secs>` |
  49. | | setting). It's affected by |
  50. | | :ref:`time_scale<class_Engine_property_time_scale>` but not by pausing. If you need a |
  51. | | ``TIME`` variable that is not affected by time scale, add your own |
  52. | | :ref:`global shader uniform<doc_shading_language_global_uniforms>` and update it each |
  53. | | frame. |
  54. +-------------------+------------------------------------------------------------------------------------------+
  55. | in float **PI** | A ``PI`` constant (``3.141592``). |
  56. | | A ratio of a circle's circumference to its diameter and amount of radians in half turn. |
  57. +-------------------+------------------------------------------------------------------------------------------+
  58. | in float **TAU** | A ``TAU`` constant (``6.283185``). |
  59. | | An equivalent of ``PI * 2`` and amount of radians in full turn. |
  60. +-------------------+------------------------------------------------------------------------------------------+
  61. | in float **E** | An ``E`` constant (``2.718281``). |
  62. | | Euler's number and a base of the natural logarithm. |
  63. +-------------------+------------------------------------------------------------------------------------------+
  64. Vertex built-ins
  65. ^^^^^^^^^^^^^^^^
  66. Vertex data (``VERTEX``) is presented in local space (pixel coordinates, relative to the Node2D's origin).
  67. If not written to, these values will not be modified and be passed through as they came.
  68. The user can disable the built-in model to world transform (world to screen and projection will still
  69. happen later) and do it manually with the following code:
  70. .. code-block:: glsl
  71. shader_type canvas_item;
  72. render_mode skip_vertex_transform;
  73. void vertex() {
  74. VERTEX = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
  75. }
  76. Other built-ins, such as ``UV`` and ``COLOR``, are also passed through to the ``fragment()`` function if not modified.
  77. For instancing, the ``INSTANCE_CUSTOM`` variable contains the instance custom data. When using particles, this information
  78. is usually:
  79. * **x**: Rotation angle in radians.
  80. * **y**: Phase during lifetime (``0.0`` to ``1.0``).
  81. * **z**: Animation frame.
  82. +--------------------------------+----------------------------------------------------------------+
  83. | Built-in | Description |
  84. +================================+================================================================+
  85. | in mat4 **MODEL_MATRIX** | Local space to world space transform. World space |
  86. | | is the coordinates you normally use in the editor. |
  87. +--------------------------------+----------------------------------------------------------------+
  88. | in mat4 **CANVAS_MATRIX** | World space to canvas space transform. In canvas |
  89. | | space the origin is the upper-left corner of the |
  90. | | screen and coordinates ranging from ``(0.0, 0.0)`` |
  91. | | to viewport size. |
  92. +--------------------------------+----------------------------------------------------------------+
  93. | in mat4 **SCREEN_MATRIX** | Canvas space to clip space. In clip space |
  94. | | coordinates ranging from ``(-1.0, -1.0)`` to |
  95. | | ``(1.0, 1.0).`` |
  96. +--------------------------------+----------------------------------------------------------------+
  97. | in int **INSTANCE_ID** | Instance ID for instancing. |
  98. +--------------------------------+----------------------------------------------------------------+
  99. | in vec4 **INSTANCE_CUSTOM** | Instance custom data. |
  100. +--------------------------------+----------------------------------------------------------------+
  101. | in bool **AT_LIGHT_PASS** | Always ``false``. |
  102. +--------------------------------+----------------------------------------------------------------+
  103. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of default 2D texture. |
  104. | | For a Sprite2D with a texture of size 64x32px, |
  105. | | **TEXTURE_PIXEL_SIZE** = ``vec2(1/64, 1/32)`` |
  106. +--------------------------------+----------------------------------------------------------------+
  107. | inout vec2 **VERTEX** | Vertex position, in local space. |
  108. +--------------------------------+----------------------------------------------------------------+
  109. | in int **VERTEX_ID** | The index of the current vertex in the vertex |
  110. | | buffer. |
  111. +--------------------------------+----------------------------------------------------------------+
  112. | inout vec2 **UV** | Normalized texture coordinates. Range from ``0.0`` |
  113. | | to ``1.0``. |
  114. +--------------------------------+----------------------------------------------------------------+
  115. | inout vec4 **COLOR** | Color from vertex primitive multiplied by CanvasItem's |
  116. | | :ref:`modulate<class_CanvasItem_property_modulate>` |
  117. | | multiplied by CanvasItem's |
  118. | | :ref:`self_modulate<class_CanvasItem_property_self_modulate>`. |
  119. +--------------------------------+----------------------------------------------------------------+
  120. | inout float **POINT_SIZE** | Point size for point drawing. |
  121. +--------------------------------+----------------------------------------------------------------+
  122. | in vec4 **CUSTOM0** | Custom value from vertex primitive. |
  123. +--------------------------------+----------------------------------------------------------------+
  124. | in vec4 **CUSTOM1** | Custom value from vertex primitive. |
  125. +--------------------------------+----------------------------------------------------------------+
  126. Fragment built-ins
  127. ^^^^^^^^^^^^^^^^^^
  128. COLOR and TEXTURE
  129. ~~~~~~~~~~~~~~~~~
  130. The built-in variable ``COLOR`` is used for a few things:
  131. - In the ``vertex()`` function, ``COLOR`` contains the color from the vertex
  132. primitive multiplied by the CanvasItem's
  133. :ref:`modulate<class_CanvasItem_property_modulate>` multiplied by the
  134. CanvasItem's :ref:`self_modulate<class_CanvasItem_property_self_modulate>`.
  135. - In the ``fragment()`` function, the input value ``COLOR`` is that same value
  136. multiplied by the color from the default ``TEXTURE`` (if present).
  137. - In the ``fragment()`` function, ``COLOR`` is also the final output.
  138. Certain nodes (for example, :ref:`Sprite2D <class_Sprite2D>`) display a texture
  139. by default, for example :ref:`texture <class_Sprite2D_property_texture>`. When
  140. using a custom ``fragment()`` function, you have a few options on how to sample
  141. this texture.
  142. To read only the contents of the default texture, ignoring the vertex ``COLOR``:
  143. .. code-block:: glsl
  144. void fragment() {
  145. COLOR = texture(TEXTURE, UV);
  146. }
  147. To read the contents of the default texture multiplied by vertex ``COLOR``:
  148. .. code-block:: glsl
  149. void fragment() {
  150. // Equivalent to an empty fragment() function, since COLOR is also the output variable.
  151. COLOR = COLOR;
  152. }
  153. To read only the vertex ``COLOR`` in ``fragment()``, ignoring the main texture,
  154. you must pass ``COLOR`` as a varying, then read it in ``fragment()``:
  155. .. code-block:: glsl
  156. varying vec4 vertex_color;
  157. void vertex() {
  158. vertex_color = COLOR;
  159. }
  160. void fragment() {
  161. COLOR = vertex_color;
  162. }
  163. NORMAL
  164. ~~~~~~
  165. Similarly, if a normal map is used in the :ref:`CanvasTexture <class_CanvasTexture>`, Godot uses
  166. it by default and assigns its value to the built-in ``NORMAL`` variable. If you are using a normal
  167. map meant for use in 3D, it will appear inverted. In order to use it in your shader, you must assign
  168. it to the ``NORMAL_MAP`` property. Godot will handle converting it for use in 2D and overwriting ``NORMAL``.
  169. .. code-block:: glsl
  170. NORMAL_MAP = texture(NORMAL_TEXTURE, UV).rgb;
  171. +---------------------------------------------+---------------------------------------------------------------+
  172. | Built-in | Description |
  173. +=============================================+===============================================================+
  174. | in vec4 **FRAGCOORD** | Coordinate of pixel center. In screen space. ``xy`` specifies |
  175. | | position in viewport. Upper-left of the viewport is the |
  176. | | origin, ``(0.0, 0.0)``. |
  177. +---------------------------------------------+---------------------------------------------------------------+
  178. | in vec2 **SCREEN_PIXEL_SIZE** | Size of individual pixels. Equal to inverse of resolution. |
  179. +---------------------------------------------+---------------------------------------------------------------+
  180. | in vec2 **POINT_COORD** | Coordinate for drawing points. |
  181. +---------------------------------------------+---------------------------------------------------------------+
  182. | sampler2D **TEXTURE** | Default 2D texture. |
  183. +---------------------------------------------+---------------------------------------------------------------+
  184. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of default 2D texture. |
  185. | | For a Sprite2D with a texture of size 64x32px, |
  186. | | ``TEXTURE_PIXEL_SIZE`` = ``vec2(1/64, 1/32)`` |
  187. +---------------------------------------------+---------------------------------------------------------------+
  188. | in bool **AT_LIGHT_PASS** | Always ``false``. |
  189. +---------------------------------------------+---------------------------------------------------------------+
  190. | sampler2D **SPECULAR_SHININESS_TEXTURE** | Specular shininess texture of this object. |
  191. +---------------------------------------------+---------------------------------------------------------------+
  192. | in vec4 **SPECULAR_SHININESS** | Specular shininess color, as sampled from the texture. |
  193. +---------------------------------------------+---------------------------------------------------------------+
  194. | in vec2 **UV** | UV from the ``vertex()`` function. |
  195. +---------------------------------------------+---------------------------------------------------------------+
  196. | in vec2 **SCREEN_UV** | Screen UV coordinate for current pixel. |
  197. +---------------------------------------------+---------------------------------------------------------------+
  198. | sampler2D **SCREEN_TEXTURE** | Removed in Godot 4. Use a ``sampler2D`` with |
  199. | | ``hint_screen_texture`` instead. |
  200. +---------------------------------------------+---------------------------------------------------------------+
  201. | inout vec3 **NORMAL** | Normal read from ``NORMAL_TEXTURE``. Writable. |
  202. +---------------------------------------------+---------------------------------------------------------------+
  203. | sampler2D **NORMAL_TEXTURE** | Default 2D normal texture. |
  204. +---------------------------------------------+---------------------------------------------------------------+
  205. | out vec3 **NORMAL_MAP** | Configures normal maps meant for 3D for use in 2D. If used, |
  206. | | overrides ``NORMAL``. |
  207. +---------------------------------------------+---------------------------------------------------------------+
  208. | out float **NORMAL_MAP_DEPTH** | Normal map depth for scaling. |
  209. +---------------------------------------------+---------------------------------------------------------------+
  210. | inout vec2 **VERTEX** | Pixel position in screen space. |
  211. +---------------------------------------------+---------------------------------------------------------------+
  212. | inout vec2 **SHADOW_VERTEX** | Same as ``VERTEX`` but can be written to alter shadows. |
  213. +---------------------------------------------+---------------------------------------------------------------+
  214. | inout vec3 **LIGHT_VERTEX** | Same as ``VERTEX`` but can be written to alter lighting. |
  215. | | Z component represents height. |
  216. +---------------------------------------------+---------------------------------------------------------------+
  217. | inout vec4 **COLOR** | ``COLOR`` from the ``vertex()`` function multiplied by the |
  218. | | ``TEXTURE`` color. Also output color value. |
  219. +---------------------------------------------+---------------------------------------------------------------+
  220. Light built-ins
  221. ^^^^^^^^^^^^^^^
  222. Light processor functions work differently in Godot 4.x than they did in Godot
  223. 3.x. In Godot 4.x all lighting is done during the regular draw pass. In other
  224. words, Godot no longer draws the object again for each light.
  225. Use the ``unshaded`` render mode if you do not want the ``light()`` function to
  226. run. Use the ``light_only`` render mode if you only want to see the impact of
  227. lighting on an object; this can be useful when you only want the object visible
  228. where it is covered by light.
  229. If you define a ``light()`` function it will replace the built-in light function,
  230. even if your light function is empty.
  231. Below is an example of a light shader that takes a CanvasItem's normal map into account:
  232. .. code-block:: glsl
  233. void light() {
  234. float cNdotL = max(0.0, dot(NORMAL, LIGHT_DIRECTION));
  235. LIGHT = vec4(LIGHT_COLOR.rgb * COLOR.rgb * LIGHT_ENERGY * cNdotL, LIGHT_COLOR.a);
  236. }
  237. +----------------------------------+------------------------------------------------------------------------------+
  238. | Built-in | Description |
  239. +==================================+==============================================================================+
  240. | in vec4 **FRAGCOORD** | Coordinate of pixel center. In screen space. ``xy`` specifies |
  241. | | position in viewport. Upper-left of the viewport is the origin, |
  242. | | ``(0.0, 0.0)``. |
  243. +----------------------------------+------------------------------------------------------------------------------+
  244. | in vec3 **NORMAL** | Input normal. |
  245. +----------------------------------+------------------------------------------------------------------------------+
  246. | in vec4 **COLOR** | Input color. This is the output of the ``fragment()`` function. |
  247. +----------------------------------+------------------------------------------------------------------------------+
  248. | in vec2 **UV** | UV from the ``vertex()`` function, equivalent to the UV in the |
  249. | | ``fragment()`` function. |
  250. +----------------------------------+------------------------------------------------------------------------------+
  251. | sampler2D **TEXTURE** | Current texture in use for CanvasItem. |
  252. +----------------------------------+------------------------------------------------------------------------------+
  253. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of ``TEXTURE``. |
  254. | | For a Sprite2D with a ``TEXTURE`` of size ``64x32`` pixels, |
  255. | | **TEXTURE_PIXEL_SIZE** = ``vec2(1/64, 1/32)`` |
  256. +----------------------------------+------------------------------------------------------------------------------+
  257. | in vec2 **SCREEN_UV** | Screen UV coordinate for current pixel. |
  258. +----------------------------------+------------------------------------------------------------------------------+
  259. | in vec2 **POINT_COORD** | UV for Point Sprite. |
  260. +----------------------------------+------------------------------------------------------------------------------+
  261. | in vec4 **LIGHT_COLOR** | :ref:`Color<class_Light2D_property_color>` of the :ref:`class_Light2D`. |
  262. | | If the light is a :ref:`class_PointLight2D`, multiplied by the light's |
  263. | | :ref:`texture<class_PointLight2D_property_texture>`. |
  264. +----------------------------------+------------------------------------------------------------------------------+
  265. | in float **LIGHT_ENERGY** | :ref:`Energy multiplier<class_Light2D_property_energy>` of the |
  266. | | :ref:`class_Light2D`. |
  267. +----------------------------------+------------------------------------------------------------------------------+
  268. | in vec3 **LIGHT_POSITION** | Position of the :ref:`class_Light2D` in screen space. If using a |
  269. | | :ref:`class_DirectionalLight2D` this is always ``(0.0, 0.0, 0.0)``. |
  270. +----------------------------------+------------------------------------------------------------------------------+
  271. | in vec3 **LIGHT_DIRECTION** | Direction of the :ref:`class_Light2D` in screen space. |
  272. +----------------------------------+------------------------------------------------------------------------------+
  273. | in bool **LIGHT_IS_DIRECTIONAL** | ``true`` if this pass is a :ref:`class_DirectionalLight2D`. |
  274. +----------------------------------+------------------------------------------------------------------------------+
  275. | in vec3 **LIGHT_VERTEX** | Pixel position, in screen space as modified in the ``fragment()`` function. |
  276. +----------------------------------+------------------------------------------------------------------------------+
  277. | inout vec4 **LIGHT** | Output color for this :ref:`class_Light2D`. |
  278. +----------------------------------+------------------------------------------------------------------------------+
  279. | in vec4 **SPECULAR_SHININESS** | Specular shininess, as set in the object's texture. |
  280. +----------------------------------+------------------------------------------------------------------------------+
  281. | out vec4 **SHADOW_MODULATE** | Multiply shadows cast at this point by this color. |
  282. +----------------------------------+------------------------------------------------------------------------------+
  283. SDF functions
  284. ^^^^^^^^^^^^^
  285. There are a few additional functions implemented to sample an automatically
  286. generated Signed Distance Field texture. These functions available for the ``fragment()``
  287. and ``light()`` functions of CanvasItem shaders. Custom functions may also use them as long
  288. as they called from supported functions.
  289. The signed distance field is generated from :ref:`class_LightOccluder2D` nodes
  290. present in the scene with the **SDF Collision** property enabled (which is the
  291. default). See the :ref:`2D lights and shadows <doc_2d_lights_and_shadows_setting_up_shadows>`
  292. documentation for more information.
  293. +-----------------------------------------------+-------------------------------------------+
  294. | Function | Description |
  295. +===============================================+===========================================+
  296. | float **texture_sdf** (vec2 sdf_pos) | Performs an SDF texture lookup. |
  297. +-----------------------------------------------+-------------------------------------------+
  298. | vec2 **texture_sdf_normal** (vec2 sdf_pos) | Calculates a normal from the SDF texture. |
  299. +-----------------------------------------------+-------------------------------------------+
  300. | vec2 **sdf_to_screen_uv** (vec2 sdf_pos) | Converts an SDF to screen UV. |
  301. +-----------------------------------------------+-------------------------------------------+
  302. | vec2 **screen_uv_to_sdf** (vec2 uv) | Converts screen UV to an SDF. |
  303. +-----------------------------------------------+-------------------------------------------+