canvas_item_shader.rst 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 less built-in variables and functionality than Spatial
  7. shaders, but they maintain the same basic structure with vertex, fragment, and
  8. 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 vertex function. |
  31. +---------------------------------+----------------------------------------------------------------------+
  32. | **world_vertex_coords** | VERTEX is modified in world coordinates instead of local. |
  33. +---------------------------------+----------------------------------------------------------------------+
  34. Built-ins
  35. ^^^^^^^^^
  36. Values marked as "in" are read-only. Values marked as "out" are for optional writing and will
  37. not necessarily contain sensible values. Values marked as "inout" provide a sensible default
  38. value, and can optionally be written to. Samplers are not subjects of writing and they are
  39. 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 (always positive). |
  47. | | It's subject to the rollover setting (which is 3,600 seconds by default). |
  48. | | It's not affected by :ref:`time_scale<class_Engine_property_time_scale>` |
  49. | | or pausing, but you can define a global shader uniform to add a "scaled" |
  50. | | ``TIME`` variable if desired. |
  51. +-------------------+----------------------------------------------------------------------------------------+
  52. | in float **PI** | A ``PI`` constant (``3.141592``). |
  53. | | A ration of circle's circumference to its diameter and amount of radians in half turn. |
  54. +-------------------+----------------------------------------------------------------------------------------+
  55. | in float **TAU** | A ``TAU`` constant (``6.283185``). |
  56. | | An equivalent of ``PI * 2`` and amount of radians in full turn. |
  57. +-------------------+----------------------------------------------------------------------------------------+
  58. | in float **E** | A ``E`` constant (``2.718281``). |
  59. | | Euler's number and a base of the natural logarithm. |
  60. +-------------------+----------------------------------------------------------------------------------------+
  61. Vertex built-ins
  62. ^^^^^^^^^^^^^^^^
  63. Vertex data (``VERTEX``) is presented in local space (pixel coordinates, relative to the Node2D's origin).
  64. If not written to, these values will not be modified and be passed through as they came.
  65. The user can disable the built-in model to world transform (world to screen and projection will still
  66. happen later) and do it manually with the following code:
  67. .. code-block:: glsl
  68. shader_type canvas_item;
  69. render_mode skip_vertex_transform;
  70. void vertex() {
  71. VERTEX = (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0)).xy;
  72. }
  73. Other built-ins, such as UV and COLOR, are also passed through to the fragment function if not modified.
  74. For instancing, the INSTANCE_CUSTOM variable contains the instance custom data. When using particles, this information
  75. is usually:
  76. * **x**: Rotation angle in radians.
  77. * **y**: Phase during lifetime (0 to 1).
  78. * **z**: Animation frame.
  79. +--------------------------------+----------------------------------------------------+
  80. | Built-in | Description |
  81. +================================+====================================================+
  82. | in mat4 **MODEL_MATRIX** | Local space to world space transform. World space |
  83. | | is the coordinates you normally use in the editor. |
  84. +--------------------------------+----------------------------------------------------+
  85. | in mat4 **CANVAS_MATRIX** | World space to canvas space transform. In canvas |
  86. | | space the origin is the upper-left corner of the |
  87. | | screen and coordinates ranging from (0, 0) to |
  88. | | viewport size. |
  89. +--------------------------------+----------------------------------------------------+
  90. | in mat4 **SCREEN_MATRIX** | Canvas space to clip space. In clip space |
  91. | | coordinates ranging from (-1, -1) to (1, 1). |
  92. +--------------------------------+----------------------------------------------------+
  93. | in int **INSTANCE_ID** | Instance ID for instancing. |
  94. +--------------------------------+----------------------------------------------------+
  95. | in vec4 **INSTANCE_CUSTOM** | Instance custom data. |
  96. +--------------------------------+----------------------------------------------------+
  97. | in bool **AT_LIGHT_PASS** | Always ``false``. |
  98. +--------------------------------+----------------------------------------------------+
  99. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of default 2D texture. |
  100. | | For a Sprite2D with a texture of size 64x32px, |
  101. | | **TEXTURE_PIXEL_SIZE** = ``vec2(1/64, 1/32)`` |
  102. +--------------------------------+----------------------------------------------------+
  103. | inout vec2 **VERTEX** | Vertex, in local space. |
  104. +--------------------------------+----------------------------------------------------+
  105. | in int **VERTEX_ID** | The index of the current vertex in the vertex |
  106. | | buffer. |
  107. +--------------------------------+----------------------------------------------------+
  108. | inout vec2 **UV** | Normalized texture coordinates. Range from 0 to 1. |
  109. +--------------------------------+----------------------------------------------------+
  110. | inout vec4 **COLOR** | Color from vertex primitive. |
  111. +--------------------------------+----------------------------------------------------+
  112. | inout float **POINT_SIZE** | Point size for point drawing. |
  113. +--------------------------------+----------------------------------------------------+
  114. Fragment built-ins
  115. ^^^^^^^^^^^^^^^^^^
  116. Certain Nodes (for example, :ref:`Sprite2Ds <class_Sprite2D>`) display a texture
  117. by default. However, when a custom fragment function is attached to these nodes,
  118. the texture lookup needs to be done manually. Godot provides the texture color
  119. in the ``COLOR`` built-in variable multiplied by the node's color. To read the
  120. texture color by itself, you can use:
  121. .. code-block:: glsl
  122. COLOR = texture(TEXTURE, UV);
  123. Similarly, if a normal map is used in the :ref:`CanvasTexture <class_CanvasTexture>`, Godot uses
  124. it by default and assigns its value to the built-in ``NORMAL`` variable. If you are using a normal
  125. map meant for use in 3D, it will appear inverted. In order to use it in your shader, you must assign
  126. it to the ``NORMALMAP`` property. Godot will handle converting it for use in 2D and overwriting ``NORMAL``.
  127. .. code-block:: glsl
  128. NORMALMAP = texture(NORMAL_TEXTURE, UV).rgb;
  129. +---------------------------------------------+---------------------------------------------------------------+
  130. | Built-in | Description |
  131. +=============================================+===============================================================+
  132. | in vec4 **FRAGCOORD** | Coordinate of pixel center. In screen space. ``xy`` specifies |
  133. | | position in window. Origin is lower-left. |
  134. +---------------------------------------------+---------------------------------------------------------------+
  135. | in vec2 **SCREEN_PIXEL_SIZE** | Size of individual pixels. Equal to inverse of resolution. |
  136. +---------------------------------------------+---------------------------------------------------------------+
  137. | in vec2 **POINT_COORD** | Coordinate for drawing points. |
  138. +---------------------------------------------+---------------------------------------------------------------+
  139. | sampler2D **TEXTURE** | Default 2D texture. |
  140. +---------------------------------------------+---------------------------------------------------------------+
  141. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of default 2D texture. |
  142. | | For a Sprite2D with a texture of size 64x32px, |
  143. | | **TEXTURE_PIXEL_SIZE** = ``vec2(1/64, 1/32)`` |
  144. +---------------------------------------------+---------------------------------------------------------------+
  145. | in bool **AT_LIGHT_PASS** | Always ``false``. |
  146. +---------------------------------------------+---------------------------------------------------------------+
  147. | sampler2D **SPECULAR_SHININESS_TEXTURE** | Specular shininess texture of this object. |
  148. +---------------------------------------------+---------------------------------------------------------------+
  149. | in vec4 **SPECULAR_SHININESS** | Specular shininess color, as sampled from the texture. |
  150. +---------------------------------------------+---------------------------------------------------------------+
  151. | in vec2 **UV** | UV from vertex function. |
  152. +---------------------------------------------+---------------------------------------------------------------+
  153. | in vec2 **SCREEN_UV** | Screen UV coordinate for current pixel. |
  154. +---------------------------------------------+---------------------------------------------------------------+
  155. | sampler2D **SCREEN_TEXTURE** | Removed in Godot 4. Use a ``sampler2D`` with |
  156. | | ``hint_screen_texture`` instead. |
  157. +---------------------------------------------+---------------------------------------------------------------+
  158. | inout vec3 **NORMAL** | Normal read from **NORMAL_TEXTURE**. Writable. |
  159. +---------------------------------------------+---------------------------------------------------------------+
  160. | sampler2D **NORMAL_TEXTURE** | Default 2D normal texture. |
  161. +---------------------------------------------+---------------------------------------------------------------+
  162. | out vec3 **NORMAL_MAP** | Configures normal maps meant for 3D for use in 2D. If used, |
  163. | | overrides **NORMAL**. |
  164. +---------------------------------------------+---------------------------------------------------------------+
  165. | out float **NORMAL_MAP_DEPTH** | Normalmap depth for scaling. |
  166. +---------------------------------------------+---------------------------------------------------------------+
  167. | inout vec2 **VERTEX** | Pixel position in screen space. |
  168. +---------------------------------------------+---------------------------------------------------------------+
  169. | inout vec2 **SHADOW_VERTEX** | Same as ``VERTEX`` but can be written to alter shadows. |
  170. +---------------------------------------------+---------------------------------------------------------------+
  171. | inout vec3 **LIGHT_VERTEX** | Same as ``VERTEX`` but can be written to alter lighting. |
  172. | | Z component represents height. |
  173. +---------------------------------------------+---------------------------------------------------------------+
  174. | inout vec4 **COLOR** | Color from vertex function multiplied by the **TEXTURE** |
  175. | | color. Also output color value. |
  176. +---------------------------------------------+---------------------------------------------------------------+
  177. Light built-ins
  178. ^^^^^^^^^^^^^^^
  179. Light processor functions work differently in Godot 4.x than they did in Godot
  180. 3.x. In Godot 4.x all lighting is done during the regular draw pass. In other
  181. words, Godot no longer draws the object again for each light.
  182. Use render_mode ``unshaded`` if you do not want the light processor function to
  183. run. Use render_mode ``light_only`` if you only want to see the impact of
  184. lighting on an object; this can be useful when you only want the object visible
  185. where it is covered by light.
  186. If you define a light function it will replace the built in light function,
  187. even if your light function is empty.
  188. Below is an example of a light shader that takes a CanvasItem's normal map into account:
  189. .. code-block:: glsl
  190. void light() {
  191. float cNdotL = max(0.0, dot(NORMAL, LIGHT_DIRECTION));
  192. LIGHT = vec4(LIGHT_COLOR.rgb * COLOR.rgb * LIGHT_ENERGY * cNdotL, LIGHT_COLOR.a);
  193. }
  194. +----------------------------------+------------------------------------------------------------------------------+
  195. | Built-in | Description |
  196. +==================================+==============================================================================+
  197. | in vec4 **FRAGCOORD** | Coordinate of pixel center. In screen space. ``xy`` specifies |
  198. | | position in window. Origin is lower-left. |
  199. +----------------------------------+------------------------------------------------------------------------------+
  200. | in vec3 **NORMAL** | Input Normal. |
  201. +----------------------------------+------------------------------------------------------------------------------+
  202. | in vec4 **COLOR** | Input Color. This is the output of the fragment function. |
  203. +----------------------------------+------------------------------------------------------------------------------+
  204. | in vec2 **UV** | UV from vertex function, equivalent to the UV in the fragment function. |
  205. +----------------------------------+------------------------------------------------------------------------------+
  206. | sampler2D **TEXTURE** | Current texture in use for CanvasItem. |
  207. +----------------------------------+------------------------------------------------------------------------------+
  208. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of **TEXTURE**. |
  209. | | For a Sprite2D with a **TEXTURE** of size 64x32px, |
  210. | | **TEXTURE_PIXEL_SIZE** = ``vec2(1/64, 1/32)`` |
  211. +----------------------------------+------------------------------------------------------------------------------+
  212. | in vec2 **SCREEN_UV** | Screen UV coordinate for current pixel. |
  213. +----------------------------------+------------------------------------------------------------------------------+
  214. | in vec2 **POINT_COORD** | UV for Point Sprite. |
  215. +----------------------------------+------------------------------------------------------------------------------+
  216. | in vec4 **LIGHT_COLOR** | Color of Light multiplied by Light's texture. |
  217. +----------------------------------+------------------------------------------------------------------------------+
  218. | in float **LIGHT_ENERGY** | Energy multiplier of Light. |
  219. +----------------------------------+------------------------------------------------------------------------------+
  220. | in vec3 **LIGHT_POSITION** | Position of Light in screen space. If using a ``DirectionalLight2D`` |
  221. | | this is always ``vec3(0,0,0)``. |
  222. +----------------------------------+------------------------------------------------------------------------------+
  223. | in vec3 **LIGHT_DIRECTION** | Direction of Light in screen space. |
  224. +----------------------------------+------------------------------------------------------------------------------+
  225. | in bool **LIGHT_IS_DIRECTIONAL** | ``true`` if this pass is a ``DirectionalLight2D``. |
  226. +----------------------------------+------------------------------------------------------------------------------+
  227. | in vec3 **LIGHT_VERTEX** | Pixel position, in screen space as modified in the fragment function. |
  228. +----------------------------------+------------------------------------------------------------------------------+
  229. | inout vec4 **LIGHT** | Output color for this Light. |
  230. +----------------------------------+------------------------------------------------------------------------------+
  231. | in vec4 **SPECULAR_SHININESS** | Specular shininess, as set in the object's texture. |
  232. +----------------------------------+------------------------------------------------------------------------------+
  233. | out vec4 **SHADOW_MODULATE** | Multiply shadows cast at this point by this color. |
  234. +----------------------------------+------------------------------------------------------------------------------+
  235. SDF functions
  236. ^^^^^^^^^^^^^
  237. There are a few additional functions implemented to sample an automatically
  238. generated Signed Distance Field texture. These functions available for Fragment
  239. and Light functions of CanvasItem shaders.
  240. The signed distance field is generated from :ref:`class_LightOccluder2D` nodes
  241. present in the scene with the **SDF Collision** property enabled (which is the
  242. default). See the :ref:`2D lights and shadows <doc_2d_lights_and_shadows_setting_up_shadows>`
  243. documentation for more information.
  244. +-----------------------------------------------+-------------------------------------------+
  245. | Function | Description |
  246. +===============================================+===========================================+
  247. | float **texture_sdf** (vec2 sdf_pos) | Performs an SDF texture lookup. |
  248. +-----------------------------------------------+-------------------------------------------+
  249. | vec2 **texture_sdf_normal** (vec2 sdf_pos) | Calculates a normal from the SDF texture. |
  250. +-----------------------------------------------+-------------------------------------------+
  251. | vec2 **sdf_to_screen_uv** (vec2 sdf_pos) | Converts a SDF to screen UV. |
  252. +-----------------------------------------------+-------------------------------------------+
  253. | vec2 **screen_uv_to_sdf** (vec2 uv) | Converts screen UV to a SDF. |
  254. +-----------------------------------------------+-------------------------------------------+