canvas_item_shader.rst 15 KB

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