4

I'm new with three.js and I need to create a "room" with doors and windows. This is a simple task but I have found only not up to date answers.

Similar questions are here: - subtracting-geometry-in-three-js - is-it-possible-to-cut-parts-of-the-shape-geometry-away-in-three-js

In my case, I have a big box and I want to subtract a smaller one JSFIDDLE example:

enter image description here

var material = new THREE.MeshBasicMaterial({color: 0xffff00});
var faceMaterial_Y = new THREE.MeshLambertMaterial( { color: 0x0087E6 } );
var faceMaterial = new THREE.MeshLambertMaterial( { color: 0x0087E6 } );
var geometry_Y = new THREE.BoxBufferGeometry( 1.5, 1.5, 0.99 );
var faceMaterial_Y = new THREE.MeshLambertMaterial( { color: 0xffff00 } );
var cube_Y = new THREE.Mesh( geometry_Y, faceMaterial_Y);

scene.add(cube_Y);

var geometry_A = new THREE.BoxBufferGeometry( 0.7, 0.7, 0.7 );
material = new THREE.MeshBasicMaterial( {color: 0x00ff00} );
var faceMaterial_A = new THREE.MeshLambertMaterial( { color: 0x00ff00 } );
var cubeA = new THREE.Mesh( geometry_A, material );
cubeA.position.set( 0.5, 0.5, 0 );

// HOW TO SUBTRACT cube_Y - cubeA?

//create a group and add the three cubes 
var group = new THREE.Group();
group.add( cubeA );
group.add( cube_Y );
scene.add( group );

Thanks for helping me!

gman
  • 100,619
  • 31
  • 269
  • 393
Uncoke
  • 1,832
  • 4
  • 26
  • 58
  • The answers you've linked to are still correct — threejs does not provide Constructive Solid Geometry operations itself, these must be done with other libraries like ThreeCSG. You may find more recent ideas in this thread: https://github.com/mrdoob/three.js/issues/16099 – Don McCurdy Jan 18 '20 at 20:23
  • Does this answer your question? [Is it possible to cut parts of the shape Geometry away in three.js?](https://stackoverflow.com/questions/9814229/is-it-possible-to-cut-parts-of-the-shape-geometry-away-in-three-js) – Mugen87 Jan 18 '20 at 22:28

2 Answers2

7

An option is to use ThreeCSG / ThreeBSP to subtract geometries.

Create ThreeBSP objects form the cube geometries:

var geometry_Y = new THREE.BoxBufferGeometry( 1.5, 1.5, 0.99 );
var geometry_A = new THREE.BoxBufferGeometry( 0.7, 0.7, 0.7 );
geometry_A.translate( 0.5, 0.5, 0 );

var bsp_A = new ThreeBSP(geometry_A);
var bsp_Y = new ThreeBSP(geometry_Y);

Subtract the geometry and create a Mesh:

var bsp_YsubA = bsp_Y.subtract(bsp_A);
var bsp_mesh = bsp_YsubA.toMesh();
bsp_mesh.material = new THREE.MeshLambertMaterial( { color: 0x00ff00 } );

scene.add( bsp_mesh );

See the example:

(function onLoad() {
  var container, camera, scene, renderer, controls;
  
  init();
  animate();

  function init() {
    container = document.getElementById('container');
    
    renderer = new THREE.WebGLRenderer({
      antialias: true
    });
    renderer.setPixelRatio(window.devicePixelRatio);
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMap.enabled = true;
    container.appendChild(renderer.domElement);

    scene = new THREE.Scene();
    scene.background = new THREE.Color(0xffffff);
    
    camera = new THREE.PerspectiveCamera(70, window.innerWidth / window.innerHeight, 1, 10000);
    camera.position.set(3, 1, -1);
    scene.add(camera);
    window.onresize = function() {
      renderer.setSize(window.innerWidth, window.innerHeight);
      camera.aspect = window.innerWidth / window.innerHeight;
      camera.updateProjectionMatrix();
    }
    
    var ambientLight = new THREE.AmbientLight(0x404040);
    scene.add(ambientLight);

    var directionalLight = new THREE.DirectionalLight( 0xffffff, 0.5 );
    directionalLight.position.x = 4;
    directionalLight.position.y = 1;
    directionalLight.position.z = -2;
    scene.add( directionalLight );
    
    controls = new THREE.OrbitControls(camera, renderer.domElement);
        
    addGridHelper();
    createModel();
  }

  function createModel() {

      var geometry_Y = new THREE.BoxBufferGeometry( 1.5, 1.5, 0.99 );
      var geometry_A = new THREE.BoxBufferGeometry( 0.7, 0.7, 0.7 );
      geometry_A.translate( 0.5, 0.5, 0 );
    
      var bsp_A = new ThreeBSP(geometry_A);
      var bsp_Y = new ThreeBSP(geometry_Y);
      
      var bsp_YsubA = bsp_Y.subtract(bsp_A);
      var bsp_mesh = bsp_YsubA.toMesh();
      bsp_mesh.material = new THREE.MeshLambertMaterial( { color: 0x00ff00 } );
      
      scene.add( bsp_mesh );
  }

  function addGridHelper() {

    var helper = new THREE.GridHelper(10, 10);
    helper.material.opacity = 0.25;
    helper.material.transparent = true;
    scene.add(helper);

    var axis = new THREE.AxesHelper(100);
    scene.add(axis);
  }

  function animate() {
    requestAnimationFrame(animate);
    render();
  }

  function render() {
    renderer.render(scene, camera);
  }
})();
<!--script src="https://threejs.org/build/three.js"></!--script-->
<script src="https://rawcdn.githack.com/mrdoob/three.js/r124/build/three.js"></script>
<script src="https://rawcdn.githack.com/mrdoob/three.js/r124/examples/js/controls/OrbitControls.js"></script>
<script src="https://rawgit.com/Wilt/ThreeCSG/develop/ThreeCSG.js"></script>
<div id="container"></div>
See also
M -
  • 26,908
  • 11
  • 49
  • 81
Rabbid76
  • 202,892
  • 27
  • 131
  • 174
  • Thanks, it works! Do you have any suggestions to apply this to a box having 4 rounded corners? I have tried an example but I got this error: https://stackoverflow.com/questions/59828669/rounded-corner-box-having-holes-using-three-js – Uncoke Jan 20 '20 at 17:59
  • 1
    @Uncoke Thank you. I've fixed the code. (Just THREE.js version issues) – Rabbid76 Dec 28 '20 at 08:58
3

https://github.com/manthrax/THREE-CSGMesh

You could try my CSG library. It is more robust than the other threejs csg solutions.

manthrax
  • 4,918
  • 1
  • 17
  • 16
  • What you mena with more robust exactly ? What issues are fixed from the official library ? – Omiod Jul 31 '20 at 14:41
  • 2
    More robust in that it handles texture coordinates... separate materials, and normals in a more complete way. I also fixed some bugs that I found in the original version I ported from: https://github.com/evanw/csg.js/ – manthrax Jul 31 '20 at 17:26
  • But.. if you don't need texcoords or normals, then it wont really matter much. – manthrax Jul 31 '20 at 17:27