1
0

GPUComputationRenderer.html 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>GPUComputationRenderer - 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. <h1 translate="no">GPUComputationRenderer</h1>
  13. <section>
  14. <header>
  15. <div class="class-description"><p>GPUComputationRenderer, based on SimulationRenderer by @zz85.</p>
  16. <p>The GPUComputationRenderer uses the concept of variables. These variables are RGBA float textures that hold 4 floats
  17. for each compute element (texel).</p>
  18. <p>Each variable has a fragment shader that defines the computation made to obtain the variable in question.
  19. You can use as many variables you need, and make dependencies so you can use textures of other variables in the shader
  20. (the sampler uniforms are added automatically) Most of the variables will need themselves as dependency.</p>
  21. <p>The renderer has actually two render targets per variable, to make ping-pong. Textures from the current frame are used
  22. as inputs to render the textures of the next frame.</p>
  23. <p>The render targets of the variables can be used as input textures for your visualization shaders.</p>
  24. <p>Variable names should be valid identifiers and should not collide with THREE GLSL used identifiers.
  25. a common approach could be to use 'texture' prefixing the variable name; i.e texturePosition, textureVelocity...</p>
  26. <p>The size of the computation (sizeX * sizeY) is defined as 'resolution' automatically in the shader. For example:</p>
  27. <p>Basic use:</p>
  28. <pre><code class="language-js">// Initialization...
  29. // Create computation renderer
  30. const gpuCompute = new GPUComputationRenderer( 1024, 1024, renderer );
  31. // Create initial state float textures
  32. const pos0 = gpuCompute.createTexture();
  33. const vel0 = gpuCompute.createTexture();
  34. // and fill in here the texture data...
  35. // Add texture variables
  36. const velVar = gpuCompute.addVariable( &quot;textureVelocity&quot;, fragmentShaderVel, vel0 );
  37. const posVar = gpuCompute.addVariable( &quot;texturePosition&quot;, fragmentShaderPos, pos0 );
  38. // Add variable dependencies
  39. gpuCompute.setVariableDependencies( velVar, [ velVar, posVar ] );
  40. gpuCompute.setVariableDependencies( posVar, [ velVar, posVar ] );
  41. // Add custom uniforms
  42. velVar.material.uniforms.time = { value: 0.0 };
  43. // Check for completeness
  44. const error = gpuCompute.init();
  45. if ( error !== null ) {
  46. console.error( error );
  47. }
  48. // In each frame...
  49. // Compute!
  50. gpuCompute.compute();
  51. // Update texture uniforms in your visualization materials with the gpu renderer output
  52. myMaterial.uniforms.myTexture.value = gpuCompute.getCurrentRenderTarget( posVar ).texture;
  53. // Do your rendering
  54. renderer.render( myScene, myCamera );
  55. </code></pre>
  56. <p>Also, you can use utility functions to create ShaderMaterial and perform computations (rendering between textures)
  57. Note that the shaders can have multiple input textures.</p>
  58. <pre><code class="language-js">const myFilter1 = gpuCompute.createShaderMaterial( myFilterFragmentShader1, { theTexture: { value: null } } );
  59. const myFilter2 = gpuCompute.createShaderMaterial( myFilterFragmentShader2, { theTexture: { value: null } } );
  60. const inputTexture = gpuCompute.createTexture();
  61. // Fill in here inputTexture...
  62. myFilter1.uniforms.theTexture.value = inputTexture;
  63. const myRenderTarget = gpuCompute.createRenderTarget();
  64. myFilter2.uniforms.theTexture.value = myRenderTarget.texture;
  65. const outputRenderTarget = gpuCompute.createRenderTarget();
  66. // Now use the output texture where you want:
  67. myMaterial.uniforms.map.value = outputRenderTarget.texture;
  68. // And compute each frame, before rendering to screen:
  69. gpuCompute.doRenderTarget( myFilter1, myRenderTarget );
  70. gpuCompute.doRenderTarget( myFilter2, outputRenderTarget );
  71. </code></pre></div>
  72. <h2>Code Example</h2>
  73. <div translate="no"><pre class="prettyprint source"><code>#DEFINE resolution vec2( 1024.0, 1024.0 )
  74. </code></pre></div>
  75. </header>
  76. <article>
  77. <h2 class="subsection-title">Import</h2>
  78. <p><span translate="no">GPUComputationRenderer</span> is an addon, and must be imported explicitly, see <a href="https://threejs.org/manual/#en/installation" target="_blank" rel="noopener">Installation#Addons</a>.</p>
  79. <pre><code class="language-js">import { GPUComputationRenderer } from 'three/addons/misc/GPUComputationRenderer.js';</code></pre>
  80. <div class="container-overview">
  81. <h2>Constructor</h2>
  82. <h3 class="name name-method" id="GPUComputationRenderer" translate="no">new <a href="#GPUComputationRenderer">GPUComputationRenderer</a><span class="signature">( sizeX : <span class="param-type">number</span>, sizeY : <span class="param-type">number</span>, renderer : <span class="param-type"><a href="WebGLRenderer.html">WebGLRenderer</a></span> )</span> </h3>
  83. <div class="method">
  84. <div class="description">
  85. <p>Constructs a new GPU computation renderer.</p>
  86. </div>
  87. <table class="params">
  88. <tbody>
  89. <tr>
  90. <td class="name">
  91. <strong>sizeX</strong>
  92. </td>
  93. <td class="description last">
  94. <p>Computation problem size is always 2d: sizeX * sizeY elements.</p>
  95. </td>
  96. </tr>
  97. <tr>
  98. <td class="name">
  99. <strong>sizeY</strong>
  100. </td>
  101. <td class="description last">
  102. <p>Computation problem size is always 2d: sizeX * sizeY elements.</p>
  103. </td>
  104. </tr>
  105. <tr>
  106. <td class="name">
  107. <strong>renderer</strong>
  108. </td>
  109. <td class="description last">
  110. <p>The renderer.</p>
  111. </td>
  112. </tr>
  113. </tbody>
  114. </table>
  115. </div>
  116. </div>
  117. <h2 class="subsection-title">Properties</h2>
  118. <div class="member">
  119. <h3 class="name" id="addResolutionDefine" translate="no">.<a href="#addResolutionDefine">addResolutionDefine</a> </h3>
  120. <div class="description">
  121. <p>Adds a resolution defined for the given material shader.</p>
  122. </div>
  123. </div>
  124. <h2 class="subsection-title">Methods</h2>
  125. <h3 class="name name-method" id="addVariable" translate="no">.<a href="#addVariable">addVariable</a><span class="signature">( variableName : <span class="param-type">string</span>, computeFragmentShader : <span class="param-type">string</span>, initialValueTexture : <span class="param-type"><a href="Texture.html">Texture</a></span> )</span><span class="type-signature"> : Object</span> </h3>
  126. <div class="method">
  127. <div class="description">
  128. <p>Adds a compute variable to the renderer.</p>
  129. </div>
  130. <table class="params">
  131. <tbody>
  132. <tr>
  133. <td class="name">
  134. <strong>variableName</strong>
  135. </td>
  136. <td class="description last">
  137. <p>The variable name.</p>
  138. </td>
  139. </tr>
  140. <tr>
  141. <td class="name">
  142. <strong>computeFragmentShader</strong>
  143. </td>
  144. <td class="description last">
  145. <p>The compute (fragment) shader source.</p>
  146. </td>
  147. </tr>
  148. <tr>
  149. <td class="name">
  150. <strong>initialValueTexture</strong>
  151. </td>
  152. <td class="description last">
  153. <p>The initial value texture.</p>
  154. </td>
  155. </tr>
  156. </tbody>
  157. </table>
  158. <dl class="details">
  159. <dt class="tag-returns"><strong>Returns:</strong> The compute variable.</dt>
  160. </dl>
  161. </div>
  162. <h3 class="name name-method" id="compute" translate="no">.<a href="#compute">compute</a><span class="signature">()</span> </h3>
  163. <div class="method">
  164. <div class="description">
  165. <p>Executes the compute. This method is usually called in the animation loop.</p>
  166. </div>
  167. </div>
  168. <h3 class="name name-method" id="createRenderTarget" translate="no">.<a href="#createRenderTarget">createRenderTarget</a><span class="signature">( sizeXTexture : <span class="param-type">number</span>, sizeYTexture : <span class="param-type">number</span>, wrapS : <span class="param-type">number</span>, wrapT : <span class="param-type">number</span>, minFilter : <span class="param-type">number</span>, magFilter : <span class="param-type">number</span> )</span><span class="type-signature"> : <a href="WebGLRenderTarget.html">WebGLRenderTarget</a></span> </h3>
  169. <div class="method">
  170. <div class="description">
  171. <p>Creates a new render target from the given parameters.</p>
  172. </div>
  173. <table class="params">
  174. <tbody>
  175. <tr>
  176. <td class="name">
  177. <strong>sizeXTexture</strong>
  178. </td>
  179. <td class="description last">
  180. <p>The width of the render target.</p>
  181. </td>
  182. </tr>
  183. <tr>
  184. <td class="name">
  185. <strong>sizeYTexture</strong>
  186. </td>
  187. <td class="description last">
  188. <p>The height of the render target.</p>
  189. </td>
  190. </tr>
  191. <tr>
  192. <td class="name">
  193. <strong>wrapS</strong>
  194. </td>
  195. <td class="description last">
  196. <p>The wrapS value.</p>
  197. </td>
  198. </tr>
  199. <tr>
  200. <td class="name">
  201. <strong>wrapT</strong>
  202. </td>
  203. <td class="description last">
  204. <p>The wrapS value.</p>
  205. </td>
  206. </tr>
  207. <tr>
  208. <td class="name">
  209. <strong>minFilter</strong>
  210. </td>
  211. <td class="description last">
  212. <p>The minFilter value.</p>
  213. </td>
  214. </tr>
  215. <tr>
  216. <td class="name">
  217. <strong>magFilter</strong>
  218. </td>
  219. <td class="description last">
  220. <p>The magFilter value.</p>
  221. </td>
  222. </tr>
  223. </tbody>
  224. </table>
  225. <dl class="details">
  226. <dt class="tag-returns"><strong>Returns:</strong> The new render target.</dt>
  227. </dl>
  228. </div>
  229. <h3 class="name name-method" id="createTexture" translate="no">.<a href="#createTexture">createTexture</a><span class="signature">()</span><span class="type-signature"> : <a href="DataTexture.html">DataTexture</a></span> </h3>
  230. <div class="method">
  231. <div class="description">
  232. <p>Creates a new data texture.</p>
  233. </div>
  234. <dl class="details">
  235. <dt class="tag-returns"><strong>Returns:</strong> The new data texture.</dt>
  236. </dl>
  237. </div>
  238. <h3 class="name name-method" id="dispose" translate="no">.<a href="#dispose">dispose</a><span class="signature">()</span> </h3>
  239. <div class="method">
  240. <div class="description">
  241. <p>Frees all internal resources. Call this method if you don't need the
  242. renderer anymore.</p>
  243. </div>
  244. </div>
  245. <h3 class="name name-method" id="doRenderTarget" translate="no">.<a href="#doRenderTarget">doRenderTarget</a><span class="signature">( material : <span class="param-type"><a href="Material.html">Material</a></span>, output : <span class="param-type"><a href="WebGLRenderTarget.html">WebGLRenderTarget</a></span> )</span> </h3>
  246. <div class="method">
  247. <div class="description">
  248. <p>Renders the given material into the given render target
  249. with a full-screen pass.</p>
  250. </div>
  251. <table class="params">
  252. <tbody>
  253. <tr>
  254. <td class="name">
  255. <strong>material</strong>
  256. </td>
  257. <td class="description last">
  258. <p>The material.</p>
  259. </td>
  260. </tr>
  261. <tr>
  262. <td class="name">
  263. <strong>output</strong>
  264. </td>
  265. <td class="description last">
  266. <p>The output.</p>
  267. </td>
  268. </tr>
  269. </tbody>
  270. </table>
  271. </div>
  272. <h3 class="name name-method" id="getAlternateRenderTarget" translate="no">.<a href="#getAlternateRenderTarget">getAlternateRenderTarget</a><span class="signature">( variable : <span class="param-type">Object</span> )</span><span class="type-signature"> : <a href="WebGLRenderTarget.html">WebGLRenderTarget</a></span> </h3>
  273. <div class="method">
  274. <div class="description">
  275. <p>Returns the alternate render target for the given compute variable.</p>
  276. </div>
  277. <table class="params">
  278. <tbody>
  279. <tr>
  280. <td class="name">
  281. <strong>variable</strong>
  282. </td>
  283. <td class="description last">
  284. <p>The compute variable.</p>
  285. </td>
  286. </tr>
  287. </tbody>
  288. </table>
  289. <dl class="details">
  290. <dt class="tag-returns"><strong>Returns:</strong> The alternate render target.</dt>
  291. </dl>
  292. </div>
  293. <h3 class="name name-method" id="getCurrentRenderTarget" translate="no">.<a href="#getCurrentRenderTarget">getCurrentRenderTarget</a><span class="signature">( variable : <span class="param-type">Object</span> )</span><span class="type-signature"> : <a href="WebGLRenderTarget.html">WebGLRenderTarget</a></span> </h3>
  294. <div class="method">
  295. <div class="description">
  296. <p>Returns the current render target for the given compute variable.</p>
  297. </div>
  298. <table class="params">
  299. <tbody>
  300. <tr>
  301. <td class="name">
  302. <strong>variable</strong>
  303. </td>
  304. <td class="description last">
  305. <p>The compute variable.</p>
  306. </td>
  307. </tr>
  308. </tbody>
  309. </table>
  310. <dl class="details">
  311. <dt class="tag-returns"><strong>Returns:</strong> The current render target.</dt>
  312. </dl>
  313. </div>
  314. <h3 class="name name-method" id="init" translate="no">.<a href="#init">init</a><span class="signature">()</span><span class="type-signature"> : string</span> </h3>
  315. <div class="method">
  316. <div class="description">
  317. <p>Initializes the renderer.</p>
  318. </div>
  319. <dl class="details">
  320. <dt class="tag-returns"><strong>Returns:</strong> Returns <code>null</code> if no errors are detected. Otherwise returns the error message.</dt>
  321. </dl>
  322. </div>
  323. <h3 class="name name-method" id="renderTexture" translate="no">.<a href="#renderTexture">renderTexture</a><span class="signature">( input : <span class="param-type"><a href="Texture.html">Texture</a></span>, output : <span class="param-type"><a href="WebGLRenderTarget.html">WebGLRenderTarget</a></span> )</span> </h3>
  324. <div class="method">
  325. <div class="description">
  326. <p>Renders the given texture into the given render target.</p>
  327. </div>
  328. <table class="params">
  329. <tbody>
  330. <tr>
  331. <td class="name">
  332. <strong>input</strong>
  333. </td>
  334. <td class="description last">
  335. <p>The input.</p>
  336. </td>
  337. </tr>
  338. <tr>
  339. <td class="name">
  340. <strong>output</strong>
  341. </td>
  342. <td class="description last">
  343. <p>The output.</p>
  344. </td>
  345. </tr>
  346. </tbody>
  347. </table>
  348. </div>
  349. <h3 class="name name-method" id="setDataType" translate="no">.<a href="#setDataType">setDataType</a><span class="signature">( type : <span class="param-type"><a href="global.html#FloatType">FloatType</a> | <a href="global.html#HalfFloatType">HalfFloatType</a></span> )</span><span class="type-signature"> : <a href="GPUComputationRenderer.html">GPUComputationRenderer</a></span> </h3>
  350. <div class="method">
  351. <div class="description">
  352. <p>Sets the data type of the internal textures.</p>
  353. </div>
  354. <table class="params">
  355. <tbody>
  356. <tr>
  357. <td class="name">
  358. <strong>type</strong>
  359. </td>
  360. <td class="description last">
  361. <p>The type to set.</p>
  362. </td>
  363. </tr>
  364. </tbody>
  365. </table>
  366. <dl class="details">
  367. <dt class="tag-returns"><strong>Returns:</strong> A reference to this renderer.</dt>
  368. </dl>
  369. </div>
  370. <h3 class="name name-method" id="setVariableDependencies" translate="no">.<a href="#setVariableDependencies">setVariableDependencies</a><span class="signature">( variable : <span class="param-type">Object</span>, dependencies : <span class="param-type">Array.&lt;Object></span> )</span> </h3>
  371. <div class="method">
  372. <div class="description">
  373. <p>Sets variable dependencies.</p>
  374. </div>
  375. <table class="params">
  376. <tbody>
  377. <tr>
  378. <td class="name">
  379. <strong>variable</strong>
  380. </td>
  381. <td class="description last">
  382. <p>The compute variable.</p>
  383. </td>
  384. </tr>
  385. <tr>
  386. <td class="name">
  387. <strong>dependencies</strong>
  388. </td>
  389. <td class="description last">
  390. <p>Other compute variables that represents the dependencies.</p>
  391. </td>
  392. </tr>
  393. </tbody>
  394. </table>
  395. </div>
  396. <h2 class="subsection-title">Source</h2>
  397. <p>
  398. <a href="https://github.com/mrdoob/three.js/blob/master/examples/jsm/misc/GPUComputationRenderer.js" translate="no" target="_blank" rel="noopener">examples/jsm/misc/GPUComputationRenderer.js</a>
  399. </p>
  400. </article>
  401. </section>
  402. <script src="../scripts/linenumber.js"></script>
  403. <script src="../scripts/page.js"></script>
  404. </body>
  405. </html>
粤ICP备19079148号