Guide on creating windows, openings, or cutting shapes in three.js

I'm new to working with three.js and I have a project where I need to create a "room" with doors and windows. It seems like a simple task, but all the answers I've found so far are not up-to-date.

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

In my specific case, I have a large box and I want to subtract a smaller one. You can view an example on JSFIDDLE here:

https://i.sstatic.net/Todln.png

var material = new THREE.MeshBasicMaterial({color: 0xffff00});
var faceMaterial_Y = new THREE.MeshLambertMaterial( { color: 0x0087E6 } );
var geometry_Y = new THREE.BoxBufferGeometry( 1.5, 1.5, 0.99 );
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 );

If anyone could provide some guidance, it would be greatly appreciated. Thank you!

Answer №1

If you're looking to subtract geometries, one option is to utilize the ThreeCSG or ThreeBSP libraries.

To start, create ThreeBSP objects from 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);

Next, perform the subtraction operation on the geometries and generate 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);

For an example of this technique, refer to the image below:

https://i.sstatic.net/GgWSu.png

(function onLoad() {
  // Code for initializing the scene with appropriate lighting & camera settings
})();

// Script tags for importing necessary libraries and setting up the container element

Check out these related discussions:
  • Unexpected result using ThreeCSG
  • After using the threeBSP method, the created spheres are not smooth

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Can a snapshot be taken of a particular section of your website?

I'm curious to know if there is a method to capture an image from a specific part of a website and store it on the computer. Is this achievable? HTML <div id="red" color="red" width="100px" height="100px"></div> <div id="green" color= ...

Modify the numerical presentation within the provided input text

Looking to format numbers in the thousands with a comma, for example changing 2000 to 2,000 and 20000 to 20,000. While I found a solution using cleave.js library, it only works for one input field. Is there another alternative that can handle multiple in ...

Thee.JSONLoader object as a publicly accessible variable

Is it possible to declare an object loaded via THREE.JSONLoader as a public variable in JavaScript? I come from a strong 3D background but am new to JS and WebGL. I would like all my loaded textures, material creation, and geometry to be easily manipulab ...

Editing the content of a div in real-time by dynamically updating the innerHTML

Looking for a way to dynamically change the innerHTML of a contentEditable div using pure JavaScript, without relying on the Rangy library. I want the change to occur on keyup without losing cursor position or focus. Here is an example setup: <div id=" ...

Organize AngularJS ng-repeat using dictionary information

I'm dealing with a dictionary consisting of key-value pairs, which looks something like this: data = { "key1": 1000, "key2": 2000, "key3": 500 } My goal is to display this data in a table using AngularJS. One way I can achieve this is b ...

Obtaining the initial value from an Observable in Angular 8+

Initially, I have a page form with preset values and buttons for navigating to the next or previous items. Upon initialization in ngOnInit, an observable provides me with a list of 3 items as the starting value - sometimes it may even contain 4 items. Ho ...

Executing several Javascript functions when the page is loaded

I have attempted to invoke 4 JavaScript functions from the HTML body when the page is loaded. Each function calls a JSP servlet to retrieve data from a database and populate the respective list boxes. Currently, I am focusing on the edit screen where I am ...

Body Zoom Browser

My website features buttons for zooming in and out, labeled as A + and A-. I wanted to make sure that the entire body of the website could be magnified easily. That's why I came up with this code: <html> <body> <style> .cont ...

The function currentTime is ineffective when attempting to stream audio through an http connection

Currently, I am implementing audio streaming through HTTP. To load the audio, I have incorporated the following code snippet. var audio = new Audio("http://example.com/get/stream/1/wav"); function seek(time){ audio.currentTime = time; } The browser h ...

Achieve Custom Styling in Material UI Stepper Label with ReactJS: Adjust Font Size and Margin Top

Is there a way to adjust the fontsize of the stepper label and the margin between the label and circle? The default marginTop is set to 16px, but I would like to reduce it. Any suggestions on how to achieve this? Check out the Codesandbox code using Mater ...

Ways to determine if your code is written in ES6

After completing a lot of Javascript coding, I just learned about the existence of various JS 'versions' such as ES5 and ES6. My project is now up on Github, and someone advised me to convert my ES6 code to ES5 using Babel. The problem is, I&ap ...

Into the depths we delve, extending beyond to an array of objects?

I have a question about the possibility of extending an array of objects like this: Imagine I have : myObj = [ {"name" : "one", "test" : ["1","2"]}, {"name" : "two", "test" : ["1","2"]} ] And I want to add to it something like - {"name" : ...

JavaScript - Issue with For Loop when Finding Symmetric Difference

Here is my solution to a coding challenge on FreeCodeCamp called "Symmetric Difference." I'm puzzled as to why my code is returning 2, 3, 4, 6 instead of the expected 2, 3, 4, 6, 7. function sym(args) { args = Array.from(arguments); var new ...

Exploring ways to incorporate mouse movements into this simple JavaScript script

I have recently decided to convert my JavaScript code into jQuery code to incorporate mouse events. After downloading the latest version of jQuery and renaming it as "jquery.js," I made modifications to my manifest.json file by adding "jquery.js." However, ...

What is the best way to determine the final letter of a column in a Google Sheet, starting from the first letter and using a set of

My current approach involves generating a single letter, but my code breaks if there is a large amount of data and it exceeds column Z. Here is the working code that will produce a, d: const countData = [1, 2, 3, 4].length; const initialLetter = 'A&a ...

What is the best way to update the Label located within a Grid view using JavaScript?

How can I implement a mechanism to refresh the label inside a grid view every second in JavaScript, similar to an Ajax timer? EDIT: OnLoad="setTimeout(window.location.reload();. 1000);" what is the issue with this code or <asp:Label ID="Label2" Width= ...

Issues arise when building with Angular using `ng build`, but everything runs smoothly when using `

My Angular 5 application uses angular-cli 1.6.8 { "name": "test", "version": "0.0.0", "license": "MIT", "scripts": { "ng": "ng", "start": "ng serve", "build": "ng build", "karma": "ng test", "test": "jest", "test:watch": "j ...

Is there a way to alter the timestamp on comments in a React Native app, similar to Instagram's functionality, using dayjs?

I am currently working on a project using react native in combination with dayjs library. My goal is to compare the timestamp of when a comment was written with the current time, and then display this compared time using console.log() with the help of day ...

Is there a way to dynamically add an option to multiple select boxes and then only redirect the browser if that specific option is chosen using jquery/js?

Presently, this is the code I am working with. The URL validator is specifically included because there was an issue with redirection occurring on any option selected, however, I only want a redirect to happen when the options I add with jQuery are clicked ...

What is the best way to insert data from a promise into MongoDB?

While attempting to integrate an array of JSON data from a different server into a MongoDB collection, I encountered the following error message: "Cannot create property '_id' on string". Even though I am passing in an array, it seems to be causi ...