shaders.rst 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. .. _doc_shaders:
  2. Shaders
  3. =======
  4. Introduction
  5. ------------
  6. Shaders are unique programs that run on the GPU. They are used to specify how to take mesh
  7. data (vertex positions, colors, normals, etc.) and draw them to the screen. Shaders do not process
  8. information the same way a normal program does because they are optimized for running on the GPU.
  9. One consequence of this is that shaders do not retain their data after they run; they output a final
  10. color to the screen and then move on. Accordingly, there is no way of accessing the color output from
  11. the last run of the shader.
  12. Godot uses a shader language very similar to GLSL, but with added functionality and slightly less
  13. flexibility. The reason for doing this is that Godot integrates built-in functionality to make
  14. writing complex shaders substantially easier. Godot wraps the user-written shader code in code
  15. of its own. That way, Godot handles a lot of the low-level stuff that the user doesn't need to
  16. worry about, and it is able to parse your shader code and use it to affect the rendering pipeline.
  17. For more advanced shaders, you can turn this functionality off using a render_mode.
  18. This document provides you with some information about shaders, specific to Godot. For a detailed
  19. reference of the shading language in Godot see the :ref:`Godot shading language doc<doc_shading_language>`.
  20. Shader types
  21. ------------
  22. Instead of supplying a general purpose configuration for all uses (2D, 3D, particles),
  23. Godot shaders must specify what they are intended for. Different types support different
  24. render modes, built-in variables, and processing functions.
  25. All shaders need to specify their type in the first line, in the following format:
  26. .. code-block:: glsl
  27. shader_type spatial;
  28. Valid types are:
  29. * :ref:`spatial <doc_spatial_shader>`: For 3D rendering.
  30. * :ref:`canvas_item <doc_canvas_item_shader>`: For 2D rendering.
  31. * :ref:`particles <doc_particle_shader>`: For particle systems.
  32. For detailed information on each shading type, see the corresponding reference document.
  33. Render modes
  34. ------------
  35. Different shader types support different render modes. They are optional and, if specified, must
  36. be after the *shader_type*. Render modes are used to alter the way built-in functionality is handled.
  37. For example, it is common to use the render mode ``unshaded`` to skip the built-in light processor
  38. function.
  39. Render modes are specified underneath the shader type:
  40. .. code-block:: glsl
  41. shader_type spatial;
  42. render_mode unshaded, cull_disabled;
  43. Each shader type has a different list of render modes available. See the document for each shader
  44. type for a complete list of render modes.
  45. Processor functions
  46. -------------------
  47. Depending on the shader type, different processor functions may be optionally overridden.
  48. For "spatial" and "canvas_item", it is possible to override ``vertex``, ``fragment``, and ``light``.
  49. For "particles", only ``vertex`` can be overridden.
  50. Vertex processor
  51. ^^^^^^^^^^^^^^^^
  52. The ``vertex`` processing function is called once for every vertex in "spatial" and "canvas_item" shaders.
  53. For "particles" shaders, it is called once for every particle.
  54. The ``vertex`` function is used to modify per-vertex information that will be passed on to the fragment
  55. function. It can also be used to establish variables that will be sent to the fragment function by using
  56. varyings(see other doc).
  57. By default, Godot will take your vertex information and transform it accordingly to be drawn. If this is
  58. undesirable, you can use render modes to transform the data yourself; see the
  59. :ref:`Spatial shader doc <doc_spatial_shader>` for an example of this.
  60. Fragment processor
  61. ^^^^^^^^^^^^^^^^^^
  62. The ``fragment`` processing function is used to set up the Godot material parameters per pixel. This code
  63. runs on every visible pixel the object or primitive draws. It is only available in "spatial" and
  64. "canvas_item" shaders.
  65. The standard use of the fragment function is to set up material properties that will be used to calculate
  66. lighting. For example, you would set values for ``ROUGHNESS``, ``RIM``, or ``TRANSMISSION`` which would
  67. tell the light function how the lights respond to that fragment. This makes it possible to control a complex
  68. shading pipeline without the user having to write much code. If you don't need this built-in functionality,
  69. you can ignore it and write your own light processing function and Godot will optimize it away. For example,
  70. if you do not write a value to ``RIM``, Godot will not calculate rim lighting. During compilation, Godot checks
  71. to see if ``RIM`` is used; if not, it cuts all the corresponding code out. Therefore, you will not
  72. waste calculations on effects that you do not use.
  73. Light processor
  74. ^^^^^^^^^^^^^^^
  75. The ``light`` processor runs per pixel too, but also runs for every light that affects the object
  76. (and does not run if no lights affect the object). It exists as a function called inside the
  77. ``fragment`` processor and typically operates on the material properties setup inside the ``fragment``
  78. function.
  79. The ``light`` processor works differently in 2D than it does in 3D; for a description of how it works
  80. in each, see their documentation, :ref:`CanvasItem shaders <doc_canvas_item_shader>` and
  81. :ref:`Spatial shaders <doc_spatial_shader>`, respectively.