2
0

webgpu_clipping.html 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgpu - clipping planes</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="info">
  11. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgpu - clipping
  12. </div>
  13. <script type="importmap">
  14. {
  15. "imports": {
  16. "three": "../build/three.module.js",
  17. "three/addons/": "./jsm/",
  18. "three/nodes": "./jsm/nodes/Nodes.js"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import { MeshPhongNodeMaterial } from 'three/nodes';
  25. import WebGPU from 'three/addons/capabilities/WebGPU.js';
  26. import WebGL from 'three/addons/capabilities/WebGL.js';
  27. import WebGPURenderer from 'three/addons/renderers/webgpu/WebGPURenderer.js';
  28. import Stats from 'three/addons/libs/stats.module.js';
  29. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  30. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  31. let camera, scene, renderer, startTime, object, stats;
  32. init();
  33. function init() {
  34. if ( WebGPU.isAvailable() === false && WebGL.isWebGL2Available() === false ) {
  35. document.body.appendChild( WebGPU.getErrorMessage() );
  36. throw new Error( 'No WebGPU or WebGL2 support' );
  37. }
  38. camera = new THREE.PerspectiveCamera( 36, window.innerWidth / window.innerHeight, 0.25, 16 );
  39. camera.position.set( 0, 1.3, 3 );
  40. scene = new THREE.Scene();
  41. // Lights
  42. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  43. const spotLight = new THREE.SpotLight( 0xffffff, 60 );
  44. spotLight.angle = Math.PI / 5;
  45. spotLight.penumbra = 0.2;
  46. spotLight.position.set( 2, 3, 3 );
  47. spotLight.castShadow = true;
  48. spotLight.shadow.camera.near = 3;
  49. spotLight.shadow.camera.far = 10;
  50. spotLight.shadow.mapSize.width = 2048;
  51. spotLight.shadow.mapSize.height = 2048;
  52. spotLight.shadow.bias = - 0.002;
  53. spotLight.shadow.radius = 4;
  54. scene.add( spotLight );
  55. const dirLight = new THREE.DirectionalLight( 0x55505a, 3 );
  56. dirLight.position.set( 0, 3, 0 );
  57. dirLight.castShadow = true;
  58. dirLight.shadow.camera.near = 1;
  59. dirLight.shadow.camera.far = 10;
  60. dirLight.shadow.camera.right = 1;
  61. dirLight.shadow.camera.left = - 1;
  62. dirLight.shadow.camera.top = 1;
  63. dirLight.shadow.camera.bottom = - 1;
  64. dirLight.shadow.mapSize.width = 1024;
  65. dirLight.shadow.mapSize.height = 1024;
  66. scene.add( dirLight );
  67. // ***** Clipping planes: *****
  68. const localPlane = new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0.8 );
  69. const localPlane2 = new THREE.Plane( new THREE.Vector3( 0, 0, -1 ), 0.1 );
  70. const globalPlane = new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 0.1 );
  71. // Geometry
  72. const material = new MeshPhongNodeMaterial( {
  73. color: 0x80ee10,
  74. shininess: 0,
  75. side: THREE.DoubleSide,
  76. // ***** Clipping setup (material): *****
  77. clippingPlanes: [ localPlane, localPlane2 ],
  78. clipShadows: true,
  79. alphaToCoverage: true,
  80. clipIntersection: true
  81. } );
  82. const geometry = new THREE.TorusKnotGeometry( 0.4, 0.08, 95, 20 );
  83. object = new THREE.Mesh( geometry, material );
  84. object.castShadow = true;
  85. scene.add( object );
  86. const ground = new THREE.Mesh(
  87. new THREE.PlaneGeometry( 9, 9, 1, 1 ),
  88. new MeshPhongNodeMaterial( { color: 0xa0adaf, shininess: 150 } )
  89. );
  90. ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
  91. ground.receiveShadow = true;
  92. scene.add( ground );
  93. // Stats
  94. stats = new Stats();
  95. document.body.appendChild( stats.dom );
  96. // Renderer
  97. renderer = new WebGPURenderer( { antialias: true } );
  98. renderer.shadowMap.enabled = true;
  99. renderer.setPixelRatio( window.devicePixelRatio );
  100. renderer.setSize( window.innerWidth, window.innerHeight );
  101. renderer.setAnimationLoop( animate );
  102. window.addEventListener( 'resize', onWindowResize );
  103. document.body.appendChild( renderer.domElement );
  104. // ***** Clipping setup (renderer): *****
  105. const globalPlanes = [ globalPlane ];
  106. const Empty = Object.freeze( [] );
  107. renderer.clippingPlanes = Empty; // GUI sets it to globalPlanes
  108. renderer.localClippingEnabled = true;
  109. // Controls
  110. const controls = new OrbitControls( camera, renderer.domElement );
  111. controls.target.set( 0, 1, 0 );
  112. controls.update();
  113. // GUI
  114. const gui = new GUI(),
  115. props = {
  116. alphaToCoverage: true,
  117. },
  118. folderLocal = gui.addFolder( 'Local Clipping' ),
  119. propsLocal = {
  120. get 'Enabled'() {
  121. return renderer.localClippingEnabled;
  122. },
  123. set 'Enabled'( v ) {
  124. renderer.localClippingEnabled = v;
  125. },
  126. get 'Shadows'() {
  127. return material.clipShadows;
  128. },
  129. set 'Shadows'( v ) {
  130. material.clipShadows = v;
  131. },
  132. get 'Intersection'() {
  133. return material.clipIntersection;
  134. },
  135. set 'Intersection'( v ) {
  136. material.clipIntersection = v;
  137. },
  138. get 'Plane'() {
  139. return localPlane.constant;
  140. },
  141. set 'Plane'( v ) {
  142. localPlane.constant = v;
  143. }
  144. },
  145. folderGlobal = gui.addFolder( 'Global Clipping' ),
  146. propsGlobal = {
  147. get 'Enabled'() {
  148. return renderer.clippingPlanes !== Empty;
  149. },
  150. set 'Enabled'( v ) {
  151. renderer.clippingPlanes = v ? globalPlanes : Empty;
  152. },
  153. get 'Plane'() {
  154. return globalPlane.constant;
  155. },
  156. set 'Plane'( v ) {
  157. globalPlane.constant = v;
  158. }
  159. };
  160. gui.add( props, 'alphaToCoverage' ).onChange( function ( value ) {
  161. ground.material.alphaToCoverage = value;
  162. ground.material.needsUpdate = true;
  163. material.alphaToCoverage = value;
  164. material.needsUpdate = true;
  165. } );
  166. folderLocal.add( propsLocal, 'Enabled' );
  167. folderLocal.add( propsLocal, 'Shadows' );
  168. folderLocal.add( propsLocal, 'Intersection' );
  169. folderLocal.add( propsLocal, 'Plane', 0.3, 1.25 );
  170. folderGlobal.add( propsGlobal, 'Enabled' );
  171. folderGlobal.add( propsGlobal, 'Plane', - 0.4, 3 );
  172. // Start
  173. startTime = Date.now();
  174. }
  175. function onWindowResize() {
  176. camera.aspect = window.innerWidth / window.innerHeight;
  177. camera.updateProjectionMatrix();
  178. renderer.setSize( window.innerWidth, window.innerHeight );
  179. }
  180. function animate( currentTime ) {
  181. const time = ( currentTime - startTime ) / 1000;
  182. object.position.y = 0.8;
  183. object.rotation.x = time * 0.5;
  184. object.rotation.y = time * 0.2;
  185. object.scale.setScalar( Math.cos( time ) * 0.125 + 0.875 );
  186. stats.begin();
  187. renderer.render( scene, camera );
  188. stats.end();
  189. }
  190. </script>
  191. </body>
  192. </html>