webgl_clipping_stencil.html 8.4 KB

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