1
0

webgl_clipping_stencil.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. <meta property="og:title" content="three.js webgl - clipping stencil">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_clipping_stencil.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_clipping_stencil.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - solid geometry with clip planes and stencil materials</div>
  15. <script type="importmap">
  16. {
  17. "imports": {
  18. "three": "../build/three.module.js",
  19. "three/addons/": "./jsm/"
  20. }
  21. }
  22. </script>
  23. <script type="module">
  24. import * as THREE from 'three';
  25. import { OrbitControls } from 'three/addons/controls/OrbitControls.js';
  26. import { GUI } from 'three/addons/libs/lil-gui.module.min.js';
  27. import Stats from 'three/addons/libs/stats.module.js';
  28. let camera, scene, renderer, object, stats;
  29. let planes, planeObjects, planeHelpers;
  30. let timer;
  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. function createPlaneStencilGroup( geometry, plane, renderOrder ) {
  51. const group = new THREE.Group();
  52. const baseMat = new THREE.MeshBasicMaterial();
  53. baseMat.depthWrite = false;
  54. baseMat.depthTest = false;
  55. baseMat.colorWrite = false;
  56. baseMat.stencilWrite = true;
  57. baseMat.stencilFunc = THREE.AlwaysStencilFunc;
  58. // back faces
  59. const mat0 = baseMat.clone();
  60. mat0.side = THREE.BackSide;
  61. mat0.clippingPlanes = [ plane ];
  62. mat0.stencilFail = THREE.IncrementWrapStencilOp;
  63. mat0.stencilZFail = THREE.IncrementWrapStencilOp;
  64. mat0.stencilZPass = THREE.IncrementWrapStencilOp;
  65. const mesh0 = new THREE.Mesh( geometry, mat0 );
  66. mesh0.renderOrder = renderOrder;
  67. group.add( mesh0 );
  68. // front faces
  69. const mat1 = baseMat.clone();
  70. mat1.side = THREE.FrontSide;
  71. mat1.clippingPlanes = [ plane ];
  72. mat1.stencilFail = THREE.DecrementWrapStencilOp;
  73. mat1.stencilZFail = THREE.DecrementWrapStencilOp;
  74. mat1.stencilZPass = THREE.DecrementWrapStencilOp;
  75. const mesh1 = new THREE.Mesh( geometry, mat1 );
  76. mesh1.renderOrder = renderOrder;
  77. group.add( mesh1 );
  78. return group;
  79. }
  80. function init() {
  81. timer = new THREE.Timer();
  82. timer.connect( document );
  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, 1.5 ) );
  87. const dirLight = new THREE.DirectionalLight( 0xffffff, 3 );
  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. // Renderer
  163. renderer = new THREE.WebGLRenderer( { antialias: true, stencil: true } );
  164. renderer.setPixelRatio( window.devicePixelRatio );
  165. renderer.setSize( window.innerWidth, window.innerHeight );
  166. renderer.setClearColor( 0x263238 );
  167. renderer.setAnimationLoop( animate );
  168. renderer.shadowMap.enabled = true;
  169. renderer.localClippingEnabled = true;
  170. document.body.appendChild( renderer.domElement );
  171. // Stats
  172. stats = new Stats();
  173. document.body.appendChild( stats.dom );
  174. //
  175. window.addEventListener( 'resize', onWindowResize );
  176. // Controls
  177. const controls = new OrbitControls( camera, renderer.domElement );
  178. controls.minDistance = 2;
  179. controls.maxDistance = 20;
  180. controls.update();
  181. // GUI
  182. const gui = new GUI();
  183. gui.add( params, 'animate' );
  184. const planeX = gui.addFolder( 'planeX' );
  185. planeX.add( params.planeX, 'displayHelper' ).onChange( v => planeHelpers[ 0 ].visible = v );
  186. planeX.add( params.planeX, 'constant' ).min( - 1 ).max( 1 ).onChange( d => planes[ 0 ].constant = d );
  187. planeX.add( params.planeX, 'negated' ).onChange( () => {
  188. planes[ 0 ].negate();
  189. params.planeX.constant = planes[ 0 ].constant;
  190. } );
  191. planeX.open();
  192. const planeY = gui.addFolder( 'planeY' );
  193. planeY.add( params.planeY, 'displayHelper' ).onChange( v => planeHelpers[ 1 ].visible = v );
  194. planeY.add( params.planeY, 'constant' ).min( - 1 ).max( 1 ).onChange( d => planes[ 1 ].constant = d );
  195. planeY.add( params.planeY, 'negated' ).onChange( () => {
  196. planes[ 1 ].negate();
  197. params.planeY.constant = planes[ 1 ].constant;
  198. } );
  199. planeY.open();
  200. const planeZ = gui.addFolder( 'planeZ' );
  201. planeZ.add( params.planeZ, 'displayHelper' ).onChange( v => planeHelpers[ 2 ].visible = v );
  202. planeZ.add( params.planeZ, 'constant' ).min( - 1 ).max( 1 ).onChange( d => planes[ 2 ].constant = d );
  203. planeZ.add( params.planeZ, 'negated' ).onChange( () => {
  204. planes[ 2 ].negate();
  205. params.planeZ.constant = planes[ 2 ].constant;
  206. } );
  207. planeZ.open();
  208. }
  209. function onWindowResize() {
  210. camera.aspect = window.innerWidth / window.innerHeight;
  211. camera.updateProjectionMatrix();
  212. renderer.setSize( window.innerWidth, window.innerHeight );
  213. }
  214. function animate() {
  215. timer.update();
  216. const delta = timer.getDelta();
  217. if ( params.animate ) {
  218. object.rotation.x += delta * 0.5;
  219. object.rotation.y += delta * 0.2;
  220. }
  221. for ( let i = 0; i < planeObjects.length; i ++ ) {
  222. const plane = planes[ i ];
  223. const po = planeObjects[ i ];
  224. plane.coplanarPoint( po.position );
  225. po.lookAt(
  226. po.position.x - plane.normal.x,
  227. po.position.y - plane.normal.y,
  228. po.position.z - plane.normal.z,
  229. );
  230. }
  231. stats.begin();
  232. renderer.render( scene, camera );
  233. stats.end();
  234. }
  235. </script>
  236. </body>
  237. </html>
粤ICP备19079148号