123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - clipping stencil</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <link type="text/css" rel="stylesheet" href="main.css">
- </head>
- <body>
- <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> - solid geometry with clip planes and stencil materials</div>
- <script type="module">
- import * as THREE from '../build/three.module.js';
- import { OrbitControls } from './jsm/controls/OrbitControls.js';
- import { GUI } from './jsm/libs/dat.gui.module.js';
- import Stats from './jsm/libs/stats.module.js';
- var camera, scene, renderer, object, stats;
- var planes, planeObjects, planeHelpers;
- var clock;
- var params = {
- animate: true,
- planeX: {
- constant: 0,
- negated: false,
- displayHelper: false
- },
- planeY: {
- constant: 0,
- negated: false,
- displayHelper: false
- },
- planeZ: {
- constant: 0,
- negated: false,
- displayHelper: false
- }
- };
- init();
- animate();
- function createPlaneStencilGroup( geometry, plane, renderOrder ) {
- var group = new THREE.Group();
- var baseMat = new THREE.MeshBasicMaterial();
- baseMat.depthWrite = false;
- baseMat.depthTest = false;
- baseMat.colorWrite = false;
- baseMat.stencilWrite = true;
- baseMat.stencilFunc = THREE.AlwaysStencilFunc;
- // back faces
- var mat0 = baseMat.clone();
- mat0.side = THREE.BackSide;
- mat0.clippingPlanes = [ plane ];
- mat0.stencilFail = THREE.IncrementWrapStencilOp;
- mat0.stencilZFail = THREE.IncrementWrapStencilOp;
- mat0.stencilZPass = THREE.IncrementWrapStencilOp;
- var mesh0 = new THREE.Mesh( geometry, mat0 );
- mesh0.renderOrder = renderOrder;
- group.add( mesh0 );
- // front faces
- var mat1 = baseMat.clone();
- mat1.side = THREE.FrontSide;
- mat1.clippingPlanes = [ plane ];
- mat1.stencilFail = THREE.DecrementWrapStencilOp;
- mat1.stencilZFail = THREE.DecrementWrapStencilOp;
- mat1.stencilZPass = THREE.DecrementWrapStencilOp;
- var mesh1 = new THREE.Mesh( geometry, mat1 );
- mesh1.renderOrder = renderOrder;
- group.add( mesh1 );
- return group;
- }
- function init() {
- clock = new THREE.Clock();
- scene = new THREE.Scene();
- camera = new THREE.PerspectiveCamera( 36, window.innerWidth / window.innerHeight, 1, 100 );
- camera.position.set( 2, 2, 2 );
- scene.add( new THREE.AmbientLight( 0xffffff, 0.5 ) );
- var dirLight = new THREE.DirectionalLight( 0xffffff, 1 );
- dirLight.position.set( 5, 10, 7.5 );
- dirLight.castShadow = true;
- dirLight.shadow.camera.right = 2;
- dirLight.shadow.camera.left = - 2;
- dirLight.shadow.camera.top = 2;
- dirLight.shadow.camera.bottom = - 2;
- dirLight.shadow.mapSize.width = 1024;
- dirLight.shadow.mapSize.height = 1024;
- scene.add( dirLight );
- planes = [
- new THREE.Plane( new THREE.Vector3( - 1, 0, 0 ), 0 ),
- new THREE.Plane( new THREE.Vector3( 0, - 1, 0 ), 0 ),
- new THREE.Plane( new THREE.Vector3( 0, 0, - 1 ), 0 )
- ];
- planeHelpers = planes.map( p => new THREE.PlaneHelper( p, 2, 0xffffff ) );
- planeHelpers.forEach( ph => {
- ph.visible = false;
- scene.add( ph );
- } );
- var geometry = new THREE.TorusKnotBufferGeometry( 0.4, 0.15, 220, 60 );
- object = new THREE.Group();
- scene.add( object );
- // Set up clip plane rendering
- planeObjects = [];
- var planeGeom = new THREE.PlaneBufferGeometry( 4, 4 );
- for ( var i = 0; i < 3; i ++ ) {
- var poGroup = new THREE.Group();
- var plane = planes[ i ];
- var stencilGroup = createPlaneStencilGroup( geometry, plane, i + 1 );
- // plane is clipped by the other clipping planes
- var planeMat =
- new THREE.MeshStandardMaterial( {
- color: 0xE91E63,
- metalness: 0.1,
- roughness: 0.75,
- clippingPlanes: planes.filter( p => p !== plane ),
- stencilWrite: true,
- stencilRef: 0,
- stencilFunc: THREE.NotEqualStencilFunc,
- stencilFail: THREE.ReplaceStencilOp,
- stencilZFail: THREE.ReplaceStencilOp,
- stencilZPass: THREE.ReplaceStencilOp,
- } );
- var po = new THREE.Mesh( planeGeom, planeMat );
- po.onAfterRender = function ( renderer ) {
- renderer.clearStencil();
- };
- po.renderOrder = i + 1.1;
- object.add( stencilGroup );
- poGroup.add( po );
- planeObjects.push( po );
- scene.add( poGroup );
- }
- var material = new THREE.MeshStandardMaterial( {
- color: 0xFFC107,
- metalness: 0.1,
- roughness: 0.75,
- clippingPlanes: planes,
- clipShadows: true,
- shadowSide: THREE.DoubleSide,
- } );
- // add the color
- var clippedColorFront = new THREE.Mesh( geometry, material );
- clippedColorFront.castShadow = true;
- clippedColorFront.renderOrder = 6;
- object.add( clippedColorFront );
- var ground = new THREE.Mesh(
- new THREE.PlaneBufferGeometry( 9, 9, 1, 1 ),
- new THREE.ShadowMaterial( { color: 0, opacity: 0.25, side: THREE.DoubleSide } )
- );
- ground.rotation.x = - Math.PI / 2; // rotates X/Y to X/Z
- ground.position.y = - 1;
- ground.receiveShadow = true;
- scene.add( ground );
- // Stats
- stats = new Stats();
- document.body.appendChild( stats.dom );
- // Renderer
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.shadowMap.enabled = true;
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.setClearColor( 0x263238 );
- window.addEventListener( 'resize', onWindowResize, false );
- document.body.appendChild( renderer.domElement );
- renderer.localClippingEnabled = true;
- // Controls
- var controls = new OrbitControls( camera, renderer.domElement );
- controls.minDistance = 2;
- controls.maxDistance = 20;
- controls.update();
- // GUI
- var gui = new GUI();
- gui.add( params, 'animate' );
- var planeX = gui.addFolder( 'planeX' );
- planeX.add( params.planeX, 'displayHelper' ).onChange( v => planeHelpers[ 0 ].visible = v );
- planeX.add( params.planeX, 'constant' ).min( - 1 ).max( 1 ).onChange( d => planes[ 0 ].constant = d );
- planeX.add( params.planeX, 'negated' ).onChange( () => {
- planes[ 0 ].negate();
- params.planeX.constant = planes[ 0 ].constant;
- } );
- planeX.open();
- var planeY = gui.addFolder( 'planeY' );
- planeY.add( params.planeY, 'displayHelper' ).onChange( v => planeHelpers[ 1 ].visible = v );
- planeY.add( params.planeY, 'constant' ).min( - 1 ).max( 1 ).onChange( d => planes[ 1 ].constant = d );
- planeY.add( params.planeY, 'negated' ).onChange( () => {
- planes[ 1 ].negate();
- params.planeY.constant = planes[ 1 ].constant;
- } );
- planeY.open();
- var planeZ = gui.addFolder( 'planeZ' );
- planeZ.add( params.planeZ, 'displayHelper' ).onChange( v => planeHelpers[ 2 ].visible = v );
- planeZ.add( params.planeZ, 'constant' ).min( - 1 ).max( 1 ).onChange( d => planes[ 2 ].constant = d );
- planeZ.add( params.planeZ, 'negated' ).onChange( () => {
- planes[ 2 ].negate();
- params.planeZ.constant = planes[ 2 ].constant;
- } );
- planeZ.open();
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- function animate() {
- var delta = clock.getDelta();
- requestAnimationFrame( animate );
- if ( params.animate ) {
- object.rotation.x += delta * 0.5;
- object.rotation.y += delta * 0.2;
- }
- for ( var i = 0; i < planeObjects.length; i ++ ) {
- var plane = planes[ i ];
- var po = planeObjects[ i ];
- plane.coplanarPoint( po.position );
- po.lookAt(
- po.position.x - plane.normal.x,
- po.position.y - plane.normal.y,
- po.position.z - plane.normal.z,
- );
- }
- stats.begin();
- renderer.render( scene, camera );
- stats.end();
- }
- </script>
- </body>
- </html>
|