webgpu_clipping.html 6.5 KB

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