shading_language.rst 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. Shading Language
  2. ================
  3. Introduction
  4. ------------
  5. Godot uses a simplified shader language (almost a subset of GLSL).
  6. Shaders can be used for:
  7. - Materials
  8. - Post-Processing
  9. - 2D
  10. and are divided in *Vertex*, *Fragment* and *Light* sections.
  11. Language
  12. --------
  13. Typing
  14. ~~~~~~
  15. The language is statically type and supports only a few operations.
  16. Arrays, classes, structures, etc are not supported. Several built-in
  17. datatypes are provided:
  18. Data Types
  19. ~~~~~~~~~~
  20. +-------------------+--------------------------------------------------------------+
  21. | DataType | Description |
  22. +===================+==============================================================+
  23. | *void* | Void |
  24. +-------------------+--------------------------------------------------------------+
  25. | *bool* | boolean (true or false) |
  26. +-------------------+--------------------------------------------------------------+
  27. | *float* | floating point |
  28. +-------------------+--------------------------------------------------------------+
  29. | *vec2* | 2-component vector, float subindices (x,y or r,g ) |
  30. +-------------------+--------------------------------------------------------------+
  31. | *vec3* | 3-component vector, float subindices (x,y,z or r,g,b ) |
  32. +-------------------+--------------------------------------------------------------+
  33. | *vec4*, *color* | 4-component vector, float subindices (x,y,z,w or r,g,b,a ) |
  34. +-------------------+--------------------------------------------------------------+
  35. | *mat2* | 2x2 matrix, vec3 subindices (x,y) |
  36. +-------------------+--------------------------------------------------------------+
  37. | *mat3* | 3x3 matrix, vec3 subindices (x,y,z) |
  38. +-------------------+--------------------------------------------------------------+
  39. | *mat4* | 4x4 matrix, vec4 subindices (x,y,z,w) |
  40. +-------------------+--------------------------------------------------------------+
  41. | *texture* | texture sampler, can only be used as uniform |
  42. +-------------------+--------------------------------------------------------------+
  43. | *cubemap* | cubemap sampler, can only be used as uniform |
  44. +-------------------+--------------------------------------------------------------+
  45. Syntax
  46. ~~~~~~
  47. | The syntax is similar to C, with statements ending in ; , and comments
  48. as // and /\* \*/.
  49. | Example:
  50. ::
  51. float a = 3;
  52. vec3 b;
  53. b.x = a;
  54. Swizzling
  55. ~~~~~~~~~
  56. It is possible to use swizzling to reasigning subindices or groups of
  57. subindices, in order:
  58. ::
  59. vec3 a = vec3(1,2,3);
  60. vec3 b = a.zyx; // b will contain vec3(3,2,1)
  61. vec2 c = a.xy; // c will contain vec2(1,2)
  62. vec4 d = a.xyzz; // d will contain vec4(1,2,3,3)
  63. Constructors
  64. ~~~~~~~~~~~~
  65. Constructors take the regular amount of elements, but can also accept
  66. less if the element has more subindices, for example:
  67. ::
  68. vec3 a = vec3( 1, vec2(2,3) );
  69. vec3 b = vec3( a );
  70. vec3 c = vec3( vec2(2,3), 1 );
  71. vec4 d = vec4( a, 5 );
  72. mat3 m = mat3( a,b,c );
  73. Conditionals
  74. ~~~~~~~~~~~~
  75. For now, only the "if" conditional is supported. Example:
  76. ::
  77. if (a < b) {
  78. c = b;
  79. }
  80. Uniforms
  81. ~~~~~~~~
  82. A variable can be declared as uniform. In this case, it's value will
  83. come from outside the shader (it will be the responsibility of the
  84. material or whatever using the shader to provide it).
  85. ::
  86. uniform vec3 direction;
  87. uniform color tint;
  88. vec3 result = tint.rgb * direction;
  89. Functions
  90. ~~~~~~~~~
  91. Simple support for functions is provided. Functions can't access
  92. uniforms or other shader variables.
  93. ::
  94. vec3 addtwo( vec3 a, vec3 b) {
  95. return a+b;
  96. }
  97. vec3 c = addtwo(vec3(1,1,1)+vec3(2,2,2));
  98. Built-In Functions
  99. ------------------
  100. Several Built-in functions are provided for convenience, listed as
  101. follows:
  102. | \|\ *. Function \|*. Description \|
  103. | \| float *sin*\ ( float ) \| Sine \|
  104. | \| float *cos*\ ( float ) \| Cosine \|
  105. | \| float *tan*\ ( float ) \| Tangent \|
  106. | \| float *asin*\ ( float ) \| arc-Sine \|
  107. | \| float *acos*\ ( float ) \| arc-Cosine \|
  108. | \| float *atan*\ ( float ) \| arc-Tangent \|
  109. | \| vec\_type *pow*\ ( vec\_type, float ) \| Power \|
  110. | \| vec\_type *pow*\ ( vec\_type, vec\_type ) \| Power (Vec. Exponent)
  111. \|
  112. | \| vec\_type *exp*\ ( vec\_type ) \| Base-e Exponential \|
  113. | \| vec\_type *log*\ ( vec\_type ) \| Natural Logarithm \|
  114. | \| vec\_type *sqrt*\ ( vec\_type ) \| Square Root \|
  115. | \| vec\_type *abs*\ ( vec\_type ) \| Absolute \|
  116. | \| vec\_type *sign*\ ( vec\_type ) \| Sign \|
  117. | \| vec\_type *floor*\ ( vec\_type ) \| Floor \|
  118. | \| vec\_type *trunc*\ ( vec\_type ) \| Trunc \|
  119. | \| vec\_type *ceil*\ ( vec\_type ) \| Ceiling \|
  120. | \| vec\_type *fract*\ ( vec\_type ) \| Fractional \|
  121. | \| vec\_type *mod*\ ( vec\_type,vec\_type ) \| Remainder \|
  122. | \| vec\_type *min*\ ( vec\_type,vec\_type ) \| Minimum \|
  123. | \| vec\_type *min*\ ( vec\_type,vec\_type ) \| Maximum \|
  124. | \| vec\_type *clamp*\ ( vec\_type value,vec\_type min, vec\_type max )
  125. \| Clamp to Min-Max \|
  126. | \| vec\_type *mix*\ ( vec\_type a,vec\_type b, float c ) \| Linear
  127. Interpolate \|
  128. | \| vec\_type *mix*\ ( vec\_type a,vec\_type b, vec\_type c ) \| Linear
  129. Interpolate (Vector Coef.)\|
  130. | \| vec\_type *step*\ ( vec\_type a,vec\_type b) \| \` a[i] < b[i] ?
  131. 0.0 : 1.0\`\|
  132. | \| vec\_type *smoothstep*\ ( vec\_type a,vec\_type b,vec\_type c) \|
  133. \|
  134. | \| float *length*\ ( vec\_type ) \| Vector Length \|
  135. | \| float *distance*\ ( vec\_type, vec\_type ) \| Distance between
  136. vector. \|
  137. | \| float *dot*\ ( vec\_type, vec\_type ) \| Dot Product \|
  138. | \| vec3 *dot*\ ( vec3, vec3 ) \| Cross Product \|
  139. | \| vec\_type *normalize*\ ( vec\_type ) \| Normalize to unit length \|
  140. | \| vec3 *reflect*\ ( vec3, vec3 ) \| Reflect \|
  141. | \| color *tex*\ ( texture, vec2 ) \| Read from a texture in
  142. noormalized coords \|
  143. | \| color *texcube*\ ( texture, vec3 ) \| Read from a cubemap \|
  144. | \| color *texscreen*\ ( vec2 ) \| Read from screen (generates a copy)
  145. \|
  146. Built-In Variables
  147. ------------------
  148. Depending on the shader type, several built-in variables are available,
  149. listed as follows:
  150. Material - VertexShader
  151. ~~~~~~~~~~~~~~~~~~~~~~~
  152. | \|\ *. Variable \|*. Description \|
  153. | \| const vec3 *SRC\_VERTEX* \| Model-Space Vertex \|
  154. | \| const vec3 *SRC\_NORMAL* \| Model-Space Normal \|
  155. | \| const vec3 *SRC\_TANGENT* \| Model-Space Tangent \|
  156. | \| const float *SRC\_BINORMALF* \| Direction to Compute Binormal \|
  157. | \| vec3 *VERTEX* \| View-Space Vertex \|
  158. | \| vec3 *NORMAL* \| View-Space Normal \|
  159. | \| vec3 *TANGENT* \| View-Space Tangent \|
  160. | \| vec3 *BINORMAL* \| View-Space Binormal \|
  161. | \| vec2 *UV* \| UV \|
  162. | \| vec2 *UV2* \| UV2 \|
  163. | \| color *COLOR* \| Vertex Color \|
  164. | \| out vec4 *VAR1* \| Varying 1 Output \|
  165. | \| out vec4 *VAR2* \| Varying 2 Output \|
  166. | \| out float *SPEC\_EXP* \| Specular Exponent (for Vertex Lighting) \|
  167. | \| out float *POINT\_SIZE* \| Point Size (for points) \|
  168. | \| const mat4 *WORLD\_MATRIX* \| Object World Matrix \|
  169. | \| const mat4 *INV\_CAMERA\_MATRIX* \| Inverse Camera Matrix \|
  170. | \| const mat4 *PROJECTION\_MATRIX* \| Projection Matrix \|
  171. | \| const mat4 *MODELVIEW\_MATRIX* \| (InvCamera \* Projection) \|
  172. | \| const float *INSTANCE\_ID* \| Instance ID (for multimesh)\|
  173. | \| const float *TIME* \| Time (in seconds) \|
  174. Material - FragmentShader
  175. ~~~~~~~~~~~~~~~~~~~~~~~~~
  176. +----------------------------------+----------------------------------------------------------------------------------+
  177. | Variable | Description |
  178. +==================================+==================================================================================+
  179. | const vec3 *VERTEX* | View-Space vertex |
  180. +----------------------------------+----------------------------------------------------------------------------------+
  181. | const vec4 *POSITION* | View-Space Position |
  182. +----------------------------------+----------------------------------------------------------------------------------+
  183. | const vec3 *NORMAL* | View-Space Normal |
  184. +----------------------------------+----------------------------------------------------------------------------------+
  185. | const vec3 *TANGENT* | View-Space Tangent |
  186. +----------------------------------+----------------------------------------------------------------------------------+
  187. | const vec3 *BINORMAL* | View-Space Binormal |
  188. +----------------------------------+----------------------------------------------------------------------------------+
  189. | const vec3 *NORMALMAP* | Alternative to NORMAL, use for normal texture output. |
  190. +----------------------------------+----------------------------------------------------------------------------------+
  191. | const vec3 *NORMALMAP\_DEPTH* | Complementary to the above, allows changing depth of normalmap. |
  192. +----------------------------------+----------------------------------------------------------------------------------+
  193. | const vec2 *UV* | UV |
  194. +----------------------------------+----------------------------------------------------------------------------------+
  195. | const vec2 *UV2* | UV2 |
  196. +----------------------------------+----------------------------------------------------------------------------------+
  197. | const color *COLOR* | Vertex Color |
  198. +----------------------------------+----------------------------------------------------------------------------------+
  199. | const vec4 *VAR1* | Varying 1 |
  200. +----------------------------------+----------------------------------------------------------------------------------+
  201. | const vec4 *VAR2* | Varying 2 |
  202. +----------------------------------+----------------------------------------------------------------------------------+
  203. | const vec2 *SCREEN\_UV* | Screen Texture Coordinate (for using with texscreen) |
  204. +----------------------------------+----------------------------------------------------------------------------------+
  205. | const float *TIME* | Time (in seconds) |
  206. +----------------------------------+----------------------------------------------------------------------------------+
  207. | const vec2 *POINT\_COORD* | UV for point, when drawing point sprites. |
  208. +----------------------------------+----------------------------------------------------------------------------------+
  209. | out vec3 *DIFFUSE* | Diffuse Color |
  210. +----------------------------------+----------------------------------------------------------------------------------+
  211. | out vec4 *DIFFUSE\_ALPHA* | Diffuse Color with Alpha (using this sends geometry to alpha pipeline) |
  212. +----------------------------------+----------------------------------------------------------------------------------+
  213. | out vec3 *SPECULAR* | Specular Color |
  214. +----------------------------------+----------------------------------------------------------------------------------+
  215. | out vec3 *EMISSION* | Emission Color |
  216. +----------------------------------+----------------------------------------------------------------------------------+
  217. | out float *SPEC\_EXP* | Specular Exponent (Fragment Version) |
  218. +----------------------------------+----------------------------------------------------------------------------------+
  219. | out float *GLOW* | Glow |
  220. +----------------------------------+----------------------------------------------------------------------------------+
  221. | out mat4 *INV\_CAMERA\_MATRIX* | Inverse camera matrix, can be used to obtain world coords (see example below). |
  222. +----------------------------------+----------------------------------------------------------------------------------+
  223. Material - LightShader
  224. ~~~~~~~~~~~~~~~~~~~~~~
  225. +--------------------------------+-------------------------------+
  226. | Variable | Description |
  227. +================================+===============================+
  228. | const vec3 *NORMAL* | View-Space normal |
  229. +--------------------------------+-------------------------------+
  230. | const vec3 *LIGHT\_DIR* | View-Space Light Direction |
  231. +--------------------------------+-------------------------------+
  232. | const vec3 *EYE\_VEC* | View-Space Eye-Point Vector |
  233. +--------------------------------+-------------------------------+
  234. | const vec3 *DIFFUSE* | Material Diffuse Color |
  235. +--------------------------------+-------------------------------+
  236. | const vec3 *LIGHT\_DIFFUSE* | Light Diffuse Color |
  237. +--------------------------------+-------------------------------+
  238. | const vec3 *SPECULAR* | Material Specular Color |
  239. +--------------------------------+-------------------------------+
  240. | const vec3 *LIGHT\_SPECULAR* | Light Specular Color |
  241. +--------------------------------+-------------------------------+
  242. | const float *SPECULAR\_EXP* | Specular Exponent |
  243. +--------------------------------+-------------------------------+
  244. | const vec1 *SHADE\_PARAM* | Generic Shade Parameter |
  245. +--------------------------------+-------------------------------+
  246. | const vec2 *POINT\_COORD* | Current UV for Point Sprite |
  247. +--------------------------------+-------------------------------+
  248. | out vec2 *LIGHT* | Resulting Light |
  249. +--------------------------------+-------------------------------+
  250. | const float *TIME* | Time (in seconds) |
  251. +--------------------------------+-------------------------------+
  252. CanvasItem (2D) - VertexShader
  253. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  254. | \|\ *. Variable \|*. Description \|
  255. | \| const vec2 *SRC\_VERTEX* \| CanvasItem space vertex. \|
  256. | \| vec2 *UV* \| UV \|
  257. | \| out vec2 *VERTEX* \| Output LocalSpace vertex. \|
  258. | \| out vec2 *WORLD\_VERTEX* \| Output WorldSpace vertex. (use this or
  259. the one above) \|
  260. | \| color *COLOR* \| Vertex Color \|
  261. | \| out vec4 *VAR1* \| Varying 1 Output \|
  262. | \| out vec4 *VAR2* \| Varying 2 Output \|
  263. | \| out float *POINT\_SIZE* \| Point Size (for points) \|
  264. | \| const mat4 *WORLD\_MATRIX* \| Object World Matrix \|
  265. | \| const mat4 *EXTRA\_MATRIX* \| Extra (user supplied) matrix via
  266. `CanvasItem.draw\_set\_transform() <https://github.com/okamstudio/godot/wiki/class_canvasitem#draw_set_transform>`__.
  267. Identity by default. \|
  268. | \| const mat4 *PROJECTION\_MATRIX* \| Projection Matrix (model coords
  269. to screen).\|
  270. | \| const float *TIME* \| Time (in seconds) \|
  271. CanvasItem (2D) - FragmentShader
  272. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  273. | \|\ *. Variable \|*. Description \|
  274. | \| const vec4 *SRC\_COLOR* \| Vertex color \|
  275. | \| const vec4 *POSITION* \| Screen Position \|
  276. | \| vec2 *UV* \| UV \|
  277. | \| out color *COLOR* \| Output Color \|
  278. | \| out vec3 *NORMAL* \| Optional Normal (used for 2D Lighting) \|
  279. | \| out vec3 *NORMALMAP* \| Optional Normal in standard normalmap
  280. format (flipped y and Z from 0 to 1) \|
  281. | \| out float *NORMALMAP\_DEPTH* \| Depth option for above normalmap
  282. output, default value is 1.0 \|
  283. | \| const texture *TEXTURE* \| Current texture in use for CanvasItem \|
  284. | \| const vec2 *TEXTURE\_PIXEL\_SIZE* \| Pixel size for current 2D
  285. texture \|
  286. | \| in vec4 *VAR1* \| Varying 1 Output \|
  287. | \| in vec4 *VAR2* \| Varying 2 Output \|
  288. | \| const vec2 *SCREEN\_UV*\ \| Screen Texture Coordinate (for using
  289. with texscreen) \|
  290. | \| const vec2 *POINT\_COORD* \| Current UV for Point Sprite \|
  291. | \| const float *TIME*\ \| Time (in seconds) \|
  292. CanvasItem (2D) - LightShader
  293. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  294. | \|\ *. Variable \|*. Description \|
  295. | \| const vec4 *POSITION* \| Screen Position \|
  296. | \| in vec3 *NORMAL* \| Input Normal \|
  297. | \| in vec2 *UV* \| UV \|
  298. | \| in color *COLOR* \| Input Color \|
  299. | \| const texture *TEXTURE* \| Current texture in use for CanvasItem \|
  300. | \| const vec2 *TEXTURE\_PIXEL\_SIZE* \| Pixel size for current 2D
  301. texture \|
  302. | \| in vec4 *VAR1* \| Varying 1 Output \|
  303. | \| in vec4 *VAR2* \| Varying 2 Output \|
  304. | \| const vec2 *SCREEN\_UV*\ \| Screen Texture Coordinate (for using
  305. with texscreen) \|
  306. | \| const vec2 *POINT\_COORD* \| Current UV for Point Sprite \|
  307. | \| const float *TIME*\ \| Time (in seconds) \|
  308. | \| vec2 *LIGHT\_VEC* \| Vector from light to fragment, can be modified
  309. to alter shadow computation. \|
  310. | \| const float *LIGHT\_HEIGHT* \| Height of Light \|
  311. | \| const color *LIGHT\_COLOR* \| Color of Light \|
  312. | \| out vec4 *LIGHT* \| Light Ouput (shader is ignored if this is not
  313. used) \|
  314. Examples
  315. --------
  316. Material that reads a texture, a color and multiples them, fragment
  317. program:
  318. ::
  319. uniform color modulate;
  320. uniform texture source;
  321. DIFFUSE = modulate.rgb * tex(source,UV).rgb;
  322. Material that glows from red to white:
  323. ::
  324. DIFFUSE = vec3(1,0,0) + vec(1,1,1)*mod(TIME,1.0);
  325. Standard Blinn Lighting Shader
  326. ::
  327. float NdotL = max(0.0,dot( NORMAL, LIGHT_DIR ));
  328. vec3 half_vec = normalize(LIGHT_DIR + EYE_VEC);
  329. float eye_light = max(dot(NORMAL, half_vec),0.0);
  330. LIGHT = LIGHT_DIFFUSE + DIFFUSE + NdotL;
  331. if (NdotL > 0.0) {
  332. LIGHT+=LIGHT_SPECULAR + SPECULAR + pow( eye_light, SPECULAR_EXP );
  333. };
  334. Obtaining world-space normal and position in material fragment program:
  335. ::
  336. //use reverse multiply because INV_CAMERA_MATRIX is world2cam
  337. vec3 world_normal = NORMAL * mat3(INV_CAMERA_MATRIX);
  338. vec3 world_pos = (VERTEX-INV_CAMERA_MATRIX.w.xyz) * mat3(INV_CAMERA_MATRIX);
  339. Notes
  340. -----
  341. | \* **Do not** use DIFFUSE\_ALPHA unless you really intend to use
  342. transparency. Transparent materials must be sorted by depth and slow
  343. down the rendering pipeline. For opaque materials, just use DIFFUSE.
  344. | \* **Do not** use DISCARD unless you really need it. Discard makes
  345. rendering slower, specially on mobile devices.
  346. | \* TIME may reset after a while (may last an hour or so), it's meant
  347. for effects that vary over time.
  348. | \* In general, every built-in variable not used results in less shader
  349. code generated, so writing a single giant shader with a lot of code
  350. and optional scenarios is often not a good idea.