webgpu_clipping.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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.bias = - 0.002;
  49. spotLight.shadow.radius = 4;
  50. scene.add( spotLight );
  51. const dirLight = new THREE.DirectionalLight( 0x55505a, 3 );
  52. dirLight.position.set( 0, 3, 0 );
  53. dirLight.castShadow = true;
  54. dirLight.shadow.camera.near = 1;
  55. dirLight.shadow.camera.far = 10;
  56. dirLight.shadow.camera.right = 1;
  57. dirLight.shadow.camera.left = - 1;
  58. dirLight.shadow.camera.top = 1;
  59. dirLight.shadow.camera.bottom = - 1;
  60. dirLight.shadow.mapSize.width = 1024;
  61. dirLight.shadow.mapSize.height = 1024;
  62. scene.add( dirLight );
  63. // Clipping planes
  64. const globalPlane = new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 0.1 );
  65. const localPlane1 = new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0.8 );
  66. const localPlane2 = new THREE.Plane( new THREE.Vector3( 0, 0, - 1 ), 0.1 );
  67. // Clipping Groups
  68. const globalClippingGroup = new THREE.ClippingGroup();
  69. globalClippingGroup.clippingPlanes = [ globalPlane ];
  70. const knotClippingGroup = new THREE.ClippingGroup();
  71. knotClippingGroup.clippingPlanes = [ localPlane1, localPlane2 ];
  72. knotClippingGroup.clipIntersection = true;
  73. scene.add( globalClippingGroup );
  74. globalClippingGroup.add( knotClippingGroup );
  75. // Geometry
  76. const material = new THREE.MeshPhongNodeMaterial( {
  77. color: 0x80ee10,
  78. shininess: 0,
  79. side: THREE.DoubleSide,
  80. // ***** Clipping setup (material): *****
  81. alphaToCoverage: true
  82. } );
  83. const geometry = new THREE.TorusKnotGeometry( 0.4, 0.08, 95, 20 );
  84. object = new THREE.Mesh( geometry, material );
  85. object.castShadow = true;
  86. knotClippingGroup.add( object );
  87. const ground = new THREE.Mesh(
  88. new THREE.PlaneGeometry( 9, 9, 1, 1 ),
  89. new THREE.MeshPhongNodeMaterial( { color: 0xa0adaf, shininess: 150, alphaToCoverage: true } )
  90. );
  91. ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
  92. ground.receiveShadow = true;
  93. globalClippingGroup.add( ground );
  94. // Renderer
  95. renderer = new THREE.WebGPURenderer( { antialias: true } );
  96. renderer.shadowMap.enabled = true;
  97. renderer.setPixelRatio( window.devicePixelRatio );
  98. renderer.setSize( window.innerWidth, window.innerHeight );
  99. renderer.setAnimationLoop( animate );
  100. renderer.inspector = new Inspector();
  101. window.addEventListener( 'resize', onWindowResize );
  102. document.body.appendChild( renderer.domElement );
  103. // Controls
  104. const controls = new OrbitControls( camera, renderer.domElement );
  105. controls.target.set( 0, 1, 0 );
  106. controls.update();
  107. // GUI
  108. const gui = renderer.inspector.createParameters( 'Clipping settings' );
  109. const props = {
  110. alphaToCoverage: true,
  111. };
  112. const folderKnot = gui.addFolder( 'Knot Clipping Group' );
  113. const propsKnot = {
  114. get 'Enabled'() {
  115. return knotClippingGroup.enabled;
  116. },
  117. set 'Enabled'( v ) {
  118. knotClippingGroup.enabled = v;
  119. },
  120. get 'Shadows'() {
  121. return knotClippingGroup.clipShadows;
  122. },
  123. set 'Shadows'( v ) {
  124. knotClippingGroup.clipShadows = v;
  125. },
  126. get 'Intersection'() {
  127. return knotClippingGroup.clipIntersection;
  128. },
  129. set 'Intersection'( v ) {
  130. knotClippingGroup.clipIntersection = v;
  131. },
  132. get 'Plane'() {
  133. return localPlane1.constant;
  134. },
  135. set 'Plane'( v ) {
  136. localPlane1.constant = v;
  137. }
  138. };
  139. const folderGlobal = gui.addFolder( 'Global Clipping Group' );
  140. const propsGlobal = {
  141. get 'Enabled'() {
  142. return globalClippingGroup.enabled;
  143. },
  144. set 'Enabled'( v ) {
  145. globalClippingGroup.enabled = v;
  146. },
  147. get 'Plane'() {
  148. return globalPlane.constant;
  149. },
  150. set 'Plane'( v ) {
  151. globalPlane.constant = v;
  152. }
  153. };
  154. gui.add( props, 'alphaToCoverage' ).onChange( function ( value ) {
  155. ground.material.alphaToCoverage = value;
  156. ground.material.needsUpdate = true;
  157. material.alphaToCoverage = value;
  158. material.needsUpdate = true;
  159. } );
  160. folderKnot.add( propsKnot, 'Enabled' );
  161. folderKnot.add( propsKnot, 'Shadows' );
  162. folderKnot.add( propsKnot, 'Intersection' );
  163. folderKnot.add( propsKnot, 'Plane', 0.3, 1.25 );
  164. folderGlobal.add( propsGlobal, 'Enabled' );
  165. folderGlobal.add( propsGlobal, 'Plane', - 0.4, 3 );
  166. // Start
  167. startTime = Date.now();
  168. }
  169. function onWindowResize() {
  170. camera.aspect = window.innerWidth / window.innerHeight;
  171. camera.updateProjectionMatrix();
  172. renderer.setSize( window.innerWidth, window.innerHeight );
  173. }
  174. function animate( currentTime ) {
  175. const time = ( currentTime - startTime ) / 1000;
  176. object.position.y = 0.8;
  177. object.rotation.x = time * 0.5;
  178. object.rotation.y = time * 0.2;
  179. object.scale.setScalar( Math.cos( time ) * 0.125 + 0.875 );
  180. renderer.render( scene, camera );
  181. }
  182. </script>
  183. </body>
  184. </html>
粤ICP备19079148号