shading_language.rst 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. .. _doc_shading_language:
  2. Shading language
  3. ================
  4. Introduction
  5. ------------
  6. Godot uses a shading language similar to GLSL ES 3.0. Most datatypes and functions are supported,
  7. and the few remaining ones will likely be added over time.
  8. If you are already familiar with GLSL, the :ref:`Godot Shader Migration Guide<doc_converting_glsl_to_godot_shaders>`
  9. is a resource that will help you transition from regular GLSL to Godot's shading language.
  10. Data types
  11. ----------
  12. Most GLSL ES 3.0 datatypes are supported:
  13. +---------------------+---------------------------------------------------------------------------------+
  14. | Type | Description |
  15. +=====================+=================================================================================+
  16. | **void** | Void datatype, useful only for functions that return nothing. |
  17. +---------------------+---------------------------------------------------------------------------------+
  18. | **bool** | Boolean datatype, can only contain ``true`` or ``false``. |
  19. +---------------------+---------------------------------------------------------------------------------+
  20. | **bvec2** | Two-component vector of booleans. |
  21. +---------------------+---------------------------------------------------------------------------------+
  22. | **bvec3** | Three-component vector of booleans. |
  23. +---------------------+---------------------------------------------------------------------------------+
  24. | **bvec4** | Four-component vector of booleans. |
  25. +---------------------+---------------------------------------------------------------------------------+
  26. | **int** | Signed scalar integer. |
  27. +---------------------+---------------------------------------------------------------------------------+
  28. | **ivec2** | Two-component vector of signed integers. |
  29. +---------------------+---------------------------------------------------------------------------------+
  30. | **ivec3** | Three-component vector of signed integers. |
  31. +---------------------+---------------------------------------------------------------------------------+
  32. | **ivec4** | Four-component vector of signed integers. |
  33. +---------------------+---------------------------------------------------------------------------------+
  34. | **uint** | Unsigned scalar integer; can't contain negative numbers. |
  35. +---------------------+---------------------------------------------------------------------------------+
  36. | **uvec2** | Two-component vector of unsigned integers. |
  37. +---------------------+---------------------------------------------------------------------------------+
  38. | **uvec3** | Three-component vector of unsigned integers. |
  39. +---------------------+---------------------------------------------------------------------------------+
  40. | **uvec4** | Four-component vector of unsigned integers. |
  41. +---------------------+---------------------------------------------------------------------------------+
  42. | **float** | Floating-point scalar. |
  43. +---------------------+---------------------------------------------------------------------------------+
  44. | **vec2** | Two-component vector of floating-point values. |
  45. +---------------------+---------------------------------------------------------------------------------+
  46. | **vec3** | Three-component vector of floating-point values. |
  47. +---------------------+---------------------------------------------------------------------------------+
  48. | **vec4** | Four-component vector of floating-point values. |
  49. +---------------------+---------------------------------------------------------------------------------+
  50. | **mat2** | 2x2 matrix, in column major order. |
  51. +---------------------+---------------------------------------------------------------------------------+
  52. | **mat3** | 3x3 matrix, in column major order. |
  53. +---------------------+---------------------------------------------------------------------------------+
  54. | **mat4** | 4x4 matrix, in column major order. |
  55. +---------------------+---------------------------------------------------------------------------------+
  56. | **sampler2D** | Sampler type for binding 2D textures, which are read as float. |
  57. +---------------------+---------------------------------------------------------------------------------+
  58. | **isampler2D** | Sampler type for binding 2D textures, which are read as signed integer. |
  59. +---------------------+---------------------------------------------------------------------------------+
  60. | **usampler2D** | Sampler type for binding 2D textures, which are read as unsigned integer. |
  61. +---------------------+---------------------------------------------------------------------------------+
  62. | **sampler2DArray** | Sampler type for binding 2D texture arrays, which are read as float. |
  63. +---------------------+---------------------------------------------------------------------------------+
  64. | **isampler2DArray** | Sampler type for binding 2D texture arrays, which are read as signed integer. |
  65. +---------------------+---------------------------------------------------------------------------------+
  66. | **usampler2DArray** | Sampler type for binding 2D texture arrays, which are read as unsigned integer. |
  67. +---------------------+---------------------------------------------------------------------------------+
  68. | **sampler3D** | Sampler type for binding 3D textures, which are read as float. |
  69. +---------------------+---------------------------------------------------------------------------------+
  70. | **isampler3D** | Sampler type for binding 3D textures, which are read as signed integer. |
  71. +---------------------+---------------------------------------------------------------------------------+
  72. | **usampler3D** | Sampler type for binding 3D textures, which are read as unsigned integer. |
  73. +---------------------+---------------------------------------------------------------------------------+
  74. | **samplerCube** | Sampler type for binding Cubemaps, which are read as floats. |
  75. +---------------------+---------------------------------------------------------------------------------+
  76. Casting
  77. ~~~~~~~
  78. Just like GLSL ES 3.0, implicit casting between scalars and vectors of the same size but different type is not allowed.
  79. Casting of types of different size is also not allowed. Conversion must be done explicitly via constructors.
  80. Example:
  81. .. code-block:: glsl
  82. float a = 2; // invalid
  83. float a = 2.0; // valid
  84. float a = float(2); // valid
  85. Default integer constants are signed, so casting is always needed to convert to unsigned:
  86. .. code-block:: glsl
  87. int a = 2; // valid
  88. uint a = 2; // invalid
  89. uint a = uint(2); // valid
  90. Members
  91. ~~~~~~~
  92. Individual scalar members of vector types are accessed via the "x", "y", "z" and "w" members.
  93. Alternatively, using "r", "g", "b" and "a" also works and is equivalent. Use whatever fits
  94. best for your needs.
  95. For matrices, use the ``m[row][column]`` indexing syntax to access each scalar, or ``m[idx]`` to access
  96. a vector by row index. For example, for accessing the y position of an object in a mat4 you use
  97. ``m[3][1]``.
  98. Constructing
  99. ~~~~~~~~~~~~
  100. Construction of vector types must always pass:
  101. .. code-block:: glsl
  102. // The required amount of scalars
  103. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  104. // Complementary vectors and/or scalars
  105. vec4 a = vec4(vec2(0.0, 1.0), vec2(2.0, 3.0));
  106. vec4 a = vec4(vec3(0.0, 1.0, 2.0), 3.0);
  107. // A single scalar for the whole vector
  108. vec4 a = vec4(0.0);
  109. Construction of matrix types requires vectors of the same dimension as the matrix. You can
  110. also build a diagonal matrix using ``matx(float)`` syntax. Accordingly, ``mat4(1.0)`` is
  111. an identity matrix.
  112. .. code-block:: glsl
  113. mat2 m2 = mat2(vec2(1.0, 0.0), vec2(0.0, 1.0));
  114. mat3 m3 = mat3(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), vec3(0.0, 0.0, 1.0));
  115. mat4 identity = mat4(1.0);
  116. Matrices can also be built from a matrix of another dimension.
  117. There are two rules :
  118. If a larger matrix is constructed from a smaller matrix, the additional rows and columns are
  119. set to the values they would have in an identity matrix. If a smaller matrix is constructed
  120. from a larger matrix, the top, left submatrix of the larger matrix is used.
  121. .. code-block:: glsl
  122. mat3 basis = mat3(WORLD_MATRIX);
  123. mat4 m4 = mat4(basis);
  124. mat2 m2 = mat2(m4);
  125. Swizzling
  126. ~~~~~~~~~
  127. It is possible to obtain any combination of components in any order, as long as the result
  128. is another vector type (or scalar). This is easier shown than explained:
  129. .. code-block:: glsl
  130. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  131. vec3 b = a.rgb; // Creates a vec3 with vec4 components.
  132. vec3 b = a.ggg; // Also valid; creates a vec3 and fills it with a single vec4 component.
  133. vec3 b = a.bgr; // Order does not matter.
  134. vec3 b = a.xyz; // Also rgba, xyzw are equivalent.
  135. vec3 b = a.stp; // And stpq (for texture coordinates).
  136. float c = b.w; // Invalid, because "w" is not present in vec3 b.
  137. vec3 c = b.xrt; // Invalid, mixing different styles is forbidden.
  138. b.rrr = a.rgb; // Invalid, assignment with duplication.
  139. b.bgr = a.rgb; // Valid assignment.
  140. Precision
  141. ~~~~~~~~~
  142. It is possible to add precision modifiers to datatypes; use them for uniforms, variables, arguments and varyings:
  143. .. code-block:: glsl
  144. lowp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // low precision, usually 8 bits per component mapped to 0-1
  145. mediump vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // medium precision, usually 16 bits or half float
  146. highp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // high precision, uses full float or integer range (default)
  147. Using lower precision for some operations can speed up the math involved (at the cost of less precision).
  148. This is rarely needed in the vertex processor function (where full precision is needed most of the time),
  149. but is often useful in the fragment processor.
  150. Some architectures (mainly mobile) can benefit significantly from this, but
  151. there are downsides such as the additional overhead of conversion between
  152. precisions. Refer to the documentation of the target architecture for further
  153. information. In many cases, mobile drivers cause inconsistent or unexpected
  154. behavior and it is best to avoid specifying precision unless necessary.
  155. Arrays
  156. ------
  157. Arrays are containers for multiple variables of a similar type.
  158. Local arrays
  159. ~~~~~~~~~~~~
  160. Local arrays are declared in functions. They can use all of the allowed datatypes, except samplers.
  161. The array declaration follows a C-style syntax: ``[const] + [precision] + typename + identifier + [array size]``.
  162. .. code-block:: glsl
  163. void fragment() {
  164. float arr[3];
  165. }
  166. They can be initialized at the beginning like:
  167. .. code-block:: glsl
  168. float float_arr[3] = float[3] (1.0, 0.5, 0.0); // first constructor
  169. int int_arr[3] = int[] (2, 1, 0); // second constructor
  170. vec2 vec2_arr[3] = { vec2(1.0, 1.0), vec2(0.5, 0.5), vec2(0.0, 0.0) }; // third constructor
  171. bool bool_arr[] = { true, true, false }; // fourth constructor - size is defined automatically from the element count
  172. You can declare multiple arrays (even with different sizes) in one expression:
  173. .. code-block:: glsl
  174. float a[3] = float[3] (1.0, 0.5, 0.0),
  175. b[2] = { 1.0, 0.5 },
  176. c[] = { 0.7 },
  177. d = 0.0,
  178. e[5];
  179. To access an array element, use the indexing syntax:
  180. .. code-block:: glsl
  181. float arr[3];
  182. arr[0] = 1.0; // setter
  183. COLOR.r = arr[0]; // getter
  184. Arrays also have a built-in function ``.length()`` (not to be confused with the built-in ``length()`` function). It doesn't accept any parameters and will return the array's size.
  185. .. code-block:: glsl
  186. float arr[] = { 0.0, 1.0, 0.5, -1.0 };
  187. for (int i = 0; i < arr.length(); i++) {
  188. // ...
  189. }
  190. .. note::
  191. If you use an index below 0 or greater than array size - the shader will crash and break rendering. To prevent this, use ``length()``, ``if``, or ``clamp()`` functions to ensure the index is between 0 and the array's length. Always carefully test and check your code. If you pass a constant expression or a simple number, the editor will check its bounds to prevent this crash.
  192. Global arrays
  193. ~~~~~~~~~~~~~
  194. You can declare arrays at global space like:
  195. .. code-block:: glsl
  196. shader_type spatial;
  197. const lowp vec3 v[1] = lowp vec3[1] ( vec3(0, 0, 1) );
  198. void fragment() {
  199. ALBEDO = v[0];
  200. }
  201. .. note::
  202. Global arrays have to be declared as global constants, otherwise they can be declared the same as local arrays.
  203. Constants
  204. ---------
  205. Use the ``const`` keyword before the variable declaration to make that variable immutable, which means that it cannot be modified. All basic types, except samplers can be declared as constants. Accessing and using a constant value is slightly faster than using a uniform. Constants must be initialized at their declaration.
  206. .. code-block:: glsl
  207. const vec2 a = vec2(0.0, 1.0);
  208. vec2 b;
  209. a = b; // invalid
  210. b = a; // valid
  211. Constants cannot be modified and additionally cannot have hints, but multiple of them (if they have the same type) can be declared in a single expression e.g
  212. .. code-block:: glsl
  213. const vec2 V1 = vec2(1, 1), V2 = vec2(2, 2);
  214. Similar to variables, arrays can also be declared with ``const``.
  215. .. code-block:: glsl
  216. const float arr[] = { 1.0, 0.5, 0.0 };
  217. arr[0] = 1.0; // invalid
  218. COLOR.r = arr[0]; // valid
  219. Constants can be declared both globally (outside of any function) or locally (inside a function).
  220. Global constants are useful when you want to have access to a value throughout your shader that does not need to be modified. Like uniforms, global constants are shared between all shader stages, but they are not accessible outside of the shader.
  221. .. code-block:: glsl
  222. shader_type spatial;
  223. const float PI = 3.14159265358979323846;
  224. Structs
  225. -------
  226. Structs are compound types which can be used for better abstraction of shader code. You can declare them at the global scope like:
  227. .. code-block:: glsl
  228. struct PointLight {
  229. vec3 position;
  230. vec3 color;
  231. float intensity;
  232. };
  233. After declaration, you can instantiate and initialize them like:
  234. .. code-block:: glsl
  235. void fragment()
  236. {
  237. PointLight light;
  238. light.position = vec3(0.0);
  239. light.color = vec3(1.0, 0.0, 0.0);
  240. light.intensity = 0.5;
  241. }
  242. Or use struct constructor for same purpose:
  243. .. code-block:: glsl
  244. PointLight light = PointLight(vec3(0.0), vec3(1.0, 0.0, 0.0), 0.5);
  245. Structs may contain other struct or array, you can also instance them as global constant:
  246. .. code-block:: glsl
  247. shader_type spatial;
  248. ...
  249. struct Scene {
  250. PointLight lights[2];
  251. };
  252. const Scene scene = Scene(PointLight[2](PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0), PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0)));
  253. void fragment()
  254. {
  255. ALBEDO = scene.lights[0].color;
  256. }
  257. You can also pass them to functions:
  258. .. code-block:: glsl
  259. shader_type canvas_item;
  260. ...
  261. Scene construct_scene(PointLight light1, PointLight light2) {
  262. return Scene({light1, light2});
  263. }
  264. void fragment()
  265. {
  266. COLOR.rgb = construct_scene(PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 0.0), 1.0), PointLight(vec3(0.0, 0.0, 0.0), vec3(1.0, 0.0, 1.0), 1.0)).lights[0].color;
  267. }
  268. Operators
  269. ---------
  270. Godot shading language supports the same set of operators as GLSL ES 3.0. Below is the list of them in precedence order:
  271. +-------------+------------------------+------------------+
  272. | Precedence | Class | Operator |
  273. +-------------+------------------------+------------------+
  274. | 1 (highest) | parenthetical grouping | **()** |
  275. +-------------+------------------------+------------------+
  276. | 2 | unary | **+, -, !, ~** |
  277. +-------------+------------------------+------------------+
  278. | 3 | multiplicative | **/, \*, %** |
  279. +-------------+------------------------+------------------+
  280. | 4 | additive | **+, -** |
  281. +-------------+------------------------+------------------+
  282. | 5 | bit-wise shift | **<<, >>** |
  283. +-------------+------------------------+------------------+
  284. | 6 | relational | **<, >, <=, >=** |
  285. +-------------+------------------------+------------------+
  286. | 7 | equality | **==, !=** |
  287. +-------------+------------------------+------------------+
  288. | 8 | bit-wise AND | **&** |
  289. +-------------+------------------------+------------------+
  290. | 9 | bit-wise exclusive OR | **^** |
  291. +-------------+------------------------+------------------+
  292. | 10 | bit-wise inclusive OR | **|** |
  293. +-------------+------------------------+------------------+
  294. | 11 | logical AND | **&&** |
  295. +-------------+------------------------+------------------+
  296. | 12 (lowest) | logical inclusive OR | **||** |
  297. +-------------+------------------------+------------------+
  298. Flow control
  299. ------------
  300. Godot Shading language supports the most common types of flow control:
  301. .. code-block:: glsl
  302. // if and else
  303. if (cond) {
  304. } else {
  305. }
  306. // switch
  307. switch(i) { // signed integer expression
  308. case -1:
  309. break;
  310. case 0:
  311. return; // break or return
  312. case 1: // pass-through
  313. case 2:
  314. break;
  315. //...
  316. default: // optional
  317. break;
  318. }
  319. // for loops
  320. for (int i = 0; i < 10; i++) {
  321. }
  322. // while
  323. while (true) {
  324. }
  325. // do while
  326. do {
  327. } while(true);
  328. Keep in mind that, in modern GPUs, an infinite loop can exist and can freeze your application (including editor).
  329. Godot can't protect you from this, so be careful not to make this mistake!
  330. .. warning::
  331. When exporting a GLES2 project to HTML5, WebGL 1.0 will be used. WebGL 1.0
  332. doesn't support dynamic loops, so shaders using those won't work there.
  333. Discarding
  334. ----------
  335. Fragment and light functions can use the **discard** keyword. If used, the fragment is discarded and nothing is written.
  336. Functions
  337. ---------
  338. It is possible to define functions in a Godot shader. They use the following syntax:
  339. .. code-block:: glsl
  340. ret_type func_name(args) {
  341. return ret_type; // if returning a value
  342. }
  343. // a more specific example:
  344. int sum2(int a, int b) {
  345. return a + b;
  346. }
  347. You can only use functions that have been defined above (higher in the editor) the function from which you are calling
  348. them.
  349. Function arguments can have special qualifiers:
  350. * **in**: Means the argument is only for reading (default).
  351. * **out**: Means the argument is only for writing.
  352. * **inout**: Means the argument is fully passed via reference.
  353. * **const**: Means the argument is a constant and cannot be changed, may be combined with **in** qualifier.
  354. Example below:
  355. .. code-block:: glsl
  356. void sum2(int a, int b, inout int result) {
  357. result = a + b;
  358. }
  359. Varyings
  360. ~~~~~~~~
  361. To send data from the vertex to the fragment processor function, *varyings* are used. They are set
  362. for every primitive vertex in the *vertex processor*, and the value is interpolated for every
  363. pixel in the fragment processor.
  364. .. code-block:: glsl
  365. shader_type spatial;
  366. varying vec3 some_color;
  367. void vertex() {
  368. some_color = NORMAL; // Make the normal the color.
  369. }
  370. void fragment() {
  371. ALBEDO = some_color;
  372. }
  373. Varying can also be an array:
  374. .. code-block:: glsl
  375. shader_type spatial;
  376. varying float var_arr[3];
  377. void vertex() {
  378. var_arr[0] = 1.0;
  379. var_arr[1] = 0.0;
  380. }
  381. void fragment() {
  382. ALBEDO = vec3(var_arr[0], var_arr[1], var_arr[2]); // red color
  383. }
  384. Interpolation qualifiers
  385. ~~~~~~~~~~~~~~~~~~~~~~~~
  386. Certain values are interpolated during the shading pipeline. You can modify how these interpolations
  387. are done by using *interpolation qualifiers*.
  388. .. code-block:: glsl
  389. shader_type spatial;
  390. varying flat vec3 our_color;
  391. void vertex() {
  392. our_color = COLOR.rgb;
  393. }
  394. void fragment() {
  395. ALBEDO = our_color;
  396. }
  397. There are two possible interpolation qualifiers:
  398. +-------------------+---------------------------------------------------------------------------------+
  399. | Qualifier | Description |
  400. +===================+=================================================================================+
  401. | **flat** | The value is not interpolated. |
  402. +-------------------+---------------------------------------------------------------------------------+
  403. | **smooth** | The value is interpolated in a perspective-correct fashion. This is the default.|
  404. +-------------------+---------------------------------------------------------------------------------+
  405. Uniforms
  406. ~~~~~~~~
  407. Passing values to shaders is possible. These are global to the whole shader and are called *uniforms*.
  408. When a shader is later assigned to a material, the uniforms will appear as editable parameters in it.
  409. Uniforms can't be written from within the shader.
  410. .. note::
  411. Uniform arrays are not implemented yet.
  412. .. code-block:: glsl
  413. shader_type spatial;
  414. uniform float some_value;
  415. You can set uniforms in the editor in the material. Or you can set them through GDScript:
  416. ::
  417. material.set_shader_param("some_value", some_value)
  418. .. note:: The first argument to ``set_shader_param`` is the name of the uniform in the shader. It
  419. must match *exactly* to the name of the uniform in the shader or else it will not be recognized.
  420. Any GLSL type except for *void* can be a uniform. Additionally, Godot provides optional shader hints
  421. to make the compiler understand for what the uniform is used.
  422. .. code-block:: glsl
  423. shader_type spatial;
  424. uniform vec4 color : hint_color;
  425. uniform float amount : hint_range(0, 1);
  426. uniform vec4 other_color : hint_color = vec4(1.0);
  427. It's important to understand that textures that are supplied as color require hints for proper sRGB->linear conversion (i.e. ``hint_albedo``), as Godot's 3D engine renders in linear color space.
  428. Full list of hints below:
  429. +----------------+------------------------------+-------------------------------------+
  430. | Type | Hint | Description |
  431. +================+==============================+=====================================+
  432. | **vec4** | hint_color | Used as color |
  433. +----------------+------------------------------+-------------------------------------+
  434. | **int, float** | hint_range(min, max[, step]) | Used as range (with min/max/step) |
  435. +----------------+------------------------------+-------------------------------------+
  436. | **sampler2D** | hint_albedo | Used as albedo color, default white |
  437. +----------------+------------------------------+-------------------------------------+
  438. | **sampler2D** | hint_black_albedo | Used as albedo color, default black |
  439. +----------------+------------------------------+-------------------------------------+
  440. | **sampler2D** | hint_normal | Used as normalmap |
  441. +----------------+------------------------------+-------------------------------------+
  442. | **sampler2D** | hint_white | As value, default to white. |
  443. +----------------+------------------------------+-------------------------------------+
  444. | **sampler2D** | hint_black | As value, default to black |
  445. +----------------+------------------------------+-------------------------------------+
  446. | **sampler2D** | hint_aniso | As flowmap, default to right. |
  447. +----------------+------------------------------+-------------------------------------+
  448. GDScript uses different variable types than GLSL does, so when passing variables from GDScript
  449. to shaders, Godot converts the type automatically. Below is a table of the corresponding types:
  450. +-----------------+-----------+
  451. | GDScript type | GLSL type |
  452. +=================+===========+
  453. | **bool** | **bool** |
  454. +-----------------+-----------+
  455. | **int** | **int** |
  456. +-----------------+-----------+
  457. | **float** | **float** |
  458. +-----------------+-----------+
  459. | **Vector2** | **vec2** |
  460. +-----------------+-----------+
  461. | **Vector3** | **vec3** |
  462. +-----------------+-----------+
  463. | **Color** | **vec4** |
  464. +-----------------+-----------+
  465. | **Transform** | **mat4** |
  466. +-----------------+-----------+
  467. | **Transform2D** | **mat4** |
  468. +-----------------+-----------+
  469. .. note:: Be careful when setting shader uniforms from GDScript, no error will be thrown if the
  470. type does not match. Your shader will just exhibit undefined behavior.
  471. Uniforms can also be assigned default values:
  472. .. code-block:: glsl
  473. shader_type spatial;
  474. uniform vec4 some_vector = vec4(0.0);
  475. uniform vec4 some_color : hint_color = vec4(1.0);
  476. Built-in functions
  477. ------------------
  478. A large number of built-in functions are supported, conforming to GLSL ES 3.0.
  479. When vec_type (float), vec_int_type, vec_uint_type, vec_bool_type nomenclature is used, it can be scalar or vector.
  480. .. note:: For a list of the functions that are not available in the GLES2 backend, please see the
  481. :ref:`Differences between GLES2 and GLES3 doc <doc_gles2_gles3_differences>`.
  482. +------------------------------------------------------------------------+---------------------------------------------------------------+
  483. | Function | Description / Return value |
  484. +========================================================================+===============================================================+
  485. | vec_type **radians** (vec_type degrees) | Convert degrees to radians |
  486. +------------------------------------------------------------------------+---------------------------------------------------------------+
  487. | vec_type **degrees** (vec_type radians) | Convert radians to degrees |
  488. +------------------------------------------------------------------------+---------------------------------------------------------------+
  489. | vec_type **sin** (vec_type x) | Sine |
  490. +------------------------------------------------------------------------+---------------------------------------------------------------+
  491. | vec_type **cos** (vec_type x) | Cosine |
  492. +------------------------------------------------------------------------+---------------------------------------------------------------+
  493. | vec_type **tan** (vec_type x) | Tangent |
  494. +------------------------------------------------------------------------+---------------------------------------------------------------+
  495. | vec_type **asin** (vec_type x) | Arcsine |
  496. +------------------------------------------------------------------------+---------------------------------------------------------------+
  497. | vec_type **acos** (vec_type x) | Arccosine |
  498. +------------------------------------------------------------------------+---------------------------------------------------------------+
  499. | vec_type **atan** (vec_type y_over_x) | Arctangent |
  500. +------------------------------------------------------------------------+---------------------------------------------------------------+
  501. | vec_type **atan** (vec_type y, vec_type x) | Arctangent to convert vector to angle |
  502. +------------------------------------------------------------------------+---------------------------------------------------------------+
  503. | vec_type **sinh** (vec_type x) | Hyperbolic sine |
  504. +------------------------------------------------------------------------+---------------------------------------------------------------+
  505. | vec_type **cosh** (vec_type x) | Hyperbolic cosine |
  506. +------------------------------------------------------------------------+---------------------------------------------------------------+
  507. | vec_type **tanh** (vec_type x) | Hyperbolic tangent |
  508. +------------------------------------------------------------------------+---------------------------------------------------------------+
  509. | vec_type **asinh** (vec_type x) | Inverse hyperbolic sine |
  510. +------------------------------------------------------------------------+---------------------------------------------------------------+
  511. | vec_type **acosh** (vec_type x) | Inverse hyperbolic cosine |
  512. +------------------------------------------------------------------------+---------------------------------------------------------------+
  513. | vec_type **atanh** (vec_type x) | Inverse hyperbolic tangent |
  514. +------------------------------------------------------------------------+---------------------------------------------------------------+
  515. | vec_type **pow** (vec_type x, vec_type y) | Power (undefined if ``x`` < 0 or if ``x`` = 0 and ``y`` <= 0) |
  516. +------------------------------------------------------------------------+---------------------------------------------------------------+
  517. | vec_type **exp** (vec_type x) | Base-e exponential |
  518. +------------------------------------------------------------------------+---------------------------------------------------------------+
  519. | vec_type **exp2** (vec_type x) | Base-2 exponential |
  520. +------------------------------------------------------------------------+---------------------------------------------------------------+
  521. | vec_type **log** (vec_type x) | Natural logarithm |
  522. +------------------------------------------------------------------------+---------------------------------------------------------------+
  523. | vec_type **log2** (vec_type x) | Base-2 logarithm |
  524. +------------------------------------------------------------------------+---------------------------------------------------------------+
  525. | vec_type **sqrt** (vec_type x) | Square root |
  526. +------------------------------------------------------------------------+---------------------------------------------------------------+
  527. | vec_type **inversesqrt** (vec_type x) | Inverse square root |
  528. +------------------------------------------------------------------------+---------------------------------------------------------------+
  529. | vec_type **abs** (vec_type x) | Absolute value (returns positive value if negative) |
  530. +------------------------------------------------------------------------+---------------------------------------------------------------+
  531. | ivec_type **abs** (ivec_type x) | Absolute value (returns positive value if negative) |
  532. +------------------------------------------------------------------------+---------------------------------------------------------------+
  533. | vec_type **sign** (vec_type x) | Sign (returns ``1.0`` if positive, ``-1.0`` if negative, |
  534. | | ``0.0`` if zero) |
  535. +------------------------------------------------------------------------+---------------------------------------------------------------+
  536. | ivec_type **sign** (ivec_type x) | Sign (returns ``1`` if positive, ``-1`` if negative, |
  537. | | ``0`` if zero) |
  538. +------------------------------------------------------------------------+---------------------------------------------------------------+
  539. | vec_type **floor** (vec_type x) | Round to the integer below |
  540. +------------------------------------------------------------------------+---------------------------------------------------------------+
  541. | vec_type **round** (vec_type x) | Round to the nearest integer |
  542. +------------------------------------------------------------------------+---------------------------------------------------------------+
  543. | vec_type **roundEven** (vec_type x) | Round to the nearest even integer |
  544. +------------------------------------------------------------------------+---------------------------------------------------------------+
  545. | vec_type **trunc** (vec_type x) | Truncation |
  546. +------------------------------------------------------------------------+---------------------------------------------------------------+
  547. | vec_type **ceil** (vec_type x) | Round to the integer above |
  548. +------------------------------------------------------------------------+---------------------------------------------------------------+
  549. | vec_type **fract** (vec_type x) | Fractional |
  550. +------------------------------------------------------------------------+---------------------------------------------------------------+
  551. | vec_type **mod** (vec_type x, vec_type y) | Modulo (division remainder) |
  552. +------------------------------------------------------------------------+---------------------------------------------------------------+
  553. | vec_type **mod** (vec_type x, float y) | Modulo (division remainder) |
  554. +------------------------------------------------------------------------+---------------------------------------------------------------+
  555. | vec_type **modf** (vec_type x, out vec_type i) | Fractional of ``x``, with ``i`` as integer part |
  556. +------------------------------------------------------------------------+---------------------------------------------------------------+
  557. | vec_type **min** (vec_type a, vec_type b) | Lowest value between ``a`` and ``b`` |
  558. +------------------------------------------------------------------------+---------------------------------------------------------------+
  559. | vec_type **max** (vec_type a, vec_type b) | Highest value between ``a`` and ``b`` |
  560. +------------------------------------------------------------------------+---------------------------------------------------------------+
  561. | vec_type **clamp** (vec_type x, vec_type min, vec_type max) | Clamp ``x`` between ``min`` and ``max`` (inclusive) |
  562. +------------------------------------------------------------------------+---------------------------------------------------------------+
  563. | float **mix** (float a, float b, float c) | Linear interpolate between ``a`` and ``b`` by ``c`` |
  564. +------------------------------------------------------------------------+---------------------------------------------------------------+
  565. | vec_type **mix** (vec_type a, vec_type b, float c) | Linear interpolate between ``a`` and ``b`` by ``c`` |
  566. | | (scalar coefficient) |
  567. +------------------------------------------------------------------------+---------------------------------------------------------------+
  568. | vec_type **mix** (vec_type a, vec_type b, vec_type c) | Linear interpolate between ``a`` and ``b`` by ``c`` |
  569. | | (vector coefficient) |
  570. +------------------------------------------------------------------------+---------------------------------------------------------------+
  571. | vec_type **mix** (vec_type a, vec_type b, bvec_type c) | Linear interpolate between ``a`` and ``b`` by ``c`` |
  572. | | (boolean-vector selection) |
  573. +------------------------------------------------------------------------+---------------------------------------------------------------+
  574. | vec_type **fma** (vec_type a, vec_type b, vec_type c) | Performs a fused multiply-add operation: ``(a * b + c)`` |
  575. | | (faster than doing it manually) |
  576. +------------------------------------------------------------------------+---------------------------------------------------------------+
  577. | vec_type **step** (vec_type a, vec_type b) | ``b[i] < a[i] ? 0.0 : 1.0`` |
  578. +------------------------------------------------------------------------+---------------------------------------------------------------+
  579. | vec_type **step** (float a, vec_type b) | ``b[i] < a ? 0.0 : 1.0`` |
  580. +------------------------------------------------------------------------+---------------------------------------------------------------+
  581. | vec_type **smoothstep** (vec_type a, vec_type b, vec_type c) | Hermite interpolate between ``a`` and ``b`` by ``c`` |
  582. +------------------------------------------------------------------------+---------------------------------------------------------------+
  583. | vec_type **smoothstep** (float a, float b, vec_type c) | Hermite interpolate between ``a`` and ``b`` by ``c`` |
  584. +------------------------------------------------------------------------+---------------------------------------------------------------+
  585. | bvec_type **isnan** (vec_type x) | Returns ``true`` if scalar or vector component is ``NaN`` |
  586. +------------------------------------------------------------------------+---------------------------------------------------------------+
  587. | bvec_type **isinf** (vec_type x) | Returns ``true`` if scalar or vector component is ``INF`` |
  588. +------------------------------------------------------------------------+---------------------------------------------------------------+
  589. | ivec_type **floatBitsToInt** (vec_type x) | Float->Int bit copying, no conversion |
  590. +------------------------------------------------------------------------+---------------------------------------------------------------+
  591. | uvec_type **floatBitsToUint** (vec_type x) | Float->UInt bit copying, no conversion |
  592. +------------------------------------------------------------------------+---------------------------------------------------------------+
  593. | vec_type **intBitsToFloat** (ivec_type x) | Int->Float bit copying, no conversion |
  594. +------------------------------------------------------------------------+---------------------------------------------------------------+
  595. | vec_type **uintBitsToFloat** (uvec_type x) | UInt->Float bit copying, no conversion |
  596. +------------------------------------------------------------------------+---------------------------------------------------------------+
  597. | float **length** (vec_type x) | Vector length |
  598. +------------------------------------------------------------------------+---------------------------------------------------------------+
  599. | float **distance** (vec_type a, vec_type b) | Distance between vectors i.e ``length(a - b)`` |
  600. +------------------------------------------------------------------------+---------------------------------------------------------------+
  601. | float **dot** (vec_type a, vec_type b) | Dot product |
  602. +------------------------------------------------------------------------+---------------------------------------------------------------+
  603. | vec3 **cross** (vec3 a, vec3 b) | Cross product |
  604. +------------------------------------------------------------------------+---------------------------------------------------------------+
  605. | vec_type **normalize** (vec_type x) | Normalize to unit length |
  606. +------------------------------------------------------------------------+---------------------------------------------------------------+
  607. | vec3 **reflect** (vec3 I, vec3 N) | Reflect |
  608. +------------------------------------------------------------------------+---------------------------------------------------------------+
  609. | vec3 **refract** (vec3 I, vec3 N, float eta) | Refract |
  610. +------------------------------------------------------------------------+---------------------------------------------------------------+
  611. | vec_type **faceforward** (vec_type N, vec_type I, vec_type Nref) | If ``dot(Nref, I)`` < 0, return N, otherwise –N |
  612. +------------------------------------------------------------------------+---------------------------------------------------------------+
  613. | mat_type **matrixCompMult** (mat_type x, mat_type y) | Matrix component multiplication |
  614. +------------------------------------------------------------------------+---------------------------------------------------------------+
  615. | mat_type **outerProduct** (vec_type column, vec_type row) | Matrix outer product |
  616. +------------------------------------------------------------------------+---------------------------------------------------------------+
  617. | mat_type **transpose** (mat_type m) | Transpose matrix |
  618. +------------------------------------------------------------------------+---------------------------------------------------------------+
  619. | float **determinant** (mat_type m) | Matrix determinant |
  620. +------------------------------------------------------------------------+---------------------------------------------------------------+
  621. | mat_type **inverse** (mat_type m) | Inverse matrix |
  622. +------------------------------------------------------------------------+---------------------------------------------------------------+
  623. | bvec_type **lessThan** (vec_type x, vec_type y) | Bool vector comparison on < int/uint/float vectors |
  624. +------------------------------------------------------------------------+---------------------------------------------------------------+
  625. | bvec_type **greaterThan** (vec_type x, vec_type y) | Bool vector comparison on > int/uint/float vectors |
  626. +------------------------------------------------------------------------+---------------------------------------------------------------+
  627. | bvec_type **lessThanEqual** (vec_type x, vec_type y) | Bool vector comparison on <= int/uint/float vectors |
  628. +------------------------------------------------------------------------+---------------------------------------------------------------+
  629. | bvec_type **greaterThanEqual** (vec_type x, vec_type y) | Bool vector comparison on >= int/uint/float vectors |
  630. +------------------------------------------------------------------------+---------------------------------------------------------------+
  631. | bvec_type **equal** (vec_type x, vec_type y) | Bool vector comparison on == int/uint/float vectors |
  632. +------------------------------------------------------------------------+---------------------------------------------------------------+
  633. | bvec_type **notEqual** (vec_type x, vec_type y) | Bool vector comparison on != int/uint/float vectors |
  634. +------------------------------------------------------------------------+---------------------------------------------------------------+
  635. | bool **any** (bvec_type x) | ``true`` if any component is ``true``, ``false`` otherwise |
  636. +------------------------------------------------------------------------+---------------------------------------------------------------+
  637. | bool **all** (bvec_type x) | ``true`` if all components are ``true``, ``false`` otherwise |
  638. +------------------------------------------------------------------------+---------------------------------------------------------------+
  639. | bvec_type **not** (bvec_type x) | Invert boolean vector |
  640. +------------------------------------------------------------------------+---------------------------------------------------------------+
  641. | ivec2 **textureSize** (sampler2D_type s, int lod) | Get the size of a 2D texture |
  642. +------------------------------------------------------------------------+---------------------------------------------------------------+
  643. | ivec3 **textureSize** (sampler2DArray_type s, int lod) | Get the size of a 2D texture array |
  644. +------------------------------------------------------------------------+---------------------------------------------------------------+
  645. | ivec3 **textureSize** (sampler3D s, int lod) | Get the size of a 3D texture |
  646. +------------------------------------------------------------------------+---------------------------------------------------------------+
  647. | ivec2 **textureSize** (samplerCube s, int lod) | Get the size of a cubemap texture |
  648. +------------------------------------------------------------------------+---------------------------------------------------------------+
  649. | vec4_type **texture** (sampler2D_type s, vec2 uv [, float bias]) | Perform a 2D texture read |
  650. +------------------------------------------------------------------------+---------------------------------------------------------------+
  651. | vec4_type **texture** (sampler2DArray_type s, vec3 uv [, float bias]) | Perform a 2D texture array read |
  652. +------------------------------------------------------------------------+---------------------------------------------------------------+
  653. | vec4_type **texture** (sampler3D_type s, vec3 uv [, float bias]) | Perform a 3D texture read |
  654. +------------------------------------------------------------------------+---------------------------------------------------------------+
  655. | vec4 **texture** (samplerCube s, vec3 uv [, float bias]) | Perform a cubemap texture read |
  656. +------------------------------------------------------------------------+---------------------------------------------------------------+
  657. | vec4_type **textureProj** (sampler2D_type s, vec3 uv [, float bias]) | Perform a 2D texture read with projection |
  658. +------------------------------------------------------------------------+---------------------------------------------------------------+
  659. | vec4_type **textureProj** (sampler2D_type s, vec4 uv [, float bias]) | Perform a 2D texture read with projection |
  660. +------------------------------------------------------------------------+---------------------------------------------------------------+
  661. | vec4_type **textureProj** (sampler3D_type s, vec4 uv [, float bias]) | Perform a 3D texture read with projection |
  662. +------------------------------------------------------------------------+---------------------------------------------------------------+
  663. | vec4_type **textureLod** (sampler2D_type s, vec2 uv, float lod) | Perform a 2D texture read at custom mipmap |
  664. +------------------------------------------------------------------------+---------------------------------------------------------------+
  665. | vec4_type **textureLod** (sampler2DArray_type s, vec3 uv, float lod) | Perform a 2D texture array read at custom mipmap |
  666. +------------------------------------------------------------------------+---------------------------------------------------------------+
  667. | vec4_type **textureLod** (sampler3D_type s, vec3 uv, float lod) | Perform a 3D texture read at custom mipmap |
  668. +------------------------------------------------------------------------+---------------------------------------------------------------+
  669. | vec4 **textureLod** (samplerCube s, vec3 uv, float lod) | Perform a 3D texture read at custom mipmap |
  670. +------------------------------------------------------------------------+---------------------------------------------------------------+
  671. | vec4_type **textureProjLod** (sampler2D_type s, vec3 uv, float lod) | Perform a 2D texture read with projection/LOD |
  672. +------------------------------------------------------------------------+---------------------------------------------------------------+
  673. | vec4_type **textureProjLod** (sampler2D_type s, vec4 uv, float lod) | Perform a 2D texture read with projection/LOD |
  674. +------------------------------------------------------------------------+---------------------------------------------------------------+
  675. | vec4_type **textureProjLod** (sampler3D_type s, vec4 uv, float lod) | Perform a 3D texture read with projection/LOD |
  676. +------------------------------------------------------------------------+---------------------------------------------------------------+
  677. | vec4_type **texelFetch** (sampler2D_type s, ivec2 uv, int lod) | Fetch a single texel using integer coordinates |
  678. +------------------------------------------------------------------------+---------------------------------------------------------------+
  679. | vec4_type **texelFetch** (sampler2DArray_type s, ivec3 uv, int lod) | Fetch a single texel using integer coordinates |
  680. +------------------------------------------------------------------------+---------------------------------------------------------------+
  681. | vec4_type **texelFetch** (sampler3D_type s, ivec3 uv, int lod) | Fetch a single texel using integer coordinates |
  682. +------------------------------------------------------------------------+---------------------------------------------------------------+
  683. | vec_type **dFdx** (vec_type p) | Derivative in ``x`` using local differencing |
  684. +------------------------------------------------------------------------+---------------------------------------------------------------+
  685. | vec_type **dFdy** (vec_type p) | Derivative in ``y`` using local differencing |
  686. +------------------------------------------------------------------------+---------------------------------------------------------------+
  687. | vec_type **fwidth** (vec_type p) | Sum of absolute derivative in ``x`` and ``y`` |
  688. +------------------------------------------------------------------------+---------------------------------------------------------------+