ShaderMaterial.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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. [page:Material] &rarr;
  11. <h1>[name]</h1>
  12. <p class="desc">
  13. A material rendered with custom shaders. A shader is a small program
  14. written in
  15. [link:https://www.khronos.org/files/opengles_shading_language.pdf GLSL]
  16. that runs on the GPU. You may want to use a custom shader if you need to:
  17. </p>
  18. <ul>
  19. <li>
  20. implement an effect not included with any of the built-in [page:Material materials]
  21. </li>
  22. <li>
  23. combine many objects into a single [page:BufferGeometry] in order to
  24. improve performance
  25. </li>
  26. </ul>
  27. There are the following notes to bear in mind when using a `ShaderMaterial`:
  28. <ul>
  29. <li>
  30. A `ShaderMaterial` will only be rendered properly by
  31. [page:WebGLRenderer], since the GLSL code in the
  32. [link:https://en.wikipedia.org/wiki/Shader#Vertex_shaders vertexShader]
  33. and [link:https://en.wikipedia.org/wiki/Shader#Pixel_shaders fragmentShader]
  34. properties must be compiled and run on the GPU using WebGL.
  35. </li>
  36. <li>
  37. As of THREE r72, directly assigning attributes in a ShaderMaterial is no
  38. longer supported. A [page:BufferGeometry] instance must be used instead,
  39. using [page:BufferAttribute] instances to define custom attributes.
  40. </li>
  41. <li>
  42. As of THREE r77, [page:WebGLRenderTarget] or
  43. [page:WebGLCubeRenderTarget] instances are no longer supposed to be used
  44. as uniforms. Their [page:Texture texture] property must be used instead.
  45. </li>
  46. <li>
  47. Built in attributes and uniforms are passed to the shaders along with
  48. your code. If you don't want the [page:WebGLProgram] to add anything to
  49. your shader code, you can use [page:RawShaderMaterial] instead of this
  50. class.
  51. </li>
  52. <li>
  53. You can use the directive #pragma unroll_loop_start and #pragma
  54. unroll_loop_end in order to unroll a `for` loop in GLSL by the shader
  55. preprocessor. The directive has to be placed right above the loop. The
  56. loop formatting has to correspond to a defined standard.
  57. <ul>
  58. <li>
  59. The loop has to be
  60. [link:https://en.wikipedia.org/wiki/Normalized_loop normalized].
  61. </li>
  62. <li>The loop variable has to be *i*.</li>
  63. <li>
  64. The value `UNROLLED_LOOP_INDEX` will be replaced with the explicitly
  65. value of *i* for the given iteration and can be used in preprocessor
  66. statements.
  67. </li>
  68. </ul>
  69. <code>
  70. #pragma unroll_loop_start
  71. for ( int i = 0; i < 10; i ++ ) {
  72. // ...
  73. }
  74. #pragma unroll_loop_end
  75. </code>
  76. </li>
  77. </ul>
  78. <h2>Code Example</h2>
  79. <code>
  80. const material = new THREE.ShaderMaterial( {
  81. uniforms: {
  82. time: { value: 1.0 },
  83. resolution: { value: new THREE.Vector2() }
  84. },
  85. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  86. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  87. } );
  88. </code>
  89. <h2>Examples</h2>
  90. <p>
  91. [example:webgl_buffergeometry_custom_attributes_particles webgl / buffergeometry / custom / attributes / particles]<br />
  92. [example:webgl_buffergeometry_selective_draw webgl / buffergeometry / selective / draw]<br />
  93. [example:webgl_custom_attributes webgl / custom / attributes]<br />
  94. [example:webgl_custom_attributes_lines webgl / custom / attributes / lines]<br />
  95. [example:webgl_custom_attributes_points webgl / custom / attributes / points]<br />
  96. [example:webgl_custom_attributes_points2 webgl / custom / attributes / points2]<br />
  97. [example:webgl_custom_attributes_points3 webgl / custom / attributes / points3]<br />
  98. [example:webgl_depth_texture webgl / depth / texture]<br />
  99. [example:webgl_gpgpu_birds webgl / gpgpu / birds]<br />
  100. [example:webgl_gpgpu_protoplanet webgl / gpgpu / protoplanet]<br />
  101. [example:webgl_gpgpu_water webgl / gpgpu / water]<br />
  102. [example:webgl_interactive_points webgl / interactive / points]<br />
  103. [example:webgl_video_kinect webgl / video / kinect]<br />
  104. [example:webgl_lights_hemisphere webgl / lights / hemisphere]<br />
  105. [example:webgl_marchingcubes webgl / marchingcubes]<br />
  106. [example:webgl_materials_envmaps webgl / materials / envmaps]<br />
  107. [example:webgl_materials_wireframe webgl / materials / wireframe]<br />
  108. [example:webgl_modifier_tessellation webgl / modifier / tessellation]<br />
  109. [example:webgl_postprocessing_dof2 webgl / postprocessing / dof2]<br />
  110. [example:webgl_postprocessing_godrays webgl / postprocessing / godrays]
  111. </p>
  112. <h2>Vertex shaders and fragment shaders</h2>
  113. <div>
  114. <p>You can specify two different types of shaders for each material:</p>
  115. <ul>
  116. <li>
  117. The vertex shader runs first; it receives `attributes`, calculates /
  118. manipulates the position of each individual vertex, and passes
  119. additional data (`varying`s) to the fragment shader.
  120. </li>
  121. <li>
  122. The fragment ( or pixel ) shader runs second; it sets the color of
  123. each individual "fragment" (pixel) rendered to the screen.
  124. </li>
  125. </ul>
  126. <p>
  127. There are three types of variables in shaders: uniforms, attributes, and
  128. varyings:
  129. </p>
  130. <ul>
  131. <li>
  132. `Uniforms` are variables that have the same value for all vertices -
  133. lighting, fog, and shadow maps are examples of data that would be
  134. stored in uniforms. Uniforms can be accessed by both the vertex shader
  135. and the fragment shader.
  136. </li>
  137. <li>
  138. `Attributes` are variables associated with each vertex---for instance,
  139. the vertex position, face normal, and vertex color are all examples of
  140. data that would be stored in attributes. Attributes can `only` be
  141. accessed within the vertex shader.
  142. </li>
  143. <li>
  144. `Varyings` are variables that are passed from the vertex shader to the
  145. fragment shader. For each fragment, the value of each varying will be
  146. smoothly interpolated from the values of adjacent vertices.
  147. </li>
  148. </ul>
  149. <p>
  150. Note that `within` the shader itself, uniforms and attributes act like
  151. constants; you can only modify their values by passing different values
  152. to the buffers from your JavaScript code.
  153. </p>
  154. </div>
  155. <h2>Built-in attributes and uniforms</h2>
  156. <div>
  157. <p>
  158. The [page:WebGLRenderer] provides many attributes and uniforms to
  159. shaders by default; definitions of these variables are prepended to your
  160. `fragmentShader` and `vertexShader` code by the [page:WebGLProgram] when
  161. the shader is compiled; you don't need to declare them yourself. See
  162. [page:WebGLProgram] for details of these variables.
  163. </p>
  164. <p>
  165. Some of these uniforms or attributes (e.g. those pertaining lighting,
  166. fog, etc.) require properties to be set on the material in order for
  167. [page:WebGLRenderer] to copy the appropriate values to the GPU - make
  168. sure to set these flags if you want to use these features in your own
  169. shader.
  170. </p>
  171. <p>
  172. If you don't want [page:WebGLProgram] to add anything to your shader
  173. code, you can use [page:RawShaderMaterial] instead of this class.
  174. </p>
  175. </div>
  176. <h2>Custom attributes and uniforms</h2>
  177. <div>
  178. <p>
  179. Both custom attributes and uniforms must be declared in your GLSL shader
  180. code (within `vertexShader` and/or `fragmentShader`). Custom uniforms
  181. must be defined in `both` the `uniforms` property of your
  182. `ShaderMaterial`, whereas any custom attributes must be defined via
  183. [page:BufferAttribute] instances. Note that `varying`s only need to be
  184. declared within the shader code (not within the material).
  185. </p>
  186. <p>
  187. To declare a custom attribute, please reference the
  188. [page:BufferGeometry] page for an overview, and the
  189. [page:BufferAttribute] page for a detailed look at the `BufferAttribute`
  190. API.
  191. </p>
  192. <p>
  193. When creating your attributes, each typed array that you create to hold
  194. your attribute's data must be a multiple of your data type's size. For
  195. example, if your attribute is a [page:Vector3 THREE.Vector3] type, and
  196. you have 3000 vertices in your [page:BufferGeometry], your typed array
  197. value must be created with a length of 3000 * 3, or 9000 (one value
  198. per-component). A table of each data type's size is shown below for
  199. reference:
  200. </p>
  201. <table>
  202. <caption>
  203. <a id="attribute-sizes">Attribute sizes</a>
  204. </caption>
  205. <thead>
  206. <tr>
  207. <th>GLSL type</th>
  208. <th>JavaScript type</th>
  209. <th>Size</th>
  210. </tr>
  211. </thead>
  212. <tbody>
  213. <tr>
  214. <td>float</td>
  215. <td>[page:Number]</td>
  216. <td>1</td>
  217. </tr>
  218. <tr>
  219. <td>vec2</td>
  220. <td>[page:Vector2 THREE.Vector2]</td>
  221. <td>2</td>
  222. </tr>
  223. <tr>
  224. <td>vec3</td>
  225. <td>[page:Vector3 THREE.Vector3]</td>
  226. <td>3</td>
  227. </tr>
  228. <tr>
  229. <td>vec3</td>
  230. <td>[page:Color THREE.Color]</td>
  231. <td>3</td>
  232. </tr>
  233. <tr>
  234. <td>vec4</td>
  235. <td>[page:Vector4 THREE.Vector4]</td>
  236. <td>4</td>
  237. </tr>
  238. </tbody>
  239. </table>
  240. <p>
  241. Note that attribute buffers are `not` refreshed automatically when their
  242. values change. To update custom attributes, set the `needsUpdate` flag
  243. to true on the [page:BufferAttribute] of the geometry (see
  244. [page:BufferGeometry] for further details).
  245. </p>
  246. <p>
  247. To declare a custom [page:Uniform], use the `uniforms` property:
  248. <code>
  249. uniforms: {
  250. time: { value: 1.0 },
  251. resolution: { value: new THREE.Vector2() }
  252. }
  253. </code>
  254. </p>
  255. <p>
  256. You're recommended to update custom [page:Uniform] values depending on
  257. [page:Object3D object] and [page:Camera camera] in
  258. [page:Object3D.onBeforeRender] because [page:Material] can be shared
  259. among [page:Mesh meshes], [page:Matrix4 matrixWorld] of [page:Scene] and
  260. [page:Camera] are updated in [page:WebGLRenderer.render], and some
  261. effects render a [page:Scene scene] with their own private [page:Camera cameras].
  262. </p>
  263. </div>
  264. <h2>Constructor</h2>
  265. <h3>[name]( [param:Object parameters] )</h3>
  266. <p>
  267. [page:Object parameters] - (optional) an object with one or more
  268. properties defining the material's appearance. Any property of the
  269. material (including any property inherited from [page:Material]) can be
  270. passed in here.
  271. </p>
  272. <h2>Properties</h2>
  273. <p>See the base [page:Material] class for common properties.</p>
  274. <h3>[property:Boolean clipping]</h3>
  275. <p>
  276. Defines whether this material supports clipping; true to let the renderer
  277. pass the clippingPlanes uniform. Default is false.
  278. </p>
  279. <h3>[property:Object defaultAttributeValues]</h3>
  280. <p>
  281. When the rendered geometry doesn't include these attributes but the
  282. material does, these default values will be passed to the shaders. This
  283. avoids errors when buffer data is missing.
  284. <code>
  285. this.defaultAttributeValues = {
  286. 'color': [ 1, 1, 1 ],
  287. 'uv': [ 0, 0 ],
  288. 'uv1': [ 0, 0 ]
  289. };
  290. </code>
  291. </p>
  292. <h3>[property:Object defines]</h3>
  293. <p>
  294. Defines custom constants using `#define` directives within the GLSL code
  295. for both the vertex shader and the fragment shader; each key/value pair
  296. yields another directive:
  297. <code>
  298. defines: {
  299. FOO: 15,
  300. BAR: true
  301. }
  302. </code>
  303. yields the lines
  304. <code>
  305. #define FOO 15
  306. #define BAR true
  307. </code>
  308. in the GLSL code.
  309. </p>
  310. <h3>[property:Object extensions]</h3>
  311. <p>
  312. An object with the following properties:
  313. <code>
  314. this.extensions = {
  315. clipCullDistance: false, // set to use vertex shader clipping
  316. multiDraw: false // set to use vertex shader multi_draw / enable gl_DrawID
  317. };
  318. </code>
  319. </p>
  320. <h3>[property:Boolean fog]</h3>
  321. <p>
  322. Define whether the material color is affected by global fog settings; true
  323. to pass fog uniforms to the shader. Default is false.
  324. </p>
  325. <h3>[property:String fragmentShader]</h3>
  326. <p>
  327. Fragment shader GLSL code. This is the actual code for the shader. In the
  328. example above, the `vertexShader` and `fragmentShader` code is extracted
  329. from the DOM; it could be passed as a string directly or loaded via AJAX
  330. instead.
  331. </p>
  332. <h3>[property:String glslVersion]</h3>
  333. <p>
  334. Defines the GLSL version of custom shader code. Valid values are
  335. `THREE.GLSL1` or `THREE.GLSL3`. Default is `null`.
  336. </p>
  337. <h3>[property:String index0AttributeName]</h3>
  338. <p>
  339. If set, this calls
  340. [link:https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bindAttribLocation gl.bindAttribLocation]
  341. to bind a generic vertex index to an attribute variable. Default is undefined.
  342. </p>
  343. <h3>[property:Boolean isShaderMaterial]</h3>
  344. <p>Read-only flag to check if a given object is of type [name].</p>
  345. <h3>[property:Boolean lights]</h3>
  346. <p>
  347. Defines whether this material uses lighting; true to pass uniform data
  348. related to lighting to this shader. Default is false.
  349. </p>
  350. <h3>[property:Float linewidth]</h3>
  351. <p>
  352. Controls wireframe thickness. Default is `1`.<br /><br />
  353. Due to limitations of the
  354. [link:https://www.khronos.org/registry/OpenGL/specs/gl/glspec46.core.pdf OpenGL Core Profile]
  355. with the [page:WebGLRenderer WebGL] renderer on most
  356. platforms linewidth will always be `1` regardless of the set value.
  357. </p>
  358. <h3>[property:Boolean flatShading]</h3>
  359. <p>
  360. Define whether the material is rendered with flat shading.
  361. Default is false.
  362. </p>
  363. <h3>[property:Object uniforms]</h3>
  364. <p>
  365. An object of the form:
  366. <code>
  367. {
  368. "uniform1": { value: 1.0 },
  369. "uniform2": { value: 2 }
  370. }
  371. </code>
  372. specifying the uniforms to be passed to the shader code; keys are uniform
  373. names, values are definitions of the form
  374. <code>
  375. {
  376. value: 1.0
  377. }
  378. </code>
  379. where `value` is the value of the uniform. Names must match the name of
  380. the uniform, as defined in the GLSL code. Note that uniforms are refreshed
  381. on every frame, so updating the value of the uniform will immediately
  382. update the value available to the GLSL code.
  383. </p>
  384. <h3>[property:Boolean uniformsNeedUpdate]</h3>
  385. <p>
  386. Can be used to force a uniform update while changing uniforms in
  387. [page:Object3D.onBeforeRender](). Default is `false`.
  388. </p>
  389. <h3>[property:Boolean vertexColors]</h3>
  390. <p>Defines whether vertex coloring is used. Default is `false`.</p>
  391. <h3>[property:String vertexShader]</h3>
  392. <p>
  393. Vertex shader GLSL code. This is the actual code for the shader. In the
  394. example above, the `vertexShader` and `fragmentShader` code is extracted
  395. from the DOM; it could be passed as a string directly or loaded via AJAX
  396. instead.
  397. </p>
  398. <h3>[property:Boolean wireframe]</h3>
  399. <p>
  400. Render geometry as wireframe (using GL_LINES instead of GL_TRIANGLES).
  401. Default is false (i.e. render as flat polygons).
  402. </p>
  403. <h3>[property:Float wireframeLinewidth]</h3>
  404. <p>
  405. Controls wireframe thickness. Default is `1`.<br /><br />
  406. Due to limitations of the
  407. [link:https://www.khronos.org/registry/OpenGL/specs/gl/glspec46.core.pdf OpenGL Core Profile]
  408. with the [page:WebGLRenderer WebGL] renderer on most
  409. platforms linewidth will always be `1` regardless of the set value.
  410. </p>
  411. <h2>Methods</h2>
  412. <p>See the base [page:Material] class for common methods.</p>
  413. <h3>[method:ShaderMaterial clone]()</h3>
  414. <p>
  415. Generates a shallow copy of this material. Note that the vertexShader and
  416. fragmentShader are copied `by reference`, as are the definitions of the
  417. `attributes`; this means that clones of the material will share the same
  418. compiled [page:WebGLProgram]. However, the `uniforms` are copied `by
  419. value`, which allows you to have different sets of uniforms for different
  420. copies of the material.
  421. </p>
  422. <h2>Source</h2>
  423. <p>
  424. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  425. </p>
  426. </body>
  427. </html>