ShaderMaterial.html 16 KB

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