webgl_clipping_stencil.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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/stats.min.js"></script>
  19. <script src="js/libs/dat.gui.min.js"></script>
  20. <script>
  21. var camera, scene, renderer, startTime, object, stats;
  22. var planes, planeObjects;
  23. var clock;
  24. var params = {
  25. animate: true
  26. };
  27. init();
  28. animate();
  29. function createPlaneStencilGroup( geometry, plane, renderOrder ) {
  30. var group = new THREE.Group();
  31. var baseMat = new THREE.MeshBasicMaterial();
  32. baseMat.depthWrite = false;
  33. baseMat.depthTest = false;
  34. baseMat.colorWrite = false;
  35. baseMat.clippingPlanes = [ plane ];
  36. baseMat.stencilWrite = true;
  37. baseMat.stencilFunc = THREE.AlwaysStencilFunc;
  38. // back faces
  39. var mat0 = baseMat.clone();
  40. mat0.side = THREE.BackSide;
  41. mat0.stencilFail = THREE.IncrementWrapStencilOp;
  42. mat0.stencilZFail = THREE.IncrementWrapStencilOp;
  43. mat0.stencilZPass = THREE.IncrementWrapStencilOp;
  44. var mesh0 = new THREE.Mesh( geometry, mat0 );
  45. mesh0.renderOrder = renderOrder;
  46. group.add( mesh0 );
  47. // front faces
  48. var mat1 = baseMat.clone();
  49. mat1.side = THREE.FrontSide;
  50. mat1.stencilFail = THREE.DecrementWrapStencilOp;
  51. mat1.stencilZFail = THREE.DecrementWrapStencilOp;
  52. mat1.stencilZPass = THREE.DecrementWrapStencilOp;
  53. var mesh1 = new THREE.Mesh( geometry, mat1 );
  54. mesh1.renderOrder = renderOrder;
  55. group.add( mesh1 );
  56. return group;
  57. }
  58. function init() {
  59. clock = new THREE.Clock();
  60. scene = new THREE.Scene();
  61. camera = new THREE.PerspectiveCamera( 36, window.innerWidth / window.innerHeight, 1 );
  62. camera.position.set( 0, 1.3, 3 );
  63. scene.add( new THREE.AmbientLight( 0xffffff, 0.5 ) );
  64. var dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  65. dirLight.position.set( 5, 10, 7.5 );
  66. dirLight.castShadow = true;
  67. dirLight.shadow.camera.right = 2;
  68. dirLight.shadow.camera.left = - 2;
  69. dirLight.shadow.camera.top = 2;
  70. dirLight.shadow.camera.bottom = - 2;
  71. dirLight.shadow.mapSize.width = 1024;
  72. dirLight.shadow.mapSize.height = 1024;
  73. scene.add( dirLight );
  74. planes = [
  75. new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0 ),
  76. new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 0 ),
  77. new THREE.Plane( new THREE.Vector3( 0, 0, - 1 ), 0 )
  78. ];
  79. var geometry = new THREE.TorusKnotBufferGeometry( 0.4, 0.15, 220, 60 );
  80. object = new THREE.Group();
  81. scene.add( object );
  82. // Set up clip plane rendering
  83. planeObjects = [];
  84. var planeGeom = new THREE.PlaneBufferGeometry( 4, 4 );
  85. for ( var i = 0; i < 3; i ++ ) {
  86. var poGroup = new THREE.Group();
  87. var plane = planes[ i ];
  88. var stencilGroup = createPlaneStencilGroup( geometry, plane, i + 1 )
  89. var planeMat =
  90. new THREE.MeshStandardMaterial( {
  91. color: 0xE91E63,
  92. metalness: 0.1,
  93. roughness: 0.75,
  94. clippingPlanes: planes.filter( p => p !== plane ),
  95. stencilWrite: true,
  96. stencilRef: 0,
  97. stencilFunc: THREE.NotEqualStencilFunc,
  98. stencilFail: THREE.ReplaceStencilOp,
  99. stencilZFail: THREE.ReplaceStencilOp,
  100. stencilZPass: THREE.ReplaceStencilOp,
  101. } );
  102. var po = new THREE.Mesh( planeGeom, planeMat );
  103. po.onAfterRender = function ( renderer ) {
  104. renderer.clearStencil();
  105. };
  106. po.renderOrder = i + 1.1;
  107. object.add( stencilGroup );
  108. poGroup.add( po );
  109. planeObjects.push( po );
  110. scene.add( poGroup );
  111. }
  112. // Handle camera entering model
  113. var material = new THREE.MeshStandardMaterial( {
  114. color: 0xFFC107,
  115. metalness: 0.1,
  116. roughness: 0.75,
  117. clippingPlanes: planes,
  118. clipShadows: true,
  119. shadowSide: THREE.DoubleSide,
  120. } );
  121. // add the color
  122. var clippedColorFront = new THREE.Mesh( geometry, material );
  123. clippedColorFront.castShadow = true;
  124. clippedColorFront.renderOrder = 6;
  125. object.add( clippedColorFront );
  126. var ground = new THREE.Mesh(
  127. new THREE.PlaneBufferGeometry( 9, 9, 1, 1 ),
  128. new THREE.ShadowMaterial( { color: 0, opacity: 0.25, side: THREE.DoubleSide } )
  129. );
  130. ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
  131. ground.position.y = - 1;
  132. ground.receiveShadow = true;
  133. scene.add( ground );
  134. // Stats
  135. stats = new Stats();
  136. document.body.appendChild( stats.dom );
  137. // Renderer
  138. renderer = new THREE.WebGLRenderer( { antialias: true } );
  139. renderer.shadowMap.enabled = true;
  140. renderer.setPixelRatio( window.devicePixelRatio );
  141. renderer.setSize( window.innerWidth, window.innerHeight );
  142. renderer.setClearColor( 0x263238 );
  143. window.addEventListener( 'resize', onWindowResize, false );
  144. document.body.appendChild( renderer.domElement );
  145. renderer.localClippingEnabled = true;
  146. // Controls
  147. var controls = new THREE.OrbitControls( camera, renderer.domElement );
  148. controls.update();
  149. // GUI
  150. var gui = new dat.GUI();
  151. gui.add( params, 'animate' );
  152. }
  153. function onWindowResize() {
  154. camera.aspect = window.innerWidth / window.innerHeight;
  155. camera.updateProjectionMatrix();
  156. renderer.setSize( window.innerWidth, window.innerHeight );
  157. }
  158. function animate() {
  159. var delta = clock.getDelta();
  160. requestAnimationFrame( animate );
  161. if ( params.animate ) {
  162. object.rotation.x += delta * 0.5;
  163. object.rotation.y += delta * 0.2;
  164. }
  165. for ( var i = 0; i < planeObjects.length; i ++ ) {
  166. var plane = planes[ i ];
  167. var po = planeObjects[ i ];
  168. plane.coplanarPoint( po.position );
  169. po.lookAt(
  170. po.position.x - plane.normal.x,
  171. po.position.y - plane.normal.y,
  172. po.position.z - plane.normal.z,
  173. );
  174. }
  175. stats.begin();
  176. renderer.render( scene, camera );
  177. stats.end();
  178. }
  179. </script>
  180. </body>
  181. </html>