canvas_item_shader.rst 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/NORMAL/etc need to be transformed manually in vertex function.|
  31. +---------------------------------+----------------------------------------------------------------------+
  32. Built-ins
  33. ^^^^^^^^^
  34. Values marked as "in" are read-only. Values marked as "out" are for optional writing and will
  35. not necessarily contain sensible values. Values marked as "inout" provide a sensible default
  36. value, and can optionally be written to. Samplers are not subjects of writing and they are
  37. not marked.
  38. Global built-ins
  39. ^^^^^^^^^^^^^^^^
  40. Global built-ins are available everywhere, including custom functions.
  41. +-------------------+----------------------------------------------------------------------------------------+
  42. | Built-in | Description |
  43. +===================+========================================================================================+
  44. | in float **TIME** | Global time since the engine has started, in seconds (always positive). |
  45. | | It's subject to the rollover setting (which is 3,600 seconds by default). |
  46. | | It's not affected by :ref:`time_scale<class_Engine_property_time_scale>` |
  47. | | or pausing, but you can define a global shader uniform to add a "scaled" |
  48. | | ``TIME`` variable if desired. |
  49. +-------------------+----------------------------------------------------------------------------------------+
  50. | in float **PI** | A ``PI`` constant (``3.141592``). |
  51. | | A ration of circle's circumference to its diameter and amount of radians in half turn. |
  52. +-------------------+----------------------------------------------------------------------------------------+
  53. | in float **TAU** | A ``TAU`` constant (``6.283185``). |
  54. | | An equivalent of ``PI * 2`` and amount of radians in full turn. |
  55. +-------------------+----------------------------------------------------------------------------------------+
  56. | in float **E** | A ``E`` constant (``2.718281``). |
  57. | | Euler's number and a base of the natural logarithm. |
  58. +-------------------+----------------------------------------------------------------------------------------+
  59. Vertex built-ins
  60. ^^^^^^^^^^^^^^^^
  61. Vertex data (``VERTEX``) is presented in local space (pixel coordinates, relative to the camera).
  62. If not written to, these values will not be modified and be passed through as they came.
  63. The user can disable the built-in modelview transform (projection will still happen later) and do
  64. it manually with the following code:
  65. .. code-block:: glsl
  66. shader_type canvas_item;
  67. render_mode skip_vertex_transform;
  68. void vertex() {
  69. VERTEX = (EXTRA_MATRIX * (MODEL_MATRIX * vec4(VERTEX, 0.0, 1.0))).xy;
  70. }
  71. .. note:: ``MODEL_MATRIX`` is known as a modelview matrix. It takes input in local space and transforms it
  72. into view space.
  73. In order to get the world space coordinates of a vertex, you have to pass in a custom uniform like so:
  74. ::
  75. material.set_shader_param("global_transform", get_global_transform())
  76. Then, in your vertex shader:
  77. .. code-block:: glsl
  78. uniform mat4 global_transform;
  79. varying vec2 world_position;
  80. void vertex(){
  81. world_position = (global_transform * vec4(VERTEX, 0.0, 1.0)).xy;
  82. }
  83. ``world_position`` can then be used in either the vertex or fragment functions.
  84. Other built-ins, such as UV and COLOR, are also passed through to the fragment function if not modified.
  85. For instancing, the INSTANCE_CUSTOM variable contains the instance custom data. When using particles, this information
  86. is usually:
  87. * **x**: Rotation angle in radians.
  88. * **y**: Phase during lifetime (0 to 1).
  89. * **z**: Animation frame.
  90. +--------------------------------+---------------------------------------------------+
  91. | Built-in | Description |
  92. +================================+===================================================+
  93. | in mat4 **MODEL_MATRIX** | Image space to view space transform. |
  94. +--------------------------------+---------------------------------------------------+
  95. | in mat4 **CANVAS_MATRIX** | |
  96. +--------------------------------+---------------------------------------------------+
  97. | in mat4 **SCREEN_MATRIX** | |
  98. +--------------------------------+---------------------------------------------------+
  99. | in vec4 **INSTANCE_CUSTOM** | Instance custom data. |
  100. +--------------------------------+---------------------------------------------------+
  101. | in bool **AT_LIGHT_PASS** | ``true`` if this is a light pass. |
  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** = :code:`vec2(1/64, 1/32)` |
  106. +--------------------------------+---------------------------------------------------+
  107. | inout vec2 **VERTEX** | Vertex, in image space. |
  108. +--------------------------------+---------------------------------------------------+
  109. | inout vec2 **UV** | Texture coordinates. |
  110. +--------------------------------+---------------------------------------------------+
  111. | inout vec4 **COLOR** | Color from vertex primitive. |
  112. +--------------------------------+---------------------------------------------------+
  113. | inout float **POINT_SIZE** | Point size for point drawing. |
  114. +--------------------------------+---------------------------------------------------+
  115. Fragment built-ins
  116. ^^^^^^^^^^^^^^^^^^
  117. Certain Nodes (for example, :ref:`Sprite2Ds <class_Sprite2D>`) display a texture by default. However,
  118. when a custom fragment function is attached to these nodes, the texture lookup needs to be done
  119. manually. Godot does not provide the texture color in the ``COLOR`` built-in variable; to read
  120. the texture color for such nodes, use:
  121. .. code-block:: glsl
  122. COLOR = texture(TEXTURE, UV);
  123. This differs from the behavior of the built-in normal map. If a normal map is attached, 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, ``z`` specifies fragment depth if |
  134. | | ``DEPTH`` is not used. Origin is lower-left. |
  135. +---------------------------------------------+---------------------------------------------------------------+
  136. | in vec2 **SCREEN_PIXEL_SIZE** | Size of individual pixels. Equal to inverse of resolution. |
  137. +---------------------------------------------+---------------------------------------------------------------+
  138. | in vec2 **POINT_COORD** | Coordinate for drawing points. |
  139. +---------------------------------------------+---------------------------------------------------------------+
  140. | sampler2D **TEXTURE** | Default 2D texture. |
  141. +---------------------------------------------+---------------------------------------------------------------+
  142. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of default 2D texture. |
  143. | | For a Sprite2D with a texture of size 64x32px, |
  144. | | **TEXTURE_PIXEL_SIZE** = :code`vec2(1/64, 1/32)` |
  145. +---------------------------------------------+---------------------------------------------------------------+
  146. | in bool **AT_LIGHT_PASS** | ``true`` if this is a light pass. |
  147. +---------------------------------------------+---------------------------------------------------------------+
  148. | sampler2D **SPECULAR_SHININESS_TEXTURE** | |
  149. +---------------------------------------------+---------------------------------------------------------------+
  150. | in vec4 **SPECULAR_SHININESS** | |
  151. +---------------------------------------------+---------------------------------------------------------------+
  152. | in vec2 **UV** | UV from vertex function. |
  153. +---------------------------------------------+---------------------------------------------------------------+
  154. | in vec2 **SCREEN_UV** | Screen UV for use with **SCREEN_TEXTURE**. |
  155. +---------------------------------------------+---------------------------------------------------------------+
  156. | sampler2D **SCREEN_TEXTURE** | Screen texture, mipmaps contain gaussian blurred versions. |
  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** | |
  168. +---------------------------------------------+---------------------------------------------------------------+
  169. | inout vec2 **SHADOW_VERTEX** | |
  170. +---------------------------------------------+---------------------------------------------------------------+
  171. | inout vec3 **LIGHT_VERTEX** | |
  172. +---------------------------------------------+---------------------------------------------------------------+
  173. | inout vec4 **COLOR** | Color from vertex function and output fragment color. If |
  174. | | unused, will be set to **TEXTURE** color. |
  175. +---------------------------------------------+---------------------------------------------------------------+
  176. Light built-ins
  177. ^^^^^^^^^^^^^^^
  178. Light processor functions work differently in 2D than they do in 3D. In CanvasItem shaders, the
  179. shader is called once for the object being drawn, and then once for each light touching that
  180. object in the scene. Use render_mode ``unshaded`` if you do not want any light passes to occur
  181. for that object. Use render_mode ``light_only`` if you only want light passes to occur for
  182. that object; this can be useful when you only want the object visible where it is covered by light.
  183. When the shader is on a light pass, the ``AT_LIGHT_PASS`` variable will be ``true``.
  184. +--------------------------------+------------------------------------------------------------------------------+
  185. | Built-in | Description |
  186. +================================+==============================================================================+
  187. | in vec4 **FRAGCOORD** | Coordinate of pixel center. In screen space. ``xy`` specifies |
  188. | | position in window, ``z`` specifies fragment depth if |
  189. | | ``DEPTH`` is not used. Origin is lower-left. |
  190. +--------------------------------+------------------------------------------------------------------------------+
  191. | in vec3 **NORMAL** | Input Normal. Although this value is passed in, |
  192. | | **normal calculation still happens outside of this function**. |
  193. +--------------------------------+------------------------------------------------------------------------------+
  194. | in vec4 **COLOR** | Input Color. |
  195. | | This is the output of the fragment function with final modulation applied. |
  196. +--------------------------------+------------------------------------------------------------------------------+
  197. | in vec2 **UV** | UV from vertex function, equivalent to the UV in the fragment function. |
  198. +--------------------------------+------------------------------------------------------------------------------+
  199. | sampler2D **TEXTURE** | Current texture in use for CanvasItem. |
  200. +--------------------------------+------------------------------------------------------------------------------+
  201. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of default 2D texture. |
  202. | | For a Sprite2D with a texture of size 64x32px, |
  203. | | **TEXTURE_PIXEL_SIZE** = :code:`vec2(1/64, 1/32)` |
  204. +--------------------------------+------------------------------------------------------------------------------+
  205. | in vec2 **SCREEN_UV** | **SCREEN_TEXTURE** Coordinate (for using with screen texture). |
  206. +--------------------------------+------------------------------------------------------------------------------+
  207. | in vec2 **POINT_COORD** | UV for Point Sprite. |
  208. +--------------------------------+------------------------------------------------------------------------------+
  209. | in vec4 **LIGHT_COLOR** | Color of Light. |
  210. +--------------------------------+------------------------------------------------------------------------------+
  211. | in vec3 **LIGHT_POSITION** | Position of Light. |
  212. +--------------------------------+------------------------------------------------------------------------------+
  213. | in vec3 **LIGHT_VERTEX** | |
  214. +--------------------------------+------------------------------------------------------------------------------+
  215. | inout vec4 **LIGHT** | Value from the Light texture and output color. Can be modified. If not used, |
  216. | | the light function is ignored. |
  217. +--------------------------------+------------------------------------------------------------------------------+
  218. | in vec4 **SPECULAR_SHININESS** | |
  219. +--------------------------------+------------------------------------------------------------------------------+
  220. | out vec4 **SHADOW_MODULATE** | |
  221. +--------------------------------+------------------------------------------------------------------------------+
  222. SDF functions
  223. ^^^^^^^^^^^^^
  224. There are a few additional functions implemented to support an SDF (Signed Distance Field) feature.
  225. They are available for Fragment and Light functions of CanvasItem shader.
  226. +-----------------------------------------------+----------------------------------------+
  227. | Function | Description |
  228. +===============================================+========================================+
  229. | float **texture_sdf** (vec2 sdf_pos) | Performs an SDF texture lookup. |
  230. +-----------------------------------------------+----------------------------------------+
  231. | vec2 **texture_sdf_normal** (vec2 sdf_pos) | Performs an SDF normal texture lookup. |
  232. +-----------------------------------------------+----------------------------------------+
  233. | vec2 **sdf_to_screen_uv** (vec2 sdf_pos) | Converts a SDF to screen UV. |
  234. +-----------------------------------------------+----------------------------------------+
  235. | vec2 **screen_uv_to_sdf** (vec2 uv) | Converts screen UV to a SDF. |
  236. +-----------------------------------------------+----------------------------------------+