webgpu_clipping.html 6.3 KB

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