introduction_to_shaders.rst 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. for x in range(width):
  22. for y in range(height):
  23. set_color(x, y, some_color)
  24. Your code is already part of a loop in a shader, so the corresponding code would
  25. look like this.
  26. .. code-block:: glsl
  27. void fragment() {
  28. COLOR = some_color;
  29. }
  30. .. note::
  31. The graphics card calls the ``fragment()`` function once or more for each pixel it has to draw. More on that below.
  32. Shaders in Godot
  33. ----------------
  34. Godot provides a shading language based on the popular OpenGL Shading Language
  35. (GLSL) but simplified. The engine handles some of the lower-level initialization
  36. work for you, making it easier to write complex shaders.
  37. In Godot, shaders are made up of three main functions: ``vertex()``,
  38. ``fragment()``, and ``light()``.
  39. 1. The ``vertex()`` function runs over all the vertices in the mesh and sets
  40. their positions and some other per-vertex variables.
  41. 2. The ``fragment()`` function runs for every pixel covered by the mesh. It uses
  42. values output by the ``vertex()`` function, interpolated between the
  43. vertices.
  44. 3. The ``light()`` function runs for every pixel and for every light. It takes
  45. variables from the ``fragment()`` function and from its previous runs.
  46. .. warning::
  47. The ``light()`` function won't run if the ``vertex_lighting`` render mode is
  48. enabled, or if **Rendering > Quality > Shading > Force Vertex Shading** is
  49. enabled in the Project Settings. It's enabled by default on mobile
  50. platforms.
  51. Shader types
  52. ------------
  53. Instead of supplying a general-purpose configuration for all uses (2D, 3D,
  54. particles), you must specify the type of shader you're writing. Different types
  55. support different render modes, built-in variables, and processing functions.
  56. In Godot, all shaders need to specify their type in the first line, like so:
  57. .. code-block:: glsl
  58. shader_type spatial;
  59. Here are the available types:
  60. * :ref:`spatial <doc_spatial_shader>` for 3D rendering.
  61. * :ref:`canvas_item <doc_canvas_item_shader>` for 2D rendering.
  62. * :ref:`particles <doc_particle_shader>` for particle systems.
  63. Render modes
  64. ------------
  65. Shaders have optional render modes you can specify on the second line, after the
  66. shader type, like so:
  67. .. code-block:: glsl
  68. shader_type spatial;
  69. render_mode unshaded, cull_disabled;
  70. Render modes alter the way Godot applies the shader. For example, the
  71. ``unshaded`` mode makes the engine skip the built-in light processor function.
  72. Each shader type has different render modes. See the reference for each shader
  73. type for a complete list of render modes.
  74. Processor functions
  75. -------------------
  76. Depending on the shader type, you can override different processor functions.
  77. For ``spatial`` and ``canvas_item``, you have access to ``vertex()``,
  78. ``fragment()``, and ``light()``. For ``particles``, you only have access to
  79. ``vertex()``.
  80. Vertex processor
  81. ^^^^^^^^^^^^^^^^
  82. The ``vertex()`` processing function is called once for every vertex in
  83. ``spatial`` and ``canvas_item`` shaders. For ``particles`` shaders, it is called
  84. once for every particle.
  85. Each vertex in your world's geometry has properties like a position and color.
  86. The function modifies those values and passes them to the fragment function. You
  87. can also use it to send extra data to the fragment function using varyings.
  88. By default, Godot transforms your vertex information for you, which is necessary
  89. to project geometry onto the screen. You can use render modes to transform the
  90. data yourself; see the :ref:`Spatial shader doc <doc_spatial_shader>` for an
  91. example.
  92. Fragment processor
  93. ^^^^^^^^^^^^^^^^^^
  94. The ``fragment()`` processing function is used to set up the Godot material
  95. parameters per pixel. This code runs on every visible pixel the object or
  96. primitive draws. It is only available in ``spatial`` and ``canvas_item`` shaders.
  97. The standard use of the fragment function is to set up material properties used
  98. to calculate lighting. For example, you would set values for ``ROUGHNESS``,
  99. ``RIM``, or ``TRANSMISSION``, which would tell the light function how the lights
  100. respond to that fragment. This makes it possible to control a complex shading
  101. pipeline without the user having to write much code. If you don't need this
  102. built-in functionality, you can ignore it and write your own light processing
  103. function, and Godot will optimize it away. For example, if you do not write a
  104. value to ``RIM``, Godot will not calculate rim lighting. During compilation,
  105. Godot checks to see if ``RIM`` is used; if not, it cuts all the corresponding
  106. code out. Therefore, you will not waste calculations on the effects that you do
  107. not use.
  108. Light processor
  109. ^^^^^^^^^^^^^^^
  110. The ``light()`` processor runs per pixel too, and it runs once for every light
  111. that affects the object. It does not run if no lights affect the object. It
  112. exists as a function called inside the ``fragment()`` processor and typically
  113. operates on the material properties setup inside the ``fragment()`` function.
  114. The ``light()`` processor works differently in 2D than it does in 3D; for a
  115. description of how it works in each, see their documentation, :ref:`CanvasItem
  116. shaders <doc_canvas_item_shader>` and :ref:`Spatial shaders
  117. <doc_spatial_shader>`, respectively.