shading_language.rst 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877
  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_migrating_to_godot_shader_language>`
  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. Keep in mind that some architectures (mainly mobile) benefit a lot from this, but are also restricted
  151. (conversion between precisions has a cost). Please read the relevant documentation on the target architecture
  152. to find out more. In all honesty though, mobile drivers are buggy, so, to stay out of trouble, make simple
  153. shaders without specifying precision unless you *really* need to.
  154. Arrays
  155. ------
  156. Arrays are containers for multiple variables of a similar type.
  157. Local arrays
  158. ~~~~~~~~~~~~
  159. Local arrays are declared in functions. They can use all of the allowed datatypes, except samplers.
  160. The array declaration follows a C-style syntax: ``[const] + [precision] + typename + identifier + [array size]``.
  161. .. code-block:: glsl
  162. void fragment() {
  163. float arr[3];
  164. }
  165. They can be initialized at the beginning like:
  166. .. code-block:: glsl
  167. float float_arr[3] = float[3] (1.0, 0.5, 0.0); // first constructor
  168. int int_arr[3] = int[] (2, 1, 0); // second constructor
  169. vec2 vec2_arr[3] = { vec2(1.0, 1.0), vec2(0.5, 0.5), vec2(0.0, 0.0) }; // third constructor
  170. bool bool_arr[] = { true, true, false }; // fourth constructor - size is defined automatically from the element count
  171. You can declare multiple arrays (even with different sizes) in one expression:
  172. .. code-block:: glsl
  173. float a[3] = float[3] (1.0, 0.5, 0.0),
  174. b[2] = { 1.0, 0.5 },
  175. c[] = { 0.7 },
  176. d = 0.0,
  177. e[5];
  178. To access an array element, use the indexing syntax:
  179. .. code-block:: glsl
  180. float arr[3];
  181. arr[0] = 1.0; // setter
  182. COLOR.r = arr[0]; // getter
  183. 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.
  184. .. code-block:: glsl
  185. float arr[] = { 0.0, 1.0, 0.5, -1.0 };
  186. for (int i = 0; i < arr.length(); i++) {
  187. // ...
  188. }
  189. .. note::
  190. 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.
  191. Global arrays
  192. ~~~~~~~~~~~~~
  193. You can declare arrays at global space like:
  194. .. code-block:: glsl
  195. shader_type spatial;
  196. const lowp vec3 v[1] = lowp vec3[1] ( vec3(0, 0, 1) );
  197. void fragment() {
  198. ALBEDO = v[0];
  199. }
  200. .. note::
  201. Global arrays have to be declared as global constants, otherwise they can be declared the same as local arrays.
  202. Constants
  203. ---------
  204. 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.
  205. .. code-block:: glsl
  206. const vec2 a = vec2(0.0, 1.0);
  207. vec2 b;
  208. a = b; // invalid
  209. b = a; // valid
  210. 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
  211. .. code-block:: glsl
  212. const vec2 V1 = vec2(1, 1), V2 = vec2(2, 2);
  213. Similar to variables, arrays can also be declared with ``const``.
  214. .. code-block:: glsl
  215. const float arr[] = { 1.0, 0.5, 0.0 };
  216. arr[0] = 1.0; // invalid
  217. COLOR.r = arr[0]; // valid
  218. Constants can be declared both globally (outside of any function) or locally (inside a function).
  219. 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.
  220. .. code-block:: glsl
  221. shader_type spatial;
  222. const float PI = 3.14159265358979323846;
  223. Structs
  224. -------
  225. Structs are compound types which can be used for better abstraction of shader code. You can declare them at the global scope like:
  226. .. code-block:: glsl
  227. struct PointLight {
  228. vec3 position;
  229. vec3 color;
  230. float intensity;
  231. };
  232. After declaration, you can instantiate and initialize them like:
  233. .. code-block:: glsl
  234. void fragment()
  235. {
  236. PointLight light;
  237. light.position = vec3(0.0);
  238. light.color = vec3(1.0, 0.0, 0.0);
  239. light.intensity = 0.5;
  240. }
  241. Or use struct constructor for same purpose:
  242. .. code-block:: glsl
  243. PointLight light = PointLight(vec3(0.0), vec3(1.0, 0.0, 0.0), 0.5);
  244. Structs may contain other struct or array, you can also instance them as global constant:
  245. .. code-block:: glsl
  246. shader_type spatial;
  247. ...
  248. struct Scene {
  249. PointLight lights[2];
  250. };
  251. 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)));
  252. void fragment()
  253. {
  254. ALBEDO = scene.lights[0].color;
  255. }
  256. You can also pass them to functions:
  257. .. code-block:: glsl
  258. shader_type canvas_item;
  259. ...
  260. Scene construct_scene(PointLight light1, PointLight light2) {
  261. return Scene({light1, light2});
  262. }
  263. void fragment()
  264. {
  265. 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;
  266. }
  267. Operators
  268. ---------
  269. Godot shading language supports the same set of operators as GLSL ES 3.0. Below is the list of them in precedence order:
  270. +-------------+------------------------+------------------+
  271. | Precedence | Class | Operator |
  272. +-------------+------------------------+------------------+
  273. | 1 (highest) | parenthetical grouping | **()** |
  274. +-------------+------------------------+------------------+
  275. | 2 | unary | **+, -, !, ~** |
  276. +-------------+------------------------+------------------+
  277. | 3 | multiplicative | **/, \*, %** |
  278. +-------------+------------------------+------------------+
  279. | 4 | additive | **+, -** |
  280. +-------------+------------------------+------------------+
  281. | 5 | bit-wise shift | **<<, >>** |
  282. +-------------+------------------------+------------------+
  283. | 6 | relational | **<, >, <=, >=** |
  284. +-------------+------------------------+------------------+
  285. | 7 | equality | **==, !=** |
  286. +-------------+------------------------+------------------+
  287. | 8 | bit-wise AND | **&** |
  288. +-------------+------------------------+------------------+
  289. | 9 | bit-wise exclusive OR | **^** |
  290. +-------------+------------------------+------------------+
  291. | 10 | bit-wise inclusive OR | **|** |
  292. +-------------+------------------------+------------------+
  293. | 11 | logical AND | **&&** |
  294. +-------------+------------------------+------------------+
  295. | 12 (lowest) | logical inclusive OR | **||** |
  296. +-------------+------------------------+------------------+
  297. Flow control
  298. ------------
  299. Godot Shading language supports the most common types of flow control:
  300. .. code-block:: glsl
  301. // if and else
  302. if (cond) {
  303. } else {
  304. }
  305. // switch
  306. switch(i) { // signed integer expression
  307. case -1:
  308. break;
  309. case 0:
  310. return; // break or return
  311. case 1: // pass-through
  312. case 2:
  313. break;
  314. //...
  315. default: // optional
  316. break;
  317. }
  318. // for loops
  319. for (int i = 0; i < 10; i++) {
  320. }
  321. // while
  322. while (true) {
  323. }
  324. // do while
  325. do {
  326. } while(true);
  327. Keep in mind that, in modern GPUs, an infinite loop can exist and can freeze your application (including editor).
  328. Godot can't protect you from this, so be careful not to make this mistake!
  329. .. warning::
  330. When exporting a GLES2 project to HTML5, WebGL 1.0 will be used. WebGL 1.0
  331. doesn't support dynamic loops, so shaders using those won't work there.
  332. Discarding
  333. ----------
  334. Fragment and light functions can use the **discard** keyword. If used, the fragment is discarded and nothing is written.
  335. Functions
  336. ---------
  337. It is possible to define functions in a Godot shader. They use the following syntax:
  338. .. code-block:: glsl
  339. ret_type func_name(args) {
  340. return ret_type; // if returning a value
  341. }
  342. // a more specific example:
  343. int sum2(int a, int b) {
  344. return a + b;
  345. }
  346. You can only use functions that have been defined above (higher in the editor) the function from which you are calling
  347. them.
  348. Function arguments can have special qualifiers:
  349. * **in**: Means the argument is only for reading (default).
  350. * **out**: Means the argument is only for writing.
  351. * **inout**: Means the argument is fully passed via reference.
  352. * **const**: Means the argument is a constant and cannot be changed, may be combined with **in** qualifier.
  353. Example below:
  354. .. code-block:: glsl
  355. void sum2(int a, int b, inout int result) {
  356. result = a + b;
  357. }
  358. Varyings
  359. ~~~~~~~~
  360. To send data from the vertex to the fragment processor function, *varyings* are used. They are set
  361. for every primitive vertex in the *vertex processor*, and the value is interpolated for every
  362. pixel in the fragment processor.
  363. .. code-block:: glsl
  364. shader_type spatial;
  365. varying vec3 some_color;
  366. void vertex() {
  367. some_color = NORMAL; // Make the normal the color.
  368. }
  369. void fragment() {
  370. ALBEDO = some_color;
  371. }
  372. Varying can also be an array:
  373. .. code-block:: glsl
  374. shader_type spatial;
  375. varying float var_arr[3];
  376. void vertex() {
  377. var_arr[0] = 1.0;
  378. var_arr[1] = 0.0;
  379. }
  380. void fragment() {
  381. ALBEDO = vec3(var_arr[0], var_arr[1], var_arr[2]); // red color
  382. }
  383. Interpolation qualifiers
  384. ~~~~~~~~~~~~~~~~~~~~~~~~
  385. Certain values are interpolated during the shading pipeline. You can modify how these interpolations
  386. are done by using *interpolation qualifiers*.
  387. .. code-block:: glsl
  388. shader_type spatial;
  389. varying flat vec3 our_color;
  390. void vertex() {
  391. our_color = COLOR.rgb;
  392. }
  393. void fragment() {
  394. ALBEDO = our_color;
  395. }
  396. There are two possible interpolation qualifiers:
  397. +-------------------+---------------------------------------------------------------------------------+
  398. | Qualifier | Description |
  399. +===================+=================================================================================+
  400. | **flat** | The value is not interpolated. |
  401. +-------------------+---------------------------------------------------------------------------------+
  402. | **smooth** | The value is interpolated in a perspective-correct fashion. This is the default.|
  403. +-------------------+---------------------------------------------------------------------------------+
  404. Uniforms
  405. ~~~~~~~~
  406. Passing values to shaders is possible. These are global to the whole shader and are called *uniforms*.
  407. When a shader is later assigned to a material, the uniforms will appear as editable parameters in it.
  408. Uniforms can't be written from within the shader.
  409. .. note::
  410. Uniform arrays are not implemented yet.
  411. .. code-block:: glsl
  412. shader_type spatial;
  413. uniform float some_value;
  414. You can set uniforms in the editor in the material. Or you can set them through GDScript:
  415. ::
  416. material.set_shader_param("some_value", some_value)
  417. .. note:: The first argument to ``set_shader_param`` is the name of the uniform in the shader. It
  418. must match *exactly* to the name of the uniform in the shader or else it will not be recognized.
  419. Any GLSL type except for *void* can be a uniform. Additionally, Godot provides optional shader hints
  420. to make the compiler understand for what the uniform is used.
  421. .. code-block:: glsl
  422. shader_type spatial;
  423. uniform vec4 color : hint_color;
  424. uniform float amount : hint_range(0, 1);
  425. uniform vec4 other_color : hint_color = vec4(1.0);
  426. 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.
  427. Full list of hints below:
  428. +----------------+------------------------------+-------------------------------------+
  429. | Type | Hint | Description |
  430. +================+==============================+=====================================+
  431. | **vec4** | hint_color | Used as color |
  432. +----------------+------------------------------+-------------------------------------+
  433. | **int, float** | hint_range(min, max[, step]) | Used as range (with min/max/step) |
  434. +----------------+------------------------------+-------------------------------------+
  435. | **sampler2D** | hint_albedo | Used as albedo color, default white |
  436. +----------------+------------------------------+-------------------------------------+
  437. | **sampler2D** | hint_black_albedo | Used as albedo color, default black |
  438. +----------------+------------------------------+-------------------------------------+
  439. | **sampler2D** | hint_normal | Used as normalmap |
  440. +----------------+------------------------------+-------------------------------------+
  441. | **sampler2D** | hint_white | As value, default to white. |
  442. +----------------+------------------------------+-------------------------------------+
  443. | **sampler2D** | hint_black | As value, default to black |
  444. +----------------+------------------------------+-------------------------------------+
  445. | **sampler2D** | hint_aniso | As flowmap, default to right. |
  446. +----------------+------------------------------+-------------------------------------+
  447. GDScript uses different variable types than GLSL does, so when passing variables from GDScript
  448. to shaders, Godot converts the type automatically. Below is a table of the corresponding types:
  449. +-----------------+-----------+
  450. | GDScript type | GLSL type |
  451. +=================+===========+
  452. | **bool** | **bool** |
  453. +-----------------+-----------+
  454. | **int** | **int** |
  455. +-----------------+-----------+
  456. | **float** | **float** |
  457. +-----------------+-----------+
  458. | **Vector2** | **vec2** |
  459. +-----------------+-----------+
  460. | **Vector3** | **vec3** |
  461. +-----------------+-----------+
  462. | **Color** | **vec4** |
  463. +-----------------+-----------+
  464. | **Transform** | **mat4** |
  465. +-----------------+-----------+
  466. | **Transform2D** | **mat4** |
  467. +-----------------+-----------+
  468. .. note:: Be careful when setting shader uniforms from GDScript, no error will be thrown if the
  469. type does not match. Your shader will just exhibit undefined behavior.
  470. Uniforms can also be assigned default values:
  471. .. code-block:: glsl
  472. shader_type spatial;
  473. uniform vec4 some_vector = vec4(0.0);
  474. uniform vec4 some_color : hint_color = vec4(1.0);
  475. Built-in functions
  476. ------------------
  477. A large number of built-in functions are supported, conforming to GLSL ES 3.0.
  478. When vec_type (float), vec_int_type, vec_uint_type, vec_bool_type nomenclature is used, it can be scalar or vector.
  479. .. note:: For a list of the functions that are not available in the GLES2 backend, please see the
  480. :ref:`Differences between GLES2 and GLES3 doc <doc_gles2_gles3_differences>`.
  481. +------------------------------------------------------------------------+---------------------------------------------------------------+
  482. | Function | Description |
  483. +========================================================================+===============================================================+
  484. | vec_type **radians** (vec_type degrees) | Convert degrees to radians |
  485. +------------------------------------------------------------------------+---------------------------------------------------------------+
  486. | vec_type **degrees** (vec_type radians) | Convert radians to degrees |
  487. +------------------------------------------------------------------------+---------------------------------------------------------------+
  488. | vec_type **sin** (vec_type x) | Sine |
  489. +------------------------------------------------------------------------+---------------------------------------------------------------+
  490. | vec_type **cos** (vec_type x) | Cosine |
  491. +------------------------------------------------------------------------+---------------------------------------------------------------+
  492. | vec_type **tan** (vec_type x) | Tangent |
  493. +------------------------------------------------------------------------+---------------------------------------------------------------+
  494. | vec_type **asin** (vec_type x) | Arcsine |
  495. +------------------------------------------------------------------------+---------------------------------------------------------------+
  496. | vec_type **acos** (vec_type x) | Arccosine |
  497. +------------------------------------------------------------------------+---------------------------------------------------------------+
  498. | vec_type **atan** (vec_type y_over_x) | Arctangent |
  499. +------------------------------------------------------------------------+---------------------------------------------------------------+
  500. | vec_type **atan** (vec_type y, vec_type x) | Arctangent to convert vector to angle |
  501. +------------------------------------------------------------------------+---------------------------------------------------------------+
  502. | vec_type **sinh** (vec_type x) | Hyperbolic sine |
  503. +------------------------------------------------------------------------+---------------------------------------------------------------+
  504. | vec_type **cosh** (vec_type x) | Hyperbolic cosine |
  505. +------------------------------------------------------------------------+---------------------------------------------------------------+
  506. | vec_type **tanh** (vec_type x) | Hyperbolic tangent |
  507. +------------------------------------------------------------------------+---------------------------------------------------------------+
  508. | vec_type **asinh** (vec_type x) | Inverse hyperbolic sine |
  509. +------------------------------------------------------------------------+---------------------------------------------------------------+
  510. | vec_type **acosh** (vec_type x) | Inverse hyperbolic cosine |
  511. +------------------------------------------------------------------------+---------------------------------------------------------------+
  512. | vec_type **atanh** (vec_type x) | Inverse hyperbolic tangent |
  513. +------------------------------------------------------------------------+---------------------------------------------------------------+
  514. | vec_type **pow** (vec_type x, vec_type y) | Power (undefined if ``x`` < 0 or if ``x`` = 0 and ``y`` <= 0) |
  515. +------------------------------------------------------------------------+---------------------------------------------------------------+
  516. | vec_type **exp** (vec_type x) | Base-e exponential |
  517. +------------------------------------------------------------------------+---------------------------------------------------------------+
  518. | vec_type **exp2** (vec_type x) | Base-2 exponential |
  519. +------------------------------------------------------------------------+---------------------------------------------------------------+
  520. | vec_type **log** (vec_type x) | Natural logarithm |
  521. +------------------------------------------------------------------------+---------------------------------------------------------------+
  522. | vec_type **log2** (vec_type x) | Base-2 logarithm |
  523. +------------------------------------------------------------------------+---------------------------------------------------------------+
  524. | vec_type **sqrt** (vec_type x) | Square root |
  525. +------------------------------------------------------------------------+---------------------------------------------------------------+
  526. | vec_type **inversesqrt** (vec_type x) | Inverse square root |
  527. +------------------------------------------------------------------------+---------------------------------------------------------------+
  528. | vec_type **abs** (vec_type x) | Absolute |
  529. +------------------------------------------------------------------------+---------------------------------------------------------------+
  530. | ivec_type **abs** (ivec_type x) | Absolute |
  531. +------------------------------------------------------------------------+---------------------------------------------------------------+
  532. | vec_type **sign** (vec_type x) | Sign |
  533. +------------------------------------------------------------------------+---------------------------------------------------------------+
  534. | ivec_type **sign** (ivec_type x) | Sign |
  535. +------------------------------------------------------------------------+---------------------------------------------------------------+
  536. | vec_type **floor** (vec_type x) | Floor |
  537. +------------------------------------------------------------------------+---------------------------------------------------------------+
  538. | vec_type **round** (vec_type x) | Round |
  539. +------------------------------------------------------------------------+---------------------------------------------------------------+
  540. | vec_type **roundEven** (vec_type x) | Round to the nearest even number |
  541. +------------------------------------------------------------------------+---------------------------------------------------------------+
  542. | vec_type **trunc** (vec_type x) | Truncation |
  543. +------------------------------------------------------------------------+---------------------------------------------------------------+
  544. | vec_type **ceil** (vec_type x) | Ceil |
  545. +------------------------------------------------------------------------+---------------------------------------------------------------+
  546. | vec_type **fract** (vec_type x) | Fractional |
  547. +------------------------------------------------------------------------+---------------------------------------------------------------+
  548. | vec_type **mod** (vec_type x, vec_type y) | Remainder |
  549. +------------------------------------------------------------------------+---------------------------------------------------------------+
  550. | vec_type **mod** (vec_type x , float y) | Remainder |
  551. +------------------------------------------------------------------------+---------------------------------------------------------------+
  552. | vec_type **modf** (vec_type x, out vec_type i) | Fractional of ``x``, with ``i`` as integer part |
  553. +------------------------------------------------------------------------+---------------------------------------------------------------+
  554. | vec_type **min** (vec_type a, vec_type b) | Minimum |
  555. +------------------------------------------------------------------------+---------------------------------------------------------------+
  556. | vec_type **max** (vec_type a, vec_type b) | Maximum |
  557. +------------------------------------------------------------------------+---------------------------------------------------------------+
  558. | vec_type **clamp** (vec_type x, vec_type min, vec_type max) | Clamp to ``min..max`` |
  559. +------------------------------------------------------------------------+---------------------------------------------------------------+
  560. | vec_type **mix** (float a, float b, float c) | Linear interpolate |
  561. +------------------------------------------------------------------------+---------------------------------------------------------------+
  562. | vec_type **mix** (vec_type a, vec_type b, float c) | Linear interpolate (scalar coefficient) |
  563. +------------------------------------------------------------------------+---------------------------------------------------------------+
  564. | vec_type **mix** (vec_type a, vec_type b, vec_type c) | Linear interpolate (vector coefficient) |
  565. +------------------------------------------------------------------------+---------------------------------------------------------------+
  566. | vec_type **mix** (vec_type a, vec_type b, bvec_type c) | Linear interpolate (boolean-vector selection) |
  567. +------------------------------------------------------------------------+---------------------------------------------------------------+
  568. | vec_type **fma** (vec_type a, vec_type b, vec_type c) | Performs a fused multiply-add operation: ``(a * b + c)`` |
  569. +------------------------------------------------------------------------+---------------------------------------------------------------+
  570. | vec_type **step** (vec_type a, vec_type b) | ``b[i] < a[i] ? 0.0 : 1.0`` |
  571. +------------------------------------------------------------------------+---------------------------------------------------------------+
  572. | vec_type **step** (float a, vec_type b) | ``b[i] < a ? 0.0 : 1.0`` |
  573. +------------------------------------------------------------------------+---------------------------------------------------------------+
  574. | vec_type **smoothstep** (vec_type a, vec_type b, vec_type c) | Hermite interpolate |
  575. +------------------------------------------------------------------------+---------------------------------------------------------------+
  576. | vec_type **smoothstep** (float a, float b, vec_type c) | Hermite interpolate |
  577. +------------------------------------------------------------------------+---------------------------------------------------------------+
  578. | bvec_type **isnan** (vec_type x) | Returns ``true`` if scalar or vector component is ``NaN`` |
  579. +------------------------------------------------------------------------+---------------------------------------------------------------+
  580. | bvec_type **isinf** (vec_type x) | Returns ``true`` if scalar or vector component is ``INF`` |
  581. +------------------------------------------------------------------------+---------------------------------------------------------------+
  582. | ivec_type **floatBitsToInt** (vec_type x) | Float->Int bit copying, no conversion |
  583. +------------------------------------------------------------------------+---------------------------------------------------------------+
  584. | uvec_type **floatBitsToUint** (vec_type x) | Float->UInt bit copying, no conversion |
  585. +------------------------------------------------------------------------+---------------------------------------------------------------+
  586. | vec_type **intBitsToFloat** (ivec_type x) | Int->Float bit copying, no conversion |
  587. +------------------------------------------------------------------------+---------------------------------------------------------------+
  588. | vec_type **uintBitsToFloat** (uvec_type x) | UInt->Float bit copying, no conversion |
  589. +------------------------------------------------------------------------+---------------------------------------------------------------+
  590. | float **length** (vec_type x) | Vector length |
  591. +------------------------------------------------------------------------+---------------------------------------------------------------+
  592. | float **distance** (vec_type a, vec_type b) | Distance between vectors i.e ``length(a - b)`` |
  593. +------------------------------------------------------------------------+---------------------------------------------------------------+
  594. | float **dot** (vec_type a, vec_type b) | Dot product |
  595. +------------------------------------------------------------------------+---------------------------------------------------------------+
  596. | vec3 **cross** (vec3 a, vec3 b) | Cross product |
  597. +------------------------------------------------------------------------+---------------------------------------------------------------+
  598. | vec_type **normalize** (vec_type x) | Normalize to unit length |
  599. +------------------------------------------------------------------------+---------------------------------------------------------------+
  600. | vec3 **reflect** (vec3 I, vec3 N) | Reflect |
  601. +------------------------------------------------------------------------+---------------------------------------------------------------+
  602. | vec3 **refract** (vec3 I, vec3 N, float eta) | Refract |
  603. +------------------------------------------------------------------------+---------------------------------------------------------------+
  604. | vec_type **faceforward** (vec_type N, vec_type I, vec_type Nref) | If ``dot(Nref, I)`` < 0, return N, otherwise –N |
  605. +------------------------------------------------------------------------+---------------------------------------------------------------+
  606. | mat_type **matrixCompMult** (mat_type x, mat_type y) | Matrix component multiplication |
  607. +------------------------------------------------------------------------+---------------------------------------------------------------+
  608. | mat_type **outerProduct** (vec_type column, vec_type row) | Matrix outer product |
  609. +------------------------------------------------------------------------+---------------------------------------------------------------+
  610. | mat_type **transpose** (mat_type m) | Transpose matrix |
  611. +------------------------------------------------------------------------+---------------------------------------------------------------+
  612. | float **determinant** (mat_type m) | Matrix determinant |
  613. +------------------------------------------------------------------------+---------------------------------------------------------------+
  614. | mat_type **inverse** (mat_type m) | Inverse matrix |
  615. +------------------------------------------------------------------------+---------------------------------------------------------------+
  616. | bvec_type **lessThan** (vec_type x, vec_type y) | Bool vector comparison on < int/uint/float vectors |
  617. +------------------------------------------------------------------------+---------------------------------------------------------------+
  618. | bvec_type **greaterThan** (vec_type x, vec_type y) | Bool vector comparison on > int/uint/float vectors |
  619. +------------------------------------------------------------------------+---------------------------------------------------------------+
  620. | bvec_type **lessThanEqual** (vec_type x, vec_type y) | Bool vector comparison on <= int/uint/float vectors |
  621. +------------------------------------------------------------------------+---------------------------------------------------------------+
  622. | bvec_type **greaterThanEqual** (vec_type x, vec_type y) | Bool vector comparison on >= int/uint/float vectors |
  623. +------------------------------------------------------------------------+---------------------------------------------------------------+
  624. | bvec_type **equal** (vec_type x, vec_type y) | Bool vector comparison on == int/uint/float vectors |
  625. +------------------------------------------------------------------------+---------------------------------------------------------------+
  626. | bvec_type **notEqual** (vec_type x, vec_type y) | Bool vector comparison on != int/uint/float vectors |
  627. +------------------------------------------------------------------------+---------------------------------------------------------------+
  628. | bool **any** (bvec_type x) | Any component is ``true`` |
  629. +------------------------------------------------------------------------+---------------------------------------------------------------+
  630. | bool **all** (bvec_type x) | All components are ``true`` |
  631. +------------------------------------------------------------------------+---------------------------------------------------------------+
  632. | bvec_type **not** (bvec_type x) | Invert boolean vector |
  633. +------------------------------------------------------------------------+---------------------------------------------------------------+
  634. | ivec2 **textureSize** (sampler2D_type s, int lod) | Get the size of a 2D texture |
  635. +------------------------------------------------------------------------+---------------------------------------------------------------+
  636. | ivec3 **textureSize** (sampler2DArray_type s, int lod) | Get the size of a 2D texture array |
  637. +------------------------------------------------------------------------+---------------------------------------------------------------+
  638. | ivec3 **textureSize** (sampler3D s, int lod) | Get the size of a 3D texture |
  639. +------------------------------------------------------------------------+---------------------------------------------------------------+
  640. | ivec2 **textureSize** (samplerCube s, int lod) | Get the size of a cubemap texture |
  641. +------------------------------------------------------------------------+---------------------------------------------------------------+
  642. | vec4_type **texture** (sampler2D_type s, vec2 uv [, float bias]) | Perform a 2D texture read |
  643. +------------------------------------------------------------------------+---------------------------------------------------------------+
  644. | vec4_type **texture** (sampler2DArray_type s, vec3 uv [, float bias]) | Perform a 2D texture array read |
  645. +------------------------------------------------------------------------+---------------------------------------------------------------+
  646. | vec4_type **texture** (sampler3D_type s, vec3 uv [, float bias]) | Perform a 3D texture read |
  647. +------------------------------------------------------------------------+---------------------------------------------------------------+
  648. | vec4 **texture** (samplerCube s, vec3 uv [, float bias]) | Perform a cubemap texture read |
  649. +------------------------------------------------------------------------+---------------------------------------------------------------+
  650. | vec4_type **textureProj** (sampler2D_type s, vec3 uv [, float bias]) | Perform a 2D texture read with projection |
  651. +------------------------------------------------------------------------+---------------------------------------------------------------+
  652. | vec4_type **textureProj** (sampler2D_type s, vec4 uv [, float bias]) | Perform a 2D texture read with projection |
  653. +------------------------------------------------------------------------+---------------------------------------------------------------+
  654. | vec4_type **textureProj** (sampler3D_type s, vec4 uv [, float bias]) | Perform a 3D texture read with projection |
  655. +------------------------------------------------------------------------+---------------------------------------------------------------+
  656. | vec4_type **textureLod** (sampler2D_type s, vec2 uv, float lod) | Perform a 2D texture read at custom mipmap |
  657. +------------------------------------------------------------------------+---------------------------------------------------------------+
  658. | vec4_type **textureLod** (sampler2DArray_type s, vec3 uv, float lod) | Perform a 2D texture array read at custom mipmap |
  659. +------------------------------------------------------------------------+---------------------------------------------------------------+
  660. | vec4_type **textureLod** (sampler3D_type s, vec3 uv, float lod) | Perform a 3D texture read at custom mipmap |
  661. +------------------------------------------------------------------------+---------------------------------------------------------------+
  662. | vec4 **textureLod** (samplerCube s, vec3 uv, float lod) | Perform a 3D texture read at custom mipmap |
  663. +------------------------------------------------------------------------+---------------------------------------------------------------+
  664. | vec4_type **textureProjLod** (sampler2D_type s, vec3 uv, float lod) | Perform a 2D texture read with projection/LOD |
  665. +------------------------------------------------------------------------+---------------------------------------------------------------+
  666. | vec4_type **textureProjLod** (sampler2D_type s, vec4 uv, float lod) | Perform a 2D texture read with projection/LOD |
  667. +------------------------------------------------------------------------+---------------------------------------------------------------+
  668. | vec4_type **textureProjLod** (sampler3D_type s, vec4 uv, float lod) | Perform a 3D texture read with projection/LOD |
  669. +------------------------------------------------------------------------+---------------------------------------------------------------+
  670. | vec4_type **texelFetch** (sampler2D_type s, ivec2 uv, int lod) | Fetch a single texel using integer coordinates |
  671. +------------------------------------------------------------------------+---------------------------------------------------------------+
  672. | vec4_type **texelFetch** (sampler2DArray_type s, ivec3 uv, int lod) | Fetch a single texel using integer coordinates |
  673. +------------------------------------------------------------------------+---------------------------------------------------------------+
  674. | vec4_type **texelFetch** (sampler3D_type s, ivec3 uv, int lod) | Fetch a single texel using integer coordinates |
  675. +------------------------------------------------------------------------+---------------------------------------------------------------+
  676. | vec_type **dFdx** (vec_type p) | Derivative in ``x`` using local differencing |
  677. +------------------------------------------------------------------------+---------------------------------------------------------------+
  678. | vec_type **dFdy** (vec_type p) | Derivative in ``y`` using local differencing |
  679. +------------------------------------------------------------------------+---------------------------------------------------------------+
  680. | vec_type **fwidth** (vec_type p) | Sum of absolute derivative in ``x`` and ``y`` |
  681. +------------------------------------------------------------------------+---------------------------------------------------------------+