webgl_clipping.html 5.5 KB

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