shading_language.rst 86 KB

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