ShaderMaterial.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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_lightmap webgl / materials / lightmap]<br />
  108. [example:webgl_materials_wireframe webgl / materials / wireframe]<br />
  109. [example:webgl_modifier_tessellation webgl / modifier / tessellation]<br />
  110. [example:webgl_postprocessing_dof2 webgl / postprocessing / dof2]<br />
  111. [example:webgl_postprocessing_godrays webgl / postprocessing / godrays]
  112. </p>
  113. <h2>Vertex shaders and fragment shaders</h2>
  114. <div>
  115. <p>You can specify two different types of shaders for each material:</p>
  116. <ul>
  117. <li>
  118. The vertex shader runs first; it receives `attributes`, calculates /
  119. manipulates the position of each individual vertex, and passes
  120. additional data (`varying`s) to the fragment shader.
  121. </li>
  122. <li>
  123. The fragment ( or pixel ) shader runs second; it sets the color of
  124. each individual "fragment" (pixel) rendered to the screen.
  125. </li>
  126. </ul>
  127. <p>
  128. There are three types of variables in shaders: uniforms, attributes, and
  129. varyings:
  130. </p>
  131. <ul>
  132. <li>
  133. `Uniforms` are variables that have the same value for all vertices -
  134. lighting, fog, and shadow maps are examples of data that would be
  135. stored in uniforms. Uniforms can be accessed by both the vertex shader
  136. and the fragment shader.
  137. </li>
  138. <li>
  139. `Attributes` are variables associated with each vertex---for instance,
  140. the vertex position, face normal, and vertex color are all examples of
  141. data that would be stored in attributes. Attributes can `only` be
  142. accessed within the vertex shader.
  143. </li>
  144. <li>
  145. `Varyings` are variables that are passed from the vertex shader to the
  146. fragment shader. For each fragment, the value of each varying will be
  147. smoothly interpolated from the values of adjacent vertices.
  148. </li>
  149. </ul>
  150. <p>
  151. Note that `within` the shader itself, uniforms and attributes act like
  152. constants; you can only modify their values by passing different values
  153. to the buffers from your JavaScript code.
  154. </p>
  155. </div>
  156. <h2>Built-in attributes and uniforms</h2>
  157. <div>
  158. <p>
  159. The [page:WebGLRenderer] provides many attributes and uniforms to
  160. shaders by default; definitions of these variables are prepended to your
  161. `fragmentShader` and `vertexShader` code by the [page:WebGLProgram] when
  162. the shader is compiled; you don't need to declare them yourself. See
  163. [page:WebGLProgram] for details of these variables.
  164. </p>
  165. <p>
  166. Some of these uniforms or attributes (e.g. those pertaining lighting,
  167. fog, etc.) require properties to be set on the material in order for
  168. [page:WebGLRenderer] to copy the appropriate values to the GPU - make
  169. sure to set these flags if you want to use these features in your own
  170. shader.
  171. </p>
  172. <p>
  173. If you don't want [page:WebGLProgram] to add anything to your shader
  174. code, you can use [page:RawShaderMaterial] instead of this class.
  175. </p>
  176. </div>
  177. <h2>Custom attributes and uniforms</h2>
  178. <div>
  179. <p>
  180. Both custom attributes and uniforms must be declared in your GLSL shader
  181. code (within `vertexShader` and/or `fragmentShader`). Custom uniforms
  182. must be defined in `both` the `uniforms` property of your
  183. `ShaderMaterial`, whereas any custom attributes must be defined via
  184. [page:BufferAttribute] instances. Note that `varying`s only need to be
  185. declared within the shader code (not within the material).
  186. </p>
  187. <p>
  188. To declare a custom attribute, please reference the
  189. [page:BufferGeometry] page for an overview, and the
  190. [page:BufferAttribute] page for a detailed look at the `BufferAttribute`
  191. API.
  192. </p>
  193. <p>
  194. When creating your attributes, each typed array that you create to hold
  195. your attribute's data must be a multiple of your data type's size. For
  196. example, if your attribute is a [page:Vector3 THREE.Vector3] type, and
  197. you have 3000 vertices in your [page:BufferGeometry], your typed array
  198. value must be created with a length of 3000 * 3, or 9000 (one value
  199. per-component). A table of each data type's size is shown below for
  200. reference:
  201. </p>
  202. <table>
  203. <caption>
  204. <a id="attribute-sizes">Attribute sizes</a>
  205. </caption>
  206. <thead>
  207. <tr>
  208. <th>GLSL type</th>
  209. <th>JavaScript type</th>
  210. <th>Size</th>
  211. </tr>
  212. </thead>
  213. <tbody>
  214. <tr>
  215. <td>float</td>
  216. <td>[page:Number]</td>
  217. <td>1</td>
  218. </tr>
  219. <tr>
  220. <td>vec2</td>
  221. <td>[page:Vector2 THREE.Vector2]</td>
  222. <td>2</td>
  223. </tr>
  224. <tr>
  225. <td>vec3</td>
  226. <td>[page:Vector3 THREE.Vector3]</td>
  227. <td>3</td>
  228. </tr>
  229. <tr>
  230. <td>vec3</td>
  231. <td>[page:Color THREE.Color]</td>
  232. <td>3</td>
  233. </tr>
  234. <tr>
  235. <td>vec4</td>
  236. <td>[page:Vector4 THREE.Vector4]</td>
  237. <td>4</td>
  238. </tr>
  239. </tbody>
  240. </table>
  241. <p>
  242. Note that attribute buffers are `not` refreshed automatically when their
  243. values change. To update custom attributes, set the `needsUpdate` flag
  244. to true on the [page:BufferAttribute] of the geometry (see
  245. [page:BufferGeometry] for further details).
  246. </p>
  247. <p>
  248. To declare a custom [page:Uniform], use the `uniforms` property:
  249. <code>
  250. uniforms: {
  251. time: { value: 1.0 },
  252. resolution: { value: new THREE.Vector2() }
  253. }
  254. </code>
  255. </p>
  256. <p>
  257. You're recommended to update custom [page:Uniform] values depending on
  258. [page:Object3D object] and [page:Camera camera] in
  259. [page:Object3D.onBeforeRender] because [page:Material] can be shared
  260. among [page:Mesh meshes], [page:Matrix4 matrixWorld] of [page:Scene] and
  261. [page:Camera] are updated in [page:WebGLRenderer.render], and some
  262. effects render a [page:Scene scene] with their own private [page:Camera cameras].
  263. </p>
  264. </div>
  265. <h2>Constructor</h2>
  266. <h3>[name]( [param:Object parameters] )</h3>
  267. <p>
  268. [page:Object parameters] - (optional) an object with one or more
  269. properties defining the material's appearance. Any property of the
  270. material (including any property inherited from [page:Material]) can be
  271. passed in here.
  272. </p>
  273. <h2>Properties</h2>
  274. <p>See the base [page:Material] class for common properties.</p>
  275. <h3>[property:Boolean clipping]</h3>
  276. <p>
  277. Defines whether this material supports clipping; true to let the renderer
  278. pass the clippingPlanes uniform. Default is false.
  279. </p>
  280. <h3>[property:Object defaultAttributeValues]</h3>
  281. <p>
  282. When the rendered geometry doesn't include these attributes but the
  283. material does, these default values will be passed to the shaders. This
  284. avoids errors when buffer data is missing.
  285. <code>
  286. this.defaultAttributeValues = {
  287. 'color': [ 1, 1, 1 ],
  288. 'uv': [ 0, 0 ],
  289. 'uv1': [ 0, 0 ]
  290. };
  291. </code>
  292. </p>
  293. <h3>[property:Object defines]</h3>
  294. <p>
  295. Defines custom constants using `#define` directives within the GLSL code
  296. for both the vertex shader and the fragment shader; each key/value pair
  297. yields another directive:
  298. <code>
  299. defines: {
  300. FOO: 15,
  301. BAR: true
  302. }
  303. </code>
  304. yields the lines
  305. <code>
  306. #define FOO 15
  307. #define BAR true
  308. </code>
  309. in the GLSL code.
  310. </p>
  311. <h3>[property:Object extensions]</h3>
  312. <p>
  313. An object with the following properties:
  314. <code>
  315. this.extensions = {
  316. clipCullDistance: false, // set to use vertex shader clipping
  317. multiDraw: false // set to use vertex shader multi_draw / enable gl_DrawID
  318. };
  319. </code>
  320. </p>
  321. <h3>[property:Boolean fog]</h3>
  322. <p>
  323. Define whether the material color is affected by global fog settings; true
  324. to pass fog uniforms to the shader. Default is false.
  325. </p>
  326. <h3>[property:String fragmentShader]</h3>
  327. <p>
  328. Fragment shader GLSL code. This is the actual code for the shader. In the
  329. example above, the `vertexShader` and `fragmentShader` code is extracted
  330. from the DOM; it could be passed as a string directly or loaded via AJAX
  331. instead.
  332. </p>
  333. <h3>[property:String glslVersion]</h3>
  334. <p>
  335. Defines the GLSL version of custom shader code. Valid values are
  336. `THREE.GLSL1` or `THREE.GLSL3`. Default is `null`.
  337. </p>
  338. <h3>[property:String index0AttributeName]</h3>
  339. <p>
  340. If set, this calls
  341. [link:https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bindAttribLocation gl.bindAttribLocation]
  342. to bind a generic vertex index to an attribute variable. Default is undefined.
  343. </p>
  344. <h3>[property:Boolean isShaderMaterial]</h3>
  345. <p>Read-only flag to check if a given object is of type [name].</p>
  346. <h3>[property:Boolean lights]</h3>
  347. <p>
  348. Defines whether this material uses lighting; true to pass uniform data
  349. related to lighting to this shader. Default is false.
  350. </p>
  351. <h3>[property:Float linewidth]</h3>
  352. <p>
  353. Controls wireframe thickness. Default is `1`.<br /><br />
  354. Due to limitations of the
  355. [link:https://www.khronos.org/registry/OpenGL/specs/gl/glspec46.core.pdf OpenGL Core Profile]
  356. with the [page:WebGLRenderer WebGL] renderer on most
  357. platforms linewidth will always be `1` regardless of the set value.
  358. </p>
  359. <h3>[property:Boolean flatShading]</h3>
  360. <p>
  361. Define whether the material is rendered with flat shading.
  362. Default is false.
  363. </p>
  364. <h3>[property:Object uniforms]</h3>
  365. <p>
  366. An object of the form:
  367. <code>
  368. {
  369. "uniform1": { value: 1.0 },
  370. "uniform2": { value: 2 }
  371. }
  372. </code>
  373. specifying the uniforms to be passed to the shader code; keys are uniform
  374. names, values are definitions of the form
  375. <code>
  376. {
  377. value: 1.0
  378. }
  379. </code>
  380. where `value` is the value of the uniform. Names must match the name of
  381. the uniform, as defined in the GLSL code. Note that uniforms are refreshed
  382. on every frame, so updating the value of the uniform will immediately
  383. update the value available to the GLSL code.
  384. </p>
  385. <h3>[property:Boolean uniformsNeedUpdate]</h3>
  386. <p>
  387. Can be used to force a uniform update while changing uniforms in
  388. [page:Object3D.onBeforeRender](). Default is `false`.
  389. </p>
  390. <h3>[property:Boolean vertexColors]</h3>
  391. <p>Defines whether vertex coloring is used. Default is `false`.</p>
  392. <h3>[property:String vertexShader]</h3>
  393. <p>
  394. Vertex shader GLSL code. This is the actual code for the shader. In the
  395. example above, the `vertexShader` and `fragmentShader` code is extracted
  396. from the DOM; it could be passed as a string directly or loaded via AJAX
  397. instead.
  398. </p>
  399. <h3>[property:Boolean wireframe]</h3>
  400. <p>
  401. Render geometry as wireframe (using GL_LINES instead of GL_TRIANGLES).
  402. Default is false (i.e. render as flat polygons).
  403. </p>
  404. <h3>[property:Float wireframeLinewidth]</h3>
  405. <p>
  406. Controls wireframe thickness. Default is `1`.<br /><br />
  407. Due to limitations of the
  408. [link:https://www.khronos.org/registry/OpenGL/specs/gl/glspec46.core.pdf OpenGL Core Profile]
  409. with the [page:WebGLRenderer WebGL] renderer on most
  410. platforms linewidth will always be `1` regardless of the set value.
  411. </p>
  412. <h2>Methods</h2>
  413. <p>See the base [page:Material] class for common methods.</p>
  414. <h3>[method:ShaderMaterial clone]()</h3>
  415. <p>
  416. Generates a shallow copy of this material. Note that the vertexShader and
  417. fragmentShader are copied `by reference`, as are the definitions of the
  418. `attributes`; this means that clones of the material will share the same
  419. compiled [page:WebGLProgram]. However, the `uniforms` are copied `by
  420. value`, which allows you to have different sets of uniforms for different
  421. copies of the material.
  422. </p>
  423. <h2>Source</h2>
  424. <p>
  425. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  426. </p>
  427. </body>
  428. </html>