webgl_clipping_stencil.html 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - clipping stencil</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. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - solid geometry with clip planes and stencil materials</div>
  11. <script type="module">
  12. import * as THREE from '../build/three.module.js';
  13. import { OrbitControls } from './jsm/controls/OrbitControls.js';
  14. import { GUI } from './jsm/libs/dat.gui.module.js';
  15. import Stats from './jsm/libs/stats.module.js';
  16. let camera, scene, renderer, object, stats;
  17. let planes, planeObjects, planeHelpers;
  18. let clock;
  19. const params = {
  20. animate: true,
  21. planeX: {
  22. constant: 0,
  23. negated: false,
  24. displayHelper: false
  25. },
  26. planeY: {
  27. constant: 0,
  28. negated: false,
  29. displayHelper: false
  30. },
  31. planeZ: {
  32. constant: 0,
  33. negated: false,
  34. displayHelper: false
  35. }
  36. };
  37. init();
  38. animate();
  39. function createPlaneStencilGroup( geometry, plane, renderOrder ) {
  40. const group = new THREE.Group();
  41. const baseMat = new THREE.MeshBasicMaterial();
  42. baseMat.depthWrite = false;
  43. baseMat.depthTest = false;
  44. baseMat.colorWrite = false;
  45. baseMat.stencilWrite = true;
  46. baseMat.stencilFunc = THREE.AlwaysStencilFunc;
  47. // back faces
  48. const mat0 = baseMat.clone();
  49. mat0.side = THREE.BackSide;
  50. mat0.clippingPlanes = [ plane ];
  51. mat0.stencilFail = THREE.IncrementWrapStencilOp;
  52. mat0.stencilZFail = THREE.IncrementWrapStencilOp;
  53. mat0.stencilZPass = THREE.IncrementWrapStencilOp;
  54. const mesh0 = new THREE.Mesh( geometry, mat0 );
  55. mesh0.renderOrder = renderOrder;
  56. group.add( mesh0 );
  57. // front faces
  58. const mat1 = baseMat.clone();
  59. mat1.side = THREE.FrontSide;
  60. mat1.clippingPlanes = [ plane ];
  61. mat1.stencilFail = THREE.DecrementWrapStencilOp;
  62. mat1.stencilZFail = THREE.DecrementWrapStencilOp;
  63. mat1.stencilZPass = THREE.DecrementWrapStencilOp;
  64. const mesh1 = new THREE.Mesh( geometry, mat1 );
  65. mesh1.renderOrder = renderOrder;
  66. group.add( mesh1 );
  67. return group;
  68. }
  69. function init() {
  70. clock = new THREE.Clock();
  71. scene = new THREE.Scene();
  72. camera = new THREE.PerspectiveCamera( 36, window.innerWidth / window.innerHeight, 1, 100 );
  73. camera.position.set( 2, 2, 2 );
  74. scene.add( new THREE.AmbientLight( 0xffffff, 0.5 ) );
  75. const dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
  76. dirLight.position.set( 5, 10, 7.5 );
  77. dirLight.castShadow = true;
  78. dirLight.shadow.camera.right = 2;
  79. dirLight.shadow.camera.left = - 2;
  80. dirLight.shadow.camera.top = 2;
  81. dirLight.shadow.camera.bottom = - 2;
  82. dirLight.shadow.mapSize.width = 1024;
  83. dirLight.shadow.mapSize.height = 1024;
  84. scene.add( dirLight );
  85. planes = [
  86. new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 0 ),
  87. new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0 ),
  88. new THREE.Plane( new THREE.Vector3( 0, 0, - 1 ), 0 )
  89. ];
  90. planeHelpers = planes.map( p => new THREE.PlaneHelper( p, 2, 0xffffff ) );
  91. planeHelpers.forEach( ph => {
  92. ph.visible = false;
  93. scene.add( ph );
  94. } );
  95. const geometry = new THREE.TorusKnotGeometry( 0.4, 0.15, 220, 60 );
  96. object = new THREE.Group();
  97. scene.add( object );
  98. // Set up clip plane rendering
  99. planeObjects = [];
  100. const planeGeom = new THREE.PlaneGeometry( 4, 4 );
  101. for ( let i = 0; i < 3; i ++ ) {
  102. const poGroup = new THREE.Group();
  103. const plane = planes[ i ];
  104. const stencilGroup = createPlaneStencilGroup( geometry, plane, i + 1 );
  105. // plane is clipped by the other clipping planes
  106. const planeMat =
  107. new THREE.MeshStandardMaterial( {
  108. color: 0xE91E63,
  109. metalness: 0.1,
  110. roughness: 0.75,
  111. clippingPlanes: planes.filter( p => p !== plane ),
  112. stencilWrite: true,
  113. stencilRef: 0,
  114. stencilFunc: THREE.NotEqualStencilFunc,
  115. stencilFail: THREE.ReplaceStencilOp,
  116. stencilZFail: THREE.ReplaceStencilOp,
  117. stencilZPass: THREE.ReplaceStencilOp,
  118. } );
  119. const po = new THREE.Mesh( planeGeom, planeMat );
  120. po.onAfterRender = function ( renderer ) {
  121. renderer.clearStencil();
  122. };
  123. po.renderOrder = i + 1.1;
  124. object.add( stencilGroup );
  125. poGroup.add( po );
  126. planeObjects.push( po );
  127. scene.add( poGroup );
  128. }
  129. const material = new THREE.MeshStandardMaterial( {
  130. color: 0xFFC107,
  131. metalness: 0.1,
  132. roughness: 0.75,
  133. clippingPlanes: planes,
  134. clipShadows: true,
  135. shadowSide: THREE.DoubleSide,
  136. } );
  137. // add the color
  138. const clippedColorFront = new THREE.Mesh( geometry, material );
  139. clippedColorFront.castShadow = true;
  140. clippedColorFront.renderOrder = 6;
  141. object.add( clippedColorFront );
  142. const ground = new THREE.Mesh(
  143. new THREE.PlaneGeometry( 9, 9, 1, 1 ),
  144. new THREE.ShadowMaterial( { color: 0, opacity: 0.25, side: THREE.DoubleSide } )
  145. );
  146. ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
  147. ground.position.y = - 1;
  148. ground.receiveShadow = true;
  149. scene.add( ground );
  150. // Stats
  151. stats = new Stats();
  152. document.body.appendChild( stats.dom );
  153. // Renderer
  154. renderer = new THREE.WebGLRenderer( { antialias: true } );
  155. renderer.shadowMap.enabled = true;
  156. renderer.setPixelRatio( window.devicePixelRatio );
  157. renderer.setSize( window.innerWidth, window.innerHeight );
  158. renderer.setClearColor( 0x263238 );
  159. window.addEventListener( 'resize', onWindowResize );
  160. document.body.appendChild( renderer.domElement );
  161. renderer.localClippingEnabled = true;
  162. // Controls
  163. const controls = new OrbitControls( camera, renderer.domElement );
  164. controls.minDistance = 2;
  165. controls.maxDistance = 20;
  166. controls.update();
  167. // GUI
  168. const gui = new GUI();
  169. gui.add( params, 'animate' );
  170. const planeX = gui.addFolder( 'planeX' );
  171. planeX.add( params.planeX, 'displayHelper' ).onChange( v => planeHelpers[ 0 ].visible = v );
  172. planeX.add( params.planeX, 'constant' ).min( - 1 ).max( 1 ).onChange( d => planes[ 0 ].constant = d );
  173. planeX.add( params.planeX, 'negated' ).onChange( () => {
  174. planes[ 0 ].negate();
  175. params.planeX.constant = planes[ 0 ].constant;
  176. } );
  177. planeX.open();
  178. const planeY = gui.addFolder( 'planeY' );
  179. planeY.add( params.planeY, 'displayHelper' ).onChange( v => planeHelpers[ 1 ].visible = v );
  180. planeY.add( params.planeY, 'constant' ).min( - 1 ).max( 1 ).onChange( d => planes[ 1 ].constant = d );
  181. planeY.add( params.planeY, 'negated' ).onChange( () => {
  182. planes[ 1 ].negate();
  183. params.planeY.constant = planes[ 1 ].constant;
  184. } );
  185. planeY.open();
  186. const planeZ = gui.addFolder( 'planeZ' );
  187. planeZ.add( params.planeZ, 'displayHelper' ).onChange( v => planeHelpers[ 2 ].visible = v );
  188. planeZ.add( params.planeZ, 'constant' ).min( - 1 ).max( 1 ).onChange( d => planes[ 2 ].constant = d );
  189. planeZ.add( params.planeZ, 'negated' ).onChange( () => {
  190. planes[ 2 ].negate();
  191. params.planeZ.constant = planes[ 2 ].constant;
  192. } );
  193. planeZ.open();
  194. }
  195. function onWindowResize() {
  196. camera.aspect = window.innerWidth / window.innerHeight;
  197. camera.updateProjectionMatrix();
  198. renderer.setSize( window.innerWidth, window.innerHeight );
  199. }
  200. function animate() {
  201. const delta = clock.getDelta();
  202. requestAnimationFrame( animate );
  203. if ( params.animate ) {
  204. object.rotation.x += delta * 0.5;
  205. object.rotation.y += delta * 0.2;
  206. }
  207. for ( let i = 0; i < planeObjects.length; i ++ ) {
  208. const plane = planes[ i ];
  209. const po = planeObjects[ i ];
  210. plane.coplanarPoint( po.position );
  211. po.lookAt(
  212. po.position.x - plane.normal.x,
  213. po.position.y - plane.normal.y,
  214. po.position.z - plane.normal.z,
  215. );
  216. }
  217. stats.begin();
  218. renderer.render( scene, camera );
  219. stats.end();
  220. }
  221. </script>
  222. </body>
  223. </html>