canvas_item_shader.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. Vertex 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. Vertex data (``VERTEX``) is presented in local space (pixel coordinates, relative to the camera).
  39. If not written to, these values will not be modified and be passed through as they came.
  40. The user can disable the built-in modelview transform (projection will still happen later) and do
  41. it manually with the following code:
  42. .. code-block:: glsl
  43. shader_type canvas_item;
  44. render_mode skip_vertex_transform;
  45. void vertex() {
  46. VERTEX = (EXTRA_MATRIX * (WORLD_MATRIX * vec4(VERTEX, 0.0, 1.0))).xy;
  47. }
  48. .. note:: ``WORLD_MATRIX`` is actually a modelview matrix. It takes input in local space and transforms it
  49. into view space.
  50. In order to get the world space coordinates of a vertex, you have to pass in a custom uniform like so:
  51. ::
  52. material.set_shader_param("global_transform", get_global_transform())
  53. Then, in your vertex shader:
  54. .. code-block:: glsl
  55. uniform mat4 global_transform;
  56. varying vec2 world_position;
  57. void vertex(){
  58. world_position = (global_transform * vec4(VERTEX, 0.0, 1.0)).xy;
  59. }
  60. ``world_position`` can then be used in either the vertex or fragment functions.
  61. Other built-ins, such as UV and COLOR, are also passed through to the fragment function if not modified.
  62. For instancing, the INSTANCE_CUSTOM variable contains the instance custom data. When using particles, this information
  63. is usually:
  64. * **x**: Rotation angle in radians.
  65. * **y**: Phase during lifetime (0 to 1).
  66. * **z**: Animation frame.
  67. +--------------------------------+----------------------------------------------------------------+
  68. | Built-in | Description |
  69. +================================+================================================================+
  70. | in mat4 **WORLD_MATRIX** | Image space to view space transform. |
  71. +--------------------------------+----------------------------------------------------------------+
  72. | in mat4 **EXTRA_MATRIX** | Extra transform. |
  73. +--------------------------------+----------------------------------------------------------------+
  74. | in mat4 **PROJECTION_MATRIX** | View space to clip space transform. |
  75. +--------------------------------+----------------------------------------------------------------+
  76. | in float **TIME** | Global time, in seconds. |
  77. +--------------------------------+----------------------------------------------------------------+
  78. | in vec4 **INSTANCE_CUSTOM** | Instance custom data. |
  79. +--------------------------------+----------------------------------------------------------------+
  80. | in bool **AT_LIGHT_PASS** | True if this is a light pass. |
  81. +--------------------------------+----------------------------------------------------------------+
  82. | inout vec2 **VERTEX** | Vertex, in image space. |
  83. +--------------------------------+----------------------------------------------------------------+
  84. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of default 2D texture. |
  85. | | For a Sprite with a texture of size 64x32px, |
  86. | | **TEXTURE_PIXEL_SIZE** = :code:`vec2(1/64, 1/32)` |
  87. +--------------------------------+----------------------------------------------------------------+
  88. | inout vec2 **UV** | UV. |
  89. +--------------------------------+----------------------------------------------------------------+
  90. | inout vec4 **COLOR** | Color from vertex primitive. |
  91. +--------------------------------+----------------------------------------------------------------+
  92. | inout float **POINT_SIZE** | Point size for point drawing. |
  93. +--------------------------------+----------------------------------------------------------------+
  94. Fragment built-ins
  95. ^^^^^^^^^^^^^^^^^^
  96. Certain Nodes (for example, :ref:`Sprites <class_Sprite>`) display a texture by default. However,
  97. when a custom fragment function is attached to these nodes, the texture lookup needs to be done
  98. manually. Godot does not provide the texture color in the ``COLOR`` built-in variable; to read
  99. the texture color for such nodes, use:
  100. .. code-block:: glsl
  101. COLOR = texture(TEXTURE, UV);
  102. This differs from the behaviour of the built-in normal map. If a normal map is attached, Godot uses
  103. it by default and assigns its value to the built-in ``NORMAL`` variable. If you are using a normal
  104. map meant for use in 3D, it will appear inverted. In order to use it in your shader, you must assign
  105. it to the ``NORMALMAP`` property. Godot will handle converting it for use in 2D and overwriting ``NORMAL``.
  106. .. code-block:: glsl
  107. NORMALMAP = texture(NORMAL_TEXTURE, UV).rgb;
  108. +----------------------------------+----------------------------------------------------------------+
  109. | Built-in | Description |
  110. +==================================+================================================================+
  111. | in vec4 **FRAGCOORD** | Coordinate of pixel center. In screen space. ``xy`` specifies |
  112. | | position in window, ``z`` specifies fragment depth if |
  113. | | ``DEPTH`` is not used. Origin is lower-left. |
  114. +----------------------------------+----------------------------------------------------------------+
  115. | inout vec3 **NORMAL** | Normal read from **NORMAL_TEXTURE**. Writable. |
  116. +----------------------------------+----------------------------------------------------------------+
  117. | out vec3 **NORMALMAP** | Configures normal maps meant for 3D for use in 2D. If used, |
  118. | | overwrites **NORMAL**. |
  119. +----------------------------------+----------------------------------------------------------------+
  120. | inout float **NORMALMAP_DEPTH** | Normalmap depth for scaling. |
  121. +----------------------------------+----------------------------------------------------------------+
  122. | in vec2 **UV** | UV from vertex function. |
  123. +----------------------------------+----------------------------------------------------------------+
  124. | inout vec4 **COLOR** | Color from vertex function and output fragment color. If |
  125. | | unused, will be set to **TEXTURE** color. |
  126. +----------------------------------+----------------------------------------------------------------+
  127. | in sampler2D **TEXTURE** | Default 2D texture. |
  128. +----------------------------------+----------------------------------------------------------------+
  129. | in sampler2D **NORMAL_TEXTURE** | Default 2D normal texture. |
  130. +----------------------------------+----------------------------------------------------------------+
  131. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of default 2D texture. |
  132. | | For a Sprite with a texture of size 64x32px, |
  133. | | **TEXTURE_PIXEL_SIZE** = :code:`vec2(1/64, 1/32)` |
  134. +----------------------------------+----------------------------------------------------------------+
  135. | in vec2 **SCREEN_UV** | Screen UV for use with **SCREEN_TEXTURE**. |
  136. +----------------------------------+----------------------------------------------------------------+
  137. | in vec2 **SCREEN_PIXEL_SIZE** | Size of individual pixels. Equal to inverse of resolution. |
  138. +----------------------------------+----------------------------------------------------------------+
  139. | in vec2 **POINT_COORD** | Coordinate for drawing points. |
  140. +----------------------------------+----------------------------------------------------------------+
  141. | in float **TIME** | Global time in seconds. |
  142. +----------------------------------+----------------------------------------------------------------+
  143. | in bool **AT_LIGHT_PASS** | True if this is a light pass. |
  144. +----------------------------------+----------------------------------------------------------------+
  145. | in sampler2D **SCREEN_TEXTURE** | Screen texture, mipmaps contain gaussian blurred versions. |
  146. +----------------------------------+----------------------------------------------------------------+
  147. Light built-ins
  148. ^^^^^^^^^^^^^^^
  149. Light processor functions work differently in 2D than they do in 3D. In CanvasItem shaders, the
  150. shader is called once for the object being drawn, and then once for each light touching that
  151. object in the scene. Use render_mode ``unshaded`` if you do not want any light passes to occur
  152. for that object. Use render_mode ``light_only`` if you only want light passes to occur for
  153. that object; this can be useful when you only want the object visible where it is covered by light.
  154. When the shader is on a light pass, the ``AT_LIGHT_PASS`` variable will be ``true``.
  155. +-------------------------------------+-------------------------------------------------------------------------------+
  156. | Built-in | Description |
  157. +=====================================+===============================================================================+
  158. | in vec4 **FRAGCOORD** | Coordinate of pixel center. In screen space. ``xy`` specifies |
  159. | | position in window, ``z`` specifies fragment depth if |
  160. | | ``DEPTH`` is not used. Origin is lower-left. |
  161. +-------------------------------------+-------------------------------------------------------------------------------+
  162. | in vec3 **NORMAL** | Input Normal. Although this value is passed in, |
  163. | | **normal calculation still happens outside of this function**. |
  164. +-------------------------------------+-------------------------------------------------------------------------------+
  165. | in vec2 **UV** | UV from vertex function, equivalent to the UV in the fragment function. |
  166. +-------------------------------------+-------------------------------------------------------------------------------+
  167. | in vec4 **COLOR** | Input Color. |
  168. | | This is the output of the fragment function with final modulation applied. |
  169. +-------------------------------------+-------------------------------------------------------------------------------+
  170. | sampler2D **TEXTURE** | Current texture in use for CanvasItem. |
  171. +-------------------------------------+-------------------------------------------------------------------------------+
  172. | in vec2 **TEXTURE_PIXEL_SIZE** | Normalized pixel size of default 2D texture. |
  173. | | For a Sprite with a texture of size 64x32px, |
  174. | | **TEXTURE_PIXEL_SIZE** = :code:`vec2(1/64, 1/32)` |
  175. +-------------------------------------+-------------------------------------------------------------------------------+
  176. | in vec2 **SCREEN_UV** | **SCREEN_TEXTURE** Coordinate (for using with screen texture). |
  177. +-------------------------------------+-------------------------------------------------------------------------------+
  178. | in vec2 **POINT_COORD** | UV for Point Sprite. |
  179. +-------------------------------------+-------------------------------------------------------------------------------+
  180. | in float **TIME** | Global time in seconds. |
  181. +-------------------------------------+-------------------------------------------------------------------------------+
  182. | inout vec2 **LIGHT_VEC** | Vector from light to fragment, can be modified to alter shadow computation. |
  183. +-------------------------------------+-------------------------------------------------------------------------------+
  184. | inout float **LIGHT_HEIGHT** | Height of Light. Only effective when normals are used. |
  185. +-------------------------------------+-------------------------------------------------------------------------------+
  186. | inout vec4 **LIGHT_COLOR** | Color of Light. |
  187. +-------------------------------------+-------------------------------------------------------------------------------+
  188. | in vec2 **LIGHT_UV** | UV for Light texture. |
  189. +-------------------------------------+-------------------------------------------------------------------------------+
  190. | out vec4 **SHADOW_COLOR** | Shadow Color of Light. |
  191. +-------------------------------------+-------------------------------------------------------------------------------+
  192. | inout vec4 **LIGHT** | Value from the Light texture and output color. Can be modified. If not used, |
  193. | | the light function is ignored. |
  194. +-------------------------------------+-------------------------------------------------------------------------------+