webgl_clipping_stencil.html 8.2 KB

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