webgl_clipping.html 4.9 KB

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