Search code examples
three.jsstencil-buffer

Creating Hole in object using ThreeJS and Stencil method


I am trying to make a hole in a solid object(box) using stencil method in ThreeJS. I found few web pages and similar questions on the net in this regard but couldn't work them out. Specifically This link is what I want to achieve(even one hole is enough). However the code in the link is using Babylon and I need it to be in ThreeJS. Any idea how I can use ThreeJS to achieve this(or to translate it to ThreeJS)?

Update 1:

There is a sample in ThreeJS examples "misc_exporter_ply.html"(it's got nothing to do with stencil buffer though) and so I tried to use it as it has a simple scene with only one box. I added another cylinder inside the box to represent the hole.

I could get it to work so that there is a visible hole. Yet it's not perfect as the stencil buffer is not working as expected:

Image 1

Image 2

And here is the code:

       <!DOCTYPE html>
    <html lang="en">
    <head>
        <title>three.js webgl - exporter - ply</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> webgl - exporter - ply<br/><br/>
            <button id="exportASCII">export ASCII</button> <button id="exportBinaryBigEndian">export binary (Big Endian)</button> <button id="exportBinaryLittleEndian">export binary (Little Endian)</button>
        </div>

        <script type="module">

            import * as THREE from '../build/three.module.js';

            import { OrbitControls } from './jsm/controls/OrbitControls.js';
            import { PLYExporter } from './jsm/exporters/PLYExporter.js';

            let scene, camera, renderer, exporter, mesh, meshHole, mesh0, mesh1;

            init();
            animate();

            function init() {

                camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
                camera.position.set( 200, 100, 200 );

                scene = new THREE.Scene();
                scene.background = new THREE.Color( 0xa1caf1 );
                //scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );

                //exporter = new PLYExporter();

                //

                const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
                hemiLight.position.set( 0, 200, 0 );
                scene.add( hemiLight );

                const directionalLight = new THREE.DirectionalLight( 0xffffff );
                directionalLight.position.set( 0, 200, 100 );
                directionalLight.castShadow = true;
                directionalLight.shadow.camera.top = 180;
                directionalLight.shadow.camera.bottom = - 100;
                directionalLight.shadow.camera.left = - 120;
                directionalLight.shadow.camera.right = 120;
                scene.add( directionalLight );

                // ground

                const ground = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0xaaaaaa, depthWrite: false } ) );
                ground.rotation.x = - Math.PI / 2;
                ground.receiveShadow = true;
                scene.add( ground );

                const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
                grid.material.opacity = 0.2;
                grid.material.transparent = true;
                scene.add( grid );

                //   mesh
 
                const boxHole = new THREE.CylinderGeometry( 15, 15, 65, 32, 32 );//BoxGeometry( 20, 20, 51 );  
                let matHole = new THREE.MeshPhongMaterial({ color: 0x00ff00 });
                matHole.colorWrite = false;
                meshHole = new THREE.Mesh( boxHole, matHole ); 
                meshHole.castShadow = true;
                meshHole.position.y = 50; 
                meshHole.rotation.x = Math.PI / 2;
                scene.add( meshHole );
                
                const geometry = new THREE.BoxGeometry( 50, 50, 50 ); 
                mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial({ color: 0xaaaaaa }) );
                mesh.castShadow = true;
                mesh.position.y = 50; 
                scene.add( mesh );
                 
                // back faces
                const mat0 = new THREE.MeshPhongMaterial({ color: 0x00ff00 });
                mat0.side = THREE.FrontSide;
                mat0.depthWrite = false; 
                mat0.depthTest = false;
                mat0.colorWrite = false;
                //mat0.stencilWrite = true;
                mat0.stencilFunc = THREE.AlwaysStencilFunc;
                mat0.stencilFail = THREE.KeepStencilOp;
                //mat0.stencilZFail = THREE.IncrementWrapStencilOp;
                //mat0.stencilZPass = THREE.ReplaceStencilOp;  
                mat0.stencilRef = 1;
                const boxHole0 = new THREE.CylinderGeometry( 15, 15, 65, 32, 32 );
                mesh0 = new THREE.Mesh( boxHole0, mat0 );
                mesh0.rotation.x = Math.PI / 2;
                mesh0.castShadow = true;
                mesh0.position.y = 50;  
                scene.add( mesh0 );
                 
                // front faces
                const mat1 = new THREE.MeshPhongMaterial({ color: 0x00ff00 });
                mat1.side = THREE.DoubleSide;
                //mat1.depthWrite = false; 
                //mat1.depthTest = true;
                //mat1.colorWrite = false;
                //mat1.stencilWrite = true; 
                mat1.depthFunc=THREE.AlwaysDepth;
                mat1.stencilFunc = THREE.EqualStencilFunc;
                mat1.stencilFail = THREE.IncrementStencilOp;
                //mat1.stencilZFail = THREE.DecrementWrapStencilOp;
                //mat1.stencilZPass = THREE.DecrementWrapStencilOp; 
                mat1.stencilRef = 1;
                const boxHole1 = new THREE.CylinderGeometry( 15, 15, 65, 32, 32, true );
                mesh1 = new THREE.Mesh( boxHole1, mat1 ); 
                mesh1.rotation.x = Math.PI / 2;
                mesh1.castShadow = true;
                mesh1.position.z = 0;
                mesh1.position.y = 50;  
                scene.add( mesh1 );
     
                //

                renderer = new THREE.WebGLRenderer( { antialias: true } );
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                renderer.shadowMap.enabled = true;
                renderer.localClippingEnabled = true;
                document.body.appendChild( renderer.domElement );

                //

                const controls = new OrbitControls( camera, renderer.domElement );
                controls.target.set( 0, 25, 0 );
                controls.update();

                //

                window.addEventListener( 'resize', onWindowResize );
 
            }

            function onWindowResize() {

                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();

                renderer.setSize( window.innerWidth, window.innerHeight );

            }

            function animate() {

                requestAnimationFrame( animate );
                renderer.render( scene, camera );

            }
 
            const link = document.createElement( 'a' );
            link.style.display = 'none';
            document.body.appendChild( link );

             

        </script>

    </body>
