webgl_clipping.html 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. <meta property="og:title" content="three.js webgl - clipping planes">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_clipping.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_clipping.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <script type="importmap">
  15. {
  16. "imports": {
  17. "three": "../build/three.module.js",
  18. "three/addons/": "./jsm/"
  19. }
  20. }
  21. </script>
  22. <script type="module">
  23. import * as THREE from 'three';
  24. import Stats from 'three/addons/libs/stats.module.js';
  25. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  26. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  27. let camera, scene, renderer, startTime, object, stats;
  28. init();
  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( 0xcccccc ) );
  35. const spotLight = new THREE.SpotLight( 0xffffff, 60 );
  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, 3 );
  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. alphaToCoverage: true,
  69. } );
  70. const geometry = new THREE.TorusKnotGeometry( 0.4, 0.08, 95, 20 );
  71. object = new THREE.Mesh( geometry, material );
  72. object.castShadow = true;
  73. scene.add( object );
  74. const ground = new THREE.Mesh(
  75. new THREE.PlaneGeometry( 9, 9, 1, 1 ),
  76. new THREE.MeshPhongMaterial( { color: 0xa0adaf, shininess: 150 } )
  77. );
  78. ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
  79. ground.receiveShadow = true;
  80. scene.add( ground );
  81. // Renderer
  82. renderer = new THREE.WebGLRenderer( { antialias: true } );
  83. renderer.setPixelRatio( window.devicePixelRatio );
  84. renderer.setSize( window.innerWidth, window.innerHeight );
  85. renderer.setAnimationLoop( animate );
  86. renderer.shadowMap.enabled = true;
  87. document.body.appendChild( renderer.domElement );
  88. window.addEventListener( 'resize', onWindowResize );
  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. // Stats
  95. stats = new Stats();
  96. document.body.appendChild( stats.dom );
  97. // Controls
  98. const controls = new OrbitControls( camera, renderer.domElement );
  99. controls.target.set( 0, 1, 0 );
  100. controls.update();
  101. // GUI
  102. const gui = new GUI(),
  103. props = {
  104. alphaToCoverage: true,
  105. },
  106. folderLocal = gui.addFolder( 'Local Clipping' ),
  107. propsLocal = {
  108. get 'Enabled'() {
  109. return renderer.localClippingEnabled;
  110. },
  111. set 'Enabled'( v ) {
  112. renderer.localClippingEnabled = v;
  113. },
  114. get 'Shadows'() {
  115. return material.clipShadows;
  116. },
  117. set 'Shadows'( v ) {
  118. material.clipShadows = v;
  119. },
  120. get 'Plane'() {
  121. return localPlane.constant;
  122. },
  123. set 'Plane'( v ) {
  124. localPlane.constant = v;
  125. }
  126. },
  127. folderGlobal = gui.addFolder( 'Global Clipping' ),
  128. propsGlobal = {
  129. get 'Enabled'() {
  130. return renderer.clippingPlanes !== Empty;
  131. },
  132. set 'Enabled'( v ) {
  133. renderer.clippingPlanes = v ? globalPlanes : Empty;
  134. },
  135. get 'Plane'() {
  136. return globalPlane.constant;
  137. },
  138. set 'Plane'( v ) {
  139. globalPlane.constant = v;
  140. }
  141. };
  142. gui.add( props, 'alphaToCoverage' ).onChange( function ( value ) {
  143. ground.material.alphaToCoverage = value;
  144. ground.material.needsUpdate = true;
  145. material.alphaToCoverage = value;
  146. material.needsUpdate = true;
  147. } );
  148. folderLocal.add( propsLocal, 'Enabled' );
  149. folderLocal.add( propsLocal, 'Shadows' );
  150. folderLocal.add( propsLocal, 'Plane', 0.3, 1.25 );
  151. folderGlobal.add( propsGlobal, 'Enabled' );
  152. folderGlobal.add( propsGlobal, 'Plane', - 0.4, 3 );
  153. // Start
  154. startTime = Date.now();
  155. }
  156. function onWindowResize() {
  157. camera.aspect = window.innerWidth / window.innerHeight;
  158. camera.updateProjectionMatrix();
  159. renderer.setSize( window.innerWidth, window.innerHeight );
  160. }
  161. function animate() {
  162. const currentTime = Date.now();
  163. const time = ( currentTime - startTime ) / 1000;
  164. object.position.y = 0.8;
  165. object.rotation.x = time * 0.5;
  166. object.rotation.y = time * 0.2;
  167. object.scale.setScalar( Math.cos( time ) * 0.125 + 0.875 );
  168. stats.begin();
  169. renderer.render( scene, camera );
  170. stats.end();
  171. }
  172. </script>
  173. </body>
  174. </html>
粤ICP备19079148号