webgpu_clipping.html 6.4 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="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/webgpu": "../build/three.webgpu.js",
  18. "three/tsl": "../build/three.tsl.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import Stats from 'three/addons/libs/stats.module.js';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  28. let camera, scene, renderer, startTime, object, stats;
  29. init();
  30. function init() {
  31. camera = new THREE.PerspectiveCamera( 36, window.innerWidth / window.innerHeight, 0.25, 16 );
  32. camera.position.set( 0, 1.3, 3 );
  33. scene = new THREE.Scene();
  34. // Lights
  35. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  36. const spotLight = new THREE.SpotLight( 0xffffff, 60 );
  37. spotLight.angle = Math.PI / 5;
  38. spotLight.penumbra = 0.2;
  39. spotLight.position.set( 2, 3, 3 );
  40. spotLight.castShadow = true;
  41. spotLight.shadow.camera.near = 3;
  42. spotLight.shadow.camera.far = 10;
  43. spotLight.shadow.mapSize.width = 2048;
  44. spotLight.shadow.mapSize.height = 2048;
  45. spotLight.shadow.bias = - 0.002;
  46. spotLight.shadow.radius = 4;
  47. scene.add( spotLight );
  48. const dirLight = new THREE.DirectionalLight( 0x55505a, 3 );
  49. dirLight.position.set( 0, 3, 0 );
  50. dirLight.castShadow = true;
  51. dirLight.shadow.camera.near = 1;
  52. dirLight.shadow.camera.far = 10;
  53. dirLight.shadow.camera.right = 1;
  54. dirLight.shadow.camera.left = - 1;
  55. dirLight.shadow.camera.top = 1;
  56. dirLight.shadow.camera.bottom = - 1;
  57. dirLight.shadow.mapSize.width = 1024;
  58. dirLight.shadow.mapSize.height = 1024;
  59. scene.add( dirLight );
  60. // Clipping planes
  61. const globalPlane = new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 0.1 );
  62. const localPlane1 = new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0.8 );
  63. const localPlane2 = new THREE.Plane( new THREE.Vector3( 0, 0, - 1 ), 0.1 );
  64. // Clipping Groups
  65. const globalClippingGroup = new THREE.ClippingGroup();
  66. globalClippingGroup.clippingPlanes = [ globalPlane ];
  67. const knotClippingGroup = new THREE.ClippingGroup();
  68. knotClippingGroup.clippingPlanes = [ localPlane1, localPlane2 ];
  69. knotClippingGroup.clipIntersection = true;
  70. scene.add( globalClippingGroup );
  71. globalClippingGroup.add( knotClippingGroup );
  72. // Geometry
  73. const material = new THREE.MeshPhongNodeMaterial( {
  74. color: 0x80ee10,
  75. shininess: 0,
  76. side: THREE.DoubleSide,
  77. // ***** Clipping setup (material): *****
  78. alphaToCoverage: true
  79. } );
  80. const geometry = new THREE.TorusKnotGeometry( 0.4, 0.08, 95, 20 );
  81. object = new THREE.Mesh( geometry, material );
  82. object.castShadow = true;
  83. knotClippingGroup.add( object );
  84. const ground = new THREE.Mesh(
  85. new THREE.PlaneGeometry( 9, 9, 1, 1 ),
  86. new THREE.MeshPhongNodeMaterial( { color: 0xa0adaf, shininess: 150, alphaToCoverage: true } )
  87. );
  88. ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
  89. ground.receiveShadow = true;
  90. globalClippingGroup.add( ground );
  91. // Stats
  92. stats = new Stats();
  93. document.body.appendChild( stats.dom );
  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. 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 = new GUI(),
  108. props = {
  109. alphaToCoverage: true,
  110. },
  111. folderKnot = gui.addFolder( 'Knot Clipping Group' ),
  112. 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. folderGlobal = gui.addFolder( 'Global Clipping Group' ),
  139. 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. stats.begin();
  180. renderer.render( scene, camera );
  181. stats.end();
  182. }
  183. </script>
  184. </body>
  185. </html>
粤ICP备19079148号