webgpurenderer.html 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <!DOCTYPE html><html lang="en"><head>
  2. <meta charset="utf-8">
  3. <title>WebGPURenderer</title>
  4. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  5. <meta name="twitter:card" content="summary_large_image">
  6. <meta name="twitter:site" content="@threejs">
  7. <meta name="twitter:title" content="Three.js – WebGPURenderer">
  8. <meta property="og:image" content="https://threejs.org/files/share.png">
  9. <link rel="shortcut icon" href="../../files/favicon_white.ico" media="(prefers-color-scheme: dark)">
  10. <link rel="shortcut icon" href="../../files/favicon.ico" media="(prefers-color-scheme: light)">
  11. <link rel="stylesheet" href="../resources/lesson.css">
  12. <link rel="stylesheet" href="../resources/lang.css">
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../../build/three.module.js"
  17. }
  18. }
  19. </script>
  20. </head>
  21. <body>
  22. <div class="container">
  23. <div class="lesson-title">
  24. <h1>WebGPURenderer</h1>
  25. </div>
  26. <div class="lesson">
  27. <div class="lesson-main">
  28. <p>
  29. The new `WebGPURenderer` is the next-generation renderer for three.js. This article provides a short overview about the new
  30. renderer and basic guidelines about the usage.
  31. </p>
  32. <h2>Overview</h2>
  33. <p>
  34. `WebGPURenderer` is designed to be the modern alternative to the long-standing `WebGLRenderer`.
  35. Its primary goal is to use WebGPU, which is a modern, high-performance graphics and compute 3D API. However, it's built to be a
  36. universal renderer. If a device/browser doesn't support WebGPU, the renderer can automatically fall back to using a WebGL 2 backend.
  37. </p>
  38. <p>
  39. Providing a WebGL 2 backend as a fallback is a crucial design decision since it allows applications to benefit from WebGPU but without
  40. sacrificing the support for devices which only support WebGL 2.
  41. </p>
  42. <p>
  43. Apart from the fact that `WebGPURenderer` enables access to WebGPU, it offers an exciting feature set:
  44. <p>
  45. <ul>
  46. <li>
  47. `WebGPURenderer` comes with a new node-based material system which allows to develop custom materials with greater flexibility and
  48. more robustness.
  49. </li>
  50. <li>
  51. The renderer supports TSL, the three.js shading language. With TSL, developers can write shader code with JavaScript in a
  52. platform-independent manner. Shader code written in TSL can be transpiled to WGSL or GLSL depending on the available backend.
  53. </li>
  54. <li>
  55. `WebGPURenderer` comes with a new post-processing stack with built-in Multiple Render Targets (MRT) support and automatic pass combination thanks to the
  56. node material.
  57. </li>
  58. </ul>
  59. Let's find out how to integrate `WebGPURenderer` in three.js applications.
  60. <h2>Usage</h2>
  61. <p>
  62. `WebGPURenderer` has different build files so the way you import three.js changes:
  63. </p>
  64. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  65. - import * as THREE from 'three';
  66. + import * as THREE from 'three/webgpu';
  67. </pre>
  68. <p>
  69. If you are using an import map, it's recommended to change it to the following (the paths differ depending on your setup):
  70. </p>
  71. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  72. &lt;script type="importmap"&gt;
  73. {
  74. "imports": {
  75. "three": "../build/three.webgpu.js",
  76. "three/webgpu": "../build/three.webgpu.js",
  77. "three/tsl": "../build/three.tsl.js",
  78. "three/addons/": "./jsm/"
  79. }
  80. }
  81. &lt;/script&gt;
  82. </pre>
  83. <p>
  84. You can create an instance of the renderer just like with `WebGLRenderer`:
  85. </p>
  86. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  87. const renderer = new THREE.WebGPURenderer( { antialias: true } );
  88. renderer.setPixelRatio( window.devicePixelRatio );
  89. renderer.setSize( window.innerWidth, window.innerHeight );
  90. renderer.setAnimationLoop( render );
  91. document.body.appendChild( renderer.domElement );
  92. </pre>
  93. <p>
  94. It's important to understand that WebGPU is initialized in an asynchronous fashion. Hence, it is recommended to use
  95. `setAnimationLoop()` to define the animation loop of your app since this approach will automatically ensure the renderer
  96. is initialized when rendering the first frame. If you prefer to manage your animation loop via `window.requestAnimationFrame()` or
  97. if you have to use the renderer in your init routine, you need an additional line in the above code section.
  98. </p>
  99. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  100. const renderer = new THREE.WebGPURenderer( { antialias: true } );
  101. renderer.setPixelRatio( window.devicePixelRatio );
  102. renderer.setSize( window.innerWidth, window.innerHeight );
  103. renderer.setAnimationLoop( render );
  104. document.body.appendChild( renderer.domElement );
  105. + await renderer.init();
  106. </pre>
  107. <p>
  108. Most common methods known from `WebGLRenderer` like `clear()`, `setRenderTarget()`or `dispose()` are also present in `WebGPURenderer`.
  109. Please have a look at the <a href="https://threejs.org/docs/#Renderer" target="_blank">API documentation</a> for a full overview of the renderer's public interface.
  110. </p>
  111. <p>
  112. Like mentioned in the initial part of the guide, `WebGPURenderer` uses a WebGPU backend by default and a WebGL 2 backend as a fallback.
  113. If you want to force the usage of WebGL 2 for testing purposes or if you want to exclude the usage of WebGPU for certain reasons, you can
  114. make use of the `forceWebGL` parameter.
  115. </p>
  116. <pre class="prettyprint showlinemods notranslate lang-js" translate="no">
  117. - const renderer = new THREE.WebGPURenderer( { antialias: true } );
  118. + const renderer = new THREE.WebGPURenderer( { antialias: true, forceWebGL: true } );
  119. </pre>
  120. <h2>Migration</h2>
  121. <p>
  122. If you want to give `WebGPURenderer` a try, you have to be aware of the following.
  123. </p>
  124. <ul>
  125. <li>
  126. Custom materials based on `ShaderMaterial`, `RawShaderMaterial` and modifications of built-in materials via `onBeforeCompile()`
  127. are not supported in `WebGPURenderer`. This part of your application must be ported to node materials and TSL.
  128. </li>
  129. <li>
  130. `EffectComposer` with its effect passes are not supported because `WebGPURenderer` comes with a new, more modern
  131. post-processing stack. Similar to materials, post-processing effects are now written in TSL and the effect chain is expressed
  132. as a node composition. All common effects have already been ported to `WebGPURenderer` and exist in a more performant version
  133. as a node class. We have also added new effects like SSGI, SSS or a better DoF exclusively for the new renderer. Check out the
  134. <a href="https://threejs.org/examples/?q=webgpu%20postprocessing" target="_blank">official examples</a> to get an overview of
  135. the current supported effects.
  136. </li>
  137. <li>
  138. The renderer itself is still in an experimental state although its maturity level has been greatly improved in the last years.
  139. Still, depending on your application and scene setup, you will encounter missing features or a better performance with `WebGLRenderer`.
  140. Feel free to file an issue at GitHub so we are aware of open tasks. We are improving `WebGPURenderer` with each release so it's
  141. recommended to upgrade to the latest version whenever possible.
  142. </li>
  143. </ul>
  144. <h2>State of WebGLRenderer</h2>
  145. <p>Although in the meanwhile a lot of work happens in context of `WebGPURenderer`, the node material and TSL, `WebGLRenderer` is still maintained
  146. and the recommended choice for pure WebGL 2 applications. However, keep in mind that there are no plans to add larger new features to
  147. the renderer since the project's focus is now on `WebGPURenderer` which you can easily see at the latest release notes. That said,
  148. we are currently investigating the possibility to add limited node material support to `WebGLRenderer` in order to make the transition to
  149. `WebGPURenderer` easier for certain projects.
  150. </p>
  151. </div>
  152. </div>
  153. </div>
  154. <script src="../resources/prettify.js"></script>
  155. <script src="../resources/lesson.js"></script>
  156. </body>
  157. </html>
粤ICP备19079148号