webgl_clipping.html 5.2 KB

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