</html>

Solution

  • Got it working at the end:

    Box with cylinder hole

                import * as THREE from '../build/three.module.js';
    
                import { OrbitControls } from './jsm/controls/OrbitControls.js';
                import { PLYExporter } from './jsm/exporters/PLYExporter.js';
    
                let scene, camera, renderer, exporter, mesh, meshHole, mesh0, mesh1;
    
     
    
                function init() {
    
                    camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
                    camera.position.set( 200, 100, 200 );
    
                    scene = new THREE.Scene();
                    scene.background = new THREE.Color( 0xa1caf1 );
                    //scene.fog = new THREE.Fog( 0xa0a0a0, 200, 1000 );
    
                    //exporter = new PLYExporter();
    
                    //
    
                    const hemiLight = new THREE.HemisphereLight( 0xffffff, 0x444444 );
                    hemiLight.position.set( 0, 200, 0 );
                    scene.add( hemiLight );
    
                    const directionalLight = new THREE.DirectionalLight( 0xffffff );
                    directionalLight.position.set( 0, 200, 100 );
                    directionalLight.castShadow = true;
                    directionalLight.shadow.camera.top = 180;
                    directionalLight.shadow.camera.bottom = - 100;
                    directionalLight.shadow.camera.left = - 120;
                    directionalLight.shadow.camera.right = 120;
                    scene.add( directionalLight );
    
                    // ground
    
                    const ground = new THREE.Mesh( new THREE.PlaneGeometry( 2000, 2000 ), new THREE.MeshPhongMaterial( { color: 0xaaaaaa, depthWrite: false } ) );
                    ground.rotation.x = - Math.PI / 2;
                    ground.receiveShadow = true;
                    scene.add( ground );
    
                    const grid = new THREE.GridHelper( 2000, 20, 0x000000, 0x000000 );
                    grid.material.opacity = 0.2;
                    grid.material.transparent = true;
                    scene.add( grid );
    
                    //   mesh
     
                    const boxHole = new THREE.CylinderGeometry( 15, 15, 51, 32, 32 );//BoxGeometry( 20, 20, 51 );  
                    let matHole = new THREE.MeshPhongMaterial({ color: 0xaaaaaa });
                    matHole.colorWrite = false;
                    meshHole = new THREE.Mesh( boxHole, matHole ); 
                    meshHole.castShadow = true;
                    meshHole.position.y = 50; 
                    meshHole.rotation.x = Math.PI / 2;
                    scene.add( meshHole );
                    
                    const geometry = new THREE.BoxGeometry( 50, 50, 50 ); 
                    mesh = new THREE.Mesh( geometry, new THREE.MeshPhongMaterial({ color: 0xaaaaaa }) );
                    mesh.castShadow = true;
                    mesh.position.y = 50; 
                    scene.add( mesh );
                     
                    // front faces
                    const mat0 = new THREE.MeshPhongMaterial({ color: 0xaaaaaa });
                    mat0.side = THREE.FrontSide;
                    //mat0.depthWrite = false; 
                    //mat0.depthTest = false;
                    mat0.colorWrite = false;
                    const boxHole0 = new THREE.CylinderGeometry( 15, 15, 51, 32, 32 );
                    mesh0 = new THREE.Mesh( boxHole0, mat0 );
                    mesh0.rotation.x = Math.PI / 2;
                    mesh0.castShadow = true;
                    mesh0.position.y = 50;  
                    scene.add( mesh0 );
                    mat0.stencilWrite = true;
                    mat0.stencilFunc = THREE.AlwaysStencilFunc;
                    mat0.stencilFail = THREE.KeepStencilOp;
                    mat0.stencilZFail = THREE.KeepStencilOp;
                    mat0.stencilZPass = THREE.ReplaceStencilOp;
                    mat0.stencilRef = 1;
                    
                    // back faces
                    const mat1 = new THREE.MeshPhongMaterial({ color: 0xaaaaaa });
                    mat1.side = THREE.BackSide;
                    mat1.depthWrite = false; 
                    mat1.depthTest = true;
                    //mat1.colorWrite = false;
                    const boxHole1 = new THREE.CylinderGeometry( 15, 15, 51, 32, 32, true );
                    mesh1 = new THREE.Mesh( boxHole1, mat1 ); 
                    mesh1.rotation.x = Math.PI / 2;
                    mesh1.castShadow = true;
                    mesh1.position.z = 0;
                    mesh1.position.y = 50;  
                    scene.add( mesh1 );
                    mat1.depthFunc=THREE.AlwaysDepth;
                    mat1.stencilWrite = true; 
                    mat1.stencilFunc = THREE.EqualStencilFunc;
                    mat1.stencilFail = THREE.DecrementWrapStencilOp;
                    mat1.stencilZFail = THREE.DecrementWrapStencilOp;
                    mat1.stencilZPass = THREE.DecrementWrapStencilOp;
                    mat1.stencilRef = 1;
        
                    mesh1.onAfterRender = function ( renderer ) {
                        renderer.clearStencil();
                    };
                    //
    
                    renderer = new THREE.WebGLRenderer( { antialias: true } );
                    renderer.setPixelRatio( window.devicePixelRatio );
                    renderer.setSize( window.innerWidth, window.innerHeight );
                    renderer.shadowMap.enabled = true;
                    renderer.localClippingEnabled = true;
                    document.body.appendChild( renderer.domElement );
    
                    //
    
                    const controls = new OrbitControls( camera, renderer.domElement );
                    controls.target.set( 0, 25, 0 );
                    controls.update();
    
                    //
    
                    window.addEventListener( 'resize', onWindowResize );
     
                }
    

    Screenshot