shading_language.rst 76 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. .. _doc_shading_language:
  2. Shading language
  3. ================
  4. Introduction
  5. ------------
  6. Godot uses a shading language very similar to GLSL ES 3.0. Most datatypes and functions are supported,
  7. and the remaining will likely be added over time.
  8. Unlike the shader language in Godot 2.x, this implementation is much closer to the original.
  9. Shader Types
  10. ------------
  11. Instead of supplying a general purpose configuration, Godot Shading Language must
  12. specify what shader is intended for. Depending on the type, different render
  13. modes, built-in variables and processing functions are supported.
  14. Any shader needs a first line specifying this type, in the following format:
  15. .. code-block:: glsl
  16. shader_type <type>;
  17. Valid types are:
  18. * "spatial": For 3D rendering.
  19. * "canvas_item": For 2D rendering.
  20. * "particles": For particle systems.
  21. Render Modes
  22. ------------
  23. Different shader types support different render modes. They are optional but, if specified, must
  24. be after the *shader_type*. Example syntax is:
  25. .. code-block:: glsl
  26. shader_type spatial;
  27. render_mode unshaded, cull_disabled;
  28. Data types:
  29. -----------
  30. Most GLSL ES 3.0 datatypes are supported. Following is the list:
  31. +-----------------+---------------------------------------------------------------------------+
  32. | Type | Description |
  33. +=================+===========================================================================+
  34. | **void** | Void datatype, useful only for functions that return nothing. |
  35. +-----------------+---------------------------------------------------------------------------+
  36. | **bool** | Boolean datatype, can only contain "true" or "false" |
  37. +-----------------+---------------------------------------------------------------------------+
  38. | **bvec2** | Two component vector of booleans. |
  39. +-----------------+---------------------------------------------------------------------------+
  40. | **bvec3** | Three component vector of booleans. |
  41. +-----------------+---------------------------------------------------------------------------+
  42. | **bvec4** | Four component vector of booleans. |
  43. +-----------------+---------------------------------------------------------------------------+
  44. | **int** | Signed scalar integer. |
  45. +-----------------+---------------------------------------------------------------------------+
  46. | **ivec2** | Two component vector of signed integers. |
  47. +-----------------+---------------------------------------------------------------------------+
  48. | **ivec3** | Three component vector of signed integers. |
  49. +-----------------+---------------------------------------------------------------------------+
  50. | **ivec4** | Four component vector of signed integers. |
  51. +-----------------+---------------------------------------------------------------------------+
  52. | **uint** | Unsigned scalar integer, can't contain negative numbers. |
  53. +-----------------+---------------------------------------------------------------------------+
  54. | **uvec2** | Two component vector of unsigned integers. |
  55. +-----------------+---------------------------------------------------------------------------+
  56. | **uvec3** | Three component vector of unsigned integers. |
  57. +-----------------+---------------------------------------------------------------------------+
  58. | **uvec4** | Four component vector of unsigned integers. |
  59. +-----------------+---------------------------------------------------------------------------+
  60. | **float** | Floating point scalar. |
  61. +-----------------+---------------------------------------------------------------------------+
  62. | **vec2** | Two component vector of floating point values. |
  63. +-----------------+---------------------------------------------------------------------------+
  64. | **vec3** | Three component vector of floating point values. |
  65. +-----------------+---------------------------------------------------------------------------+
  66. | **vec4** | Four component vector of floating point values. |
  67. +-----------------+---------------------------------------------------------------------------+
  68. | **mat2** | 2x2 matrix, in column major order. |
  69. +-----------------+---------------------------------------------------------------------------+
  70. | **mat3** | 3x3 matrix, in column major order. |
  71. +-----------------+---------------------------------------------------------------------------+
  72. | **mat4** | 4x4 matrix, in column major order. |
  73. +-----------------+---------------------------------------------------------------------------+
  74. | **sampler2D** | Sampler type, for binding 2D textures, which are read as float. |
  75. +-----------------+---------------------------------------------------------------------------+
  76. | **isampler2D** | Sampler type for binding 2D textures, which are read as signed integer. |
  77. +-----------------+---------------------------------------------------------------------------+
  78. | **usampler2D** | Sampler type for binding 2D textures, which are read as unsigned integer. |
  79. +-----------------+---------------------------------------------------------------------------+
  80. | **samplerCube** | Sampler type for binding Cubemaps, which are read as floats. |
  81. +-----------------+---------------------------------------------------------------------------+
  82. Casting
  83. ~~~~~~~
  84. Just like GLSL ES 3.0, implicit casting is not allowed between scalars and vectors of the same size but different type.
  85. Casting of types of different size is also not allowed. Conversion must be done explicitly via constructors.
  86. Example:
  87. .. code-block:: glsl
  88. float a = 2; // valid
  89. float a = 2.0; // valid
  90. float a = float(2); // valid
  91. Default integer constants are signed, so casting is always needed to convert to unsigned:
  92. .. code-block:: glsl
  93. int a = 2; // valid
  94. uint a = 2; // invalid
  95. uint a = uint(2); // valid
  96. Members
  97. ~~~~~~~
  98. Individual scalar members of vector types are accessed via the "x", "y", "z" and "w" members. Alternatively, using "r", "g", "b" and "a" also works and is equivalent.
  99. Use whatever fits best for your use case.
  100. For matrices, use [idx] indexing syntax to access each vector.
  101. Constructing
  102. ~~~~~~~~~~~~
  103. Construction of vector types must always pass:
  104. .. code-block:: glsl
  105. // The required amount of scalars
  106. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  107. // Complementary vectors and/or scalars
  108. vec4 a = vec4(vec2(0.0, 1.0), vec2(2.0, 3.0));
  109. vec4 a = vec4(vec3(0.0, 1.0, 2.0), 3.0);
  110. // A single scalar for the whole vector
  111. vec4 a = vec4(0.0);
  112. Swizzling
  113. ~~~~~~~~~
  114. It is possible to obtain any combination of them in any order, as long as the result is another vector type (or scalar).
  115. This is easier shown than explained:
  116. .. code-block:: glsl
  117. vec4 a = vec4(0.0, 1.0, 2.0, 3.0);
  118. vec3 b = a.rgb; // Creates a vec3 with vec4 components
  119. vec3 b = a.aaa; // Also valid, creates a vec3 and fills it with "a".
  120. vec3 b = a.bgr; // Order does not matter
  121. vec3 b = a.xyz; // Also rgba, xyzw are equivalent
  122. float c = b.w; // Invalid, because "w" is not present in vec3 b
  123. Precision
  124. ~~~~~~~~~
  125. It is possible to add precision modifiers to datatypes, use them for uniforms, variables, arguments and varyings:
  126. .. code-block:: glsl
  127. lowp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // low precision, usually 8 bits per component mapped to 0-1
  128. mediump vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // medium precision, usually 16 bits or half float
  129. highp vec4 a = vec4(0.0, 1.0, 2.0, 3.0); // high precision, uses full float or integer range (default)
  130. Using lower precision for some operations can speed up the math involved (at the cost of, of course, less precision).
  131. This is rarely needed in the vertex shader (where full precision is needed most of the time), but often needed in the fragment one.
  132. Keep in mind that some architectures (mainly mobile) benefit a lot from this, but are also restricted (conversion between precisions has a cost).
  133. Please read the relevant documentation on the target architecture to find out more. In all honesty though, mobile drivers are really buggy
  134. so just stay out of trouble and make simple shaders without specifying precision unless you *really* need to.
  135. Operators:
  136. ----------
  137. Godot shading language supports the same set of operators as GLSL ES 3.0. Below is the list of them in precedence order:
  138. +-------------+-----------------------+--------------------+
  139. | Precedence | Class | Operator |
  140. +-------------+-----------------------+--------------------+
  141. | 1 (highest) | parenthetical grouping| **()** |
  142. +-------------+-----------------------+--------------------+
  143. | 2 | unary | **+, -, !, ~** |
  144. +-------------+-----------------------+--------------------+
  145. | 3 | multiplicative | **/, \*, %** |
  146. +-------------+-----------------------+--------------------+
  147. | 4 | additive | **+, -** |
  148. +-------------+-----------------------+--------------------+
  149. | 5 | bit-wise shift | **<<, >>** |
  150. +-------------+-----------------------+--------------------+
  151. | 6 | relational | **<, >, <=, >=** |
  152. +-------------+-----------------------+--------------------+
  153. | 7 | equality | **==, !=** |
  154. +-------------+-----------------------+--------------------+
  155. | 8 | bit-wise and | **&** |
  156. +-------------+-----------------------+--------------------+
  157. | 9 | bit-wise exclusive or | **^** |
  158. +-------------+-----------------------+--------------------+
  159. | 10 | bit-wise inclusive or | **|** |
  160. +-------------+-----------------------+--------------------+
  161. | 11 | logical and | **&&** |
  162. +-------------+-----------------------+--------------------+
  163. | 12 (lowest) | logical inclusive or | **||** |
  164. +-------------+-----------------------+--------------------+
  165. Flow Control
  166. ------------
  167. Godot Shading language supports the most common types of flow control:
  168. .. code-block:: glsl
  169. // if and else
  170. if (cond) {
  171. } else {
  172. }
  173. // for loops
  174. for (int i = 0; i < 10; i++) {
  175. }
  176. // while
  177. while (true) {
  178. }
  179. Keep in mind that, in modern GPUs, an infinite loop can exist and can freeze your application (including editor).
  180. Godot can't protect you from this, so be careful to not make this mistake!
  181. Discarding
  182. -----------
  183. Fragment and light functions can use the *discard* keyword. If used, the fragment is discarded and nothing is written.
  184. Functions
  185. ---------
  186. It's possible to define any function in a Godot shader. They take the following syntax:
  187. .. code-block:: glsl
  188. ret_type func_name(args) {
  189. return ret_type; // if returning a value
  190. }
  191. // a better example:
  192. int sum2(int a, int b) {
  193. return a + b;
  194. }
  195. Functions can be used from any other function that is below it.
  196. Function argument can have special qualifiers:
  197. * **in**: Means the argument is only for reading (default).
  198. * **out**: Means the argument is only for writing.
  199. * **inout**: Means the argument is fully passed via reference.
  200. Example below:
  201. .. code-block:: glsl
  202. void sum2(int a, int b, inout int result) {
  203. result = a + b;
  204. }
  205. Processor Functions
  206. -------------------
  207. Depending on shader type, processor functions may be available to optionally override.
  208. For "spatial" and "canvas_item", it is possible to override "vertex", "fragment" and "light".
  209. For "particles", only "vertex" can be overridden.
  210. Vertex Processor
  211. ~~~~~~~~~~~~~~~~~
  212. The "vertex" processing function is called for every vertex, 2D or 3D. For particles, it's called for every
  213. particle.
  214. Depending on shader type, a different set of built-in inputs and outputs are provided. In general,
  215. vertex functions are not that commonly used.
  216. .. code-block:: glsl
  217. shader_type spatial;
  218. void vertex() {
  219. VERTEX.x += sin(TIME); // offset vertex x by sine function on time elapsed
  220. }
  221. Fragment Processor
  222. ~~~~~~~~~~~~~~~~~~
  223. The "fragment" processor is used to set up the Godot material parameters per pixel. This code
  224. runs on every visible pixel the object or primitive is drawn to.
  225. .. code-block:: glsl
  226. shader_type spatial;
  227. void fragment() {
  228. ALBEDO = vec3(1.0, 0.0, 0.0); // use red for material albedo
  229. }
  230. Light Processor
  231. ~~~~~~~~~~~~~~~
  232. The "light" processor runs per pixel too, but also runs for every light that affects the object (
  233. and does not run if no lights affect the object).
  234. .. code-block:: glsl
  235. shader_type spatial;
  236. void light() {
  237. COLOR = vec3(0.0, 1.0, 0.0);
  238. }
  239. Varyings
  240. ~~~~~~~~
  241. To send data from vertex to fragment shader, *varyings* are used. They are set for every primitive vertex
  242. in the *vertex processor*, and the value is interpolated (and perspective corrected) when reaching every
  243. pixel in the fragment processor.
  244. .. code-block:: glsl
  245. shader_type spatial;
  246. varying vec3 some_color;
  247. void vertex() {
  248. some_color = NORMAL; // make the normal the color
  249. }
  250. void fragment() {
  251. ALBEDO = some_color;
  252. }
  253. Uniforms
  254. ~~~~~~~~
  255. Passing values to shaders is possible. These are global to the whole shader and called *uniforms*.
  256. When a shader is later assigned to a material, the uniforms will appear as editable parameters on it.
  257. Uniforms can't be written from within the shader.
  258. .. code-block:: glsl
  259. shader_type spatial;
  260. uniform float some_value;
  261. Any type except for *void* can be a uniform. Additionally, Godot provides optional shader hints
  262. to make the compiler understand what the uniform is used for.
  263. .. code-block:: glsl
  264. shader_type spatial;
  265. uniform vec4 color : hint_color;
  266. uniform float amount : hint_range(0, 1);
  267. Full list of hints below:
  268. +----------------+-------------------------------+-------------------------------------+
  269. | Type | Hint | Description |
  270. +================+===============================+=====================================+
  271. | **vec4** | hint_color | Used as color |
  272. +----------------+-------------------------------+-------------------------------------+
  273. | **int, float** | hint_range(min,max [,step] ) | Used as range (with min/max/step) |
  274. +----------------+-------------------------------+-------------------------------------+
  275. | **sampler2D** | hint_albedo | Used as albedo color, default white |
  276. +----------------+-------------------------------+-------------------------------------+
  277. | **sampler2D** | hint_black_albedo | Used as albedo color, default black |
  278. +----------------+-------------------------------+-------------------------------------+
  279. | **sampler2D** | hint_normal | Used as normalmap |
  280. +----------------+-------------------------------+-------------------------------------+
  281. | **sampler2D** | hint_white | As value, default to white. |
  282. +----------------+-------------------------------+-------------------------------------+
  283. | **sampler2D** | hint_black | As value, default to black |
  284. +----------------+-------------------------------+-------------------------------------+
  285. | **sampler2D** | hint_aniso | As flowmap, default to right. |
  286. +----------------+-------------------------------+-------------------------------------+
  287. As Godot 3D engine renders in linear color space, it's important to understand that textures
  288. that are supplied as color (ie, albedo) need to be specified as such for proper SRGB->linear
  289. conversion.
  290. Uniforms can also be assigned default values:
  291. .. code-block:: glsl
  292. shader_type spatial;
  293. uniform vec4 some_vector = vec4(0.0);
  294. Built-in Functions
  295. ------------------
  296. A large number of built-in functions are supported, conforming mostly to GLSL ES 3.0.
  297. When vec_type (float), vec_int_type, vec_uint_type, vec_bool_type nomenclature is used, it can be scalar or vector.
  298. +-----------------------------------------------------------------------+---------------------------------------------+
  299. | Function | Description |
  300. +=======================================================================+=============================================+
  301. | float **sin** ( float ) | Sine |
  302. +-----------------------------------------------------------------------+---------------------------------------------+
  303. | float **cos** ( float ) | Cosine |
  304. +-----------------------------------------------------------------------+---------------------------------------------+
  305. | float **tan** ( float ) | Tangent |
  306. +-----------------------------------------------------------------------+---------------------------------------------+
  307. | float **asin** ( float ) | arc-Sine |
  308. +-----------------------------------------------------------------------+---------------------------------------------+
  309. | float **acos** ( float ) | arc-Cosine |
  310. +-----------------------------------------------------------------------+---------------------------------------------+
  311. | float **atan** ( float ) | arc-Tangent |
  312. +-----------------------------------------------------------------------+---------------------------------------------+
  313. | float **atan2** ( float x, float y) | arc-Tangent to convert vector to angle |
  314. +-----------------------------------------------------------------------+---------------------------------------------+
  315. | float **sinh** ( float ) | Hyperbolic-Sine |
  316. +-----------------------------------------------------------------------+---------------------------------------------+
  317. | float **cosh** ( float ) | Hyperbolic-Cosine |
  318. +-----------------------------------------------------------------------+---------------------------------------------+
  319. | float **tanh** ( float ) | Hyperbolic-Tangent |
  320. +-----------------------------------------------------------------------+---------------------------------------------+
  321. | vec\_type **pow** ( float x, float y) | Power, x elevated to y |
  322. +-----------------------------------------------------------------------+---------------------------------------------+
  323. | vec\_type **pow** ( vec\_type, vec\_type ) | Power (Vec. Exponent) |
  324. +-----------------------------------------------------------------------+---------------------------------------------+
  325. | vec\_type **exp** ( vec\_type ) | Base-e Exponential |
  326. +-----------------------------------------------------------------------+---------------------------------------------+
  327. | vec\_type **log** ( vec\_type ) | Natural Logarithm |
  328. +-----------------------------------------------------------------------+---------------------------------------------+
  329. | vec\_type **sqrt** ( vec\_type ) | Square Root |
  330. +-----------------------------------------------------------------------+---------------------------------------------+
  331. | vec\_type **inversesqrt** ( vec\_type ) | Inverse Square Root |
  332. +-----------------------------------------------------------------------+---------------------------------------------+
  333. | vec\_type **abs** ( vec\_type ) | Absolute |
  334. +-----------------------------------------------------------------------+---------------------------------------------+
  335. | vec\_type **sign** ( vec\_type ) | Sign |
  336. +-----------------------------------------------------------------------+---------------------------------------------+
  337. | vec\_type **floor** ( vec\_type ) | Floor |
  338. +-----------------------------------------------------------------------+---------------------------------------------+
  339. | vec\_type **round** ( vec\_type ) | Round |
  340. +-----------------------------------------------------------------------+---------------------------------------------+
  341. | vec\_type **trunc** ( vec\_type ) | Trunc |
  342. +-----------------------------------------------------------------------+---------------------------------------------+
  343. | vec\_type **ceil** ( vec\_type ) | Ceiling |
  344. +-----------------------------------------------------------------------+---------------------------------------------+
  345. | vec\_type **fract** ( vec\_type ) | Fractional |
  346. +-----------------------------------------------------------------------+---------------------------------------------+
  347. | vec\_type **mod** ( vec\_type,vec\_type ) | Remainder |
  348. +-----------------------------------------------------------------------+---------------------------------------------+
  349. | vec\_type **modf** ( vec\_type x,out vec\_type i) | Fractional of x, with i has integer part |
  350. +-----------------------------------------------------------------------+---------------------------------------------+
  351. | vec\_type **min** ( vec\_type,vec\_type ) | Minimum |
  352. +-----------------------------------------------------------------------+---------------------------------------------+
  353. | vec\_type **max** ( vec\_type,vec\_type ) | Maximum |
  354. +-----------------------------------------------------------------------+---------------------------------------------+
  355. | vec\_type **clamp** ( vec\_type value,vec\_type min, vec\_type max ) | Clamp to Min-Max |
  356. +-----------------------------------------------------------------------+---------------------------------------------+
  357. | vec\_type **mix** ( vec\_type a,vec\_type b, float c ) | Linear Interpolate |
  358. +-----------------------------------------------------------------------+---------------------------------------------+
  359. | vec\_type **mix** ( vec\_type a,vec\_type b, vec\_type c ) | Linear Interpolate (Vector Coef.) |
  360. +-----------------------------------------------------------------------+---------------------------------------------+
  361. | vec\_type **step** ( vec\_type a,vec\_type b) | \` a[i] < b[i] ? 0.0 : 1.0\` |
  362. +-----------------------------------------------------------------------+---------------------------------------------+
  363. | vec\_type **smoothstep** ( vec\_type a,vec\_type b,vec\_type c) | |
  364. +-----------------------------------------------------------------------+---------------------------------------------+
  365. | vec_bool_type **isnan** ( vec\_type ) | scalar, or vector component being nan |
  366. +-----------------------------------------------------------------------+---------------------------------------------+
  367. | vec_bool_type **isinf** ( vec\_type ) | scalar, or vector component being inf |
  368. +-----------------------------------------------------------------------+---------------------------------------------+
  369. | vec_int_type **floatBitsToInt** ( vec_type ) | Float->Int bit copying, no conversion |
  370. +-----------------------------------------------------------------------+---------------------------------------------+
  371. | vec_uint_type **floatBitsToUInt** ( vec_type ) | Float->UInt bit copying, no conversion |
  372. +-----------------------------------------------------------------------+---------------------------------------------+
  373. | vec_type **intBitsToFloat** ( vec_int_type ) | Int->Float bit copying, no conversion |
  374. +-----------------------------------------------------------------------+---------------------------------------------+
  375. | vec_type **uintBitsToFloat** ( vec_uint_type ) | UInt->Float bit copying, no conversion |
  376. +-----------------------------------------------------------------------+---------------------------------------------+
  377. | float **length** ( vec\_type ) | Vector Length |
  378. +-----------------------------------------------------------------------+---------------------------------------------+
  379. | float **distance** ( vec\_type, vec\_type ) | Distance between vector. |
  380. +-----------------------------------------------------------------------+---------------------------------------------+
  381. | float **dot** ( vec\_type, vec\_type ) | Dot Product |
  382. +-----------------------------------------------------------------------+---------------------------------------------+
  383. | vec3 **cross** ( vec3, vec3 ) | Cross Product |
  384. +-----------------------------------------------------------------------+---------------------------------------------+
  385. | vec\_type **normalize** ( vec\_type ) | Normalize to unit length |
  386. +-----------------------------------------------------------------------+---------------------------------------------+
  387. | vec3 **reflect** ( vec3, vec3 ) | Reflect |
  388. +-----------------------------------------------------------------------+---------------------------------------------+
  389. | vec3 **refract** ( vec3, vec3 ) | Refract |
  390. +-----------------------------------------------------------------------+---------------------------------------------+
  391. | vec_type **faceforward** ( vec_type N, vec_type I, vec_type NRef) | If dot(Nref, I) < 0 return N, otherwise –N |
  392. +-----------------------------------------------------------------------+---------------------------------------------+
  393. | mat_type **matrixCompMult** ( mat_type, mat_type ) | Matrix Component Multiplication |
  394. +-----------------------------------------------------------------------+---------------------------------------------+
  395. | mat_type **outerProduct** ( vec_type, vec_type ) | Matrix Outer Product |
  396. +-----------------------------------------------------------------------+---------------------------------------------+
  397. | mat_type **transpose** ( mat_type ) | Transpose Matrix |
  398. +-----------------------------------------------------------------------+---------------------------------------------+
  399. | float **determinant** ( mat_type ) | Matrix Determinant |
  400. +-----------------------------------------------------------------------+---------------------------------------------+
  401. | mat_type **inverse** ( mat_type ) | Inverse Matrix |
  402. +-----------------------------------------------------------------------+---------------------------------------------+
  403. | vec\_bool_type **lessThan** ( vec_scalar_type ) | Bool vector cmp on < int/uint/float vectors |
  404. +-----------------------------------------------------------------------+---------------------------------------------+
  405. | vec\_bool_type **greaterThan** ( vec_scalar_type ) | Bool vector cmp on > int/uint/float vectors |
  406. +-----------------------------------------------------------------------+---------------------------------------------+
  407. | vec\_bool_type **lessThanEqual** ( vec_scalar_type ) | Bool vector cmp on <= int/uint/float vectors|
  408. +-----------------------------------------------------------------------+---------------------------------------------+
  409. | vec\_bool_type **greaterThanEqual** ( vec_scalar_type ) | Bool vector cmp on >= int/uint/float vectors|
  410. +-----------------------------------------------------------------------+---------------------------------------------+
  411. | vec\_bool_type **equal** ( vec_scalar_type ) | Bool vector cmp on == int/uint/float vectors|
  412. +-----------------------------------------------------------------------+---------------------------------------------+
  413. | vec\_bool_type **notEqual** ( vec_scalar_type ) | Bool vector cmp on != int/uint/float vectors|
  414. +-----------------------------------------------------------------------+---------------------------------------------+
  415. | bool **any** ( vec_bool_type ) | Any component is true |
  416. +-----------------------------------------------------------------------+---------------------------------------------+
  417. | bool **all** ( vec_bool_type ) | All components are true |
  418. +-----------------------------------------------------------------------+---------------------------------------------+
  419. | bool **not** ( vec_bool_type ) | No components are true |
  420. +-----------------------------------------------------------------------+---------------------------------------------+
  421. | ivec2 **textureSize** ( sampler2D_type s, int lod ) | Get the size of a texture |
  422. +-----------------------------------------------------------------------+---------------------------------------------+
  423. | ivec2 **textureSize** ( samplerCube s, int lod ) | Get the size of a cubemap |
  424. +-----------------------------------------------------------------------+---------------------------------------------+
  425. | vec4_type **texture** ( sampler2D_type s, vec2 uv [, float bias]) | Perform a 2D texture read |
  426. +-----------------------------------------------------------------------+---------------------------------------------+
  427. | vec4_type **texture** ( samplerCube s, vec3 uv [, float bias]) | Perform a Cube texture read |
  428. +-----------------------------------------------------------------------+---------------------------------------------+
  429. | vec4_type **textureProj** ( sampler2d_type s, vec3 uv [, float bias]) | Perform a texture read with projection |
  430. +-----------------------------------------------------------------------+---------------------------------------------+
  431. | vec4_type **textureProj** ( sampler2d_type s, vec4 uv [, float bias]) | Perform a texture read with projection |
  432. +-----------------------------------------------------------------------+---------------------------------------------+
  433. | vec4_type **textureLod** ( sampler2D_type s, vec2 uv , float lod) | Perform a 2D texture read at custom mipmap |
  434. +-----------------------------------------------------------------------+---------------------------------------------+
  435. | vec4_type **textureProjLod** ( sampler2d_type s, vec3 uv , float lod) | Perform a texture read with projection/lod |
  436. +-----------------------------------------------------------------------+---------------------------------------------+
  437. | vec4_type **textureProjLod** ( sampler2d_type s, vec4 uv , float lod) | Perform a texture read with projection/lod |
  438. +-----------------------------------------------------------------------+---------------------------------------------+
  439. | vec_type **texelFetch** ( samplerCube s, ivec2 uv, int lod ) | Fetch a single texel using integer coords |
  440. +-----------------------------------------------------------------------+---------------------------------------------+
  441. | vec_type **dFdx** ( vec_type ) | Derivative in x using local differencing |
  442. +-----------------------------------------------------------------------+---------------------------------------------+
  443. | vec_type **dFdy** ( vec_type ) | Derivative in y using local differencing |
  444. +-----------------------------------------------------------------------+---------------------------------------------+
  445. | vec_type **fwidth** ( vec_type ) | Sum of absolute derivative in x and y |
  446. +-----------------------------------------------------------------------+---------------------------------------------+
  447. Shader Types In-Depth
  448. ---------------------
  449. Spatial
  450. ~~~~~~~
  451. Accepted render modes and built-ins for "shader_type spatial;".
  452. Render Modes
  453. ^^^^^^^^^^^^
  454. +---------------------------------+----------------------------------------------------------------------+
  455. | Render Mode | Description |
  456. +=================================+======================================================================+
  457. | **blend_mix** | Mix blend mode (alpha is transparency), default. |
  458. +---------------------------------+----------------------------------------------------------------------+
  459. | **blend_add** | Additive blend mode. |
  460. +---------------------------------+----------------------------------------------------------------------+
  461. | **blend_sub** | Substractive blend mode. |
  462. +---------------------------------+----------------------------------------------------------------------+
  463. | **blend_mul** | Multiplicative blend mode. |
  464. +---------------------------------+----------------------------------------------------------------------+
  465. | **depth_draw_opaque** | Only draw depth for opaque geometry (not transparent). |
  466. +---------------------------------+----------------------------------------------------------------------+
  467. | **depth_draw_always** | Always draw depth (opaque and transparent). |
  468. +---------------------------------+----------------------------------------------------------------------+
  469. | **depth_draw_never** | Never draw depth. |
  470. +---------------------------------+----------------------------------------------------------------------+
  471. | **depth_draw_alpha_prepass** | Do opaque depth pre-pass for transparent geometry. |
  472. +---------------------------------+----------------------------------------------------------------------+
  473. | **depth_test_disable** | Disable depth testing. |
  474. +---------------------------------+----------------------------------------------------------------------+
  475. | **cull_front** | Cull front-faces. |
  476. +---------------------------------+----------------------------------------------------------------------+
  477. | **cull_back** | Cull back-faces (default). |
  478. +---------------------------------+----------------------------------------------------------------------+
  479. | **cull_disabled** | Culling disabled (double sided). |
  480. +---------------------------------+----------------------------------------------------------------------+
  481. | **unshaded** | Result is just albedo. No lighting/shading happens in material. |
  482. +---------------------------------+----------------------------------------------------------------------+
  483. | **diffuse_lambert** | Lambert shading for diffuse (default). |
  484. +---------------------------------+----------------------------------------------------------------------+
  485. | **diffuse_lambert_wrap** | Lambert wrapping (roughness dependent) for diffuse. |
  486. +---------------------------------+----------------------------------------------------------------------+
  487. | **diffuse_oren_nayar** | Oren Nayar for diffuse. |
  488. +---------------------------------+----------------------------------------------------------------------+
  489. | **diffuse_burley** | Burley (Disney PBS) for diffuse. |
  490. +---------------------------------+----------------------------------------------------------------------+
  491. | **diffuse toon** | Toon shading for diffuse. |
  492. +---------------------------------+----------------------------------------------------------------------+
  493. | **specular_schlick_ggx** | Schlick-GGX for specular (default). |
  494. +---------------------------------+----------------------------------------------------------------------+
  495. | **specular_blinn** | Blinn for specular (compatibility). |
  496. +---------------------------------+----------------------------------------------------------------------+
  497. | **specular_phong** | Phong for specular (compatibility). |
  498. +---------------------------------+----------------------------------------------------------------------+
  499. | **specular_toon** | Toon for specular. |
  500. +---------------------------------+----------------------------------------------------------------------+
  501. | **specular_disabled** | Disable specular. |
  502. +---------------------------------+----------------------------------------------------------------------+
  503. | **skip_vertex_transform** | VERTEX/NORMAL/etc need to be transformed manually in VS. |
  504. +---------------------------------+----------------------------------------------------------------------+
  505. | **world_vertex_coords** | VERTEX/NORMAL/etc are modified in world coordinates instead of local.|
  506. +---------------------------------+----------------------------------------------------------------------+
  507. | **vertex_lighting** | Use vertex-based lighting. |
  508. +---------------------------------+----------------------------------------------------------------------+
  509. Vertex Built-Ins
  510. ^^^^^^^^^^^^^^^^
  511. +----------------------------------+-------------------------------------------------------+
  512. | Built-In | Object vertex to world space transform. |
  513. +==================================+=======================================================+
  514. | mat4 **WORLD_MATRIX** | Model space to world space transform. |
  515. +----------------------------------+-------------------------------------------------------+
  516. | mat4 **INV_CAMERA_MATRIX** | World space to view space transform. |
  517. +----------------------------------+-------------------------------------------------------+
  518. | mat4 **PROJECTION_MATRIX** | View space to clip space transform. |
  519. +----------------------------------+-------------------------------------------------------+
  520. | mat4 **CAMERA_MATRIX** | View space to world space transform. |
  521. +----------------------------------+-------------------------------------------------------+
  522. | mat4 **MODELVIEW_MATRIX** | Model space to view space transform (use if possible).|
  523. +----------------------------------+-------------------------------------------------------+
  524. | mat4 **INV_PROJECTION_MATRIX** | Clip space to view space transform. |
  525. +----------------------------------+-------------------------------------------------------+
  526. | float **TIME** | Elapsed total time in seconds. |
  527. +----------------------------------+-------------------------------------------------------+
  528. | vec2 **VIEWPORT_SIZE** | Size of viewport (in pixels). |
  529. +----------------------------------+-------------------------------------------------------+
  530. | vec3 **VERTEX** | Vertex in local coords (see doc below). |
  531. +----------------------------------+-------------------------------------------------------+
  532. | vec3 **NORMAL** | Normal in local coords. |
  533. +----------------------------------+-------------------------------------------------------+
  534. | vec3 **TANGENT** | Tangent in local coords. |
  535. +----------------------------------+-------------------------------------------------------+
  536. | vec3 **BINORMAL** | Binormal in local coords. |
  537. +----------------------------------+-------------------------------------------------------+
  538. | vec2 **UV** | UV main channel. |
  539. +----------------------------------+-------------------------------------------------------+
  540. | vec2 **UV2** | UV secondary channel. |
  541. +----------------------------------+-------------------------------------------------------+
  542. | vec4 **COLOR** | Color from vertices. |
  543. +----------------------------------+-------------------------------------------------------+
  544. | vec2 **POINT_SIZE** | Point size for point rendering. |
  545. +----------------------------------+-------------------------------------------------------+
  546. | int **INSTANCE_ID** | Instance ID for instancing. |
  547. +----------------------------------+-------------------------------------------------------+
  548. | vec4 **INSTANCE_CUSTOM** | Instance custom data (for particles, mostly). |
  549. +----------------------------------+-------------------------------------------------------+
  550. | float **ROUGHNESS** | Roughness for vertex lighting. |
  551. +----------------------------------+-------------------------------------------------------+
  552. Vertex data (VERTEX, NORMAL, TANGENT, BITANGENT) is presented in local model space. If not
  553. written to, these values will not be modified and be passed through as they came.
  554. They can be optionally set to be presented in world space (after being transformed by world)
  555. by adding the *world_vertex_coords* render mode.
  556. It is also possible to completely disable the built-in modelview transform (projection will still
  557. happen later, though) with the following code, so it can be done manually:
  558. .. code-block:: glsl
  559. shader_type spatial;
  560. render_mode skip_vertex_transform;
  561. void vertex() {
  562. VERTEX = (MODELVIEW_MATRIX * vec4(VERTEX, 1.0)).xyz;
  563. NORMAL = (MODELVIEW_MATRIX * vec4(VERTEX, 0.0)).xyz;
  564. // same as above for binormal and tangent, if normal mapping is used
  565. }
  566. Other built-ins such as UV, UV2 and COLOR are also passed through to the fragment function if not modified.
  567. For instancing, the INSTANCE_CUSTOM variable contains the instance custom data. When using particles, this information
  568. is usually:
  569. * **x**: Rotation angle in radians.
  570. * **y**: Phase during lifetime (0 to 1).
  571. * **z**: Animation frame.
  572. This allows to easily adjust the shader to a particle system using default particles material. When writing a custom particles
  573. shader, this value can be used as desired.
  574. Fragment Built-Ins
  575. ^^^^^^^^^^^^^^^^^^
  576. +----------------------------------+--------------------------------------------------------------------------------------------------+
  577. | Built-In | Description |
  578. +==================================+==================================================================================================+
  579. | in mat4 **WORLD_MATRIX** | Model space to world space transform. |
  580. +----------------------------------+--------------------------------------------------------------------------------------------------+
  581. | in mat4 **INV_CAMERA_MATRIX** | World space to view space transform. |
  582. +----------------------------------+--------------------------------------------------------------------------------------------------+
  583. | in mat4 **PROJECTION_MATRIX** | View space to clip space transform. |
  584. +----------------------------------+--------------------------------------------------------------------------------------------------+
  585. | in mat4 **CAMERA_MATRIX** | View space to world space transform. |
  586. +----------------------------------+--------------------------------------------------------------------------------------------------+
  587. | in mat4 **INV_PROJECTION_MATRIX**| Clip space to view space transform. |
  588. +----------------------------------+--------------------------------------------------------------------------------------------------+
  589. | float **TIME** | Elapsed total time in seconds. |
  590. +----------------------------------+--------------------------------------------------------------------------------------------------+
  591. | vec2 **VIEWPORT_SIZE** | Size of viewport (in pixels). |
  592. +----------------------------------+--------------------------------------------------------------------------------------------------+
  593. | vec3 **VERTEX** | Vertex that comes from vertex function, in view space. |
  594. +----------------------------------+--------------------------------------------------------------------------------------------------+
  595. | in vec4 **FRAGCOORD** | Fragment cordinate, pixel adjusted. |
  596. +----------------------------------+--------------------------------------------------------------------------------------------------+
  597. | in bool **FRONT_FACING** | true whether current face is front face. |
  598. +----------------------------------+--------------------------------------------------------------------------------------------------+
  599. | inout vec3 **NORMAL** | Normal that comes from vertex function, in view space. |
  600. +----------------------------------+--------------------------------------------------------------------------------------------------+
  601. | inout vec3 **TANGENT** | Tangent that comes from vertex function. |
  602. +----------------------------------+--------------------------------------------------------------------------------------------------+
  603. | inout vec3 **BINORMAL** | Binormal that comes from vertex function. |
  604. +----------------------------------+--------------------------------------------------------------------------------------------------+
  605. | out vec3 **NORMALMAP** | Output this if reading normal from a texture instead of NORMAL. |
  606. +----------------------------------+--------------------------------------------------------------------------------------------------+
  607. | out float **NORMALMAP_DEPTH** | Depth from variable above. Defaults to 1.0. |
  608. +----------------------------------+--------------------------------------------------------------------------------------------------+
  609. | in vec2 **UV** | UV that comes from vertex function. |
  610. +----------------------------------+--------------------------------------------------------------------------------------------------+
  611. | in vec2 **UV2** | UV2 that coems from vertex function. |
  612. +----------------------------------+--------------------------------------------------------------------------------------------------+
  613. | in vec4 **COLOR** | COLOR that comes from vertex function. |
  614. +----------------------------------+--------------------------------------------------------------------------------------------------+
  615. | out vec3 **ALBEDO** | Albedo (default white). |
  616. +----------------------------------+--------------------------------------------------------------------------------------------------+
  617. | out float **ALPHA** | Alpha (0..1), if written to the material will go to transparent pipeline. |
  618. +----------------------------------+--------------------------------------------------------------------------------------------------+
  619. | out float **METALLIC** | Metallic (0..1). |
  620. +----------------------------------+--------------------------------------------------------------------------------------------------+
  621. | out float **SPECULAR** | Specular. Defaults to 0.5, best to not modify unless you want to change IOR. |
  622. +----------------------------------+--------------------------------------------------------------------------------------------------+
  623. | out float **ROUGHNESS** | Roughness (0..1). |
  624. +----------------------------------+--------------------------------------------------------------------------------------------------+
  625. | out float **RIM** | Rim (0..1). |
  626. +----------------------------------+--------------------------------------------------------------------------------------------------+
  627. | out float **RIM_TINT** | Rim Tint, goes from 0 (white) to 1 (albedo). |
  628. +----------------------------------+--------------------------------------------------------------------------------------------------+
  629. | out float **CLEARCOAT** | Small added specular blob. |
  630. +----------------------------------+--------------------------------------------------------------------------------------------------+
  631. | out float **CLEARCOAT_GLOSS** | Gloss of Clearcoat. |
  632. +----------------------------------+--------------------------------------------------------------------------------------------------+
  633. | out float **ANISOTROPY** | For distorting the specular blob according to tangent space. |
  634. +----------------------------------+--------------------------------------------------------------------------------------------------+
  635. | out float **ANISOTROPY_FLOW** | Distortion direction, use with flowmaps. |
  636. +----------------------------------+--------------------------------------------------------------------------------------------------+
  637. | out float **SSS_STRENGTH** | Strength of Subsurface Scattering (default 0). |
  638. +----------------------------------+--------------------------------------------------------------------------------------------------+
  639. | out vec3 **TRANSMISSION** | Transmission mask (default 0,0,0). |
  640. +----------------------------------+--------------------------------------------------------------------------------------------------+
  641. | out foat **AO** | Ambient Occlusion (pre-baked). |
  642. +----------------------------------+--------------------------------------------------------------------------------------------------+
  643. | out float **AO_LIGHT_AFFECT** | How much AO affects lights (0..1. default 0, none). |
  644. +----------------------------------+--------------------------------------------------------------------------------------------------+
  645. | out vec3 **EMISSION** | Emission color (can go over 1,1,1 for HDR). |
  646. +----------------------------------+--------------------------------------------------------------------------------------------------+
  647. | in sampler2D **SCREEN_TEXTURE** | Built-in Texture for reading from the screen. Mipmaps contain increasingly blurred copies. |
  648. +----------------------------------+--------------------------------------------------------------------------------------------------+
  649. | in sampler2D **DEPTH_TEXTURE** | Built-in Texture for reading depth from the screen. Must convert to linear using INV_PROJECTION. |
  650. +----------------------------------+--------------------------------------------------------------------------------------------------+
  651. | in vec2 **SCREEN_UV** | Screen UV coordinate for current pixel. |
  652. +----------------------------------+--------------------------------------------------------------------------------------------------+
  653. | in vec2 **POINT_COORD** | Point Coord for drawing points with POINT_SIZE. |
  654. +----------------------------------+--------------------------------------------------------------------------------------------------+
  655. | in float **SIDE** | SIDE multiplier, for double sided materials. |
  656. +----------------------------------+--------------------------------------------------------------------------------------------------+
  657. | out float **ALPHA_SCISSOR** | If written to, values below a certain amount of alpha are discarded. |
  658. +----------------------------------+--------------------------------------------------------------------------------------------------+
  659. Values marked as "in" are read-only. Values marked as "out" are for optional writing. If nothing is written, a default value is used.
  660. Light Built-Ins
  661. ^^^^^^^^^^^^^^^
  662. +-----------------------------------+------------------------------------------+
  663. | Built-in | Description |
  664. +===================================+==========================================+
  665. | in mat4 **WORLD_MATRIX** | Model space to world space transform. |
  666. +-----------------------------------+------------------------------------------+
  667. | in mat4 **INV_CAMERA_MATRIX** | World space to view space transform. |
  668. +-----------------------------------+------------------------------------------+
  669. | in mat4 **PROJECTION_MATRIX** | View space to clip space transform. |
  670. +-----------------------------------+------------------------------------------+
  671. | in mat4 **CAMERA_MATRIX** | View space to world space transform. |
  672. +-----------------------------------+------------------------------------------+
  673. | in mat4 **INV_PROJECTION_MATRIX** | Clip space to view space transform. |
  674. +-----------------------------------+------------------------------------------+
  675. | in float **TIME** | Elapsed total time in seconds. |
  676. +-----------------------------------+------------------------------------------+
  677. | in vec2 **VIEWPORT_SIZE** | Size of viewport (in pixels). |
  678. +-----------------------------------+------------------------------------------+
  679. | in vec3 **NORMAL** | Normal vector. |
  680. +-----------------------------------+------------------------------------------+
  681. | in vec3 **VIEW** | View vector. |
  682. +-----------------------------------+------------------------------------------+
  683. | in vec3 **LIGHT** | Light Vector. |
  684. +-----------------------------------+------------------------------------------+
  685. | in vec3 **LIGHT_COLOR** | Color of light multiplied by energy. |
  686. +-----------------------------------+------------------------------------------+
  687. | in vec3 **ATTENUATION** | Attenuation based on distance or shadow. |
  688. +-----------------------------------+------------------------------------------+
  689. | in vec3 **ALBEDO** | Base albedo. |
  690. +-----------------------------------+------------------------------------------+
  691. | in vec3 **TRANSMISSION** | Transmission mask. |
  692. +-----------------------------------+------------------------------------------+
  693. | in float **ROUGHNESS** | Roughness. |
  694. +-----------------------------------+------------------------------------------+
  695. | out vec3 **DIFFUSE_LIGHT** | Diffuse light result. |
  696. +-----------------------------------+------------------------------------------+
  697. | out vec3 **SPECULAR_LIGHT** | Specular light result. |
  698. +-----------------------------------+------------------------------------------+
  699. Writing light shaders is completely optional. Unlike other game engines, they don't affect
  700. performance or force a specific pipeline.
  701. To write a light shader, simply make sure to assign something to DIFFUSE_LIGHT or SPECULAR_LIGHT.
  702. Assigning nothing means no light is processed.
  703. Canvas Item
  704. ~~~~~~~~~~~~
  705. Accepted render modes and built-ins for "shader_type canvas_item;".
  706. Render Modes
  707. ^^^^^^^^^^^^
  708. +---------------------------------+----------------------------------------------------------------------+
  709. | Render Mode | Description |
  710. +=================================+======================================================================+
  711. | **blend_mix** | Mix blend mode (alpha is transparency), default. |
  712. +---------------------------------+----------------------------------------------------------------------+
  713. | **blend_add** | Additive blend mode. |
  714. +---------------------------------+----------------------------------------------------------------------+
  715. | **blend_sub** | Subtractive blend mode. |
  716. +---------------------------------+----------------------------------------------------------------------+
  717. | **blend_mul** | Multiplicative blend mode. |
  718. +---------------------------------+----------------------------------------------------------------------+
  719. | **blend_premul_alpha** | Premultiplied alpha blend mode. |
  720. +---------------------------------+----------------------------------------------------------------------+
  721. | **unshaded** | Result is just albedo. No lighting/shading happens in material. |
  722. +---------------------------------+----------------------------------------------------------------------+
  723. | **light_only** | Only draw for light pass (when multipass is used). |
  724. +---------------------------------+----------------------------------------------------------------------+
  725. | **skip_vertex_transform** | VERTEX/NORMAL/etc need to be transformed manually in VS. |
  726. +---------------------------------+----------------------------------------------------------------------+
  727. Vertex Built-Ins
  728. ^^^^^^^^^^^^^^^^
  729. +--------------------------------+----------------------------------------------------------------+
  730. | Built-In | Description |
  731. +================================+================================================================+
  732. | in mat4 **WORLD_MATRIX** | Image to World transform. |
  733. +--------------------------------+----------------------------------------------------------------+
  734. | in mat4 **EXTRA_MATRIX** | Extra transform. |
  735. +--------------------------------+----------------------------------------------------------------+
  736. | in mat4 **PROJECTION_MATRIX** | World to view transform. |
  737. +--------------------------------+----------------------------------------------------------------+
  738. | in float **TIME** | Global time, in seconds. |
  739. +--------------------------------+----------------------------------------------------------------+
  740. | in vec4 **INSTANCE_CUSTOM** | Instance custom data. |
  741. +--------------------------------+----------------------------------------------------------------+
  742. | in bool **AT_LIGHT_PASS** | True if this is a light pass (for multi-pass light rendering). |
  743. +--------------------------------+----------------------------------------------------------------+
  744. | inout vec2 **VERTEX** | Vertex in image space. |
  745. +--------------------------------+----------------------------------------------------------------+
  746. | inout vec2 **UV** | UV. |
  747. +--------------------------------+----------------------------------------------------------------+
  748. | inout vec4 **COLOR** | Color from vertex primitive. |
  749. +--------------------------------+----------------------------------------------------------------+
  750. | out vec2 **POINT_SIZE** | Point size for point drawing. |
  751. +--------------------------------+----------------------------------------------------------------+
  752. Vertex data (VERTEX) is presented in local space.
  753. If not written to, these values will not be modified and be passed through as they came.
  754. It is possible to completely disable the built-in modelview transform (projection will still
  755. happen later, though) with the following code, so it can be done manually:
  756. .. code-block:: glsl
  757. shader_type spatial;
  758. render_mode skip_vertex_transform;
  759. void vertex() {
  760. VERTEX = (EXTRA_MATRIX * (WORLD_MATRIX * vec4(VERTEX, 0.0, 1.0))).xy;
  761. }
  762. Other built-ins such as UV and COLOR are also passed through to the fragment function if not modified.
  763. For instancing, the INSTANCE_CUSTOM variable contains the instance custom data. When using particles, this information
  764. is usually:
  765. * **x**: Rotation angle in radians.
  766. * **y**: Phase during lifetime (0 to 1).
  767. * **z**: Animation frame.
  768. This allows to easily adjust the shader to a particle system using default particles material. When writing a custom particles
  769. shader, this value can be used as desired.
  770. Fragment Built-Ins
  771. ^^^^^^^^^^^^^^^^^^
  772. +----------------------------------+------------------------------------------------------------+
  773. | Built-In | Description |
  774. +==================================+============================================================+
  775. | in vec4 **FRAGCOORD** | Fragment coordinate, pixel adjusted. |
  776. +----------------------------------+------------------------------------------------------------+
  777. | out vec3 **NORMAL** | Normal, writable. |
  778. +----------------------------------+------------------------------------------------------------+
  779. | out vec3 **NORMALMAP** | Normal from texture, default is read from NORMAL_TEXTURE. |
  780. +----------------------------------+------------------------------------------------------------+
  781. | out float **NORMALMAP_DEPTH** | Normalmap depth for scaling. |
  782. +----------------------------------+------------------------------------------------------------+
  783. | in vec2 **UV** | UV from vertex function. |
  784. +----------------------------------+------------------------------------------------------------+
  785. | in vec4 **COLOR** | Color from vertex function. |
  786. +----------------------------------+------------------------------------------------------------+
  787. | in sampler2D **TEXTURE** | Default 2D texture. |
  788. +----------------------------------+------------------------------------------------------------+
  789. | in sampler2D **NORMAL_TEXTURE** | Default 2D normal texture. |
  790. +----------------------------------+------------------------------------------------------------+
  791. | in vec2 **TEXTURE_PIXEL_SIZE** | Default 2D texture pixel size. |
  792. +----------------------------------+------------------------------------------------------------+
  793. | in vec2 **SCREEN_UV** | Screen UV for use with SCREEN_TEXTURE. |
  794. +----------------------------------+------------------------------------------------------------+
  795. | in vec2 **SCREEN_PIXEL_SIZE** | Screen pixel size. |
  796. +----------------------------------+------------------------------------------------------------+
  797. | in vec2 **POINT_COORD** | Coordinate for drawing points. |
  798. +----------------------------------+------------------------------------------------------------+
  799. | in float **TIME** | Global time in seconds. |
  800. +----------------------------------+------------------------------------------------------------+
  801. | in sampler2D **SCREEN_TEXTURE** | Screen texture, mipmaps contain gaussian blurred versions. |
  802. +----------------------------------+------------------------------------------------------------+
  803. Light Built-Ins
  804. ^^^^^^^^^^^^^^^^
  805. +-------------------------------------+-------------------------------------------------------------------------------+
  806. | Built-In | Description |
  807. +=====================================+===============================================================================+
  808. | in vec4 **POSITION** | Screen Position. |
  809. +-------------------------------------+-------------------------------------------------------------------------------+
  810. | in vec3 **NORMAL** | Input Normal. |
  811. +-------------------------------------+-------------------------------------------------------------------------------+
  812. | in vec2 **UV** | UV. |
  813. +-------------------------------------+-------------------------------------------------------------------------------+
  814. | in vec4 **COLOR** | Input Color. |
  815. +-------------------------------------+-------------------------------------------------------------------------------+
  816. | in sampler2D **TEXTURE** | Current texture in use for CanvasItem. |
  817. +-------------------------------------+-------------------------------------------------------------------------------+
  818. | in vec2 **TEXTURE_PIXEL_SIZE** | Pixel size for current 2D texture. |
  819. +-------------------------------------+-------------------------------------------------------------------------------+
  820. | in vec2 **SCREEN_UV** | Screen Texture Coordinate (for using with texscreen). |
  821. +-------------------------------------+-------------------------------------------------------------------------------+
  822. | in vec2 **POINT_COORD** | Current UV for Point Sprite. |
  823. +-------------------------------------+-------------------------------------------------------------------------------+
  824. | in float **TIME** | Time (in seconds). |
  825. +-------------------------------------+-------------------------------------------------------------------------------+
  826. | vec2 **LIGHT\_VEC** | Vector from light to fragment, can be modified to alter shadow computation. |
  827. +-------------------------------------+-------------------------------------------------------------------------------+
  828. | in float **LIGHT\_HEIGHT** | Height of Light. |
  829. +-------------------------------------+-------------------------------------------------------------------------------+
  830. | in color **LIGHT\_COLOR** | Color of Light. |
  831. +-------------------------------------+-------------------------------------------------------------------------------+
  832. | in color **LIGHT\_SHADOW\_COLOR** | Color of Light shadow. |
  833. +-------------------------------------+-------------------------------------------------------------------------------+
  834. | in vec2 **LIGHT\_UV** | UV for light image. |
  835. +-------------------------------------+-------------------------------------------------------------------------------+
  836. | in vec4 **SHADOW** | Light shadow color override. |
  837. +-------------------------------------+-------------------------------------------------------------------------------+
  838. | out vec4 **LIGHT** | Light Output (shader is ignored if this is not used). |
  839. +-------------------------------------+-------------------------------------------------------------------------------+
  840. Particles
  841. ~~~~~~~~~
  842. Accepted render modes and built-ins for "shader_type particles;".
  843. Render Modes
  844. ^^^^^^^^^^^^
  845. +---------------------------------+----------------------------------------------------------------------+
  846. | Render Mode | Description |
  847. +=================================+======================================================================+
  848. | **billboard** | Particle will be shown as a billboard. |
  849. +---------------------------------+----------------------------------------------------------------------+
  850. | **keep_data** | Do not clear previous data on restart. |
  851. +---------------------------------+----------------------------------------------------------------------+
  852. Vertex Built-Ins
  853. ^^^^^^^^^^^^^^^^
  854. +---------------------------------+-----------------------------------------------------------+
  855. | Built-In | Description |
  856. +=================================+===========================================================+
  857. | inout vec4 **COLOR** | Particle color, can be written to. |
  858. +---------------------------------+-----------------------------------------------------------+
  859. | inout vec3 **VELOCITY** | Particle velocity, can be modified. |
  860. +---------------------------------+-----------------------------------------------------------+
  861. | out float **MASS** | Particle mass, use for attractors (default 1). |
  862. +---------------------------------+-----------------------------------------------------------+
  863. | inout bool **ACTIVE** | Particle is active, can be set to false. |
  864. +---------------------------------+-----------------------------------------------------------+
  865. | in bool **RESTART** | Set to true when particle must restart (lifetime cycled). |
  866. +---------------------------------+-----------------------------------------------------------+
  867. | inout vec4 **CUSTOM** | Custom particle data. |
  868. +---------------------------------+-----------------------------------------------------------+
  869. | inout mat4 **TRANSFORM** | Particle transform. |
  870. +---------------------------------+-----------------------------------------------------------+
  871. | in float **TIME** | Global time in seconds. |
  872. +---------------------------------+-----------------------------------------------------------+
  873. | in float **DELTA** | Delta process time. |
  874. +---------------------------------+-----------------------------------------------------------+
  875. | in uint **NUMBER** | Unique number since emission start. |
  876. +---------------------------------+-----------------------------------------------------------+
  877. | in int **INDEX** | Particle index (from total particles). |
  878. +---------------------------------+-----------------------------------------------------------+
  879. | in mat4 **EMISSION_TRANSFORM** | Emitter transform (used for non-local systems). |
  880. +---------------------------------+-----------------------------------------------------------+
  881. | in uint **RANDOM_SEED** | Random seed used as base for random. |
  882. +---------------------------------+-----------------------------------------------------------+
  883. Particle shades only support vertex processing. They are drawn with any regular material for CanvasItem or Spatial, depending on
  884. whether they are 2D or 3D.