introduction_to_shaders.rst 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. .. _doc_introduction_to_shaders:
  2. Introduction to shaders
  3. =======================
  4. This page explains what shaders are and will give you an overview of how they
  5. work in Godot. For a detailed reference of the engine's shading language, see
  6. :ref:`doc_shading_language`.
  7. Shaders are a special kind of program that runs on Graphics Processing Units
  8. (GPUs). They were initially used to shade 3D scenes but can nowadays do much
  9. more. You can use them to control how the engine draws geometry and pixels on
  10. the screen, allowing you to achieve all sorts of effects.
  11. Modern rendering engines like Godot draw everything with shaders: graphics cards
  12. can run thousands of instructions in parallel, leading to incredible rendering
  13. speed.
  14. Because of their parallel nature, though, shaders don't process information the
  15. way a typical program does. Shader code runs on each vertex or pixel in
  16. isolation. You cannot store data between frames either. As a result, when
  17. working with shaders, you need to code and think differently from other
  18. programming languages.
  19. Suppose you want to update all the pixels in a texture to a given color. In
  20. GDScript, your code would use ``for`` loops:
  21. ::
  22. for x in range(width):
  23. for y in range(height):
  24. set_color(x, y, some_color)
  25. Your code is already part of a loop in a shader, so the corresponding code would
  26. look like this.
  27. .. code-block:: glsl
  28. void fragment() {
  29. COLOR = some_color;
  30. }
  31. .. note::
  32. The graphics card calls the ``fragment()`` function once or more for each
  33. pixel it has to draw. More on that below.
  34. Shaders in Godot
  35. ----------------
  36. Godot provides a shading language based on the popular OpenGL Shading Language
  37. (GLSL) but simplified. The engine handles some of the lower-level initialization
  38. work for you, making it easier to write complex shaders.
  39. In Godot, shaders are made up of main functions called "processor functions".
  40. Processor functions are the entry point for your shader into the program. There
  41. are seven different processor functions.
  42. 1. The ``vertex()`` function runs over all the vertices in the mesh and sets
  43. their positions and some other per-vertex variables. Used in
  44. :ref:`canvas_item shaders <doc_canvas_item_shader>` and
  45. :ref:`spatial shaders <doc_spatial_shader>`.
  46. 2. The ``fragment()`` function runs for every pixel covered by the mesh. It uses
  47. values output by the ``vertex()`` function, interpolated between the
  48. vertices. Used in :ref:`canvas_item shaders <doc_canvas_item_shader>` and
  49. :ref:`spatial shaders <doc_spatial_shader>`.
  50. 3. The ``light()`` function runs for every pixel and for every light. It takes
  51. variables from the ``fragment()`` function and from its previous runs. Used
  52. in :ref:`canvas_item shaders <doc_canvas_item_shader>` and
  53. :ref:`spatial shaders <doc_spatial_shader>`.
  54. 4. The ``start()`` function runs for every particle in a particle system once
  55. when the particle is first spawned. Used in
  56. :ref:`particles shaders <doc_particle_shader>`.
  57. 5. The ``process()`` function runs for every particle in a particle system for
  58. each frame. Used in :ref:`particles shaders <doc_particle_shader>`.
  59. 6. The ``sky()`` function runs for every pixel in the radiance cubemap when the
  60. radiance cubemap needs to be updated, and for every pixel on the current
  61. screen. Used in :ref:`sky shaders <doc_sky_shader>`.
  62. 7. The ``fog()`` function runs for every froxel in the volumetric fog froxel
  63. buffer that intersects with the :ref:`FogVolume <class_FogVolume>`. Used by
  64. :ref:`fog shaders <doc_fog_shader>`.
  65. .. warning::
  66. The ``light()`` function won't run if the ``vertex_lighting`` render mode is
  67. enabled, or if **Rendering > Quality > Shading > Force Vertex Shading** is
  68. enabled in the Project Settings. It's enabled by default on mobile
  69. platforms.
  70. .. note::
  71. Godot also exposes an API for users to write totally custom GLSL shaders. For
  72. more information see :ref:`doc_compute_shaders`.
  73. Shader types
  74. ------------
  75. Instead of supplying a general-purpose configuration for all uses (2D, 3D,
  76. particles, sky, fog), you must specify the type of shader you're writing.
  77. Different types support different render modes, built-in variables, and
  78. processing functions.
  79. In Godot, all shaders need to specify their type in the first line, like so:
  80. .. code-block:: glsl
  81. shader_type spatial;
  82. Here are the available types:
  83. * :ref:`spatial <doc_spatial_shader>` for 3D rendering.
  84. * :ref:`canvas_item <doc_canvas_item_shader>` for 2D rendering.
  85. * :ref:`particles <doc_particle_shader>` for particle systems.
  86. * :ref:`sky <doc_sky_shader>` to render :ref:`Skies <class_Sky>`.
  87. * :ref:`fog <doc_fog_shader>` to render :ref:`FogVolumes <class_FogVolume>`
  88. Render modes
  89. ------------
  90. Shaders have optional render modes you can specify on the second line, after the
  91. shader type, like so:
  92. .. code-block:: glsl
  93. shader_type spatial;
  94. render_mode unshaded, cull_disabled;
  95. Render modes alter the way Godot applies the shader. For example, the
  96. ``unshaded`` mode makes the engine skip the built-in light processor function.
  97. Each shader type has different render modes. See the reference for each shader
  98. type for a complete list of render modes.
  99. Vertex processor
  100. ~~~~~~~~~~~~~~~~
  101. The ``vertex()`` processing function is called once for every vertex in
  102. ``spatial`` and ``canvas_item`` shaders.
  103. Each vertex in your world's geometry has properties like a position and color.
  104. The function modifies those values and passes them to the fragment function. You
  105. can also use it to send extra data to the fragment function using varyings.
  106. By default, Godot transforms your vertex information for you, which is necessary
  107. to project geometry onto the screen. You can use render modes to transform the
  108. data yourself; see the :ref:`Spatial shader doc <doc_spatial_shader>` for an
  109. example.
  110. Fragment processor
  111. ~~~~~~~~~~~~~~~~~~
  112. The ``fragment()`` processing function is used to set up the Godot material
  113. parameters per pixel. This code runs on every visible pixel the object or
  114. primitive draws. It is only available in ``spatial`` and ``canvas_item`` shaders.
  115. The standard use of the fragment function is to set up material properties used
  116. to calculate lighting. For example, you would set values for ``ROUGHNESS``,
  117. ``RIM``, or ``TRANSMISSION``, which would tell the light function how the lights
  118. respond to that fragment. This makes it possible to control a complex shading
  119. pipeline without the user having to write much code. If you don't need this
  120. built-in functionality, you can ignore it and write your own light processing
  121. function, and Godot will optimize it away. For example, if you do not write a
  122. value to ``RIM``, Godot will not calculate rim lighting. During compilation,
  123. Godot checks to see if ``RIM`` is used; if not, it cuts all the corresponding
  124. code out. Therefore, you will not waste calculations on the effects that you do
  125. not use.
  126. Light processor
  127. ~~~~~~~~~~~~~~~
  128. The ``light()`` processor runs per pixel too, and it runs once for every light
  129. that affects the object. It does not run if no lights affect the object. It
  130. exists as a function called inside the ``fragment()`` processor and typically
  131. operates on the material properties setup inside the ``fragment()`` function.
  132. The ``light()`` processor works differently in 2D than it does in 3D; for a
  133. description of how it works in each, see their documentation, :ref:`CanvasItem
  134. shaders <doc_canvas_item_shader>` and :ref:`Spatial shaders
  135. <doc_spatial_shader>`, respectively.