webgpu_clipping.html 6.4 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="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 globalPlane = new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 0.1 );
  61. const localPlane1 = new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0.8 );
  62. const localPlane2 = new THREE.Plane( new THREE.Vector3( 0, 0, - 1 ), 0.1 );
  63. // Clipping Groups
  64. const globalClippingGroup = new THREE.ClippingGroup();
  65. globalClippingGroup.clippingPlanes = [ globalPlane ];
  66. const knotClippingGroup = new THREE.ClippingGroup();
  67. knotClippingGroup.clippingPlanes = [ localPlane1, localPlane2 ];
  68. knotClippingGroup.clipIntersection = true;
  69. scene.add( globalClippingGroup );
  70. globalClippingGroup.add( knotClippingGroup );
  71. // Geometry
  72. const material = new THREE.MeshPhongNodeMaterial( {
  73. color: 0x80ee10,
  74. shininess: 0,
  75. side: THREE.DoubleSide,
  76. // ***** Clipping setup (material): *****
  77. alphaToCoverage: true
  78. } );
  79. const geometry = new THREE.TorusKnotGeometry( 0.4, 0.08, 95, 20 );
  80. object = new THREE.Mesh( geometry, material );
  81. object.castShadow = true;
  82. knotClippingGroup.add( object );
  83. const ground = new THREE.Mesh(
  84. new THREE.PlaneGeometry( 9, 9, 1, 1 ),
  85. new THREE.MeshPhongNodeMaterial( { color: 0xa0adaf, shininess: 150, alphaToCoverage: true } )
  86. );
  87. ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
  88. ground.receiveShadow = true;
  89. globalClippingGroup.add( ground );
  90. // Stats
  91. stats = new Stats();
  92. document.body.appendChild( stats.dom );
  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. window.addEventListener( 'resize', onWindowResize );
  100. document.body.appendChild( renderer.domElement );
  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. folderKnot = gui.addFolder( 'Knot Clipping Group' ),
  111. propsKnot = {
  112. get 'Enabled'() {
  113. return knotClippingGroup.enabled;
  114. },
  115. set 'Enabled'( v ) {
  116. knotClippingGroup.enabled = v;
  117. },
  118. get 'Shadows'() {
  119. return knotClippingGroup.clipShadows;
  120. },
  121. set 'Shadows'( v ) {
  122. knotClippingGroup.clipShadows = v;
  123. },
  124. get 'Intersection'() {
  125. return knotClippingGroup.clipIntersection;
  126. },
  127. set 'Intersection'( v ) {
  128. knotClippingGroup.clipIntersection = v;
  129. },
  130. get 'Plane'() {
  131. return localPlane1.constant;
  132. },
  133. set 'Plane'( v ) {
  134. localPlane1.constant = v;
  135. }
  136. },
  137. folderGlobal = gui.addFolder( 'Global Clipping Group' ),
  138. propsGlobal = {
  139. get 'Enabled'() {
  140. return globalClippingGroup.enabled;
  141. },
  142. set 'Enabled'( v ) {
  143. globalClippingGroup.enabled = v;
  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. folderKnot.add( propsKnot, 'Enabled' );
  159. folderKnot.add( propsKnot, 'Shadows' );
  160. folderKnot.add( propsKnot, 'Intersection' );
  161. folderKnot.add( propsKnot, '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>
粤ICP备19079148号