ShaderMaterial.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>ShaderMaterial - Three.js Docs</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <script src="../scripts/highlight.min.js"></script>
  8. <link type="text/css" rel="stylesheet" href="../styles/highlight-three.css">
  9. <link type="text/css" rel="stylesheet" href="../styles/page.css">
  10. </head>
  11. <body>
  12. <p class="inheritance" translate="no"><a href="EventDispatcher.html">EventDispatcher</a> → <a href="Material.html">Material</a> → </p>
  13. <h1 translate="no">ShaderMaterial</h1>
  14. <section>
  15. <header>
  16. <div class="class-description"><p>A material rendered with custom shaders. A shader is a small program written in GLSL.
  17. that runs on the GPU. You may want to use a custom shader if you need to implement an
  18. effect not included with any of the built-in materials.</p>
  19. <p>There are the following notes to bear in mind when using a <code>ShaderMaterial</code>:</p>
  20. <ul>
  21. <li><code>ShaderMaterial</code> can only be used with <a href="WebGLRenderer.html">WebGLRenderer</a>.</li>
  22. <li>Built in attributes and uniforms are passed to the shaders along with your code. If
  23. you don't want that, use <a href="RawShaderMaterial.html">RawShaderMaterial</a> instead.</li>
  24. <li>You can use the directive <code>#pragma unroll_loop_start</code> and <code>#pragma unroll_loop_end</code>
  25. in order to unroll a <code>for</code> loop in GLSL by the shader preprocessor. The directive has
  26. to be placed right above the loop. The loop formatting has to correspond to a defined standard.
  27. <ul>
  28. <li>The loop has to be <a href="https://en.wikipedia.org/wiki/Normalized_loop" target="_blank" rel="noopener">normalized</a>.</li>
  29. <li>The loop variable has to be <em>i</em>.</li>
  30. <li>The value <code>UNROLLED_LOOP_INDEX</code> will be replaced with the explicitly
  31. value of <em>i</em> for the given iteration and can be used in preprocessor
  32. statements.</li>
  33. </ul>
  34. </li>
  35. </ul></div>
  36. <h2>Code Example</h2>
  37. <div translate="no"><pre><code class="language-js">const material = new THREE.ShaderMaterial( {
  38. uniforms: {
  39. time: { value: 1.0 },
  40. resolution: { value: new THREE.Vector2() }
  41. },
  42. vertexShader: document.getElementById( 'vertexShader' ).textContent,
  43. fragmentShader: document.getElementById( 'fragmentShader' ).textContent
  44. } );
  45. </code></pre></div>
  46. </header>
  47. <article>
  48. <div class="container-overview">
  49. <h2>Constructor</h2>
  50. <h3 class="name name-method" id="ShaderMaterial" translate="no">new <a href="#ShaderMaterial">ShaderMaterial</a><span class="signature">( parameters : <span class="param-type">Object</span> )</span> </h3>
  51. <div class="method">
  52. <div class="description">
  53. <p>Constructs a new shader material.</p>
  54. </div>
  55. <table class="params">
  56. <tbody>
  57. <tr>
  58. <td class="name">
  59. <strong>parameters</strong>
  60. </td>
  61. <td class="description last">
  62. <p>An object with one or more properties
  63. defining the material's appearance. Any property of the material
  64. (including any property from inherited materials) can be passed
  65. in here. Color values can be passed any type of value accepted
  66. by <a href="Color.html#set">Color#set</a>.</p>
  67. </td>
  68. </tr>
  69. </tbody>
  70. </table>
  71. </div>
  72. </div>
  73. <h2 class="subsection-title">Properties</h2>
  74. <div class="member">
  75. <h3 class="name" id="clipping" translate="no">.<a href="#clipping">clipping</a><span class="type-signature"> : boolean</span> </h3>
  76. <div class="description">
  77. <p>Defines whether this material supports clipping; <code>true</code> to let the renderer
  78. pass the clippingPlanes uniform.</p>
  79. <p>Default is <code>false</code>.</p>
  80. </div>
  81. </div>
  82. <div class="member">
  83. <h3 class="name" id="defaultAttributeValues" translate="no">.<a href="#defaultAttributeValues">defaultAttributeValues</a><span class="type-signature"> : Object</span> </h3>
  84. <div class="description">
  85. <p>When the rendered geometry doesn't include these attributes but the
  86. material does, these default values will be passed to the shaders. This
  87. avoids errors when buffer data is missing.</p>
  88. <ul>
  89. <li>color: [ 1, 1, 1 ]</li>
  90. <li>uv: [ 0, 0 ]</li>
  91. <li>uv1: [ 0, 0 ]</li>
  92. </ul>
  93. </div>
  94. </div>
  95. <div class="member">
  96. <h3 class="name" id="defines" translate="no">.<a href="#defines">defines</a><span class="type-signature"> : Object</span> </h3>
  97. <div class="description">
  98. <p>Defines custom constants using <code>#define</code> directives within the GLSL code
  99. for both the vertex shader and the fragment shader; each key/value pair
  100. yields another directive.</p>
  101. <pre><code class="language-js">defines: {
  102. FOO: 15,
  103. BAR: true
  104. }
  105. </code></pre>
  106. <p>Yields the lines:</p>
  107. <pre class="prettyprint source"><code>#define FOO 15
  108. #define BAR true
  109. </code></pre>
  110. </div>
  111. </div>
  112. <div class="member">
  113. <h3 class="name" id="extensions" translate="no">.<a href="#extensions">extensions</a><span class="type-signature"> : Object</span> </h3>
  114. <div class="description">
  115. <p>This object allows to enable certain WebGL 2 extensions.</p>
  116. <ul>
  117. <li>clipCullDistance: set to <code>true</code> to use vertex shader clipping</li>
  118. <li>multiDraw: set to <code>true</code> to use vertex shader multi_draw / enable gl_DrawID</li>
  119. </ul>
  120. </div>
  121. </div>
  122. <div class="member">
  123. <h3 class="name" id="fog" translate="no">.<a href="#fog">fog</a><span class="type-signature"> : boolean</span> </h3>
  124. <div class="description">
  125. <p>Defines whether the material color is affected by global fog settings; <code>true</code>
  126. to pass fog uniforms to the shader.</p>
  127. <p>Setting this property to <code>true</code> requires the definition of fog uniforms. It is
  128. recommended to use <code>UniformsUtils.merge()</code> to combine the custom shader uniforms
  129. with predefined fog uniforms.</p>
  130. <pre><code class="language-js">const material = new ShaderMaterial( {
  131. uniforms: UniformsUtils.merge( [ UniformsLib[ 'fog' ], shaderUniforms ] );
  132. vertexShader: vertexShader,
  133. fragmentShader: fragmentShader,
  134. fog: true
  135. } );
  136. </code></pre>
  137. <p>Default is <code>false</code>.</p>
  138. </div>
  139. </div>
  140. <div class="member">
  141. <h3 class="name" id="forceSinglePass" translate="no">.<a href="#forceSinglePass">forceSinglePass</a><span class="type-signature"> : boolean</span> </h3>
  142. <div class="description">
  143. <p>Overwritten and set to <code>true</code> by default.</p>
  144. <p>Default is <code>true</code>.</p>
  145. </div>
  146. <dl class="details">
  147. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Material.html#forceSinglePass">Material#forceSinglePass</a></dt>
  148. </dl>
  149. </div>
  150. <div class="member">
  151. <h3 class="name" id="fragmentShader" translate="no">.<a href="#fragmentShader">fragmentShader</a><span class="type-signature"> : string</span> </h3>
  152. <div class="description">
  153. <p>Fragment shader GLSL code. This is the actual code for the shader.</p>
  154. </div>
  155. </div>
  156. <div class="member">
  157. <h3 class="name" id="glslVersion" translate="no">.<a href="#glslVersion">glslVersion</a><span class="type-signature"> : <a href="global.html#GLSL1">GLSL1</a> | <a href="global.html#GLSL3">GLSL3</a></span> </h3>
  158. <div class="description">
  159. <p>Defines the GLSL version of custom shader code.</p>
  160. <p>Default is <code>null</code>.</p>
  161. </div>
  162. </div>
  163. <div class="member">
  164. <h3 class="name" id="index0AttributeName" translate="no">.<a href="#index0AttributeName">index0AttributeName</a><span class="type-signature"> : string | undefined</span> </h3>
  165. <div class="description">
  166. <p>If set, this calls <a href="https://developer.mozilla.org/en-US/docs/Web/API/WebGLRenderingContext/bindAttribLocation" target="_blank" rel="noopener">gl.bindAttribLocation</a>
  167. to bind a generic vertex index to an attribute variable.</p>
  168. <p>Default is <code>undefined</code>.</p>
  169. </div>
  170. </div>
  171. <div class="member">
  172. <h3 class="name" id="isShaderMaterial" translate="no">.<a href="#isShaderMaterial">isShaderMaterial</a><span class="type-signature"> : boolean</span> <span class="type-signature">(readonly) </span></h3>
  173. <div class="description">
  174. <p>This flag can be used for type testing.</p>
  175. <p>Default is <code>true</code>.</p>
  176. </div>
  177. </div>
  178. <div class="member">
  179. <h3 class="name" id="lights" translate="no">.<a href="#lights">lights</a><span class="type-signature"> : boolean</span> </h3>
  180. <div class="description">
  181. <p>Defines whether this material uses lighting; <code>true</code> to pass uniform data
  182. related to lighting to this shader.</p>
  183. <p>Default is <code>false</code>.</p>
  184. </div>
  185. </div>
  186. <div class="member">
  187. <h3 class="name" id="linewidth" translate="no">.<a href="#linewidth">linewidth</a><span class="type-signature"> : number</span> </h3>
  188. <div class="description">
  189. <p>Controls line thickness or lines.</p>
  190. <p>WebGL and WebGPU ignore this setting and always render line primitives with a
  191. width of one pixel.</p>
  192. <p>Default is <code>1</code>.</p>
  193. </div>
  194. </div>
  195. <div class="member">
  196. <h3 class="name" id="uniforms" translate="no">.<a href="#uniforms">uniforms</a><span class="type-signature"> : Object</span> </h3>
  197. <div class="description">
  198. <p>An object of the form:</p>
  199. <pre><code class="language-js">{
  200. &quot;uniform1&quot;: { value: 1.0 },
  201. &quot;uniform2&quot;: { value: 2 }
  202. }
  203. </code></pre>
  204. <p>specifying the uniforms to be passed to the shader code; keys are uniform
  205. names, values are definitions of the form</p>
  206. <pre class="prettyprint source"><code>{
  207. value: 1.0
  208. }
  209. </code></pre>
  210. <p>where <code>value</code> is the value of the uniform. Names must match the name of
  211. the uniform, as defined in the GLSL code. Note that uniforms are refreshed
  212. on every frame, so updating the value of the uniform will immediately
  213. update the value available to the GLSL code.</p>
  214. </div>
  215. </div>
  216. <div class="member">
  217. <h3 class="name" id="uniformsGroups" translate="no">.<a href="#uniformsGroups">uniformsGroups</a><span class="type-signature"> : Array.&lt;<a href="UniformsGroup.html">UniformsGroup</a>></span> </h3>
  218. <div class="description">
  219. <p>An array holding uniforms groups for configuring UBOs.</p>
  220. </div>
  221. </div>
  222. <div class="member">
  223. <h3 class="name" id="uniformsNeedUpdate" translate="no">.<a href="#uniformsNeedUpdate">uniformsNeedUpdate</a><span class="type-signature"> : boolean</span> </h3>
  224. <div class="description">
  225. <p>Can be used to force a uniform update while changing uniforms in
  226. <a href="Object3D.html#onBeforeRender">Object3D#onBeforeRender</a>.</p>
  227. <p>Default is <code>false</code>.</p>
  228. </div>
  229. </div>
  230. <div class="member">
  231. <h3 class="name" id="vertexShader" translate="no">.<a href="#vertexShader">vertexShader</a><span class="type-signature"> : string</span> </h3>
  232. <div class="description">
  233. <p>Vertex shader GLSL code. This is the actual code for the shader.</p>
  234. </div>
  235. </div>
  236. <div class="member">
  237. <h3 class="name" id="wireframe" translate="no">.<a href="#wireframe">wireframe</a><span class="type-signature"> : boolean</span> </h3>
  238. <div class="description">
  239. <p>Renders the geometry as a wireframe.</p>
  240. <p>Default is <code>false</code>.</p>
  241. </div>
  242. </div>
  243. <div class="member">
  244. <h3 class="name" id="wireframeLinewidth" translate="no">.<a href="#wireframeLinewidth">wireframeLinewidth</a><span class="type-signature"> : number</span> </h3>
  245. <div class="description">
  246. <p>Controls the thickness of the wireframe.</p>
  247. <p>WebGL and WebGPU ignore this property and always render
  248. 1 pixel wide lines.</p>
  249. <p>Default is <code>1</code>.</p>
  250. </div>
  251. </div>
  252. <h2 class="subsection-title">Type Definitions</h2>
  253. <div class="member">
  254. <h3 class="name" id="~Shader" translate="no">.<a href="#~Shader">Shader</a> </h3>
  255. <div class="description">
  256. <p>This type represents the fields required to store and run the shader code.</p>
  257. </div>
  258. <table class="props">
  259. <tbody>
  260. <tr>
  261. <td class="name">
  262. <strong>name</strong>
  263. <br>
  264. <span class="param-type">string</span>
  265. </td>
  266. <td class="description last">
  267. <p>The name of the shader.</p>
  268. </td>
  269. </tr>
  270. <tr>
  271. <td class="name">
  272. <strong>uniforms</strong>
  273. <br>
  274. <span class="param-type">Object.&lt;string, <a href="Uniform.html">Uniform</a>></span>
  275. </td>
  276. <td class="description last">
  277. <p>The uniforms of the shader.</p>
  278. </td>
  279. </tr>
  280. <tr>
  281. <td class="name">
  282. <strong>defines</strong>
  283. <br>
  284. <span class="param-type">Object.&lt;string, <a href="TSL.html#any">any</a>></span>
  285. </td>
  286. <td class="description last">
  287. <p>The defines of the shader.</p>
  288. </td>
  289. </tr>
  290. <tr>
  291. <td class="name">
  292. <strong>vertexShader</strong>
  293. <br>
  294. <span class="param-type">string</span>
  295. </td>
  296. <td class="description last">
  297. <p>The vertex shader code.</p>
  298. </td>
  299. </tr>
  300. <tr>
  301. <td class="name">
  302. <strong>fragmentShader</strong>
  303. <br>
  304. <span class="param-type">string</span>
  305. </td>
  306. <td class="description last">
  307. <p>The fragment shader code.</p>
  308. </td>
  309. </tr>
  310. </tbody>
  311. </table>
  312. </div>
  313. <h2 class="subsection-title">Source</h2>
  314. <p>
  315. <a href="https://github.com/mrdoob/three.js/blob/master/src/materials/ShaderMaterial.js" translate="no" target="_blank" rel="noopener">src/materials/ShaderMaterial.js</a>
  316. </p>
  317. </article>
  318. </section>
  319. <script src="../scripts/linenumber.js"></script>
  320. <script src="../scripts/page.js"></script>
  321. </body>
  322. </html>
粤ICP备19079148号