webgpu_clipping.html 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. <meta property="og:title" content="three.js webgpu - clipping planes">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgpu_clipping.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgpu_clipping.jpg">
  11. <link type="text/css" rel="stylesheet" href="example.css">
  12. </head>
  13. <body>
  14. <div id="info">
  15. <a href="https://threejs.org/" target="_blank" rel="noopener" class="logo-link"></a>
  16. <div class="title-wrapper">
  17. <a href="https://threejs.org/" target="_blank" rel="noopener">three.js</a><span>Clipping Planes</span>
  18. </div>
  19. <small>Clipping 3D objects via Clipping Planes.</small>
  20. </div>
  21. <script type="importmap">
  22. {
  23. "imports": {
  24. "three": "../build/three.webgpu.js",
  25. "three/webgpu": "../build/three.webgpu.js",
  26. "three/tsl": "../build/three.tsl.js",
  27. "three/addons/": "./jsm/"
  28. }
  29. }
  30. </script>
  31. <script type="module">
  32. import * as THREE from 'three/webgpu';
  33. import { Inspector } from 'three/addons/inspector/Inspector.js';
  34. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  35. let camera, scene, renderer, startTime, object;
  36. init();
  37. function init() {
  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.radius = 4;
  53. scene.add( spotLight );
  54. const dirLight = new THREE.DirectionalLight( 0x55505a, 3 );
  55. dirLight.position.set( 0, 3, 0 );
  56. dirLight.castShadow = true;
  57. dirLight.shadow.camera.near = 1;
  58. dirLight.shadow.camera.far = 10;
  59. dirLight.shadow.camera.right = 1;
  60. dirLight.shadow.camera.left = - 1;
  61. dirLight.shadow.camera.top = 1;
  62. dirLight.shadow.camera.bottom = - 1;
  63. dirLight.shadow.mapSize.width = 1024;
  64. dirLight.shadow.mapSize.height = 1024;
  65. scene.add( dirLight );
  66. // Clipping planes
  67. const globalPlane = new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 0.1 );
  68. const localPlane1 = 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. // Clipping Groups
  71. const globalClippingGroup = new THREE.ClippingGroup();
  72. globalClippingGroup.clippingPlanes = [ globalPlane ];
  73. const knotClippingGroup = new THREE.ClippingGroup();
  74. knotClippingGroup.clippingPlanes = [ localPlane1, localPlane2 ];
  75. knotClippingGroup.clipIntersection = true;
  76. scene.add( globalClippingGroup );
  77. globalClippingGroup.add( knotClippingGroup );
  78. // Geometry
  79. const material = new THREE.MeshPhongNodeMaterial( {
  80. color: 0x80ee10,
  81. shininess: 0,
  82. side: THREE.DoubleSide,
  83. alphaToCoverage: true
  84. } );
  85. const geometry = new THREE.TorusKnotGeometry( 0.4, 0.08, 95, 20 );
  86. object = new THREE.Mesh( geometry, material );
  87. object.castShadow = true;
  88. knotClippingGroup.add( object );
  89. const ground = new THREE.Mesh(
  90. new THREE.PlaneGeometry( 9, 9, 1, 1 ),
  91. new THREE.MeshPhongNodeMaterial( { color: 0xa0adaf, shininess: 150, alphaToCoverage: true } )
  92. );
  93. ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
  94. ground.receiveShadow = true;
  95. globalClippingGroup.add( ground );
  96. // Renderer
  97. renderer = new THREE.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. renderer.inspector = new Inspector();
  103. window.addEventListener( 'resize', onWindowResize );
  104. document.body.appendChild( renderer.domElement );
  105. // Controls
  106. const controls = new OrbitControls( camera, renderer.domElement );
  107. controls.target.set( 0, 1, 0 );
  108. controls.update();
  109. // GUI
  110. const gui = renderer.inspector.createParameters( 'Clipping settings' );
  111. const props = {
  112. alphaToCoverage: true,
  113. };
  114. const folderKnot = gui.addFolder( 'Knot Clipping Group' );
  115. const propsKnot = {
  116. get 'Enabled'() {
  117. return knotClippingGroup.enabled;
  118. },
  119. set 'Enabled'( v ) {
  120. knotClippingGroup.enabled = v;
  121. },
  122. get 'Shadows'() {
  123. return knotClippingGroup.clipShadows;
  124. },
  125. set 'Shadows'( v ) {
  126. knotClippingGroup.clipShadows = v;
  127. },
  128. get 'Intersection'() {
  129. return knotClippingGroup.clipIntersection;
  130. },
  131. set 'Intersection'( v ) {
  132. knotClippingGroup.clipIntersection = v;
  133. },
  134. get 'Plane'() {
  135. return localPlane1.constant;
  136. },
  137. set 'Plane'( v ) {
  138. localPlane1.constant = v;
  139. }
  140. };
  141. const folderGlobal = gui.addFolder( 'Global Clipping Group' );
  142. const propsGlobal = {
  143. get 'Enabled'() {
  144. return globalClippingGroup.enabled;
  145. },
  146. set 'Enabled'( v ) {
  147. globalClippingGroup.enabled = v;
  148. },
  149. get 'Plane'() {
  150. return globalPlane.constant;
  151. },
  152. set 'Plane'( v ) {
  153. globalPlane.constant = v;
  154. }
  155. };
  156. gui.add( props, 'alphaToCoverage' ).onChange( function ( value ) {
  157. ground.material.alphaToCoverage = value;
  158. ground.material.needsUpdate = true;
  159. material.alphaToCoverage = value;
  160. material.needsUpdate = true;
  161. } );
  162. folderKnot.add( propsKnot, 'Enabled' );
  163. folderKnot.add( propsKnot, 'Shadows' );
  164. folderKnot.add( propsKnot, 'Intersection' );
  165. folderKnot.add( propsKnot, 'Plane', 0.3, 1.25 );
  166. folderGlobal.add( propsGlobal, 'Enabled' );
  167. folderGlobal.add( propsGlobal, 'Plane', - 0.4, 3 );
  168. // Start
  169. startTime = Date.now();
  170. }
  171. function onWindowResize() {
  172. camera.aspect = window.innerWidth / window.innerHeight;
  173. camera.updateProjectionMatrix();
  174. renderer.setSize( window.innerWidth, window.innerHeight );
  175. }
  176. function animate( currentTime ) {
  177. const time = ( currentTime - startTime ) / 1000;
  178. object.position.y = 0.8;
  179. object.rotation.x = time * 0.5;
  180. object.rotation.y = time * 0.2;
  181. object.scale.setScalar( Math.cos( time ) * 0.125 + 0.875 );
  182. renderer.render( scene, camera );
  183. }
  184. </script>
  185. </body>
  186. </html>
粤ICP备19079148号