webgl_clipping.html 5.6 KB

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