ShaderMaterial.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8" />
  5. <base href="../../" />
  6. <script src="list.js"></script>
  7. <script src="page.js"></script>
  8. <link type="text/css" rel="stylesheet" href="page.css" />
  9. </head>
  10. <body>
  11. [page:Material] &rarr;
  12. <h1>[name]</h1>
  13. <div class="desc">Material rendered with custom shaders. A shader is a small program written in [link:https://www.opengl.org/documentation/glsl/ GLSL] to run on the GPU. You may want to use a custom shader if you need to:
  14. <ul>
  15. <li>implement an effect not included with any of the built-in [page:Material materials]</li>
  16. <li>combine many objects into a single [page:Geometry] or [page:BufferGeometry] in order to improve performance</li>
  17. </ul>
  18. There are the following notes to bear in mind when using a *ShaderMaterial*:
  19. <ul>
  20. <li>A *ShaderMaterial* will only be rendered properly by [page:WebGLRenderer], since the GLSL code in the *vertexShader* and *fragmentShader* properties must be compiled and run on the GPU using WebGL.</li>
  21. <li>As of THREE r72, directly assigning attributes in a *ShaderMaterial* is no longer supported. A [page:BufferGeometry] instance (instead of a [page:Geometry] instance) must be used instead, using [page:BufferAttribute] instances to define custom attributes.</li>
  22. <li>As of THREE r77, [page:WebGLRenderTarget] or [page:WebGLRenderTargetCube] instances are no longer supposed to be used as uniforms. Their [page:Texture texture] property must be used instead.</li>
  23. </ul>
  24. </div>
  25. <h3>Example</h3>
  26. <code>
  27. var material = new THREE.ShaderMaterial( {
  28. uniforms: {
  29. time: { value: 1.0 },
  30. resolution: { value: new THREE.Vector2() }
  31. },
  32. attributes: {
  33. vertexOpacity: { value: [] }
  34. },
  35. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  36. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  37. } );
  38. </code>
  39. <h3>Vertex shaders and fragment shaders</h3>
  40. <p>You can specify two different types of shaders for each material:</p>
  41. <ul>
  42. <li>The *vertex shader* runs first; it receives *attributes*, calculates/manipulates the position of each individual vertex, and passes additional data (*varying*s) to the fragment shader.</li>
  43. <li>The *fragment shader* runs second; it sets the color of each individual "fragment" (pixel) rendered to the screen.</li>
  44. </ul>
  45. <p>There are three types of variables in shaders: uniforms, attributes, and varyings:</p>
  46. <ul>
  47. <li>*Uniforms* are variables that have the same value for all vertices---lighting, fog, and shadow maps are examples of data that would be stored in uniforms. Uniforms can be accessed by both the vertex shader and the fragment shader.</li>
  48. <li>*Attributes* are variables associated with each vertex---for instance, the vertex position, face normal, and vertex color are all examples of data that would be stored in attributes. Attributes can <em>only</em> be accessed within the vertex shader.</li>
  49. <li>*Varyings* are variables that are passed from the vertex shader to the fragment shader. For each fragment, the value of each varying will be smoothly interpolated from the values of adjacent vertices.</li>
  50. </ul>
  51. <p>Note that <em>within</em> the shader itself, uniforms and attributes act like constants; you can only modify their values by passing different values to the buffers from your JavaScript code.</p>
  52. <h3>Built-in attributes and uniforms</h3>
  53. <p>
  54. [page:WebGLRenderer] provides many attributes and uniforms to shaders by default; definitions of these variables are prepended to your *fragmentShader* and *vertexShader* code by [page:WebGLProgram] when the shader is compiled; you don't need to declare them yourself. These variables are described in [page:WebGLProgram].
  55. </p>
  56. <p>
  57. Some of these uniforms or attributes (e.g. those pertaining lighting, fog, etc.) require properties to be set on the material in order for [page:WebGLRenderer] to copy the appropriate values to the GPU---make sure to set these flags if you want to use these features in your own shader.
  58. </p>
  59. <p>
  60. If you don't want [page:WebGLProgram] to add anything to your shader code, you can use [page:RawShaderMaterial] instead of this class.
  61. </p>
  62. <h3>Custom attributes and uniforms</h3>
  63. <p>Both custom attributes and uniforms must be declared in your GLSL shader code (within *vertexShader* and/or *fragmentShader*). Custom uniforms must be defined in <em>both</em> the *uniforms* property of your *ShaderMaterial*, whereas any custom attributes must be defined via [page:BufferAttribute] instances. Note that *varying*s only need to be declared within the shader code (not within the material).
  64. </p>
  65. <p>To declare a custom attribute, please reference the [page:BufferGeometry] page for an overview, and the [page:BufferAttribute] page for a detailed look at the *BufferAttribute* API.</p>
  66. <p>When creating your attributes, each typed array that you create to hold your attribute's data must be a multiple of your data type's size. For example, if your attribute is a [page:Vector3 THREE.Vector3] type, and you have 3000 vertices in your [page:BufferGeometry], your typed array value must be created with a length of 3000 * 3, or 9000 (one value per-component). A table of each data type's size is shown below for reference:</p>
  67. <table>
  68. <caption><a id="attribute-sizes">Attribute sizes</a></caption>
  69. <thead>
  70. <tr>
  71. <th>GLSL type</th>
  72. <th>JavaScript type</th>
  73. <th>Size</th>
  74. </tr>
  75. </thead>
  76. <tbody>
  77. <tr>
  78. <td>float</td>
  79. <td>[page:Number]</td>
  80. <td>1</td>
  81. </tr>
  82. <tr>
  83. <td>vec2</td>
  84. <td>[page:Vector2 THREE.Vector2]</td>
  85. <td>2</td>
  86. </tr>
  87. <tr>
  88. <td>vec3</td>
  89. <td>[page:Vector3 THREE.Vector3]</td>
  90. <td>3</td>
  91. </tr>
  92. <tr>
  93. <td>vec3</td>
  94. <td>[page:Color THREE.Color]</td>
  95. <td>3</td>
  96. </tr>
  97. <tr>
  98. <td>vec4</td>
  99. <td>[page:Vector4 THREE.Vector4]</td>
  100. <td>4</td>
  101. </tr>
  102. </tbody>
  103. </table>
  104. <p>Note that attribute buffers are <em>not</em> refreshed automatically when their values change. To update custom attributes, set the *needsUpdate* flag to true on the [page:BufferAttribute] of the geometry (see [page:BufferGeometry] for further details).</p>
  105. <p>
  106. To declare a custom [page:Uniform], use the *uniforms* property:
  107. <code>
  108. uniforms: {
  109. time: { value: 1.0 },
  110. resolution: { value: new THREE.Vector2() }
  111. }
  112. </code>
  113. </p>
  114. <h2>Constructor</h2>
  115. <h3>[name]( [page:Object parameters] )</h3>
  116. <div>
  117. parameters -- An object containing various parameters setting up shaders and their uniforms.
  118. </div>
  119. <div>
  120. shading — Define shading type. Default is THREE.SmoothShading.<br />
  121. fog — Define whether the material color is affected by global fog settings. Default is true.<br />
  122. wireframe — render geometry as wireframe. Default is false.<br />
  123. wireframeLinewidth — Line thickness. Default is 1.<br />
  124. vertexColors — Define how the vertices gets colored. Default is THREE.NoColors.<br />
  125. skinning — Define whether the material uses skinning. Default is false.<br />
  126. morphTargets — Define whether the material uses morphTargets. Default is false.
  127. </div>
  128. <h2>Properties</h2>
  129. <div>See the base [page:Material] class for common properties.</div>
  130. <h3>[property:Object uniforms]</h3>
  131. <div>
  132. Object specifying the uniforms to be passed to the shader code; keys are uniform names, values are definitions of the form
  133. <code>
  134. { value: 1.0 }
  135. </code>
  136. where *value* is the value of the uniform. Names must match the name of the uniform, as defined in the GLSL code. Note that uniforms are refreshed on every frame, so updating the value of the uniform will immediately update the value available to the GLSL code.
  137. </div>
  138. <h3>[property:Object defines]</h3>
  139. <div>
  140. Defines custom constants using *#define* directives within the GLSL code for both the vertex shader and the fragment shader; each key/value pair yields another directive:
  141. <code>
  142. defines: {
  143. FOO: 15,
  144. BAR: true
  145. }
  146. </code>
  147. yields the lines
  148. <code>
  149. #define FOO 15
  150. #define BAR true
  151. </code>
  152. in the GLSL code.
  153. </div>
  154. <h3>[property:String vertexShader]</h3>
  155. <div>
  156. Vertex shader GLSL code. This is the actual code for the shader. In the example above, the *vertexShader* and *fragmentShader* code is extracted from the DOM; it could be passed as a string directly or loaded via AJAX instead.
  157. </div>
  158. <h3>[property:String fragmentShader]</h3>
  159. <div>
  160. Fragment shader GLSL code. This is the actual code for the shader. In the example above, the *vertexShader* and *fragmentShader* code is extracted from the DOM; it could be passed as a string directly or loaded via AJAX instead.
  161. </div>
  162. <h3>[property:Number shading]</h3>
  163. <div>
  164. Define shading type, which determines whether normals are smoothed between vertices; possible values are [page:Materials THREE.SmoothShading] or [page:Materials THREE.FlatShading]. Default is THREE.SmoothShading.
  165. </div>
  166. <h3>[property:Number linewidth]</h3>
  167. <div>Controls line thickness; only effective if the material is attached to a [page:Line]. Default is 1.</div>
  168. <div>Due to limitations in the <a href="https://code.google.com/p/angleproject/" target="_blank">ANGLE layer</a>, on Windows platforms linewidth will always be 1 regardless of the set value.</div>
  169. <h3>[property:Boolean wireframe]</h3>
  170. <div>
  171. Render geometry as wireframe (using GL_LINES instead of GL_TRIANGLES). Default is false (i.e. render as flat polygons).
  172. </div>
  173. <h3>[property:Number wireframeLinewidth]</h3>
  174. <div>Controls wireframe thickness; only effective if the material is attached to a [page:Mesh] and *wireframe* is true. Default is 1.</div>
  175. <div>Due to limitations in the <a href="https://code.google.com/p/angleproject/" target="_blank">ANGLE layer</a>, on Windows platforms linewidth will always be 1 regardless of the set value.</div>
  176. <h3>[property:Boolean fog]</h3>
  177. <div>Define whether the material color is affected by global fog settings; true to pass fog uniforms to the shader. Default is false.</div>
  178. <h3>[property:Boolean lights]</h3>
  179. <div>
  180. Defines whether this material uses lighting; true to pass uniform data related to lighting to this shader. Default is false.
  181. </div>
  182. <h3>[property:Boolean clipping]</h3>
  183. <div>
  184. Defines whether this material supports clipping; true to let the renderer pass the clippingPlanes uniform. Default is false.
  185. </div>
  186. <h3>[property:Number vertexColors]</h3>
  187. <div>
  188. Define how the vertices are colored, by defining how the *colors* attribute gets populated. Possible values are [page:Materials THREE.NoColors], [page:Materials THREE.FaceColors] and [page:Materials THREE.VertexColors]. Default is THREE.NoColors.
  189. </div>
  190. <h3>[property:Boolean skinning]</h3>
  191. <div>
  192. Define whether the material uses skinning; true to pass skinning attributes to the shader. Default is false.
  193. </div>
  194. <h3>[property:Boolean morphTargets]</h3>
  195. <div>
  196. Defines whether the material uses morphTargets; true morphTarget attributes to this shader
  197. </div>
  198. <h3>[property:boolean morphNormals]</h3>
  199. <div>
  200. Defines whether the material uses morphNormals. Set as true to pass morphNormal attributes from the [page:Geometry]
  201. to the shader. Default is *false*.
  202. </div>
  203. <h3>[property:WebGLProgram program]</h3>
  204. <div>
  205. The compiled shader program associated with this material, generated by [page:WebGLRenderer]. You should not need to access this property.
  206. </div>
  207. <h2>Methods</h2>
  208. <h3>[method:ShaderMaterial clone]() [page:ShaderMaterial this]</h3>
  209. <div>
  210. Generates a shallow copy of this material. Note that the vertexShader and fragmentShader are copied <em>by reference</em>, as are the definitions of the *attributes*; this means that clones of the material will share the same compiled [page:WebGLProgram]. However, the *uniforms* are copied <em>by value</em>, which allows you to have different sets of uniforms for different copies of the material.
  211. </div>
  212. <h2>Source</h2>
  213. [link:https://github.com/mrdoob/three.js/blob/master/src/[path].js src/[path].js]
  214. </body>
  215. </html>