uniform-types.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>Uniform Types</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – Uniform Types">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>Uniform Types</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>
  29. Each uniform must have a `value` property. The type of the value must
  30. correspond to the type of the uniform variable in the GLSL code as
  31. specified for the primitive GLSL types in the table below. Uniform
  32. structures and arrays are also supported. GLSL arrays of primitive type
  33. must either be specified as an array of the corresponding THREE objects or
  34. as a flat array containing the data of all the objects. In other words;
  35. GLSL primitives in arrays must not be represented by arrays. This rule
  36. does not apply transitively. An array of `vec2` arrays, each with a length
  37. of five vectors, must be an array of arrays, of either five `Vector2`
  38. objects or ten `number`s.
  39. </p>
  40. <table>
  41. <thead>
  42. <tr>
  43. <th>GLSL type</th>
  44. <th>JavaScript type</th>
  45. </tr>
  46. </thead>
  47. <tbody>
  48. <tr>
  49. <td>int</td>
  50. <td>Number</td>
  51. </tr>
  52. <tr>
  53. <td>uint</td>
  54. <td>Number</td>
  55. </tr>
  56. <tr>
  57. <td>float</td>
  58. <td>Number</td>
  59. </tr>
  60. <tr>
  61. <td>bool</td>
  62. <td>Boolean</td>
  63. </tr>
  64. <tr>
  65. <td>bool</td>
  66. <td>Number</td>
  67. </tr>
  68. <tr>
  69. <td>vec2</td>
  70. <td>Vector2</td>
  71. </tr>
  72. <tr>
  73. <td>vec2</td>
  74. <td>Float32Array (*)</td>
  75. </tr>
  76. <tr>
  77. <td>vec2</td>
  78. <td>Array (*)</td>
  79. </tr>
  80. <tr>
  81. <td>vec3</td>
  82. <td>Vector3</td>
  83. </tr>
  84. <tr>
  85. <td>vec3</td>
  86. <td>Color</td>
  87. </tr>
  88. <tr>
  89. <td>vec3</td>
  90. <td>Float32Array (*)</td>
  91. </tr>
  92. <tr>
  93. <td>vec3</td>
  94. <td>Array (*)</td>
  95. </tr>
  96. <tr>
  97. <td>vec4</td>
  98. <td>Vector4</td>
  99. </tr>
  100. <tr>
  101. <td>vec4</td>
  102. <td>Quaternion</td>
  103. </tr>
  104. <tr>
  105. <td>vec4</td>
  106. <td>Float32Array (*)</td>
  107. </tr>
  108. <tr>
  109. <td>vec4</td>
  110. <td>Array (*)</td>
  111. </tr>
  112. <tr>
  113. <td>mat2</td>
  114. <td>Float32Array (*)</td>
  115. </tr>
  116. <tr>
  117. <td>mat2</td>
  118. <td>Array (*)</td>
  119. </tr>
  120. <tr>
  121. <td>mat3</td>
  122. <td>Matrix3</td>
  123. </tr>
  124. <tr>
  125. <td>mat3</td>
  126. <td>Float32Array (*)</td>
  127. </tr>
  128. <tr>
  129. <td>mat3</td>
  130. <td>Array (*)</td>
  131. </tr>
  132. <tr>
  133. <td>mat4</td>
  134. <td>Matrix4</td>
  135. </tr>
  136. <tr>
  137. <td>mat4</td>
  138. <td>Float32Array (*)</td>
  139. </tr>
  140. <tr>
  141. <td>mat4</td>
  142. <td>Array (*)</td>
  143. </tr>
  144. <tr>
  145. <td>ivec2, bvec2</td>
  146. <td>Float32Array (*)</td>
  147. </tr>
  148. <tr>
  149. <td>ivec2, bvec2</td>
  150. <td>Array (*)</td>
  151. </tr>
  152. <tr>
  153. <td>ivec3, bvec3</td>
  154. <td>Int32Array (*)</td>
  155. </tr>
  156. <tr>
  157. <td>ivec3, bvec3</td>
  158. <td>Array (*)</td>
  159. </tr>
  160. <tr>
  161. <td>ivec4, bvec4</td>
  162. <td>Int32Array (*)</td>
  163. </tr>
  164. <tr>
  165. <td>ivec4, bvec4</td>
  166. <td>Array (*)</td>
  167. </tr>
  168. <tr>
  169. <td>sampler2D</td>
  170. <td>Texture</td>
  171. </tr>
  172. <tr>
  173. <td>samplerCube</td>
  174. <td>CubeTexture</td>
  175. </tr>
  176. </tbody>
  177. </table>
  178. <p>
  179. (*) Same for an (innermost) array (dimension) of the same GLSL type,
  180. containing the components of all vectors or matrices in the array.
  181. </p>
  182. <h2>Structured Uniforms</h2>
  183. <p>
  184. Sometimes you want to organize uniforms as `structs` in your shader code.
  185. The following style must be used so `three.js` is able to process
  186. structured uniform data.
  187. </p>
  188. <pre class="prettyprint notranslate lang-js" translate="no">
  189. uniforms = {
  190. data: {
  191. value: {
  192. position: new Vector3(),
  193. direction: new Vector3( 0, 0, 1 )
  194. }
  195. }
  196. };
  197. </pre>
  198. This definition can be mapped on the following GLSL code:
  199. <pre class="prettyprint notranslate lang-js" translate="no">
  200. struct Data {
  201. vec3 position;
  202. vec3 direction;
  203. };
  204. uniform Data data;
  205. </pre>
  206. <h2>Structured Uniforms with Arrays</h2>
  207. <p>
  208. It's also possible to manage `structs` in arrays. The syntax for this use
  209. case looks like so:
  210. </p>
  211. <pre class="prettyprint notranslate lang-js" translate="no">
  212. const entry1 = {
  213. position: new Vector3(),
  214. direction: new Vector3( 0, 0, 1 )
  215. };
  216. const entry2 = {
  217. position: new Vector3( 1, 1, 1 ),
  218. direction: new Vector3( 0, 1, 0 )
  219. };
  220. uniforms = {
  221. data: {
  222. value: [ entry1, entry2 ]
  223. }
  224. };
  225. </pre>
  226. This definition can be mapped on the following GLSL code:
  227. <pre class="prettyprint notranslate lang-js" translate="no">
  228. struct Data {
  229. vec3 position;
  230. vec3 direction;
  231. };
  232. uniform Data data[ 2 ];
  233. </pre>
  234. </div>
  235. </div>
  236. </div>
  237. <script src="../resources/prettify.js"></script>
  238. <script src="../resources/lesson.js"></script>
  239. </body></html>
粤ICP备19079148号