Uniform.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../../" />
  6. <script src="page.js"></script>
  7. <link type="text/css" rel="stylesheet" href="page.css" />
  8. </head>
  9. <body>
  10. <h1>[name]</h1>
  11. <p class="desc">
  12. Uniforms are global GLSL variables. They are passed to shader programs.
  13. </p>
  14. <h2>Code Example</h2>
  15. <p>
  16. When declaring a uniform of a [page:ShaderMaterial], it is declared by
  17. value or by object.
  18. </p>
  19. <code>
  20. uniforms: {
  21. time: { value: 1.0 },
  22. resolution: new Uniform( new Vector2() )
  23. };
  24. </code>
  25. <h2>Uniform types</h2>
  26. <p>
  27. Each uniform must have a `value` property. The type of the value must
  28. correspond to the type of the uniform variable in the GLSL code as
  29. specified for the primitive GLSL types in the table below. Uniform
  30. structures and arrays are also supported. GLSL arrays of primitive type
  31. must either be specified as an array of the corresponding THREE objects or
  32. as a flat array containing the data of all the objects. In other words;
  33. GLSL primitives in arrays must not be represented by arrays. This rule
  34. does not apply transitively. An array of `vec2` arrays, each with a length
  35. of five vectors, must be an array of arrays, of either five [page:Vector2]
  36. objects or ten `number`s.
  37. </p>
  38. <table>
  39. <caption>
  40. <a id="uniform-types">Uniform types</a>
  41. </caption>
  42. <thead>
  43. <tr>
  44. <th>GLSL type</th>
  45. <th>JavaScript type</th>
  46. </tr>
  47. </thead>
  48. <tbody>
  49. <tr>
  50. <td>int</td>
  51. <td>[page:Number]</td>
  52. </tr>
  53. <tr>
  54. <td>uint</td>
  55. <td>[page:Number]</td>
  56. </tr>
  57. <tr>
  58. <td>float</td>
  59. <td>[page:Number]</td>
  60. </tr>
  61. <tr>
  62. <td>bool</td>
  63. <td>[page:Boolean]</td>
  64. </tr>
  65. <tr>
  66. <td>bool</td>
  67. <td>[page:Number]</td>
  68. </tr>
  69. <tr>
  70. <td>vec2</td>
  71. <td>[page:Vector2 THREE.Vector2]</td>
  72. </tr>
  73. <tr>
  74. <td>vec2</td>
  75. <td>[page:Float32Array Float32Array] (*)</td>
  76. </tr>
  77. <tr>
  78. <td>vec2</td>
  79. <td>[page:Array Array] (*)</td>
  80. </tr>
  81. <tr>
  82. <td>vec3</td>
  83. <td>[page:Vector3 THREE.Vector3]</td>
  84. </tr>
  85. <tr>
  86. <td>vec3</td>
  87. <td>[page:Color THREE.Color]</td>
  88. </tr>
  89. <tr>
  90. <td>vec3</td>
  91. <td>[page:Float32Array Float32Array] (*)</td>
  92. </tr>
  93. <tr>
  94. <td>vec3</td>
  95. <td>[page:Array Array] (*)</td>
  96. </tr>
  97. <tr>
  98. <td>vec4</td>
  99. <td>[page:Vector4 THREE.Vector4]</td>
  100. </tr>
  101. <tr>
  102. <td>vec4</td>
  103. <td>[page:Quaternion THREE.Quaternion]</td>
  104. </tr>
  105. <tr>
  106. <td>vec4</td>
  107. <td>[page:Float32Array Float32Array] (*)</td>
  108. </tr>
  109. <tr>
  110. <td>vec4</td>
  111. <td>[page:Array Array] (*)</td>
  112. </tr>
  113. <tr>
  114. <td>mat2</td>
  115. <td>[page:Float32Array Float32Array] (*)</td>
  116. </tr>
  117. <tr>
  118. <td>mat2</td>
  119. <td>[page:Array Array] (*)</td>
  120. </tr>
  121. <tr>
  122. <td>mat3</td>
  123. <td>[page:Matrix3 THREE.Matrix3]</td>
  124. </tr>
  125. <tr>
  126. <td>mat3</td>
  127. <td>[page:Float32Array Float32Array] (*)</td>
  128. </tr>
  129. <tr>
  130. <td>mat3</td>
  131. <td>[page:Array Array] (*)</td>
  132. </tr>
  133. <tr>
  134. <td>mat4</td>
  135. <td>[page:Matrix4 THREE.Matrix4]</td>
  136. </tr>
  137. <tr>
  138. <td>mat4</td>
  139. <td>[page:Float32Array Float32Array] (*)</td>
  140. </tr>
  141. <tr>
  142. <td>mat4</td>
  143. <td>[page:Array Array] (*)</td>
  144. </tr>
  145. <tr>
  146. <td>ivec2, bvec2</td>
  147. <td>[page:Float32Array Float32Array] (*)</td>
  148. </tr>
  149. <tr>
  150. <td>ivec2, bvec2</td>
  151. <td>[page:Array Array] (*)</td>
  152. </tr>
  153. <tr>
  154. <td>ivec3, bvec3</td>
  155. <td>[page:Int32Array Int32Array] (*)</td>
  156. </tr>
  157. <tr>
  158. <td>ivec3, bvec3</td>
  159. <td>[page:Array Array] (*)</td>
  160. </tr>
  161. <tr>
  162. <td>ivec4, bvec4</td>
  163. <td>[page:Int32Array Int32Array] (*)</td>
  164. </tr>
  165. <tr>
  166. <td>ivec4, bvec4</td>
  167. <td>[page:Array Array] (*)</td>
  168. </tr>
  169. <tr>
  170. <td>sampler2D</td>
  171. <td>[page:Texture THREE.Texture]</td>
  172. </tr>
  173. <tr>
  174. <td>samplerCube</td>
  175. <td>[page:CubeTexture THREE.CubeTexture]</td>
  176. </tr>
  177. </tbody>
  178. </table>
  179. <p>
  180. (*) Same for an (innermost) array (dimension) of the same GLSL type,
  181. containing the components of all vectors or matrices in the array.
  182. </p>
  183. <h2>Structured Uniforms</h2>
  184. <p>
  185. Sometimes you want to organize uniforms as `structs` in your shader code.
  186. The following style must be used so `three.js` is able to process
  187. structured uniform data.
  188. </p>
  189. <code>
  190. uniforms = {
  191. data: {
  192. value: {
  193. position: new Vector3(),
  194. direction: new Vector3( 0, 0, 1 )
  195. }
  196. }
  197. };
  198. </code>
  199. This definition can be mapped on the following GLSL code:
  200. <code>
  201. struct Data {
  202. vec3 position;
  203. vec3 direction;
  204. };
  205. uniform Data data;
  206. </code>
  207. <h2>Structured Uniforms with Arrays</h2>
  208. <p>
  209. It's also possible to manage `structs` in arrays. The syntax for this use
  210. case looks like so:
  211. </p>
  212. <code>
  213. const entry1 = {
  214. position: new Vector3(),
  215. direction: new Vector3( 0, 0, 1 )
  216. };
  217. const entry2 = {
  218. position: new Vector3( 1, 1, 1 ),
  219. direction: new Vector3( 0, 1, 0 )
  220. };
  221. uniforms = {
  222. data: {
  223. value: [ entry1, entry2 ]
  224. }
  225. };
  226. </code>
  227. This definition can be mapped on the following GLSL code:
  228. <code>
  229. struct Data {
  230. vec3 position;
  231. vec3 direction;
  232. };
  233. uniform Data data[ 2 ];
  234. </code>
  235. <h2>Constructor</h2>
  236. <h3>[name]( [param:Object value] )</h3>
  237. <p>
  238. value -- An object containing the value to set up the uniform. It's type
  239. must be one of the Uniform Types described above.
  240. </p>
  241. <h2>Properties</h2>
  242. <h3>[property:Object value]</h3>
  243. <p>Current value of the uniform.</p>
  244. <h2>Methods</h2>
  245. <h3>[method:Uniform clone]()</h3>
  246. <p>
  247. Returns a clone of this uniform.<br />
  248. If the uniform's value property is an [page:Object] with a clone() method,
  249. this is used, otherwise the value is copied by assignment. Array values
  250. are shared between cloned [page:Uniform]s.
  251. </p>
  252. <h2>Source</h2>
  253. <p>
  254. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  255. </p>
  256. </body>
  257. </html>