webgl_clipping.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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. <script type="importmap">
  11. {
  12. "imports": {
  13. "three": "../build/three.module.js",
  14. "three/addons/": "./jsm/"
  15. }
  16. }
  17. </script>
  18. <script type="module">
  19. import * as THREE from 'three';
  20. import Stats from 'three/addons/libs/stats.module.js';
  21. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  22. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  23. let camera, scene, renderer, startTime, object, stats;
  24. init();
  25. animate();
  26. function init() {
  27. camera = new THREE.PerspectiveCamera( 36, window.innerWidth / window.innerHeight, 0.25, 16 );
  28. camera.position.set( 0, 1.3, 3 );
  29. scene = new THREE.Scene();
  30. // Lights
  31. scene.add( new THREE.AmbientLight( 0xcccccc ) );
  32. const spotLight = new THREE.SpotLight( 0xffffff, 60 );
  33. spotLight.angle = Math.PI / 5;
  34. spotLight.penumbra = 0.2;
  35. spotLight.position.set( 2, 3, 3 );
  36. spotLight.castShadow = true;
  37. spotLight.shadow.camera.near = 3;
  38. spotLight.shadow.camera.far = 10;
  39. spotLight.shadow.mapSize.width = 1024;
  40. spotLight.shadow.mapSize.height = 1024;
  41. scene.add( spotLight );
  42. const dirLight = new THREE.DirectionalLight( 0x55505a, 3 );
  43. dirLight.position.set( 0, 3, 0 );
  44. dirLight.castShadow = true;
  45. dirLight.shadow.camera.near = 1;
  46. dirLight.shadow.camera.far = 10;
  47. dirLight.shadow.camera.right = 1;
  48. dirLight.shadow.camera.left = - 1;
  49. dirLight.shadow.camera.top = 1;
  50. dirLight.shadow.camera.bottom = - 1;
  51. dirLight.shadow.mapSize.width = 1024;
  52. dirLight.shadow.mapSize.height = 1024;
  53. scene.add( dirLight );
  54. // ***** Clipping planes: *****
  55. const localPlane = new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0.8 );
  56. const globalPlane = new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 0.1 );
  57. // Geometry
  58. const material = new THREE.MeshPhongMaterial( {
  59. color: 0x80ee10,
  60. shininess: 100,
  61. side: THREE.DoubleSide,
  62. // ***** Clipping setup (material): *****
  63. clippingPlanes: [ localPlane ],
  64. clipShadows: true
  65. } );
  66. const geometry = new THREE.TorusKnotGeometry( 0.4, 0.08, 95, 20 );
  67. object = new THREE.Mesh( geometry, material );
  68. object.castShadow = true;
  69. scene.add( object );
  70. const ground = new THREE.Mesh(
  71. new THREE.PlaneGeometry( 9, 9, 1, 1 ),
  72. new THREE.MeshPhongMaterial( { color: 0xa0adaf, shininess: 150 } )
  73. );
  74. ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
  75. ground.receiveShadow = true;
  76. scene.add( ground );
  77. // Stats
  78. stats = new Stats();
  79. document.body.appendChild( stats.dom );
  80. // Renderer
  81. renderer = new THREE.WebGLRenderer();
  82. renderer.shadowMap.enabled = true;
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. window.addEventListener( 'resize', onWindowResize );
  86. document.body.appendChild( renderer.domElement );
  87. // ***** Clipping setup (renderer): *****
  88. const globalPlanes = [ globalPlane ],
  89. Empty = Object.freeze( [] );
  90. renderer.clippingPlanes = Empty; // GUI sets it to globalPlanes
  91. renderer.localClippingEnabled = true;
  92. // Controls
  93. const controls = new OrbitControls( camera, renderer.domElement );
  94. controls.target.set( 0, 1, 0 );
  95. controls.update();
  96. // GUI
  97. const gui = new GUI(),
  98. folderLocal = gui.addFolder( 'Local Clipping' ),
  99. propsLocal = {
  100. get 'Enabled'() {
  101. return renderer.localClippingEnabled;
  102. },
  103. set 'Enabled'( v ) {
  104. renderer.localClippingEnabled = v;
  105. },
  106. get 'Shadows'() {
  107. return material.clipShadows;
  108. },
  109. set 'Shadows'( v ) {
  110. material.clipShadows = v;
  111. },
  112. get 'Plane'() {
  113. return localPlane.constant;
  114. },
  115. set 'Plane'( v ) {
  116. localPlane.constant = v;
  117. }
  118. },
  119. folderGlobal = gui.addFolder( 'Global Clipping' ),
  120. propsGlobal = {
  121. get 'Enabled'() {
  122. return renderer.clippingPlanes !== Empty;
  123. },
  124. set 'Enabled'( v ) {
  125. renderer.clippingPlanes = v ? globalPlanes : Empty;
  126. },
  127. get 'Plane'() {
  128. return globalPlane.constant;
  129. },
  130. set 'Plane'( v ) {
  131. globalPlane.constant = v;
  132. }
  133. };
  134. folderLocal.add( propsLocal, 'Enabled' );
  135. folderLocal.add( propsLocal, 'Shadows' );
  136. folderLocal.add( propsLocal, 'Plane', 0.3, 1.25 );
  137. folderGlobal.add( propsGlobal, 'Enabled' );
  138. folderGlobal.add( propsGlobal, 'Plane', - 0.4, 3 );
  139. // Start
  140. startTime = Date.now();
  141. }
  142. function onWindowResize() {
  143. camera.aspect = window.innerWidth / window.innerHeight;
  144. camera.updateProjectionMatrix();
  145. renderer.setSize( window.innerWidth, window.innerHeight );
  146. }
  147. function animate() {
  148. const currentTime = Date.now();
  149. const time = ( currentTime - startTime ) / 1000;
  150. requestAnimationFrame( animate );
  151. object.position.y = 0.8;
  152. object.rotation.x = time * 0.5;
  153. object.rotation.y = time * 0.2;
  154. object.scale.setScalar( Math.cos( time ) * 0.125 + 0.875 );
  155. stats.begin();
  156. renderer.render( scene, camera );
  157. stats.end();
  158. }
  159. </script>
  160. </body>
  161. </html>