1
0

webgl_clipping.html 5.7 KB

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