Issue with ThreeJS CSG when trying to intersect extruded geometries

element, I'm facing a challenge with extracting multiple views and intersecting them to form a final polygon. The issue arises when there are floating extra parts in the result that are unexpected. My goal is to find a solution to detect these extraneous parts and eliminate them if possible. I'm utilizing the library at https://www.npmjs.com/package/three-csg-ts/v/3.1.10 for performing binary operations during intersection. It's unclear whether this discrepancy is due to a bug or an error on my part. Despite experimenting with various configurations for the extrusion settings, I continue to encounter the same issue. Admittedly, my familiarity with JavaScript and ThreeJS is limited, so I apologize if my code lacks clarity. I've made efforts to optimize it given my current knowledge level.

import './style.css' import * as THREE from 'three' import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' import { CSG } from 'three-csg-ts' // Canvas const canvas = document.querySelector('canvas.webgl') ... The screenshots accompanying this description visually demonstrate the problem: - View Screenshot: [Link] - View Screenshot: [Link] My suspicion leans towards potential flaws in the extrusion technique currently employed. I'm uncertain about the correct method required for consistent functionality. Alternatively, the methodology used for intersections—sequentially intersecting each shape with the prior result—might be contributing to the issue. I experimented with a different approach, trying to intersect two objects simultaneously and then combining their intersection outputs, but progress remains elusive.

Answer №1

After some experimentation with the provided code, I believe my outcome is accurate.

The issue seems to lie within the intersection portion of the code:

//Intersection
var intersection = meshes[0]
for (let mesh in meshes){
    meshes[mesh].updateMatrix()
    intersection = CSG.intersect(intersection,meshes[mesh])
}
intersection.material = new THREE.MeshNormalMaterial()
scene.add(intersection)

There appears to be a flaw here. According to the associative property of logical set intersection, it shouldn't make a difference whether you intersect each mesh individually (please correct me if I'm mistaken). Thus, that may not be the root cause of the problem.

To address this issue, I noticed that setting the initial intersectee as mesh[0] and then looping through the entire array might lead to intersecting at least one shape with itself. Introducing

if(meshes[mesh].geometry.uuid === meshes[0].geometry.uuid) continue;
in the loop seemed to improve the results, although it's not a definitive solution.

My suggested solution involves replacing the entire intersection segment of the code with:

//Intersection
meshes.forEach((mesh) => mesh.updateMatrix());
var intersection = CSG.intersect(...meshes);
intersection.material = new THREE.MeshNormalMaterial()
scene.add(intersection)

In this revision, I switched the loop for a more functional forEach statement to update the matrices efficiently.

Additionally, I utilized spread syntax to invoke CSG.intersect with all meshes from the array simultaneously.

The resulting shape can be viewed here: https://i.sstatic.net/84vHa.png

Although I cannot guarantee that this accurately represents the intersection of multiple polygons, it appears neat and devoid of any extraneous floating components seen in your original example.

P.S Thanks for the engaging intersection code! Delving into both Three.js and exploring CSG was an enjoyable experience. You can access my codesandbox demo here.

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

Confirm the value of $index and apply a specific style

Trying to figure out how to highlight a table row using AngularJS within a directive. Here is some pseudo code I came up with: if(highlight.indexOf($index) != -1) set .highlight css class Below is an example of my code snippet: $scope.highlight = [0, ...

What could be causing my Wikipedia opensearch AJAX request to not return successfully?

I've been attempting various methods to no avail when trying to execute the success block. The ajax request keeps returning an error despite having the correct URL. My current error message reads "undefined". Any suggestions on alternative approaches ...

Vue.js - Implementing multiple values (array) for a component through a property

I am currently working with vue.js components that receive their data from external sources. For example: <vue-button icon="fa-arrow-right" text="mytext"></vue-button> Initially, this setup is effective. However, I encountered a challenge wh ...

Adding caller information to error stack trace in a Node.js application

I have a function named inner that executes a series of asynchronous operations: function inner(input) { return step1(input) .then(step2) .then(step3) .catch((e) => { throw e }) } I propagate the error from inner so I can m ...

Examine the spans and delete the first-child node

Basically, this functionality allows the user to cycle through hidden information by clicking on it. Here is how you can structure your HTML: <div id="facts"> <span>click to cycle</span> <span>fact 1</span> <s ...

What is the correct way to import the THREE.js library into your project?

I recently added the three library to my project via npm. Within the node_modules directory, there is a folder named three. However, when I tried to import it using: import * as THREE from 'three'; I encountered the following error: ReferenceEr ...

Using node appendChild() in HTML for animating elements

As someone who is brand new to the world of web development, I am trying my hand at creating a webpage that expands as a button is clicked. Recently, I stumbled upon this helpful link which includes some code: The HTML code snippet is: <ul id="myList" ...

Having trouble accessing req.user on my Node.js server using Auth0 and Angular

Currently, I am utilizing auth0 for my admin panel's login system and it is functioning smoothly. However, I have encountered an issue in node where 'req.user' is returning as undefined for some unknown reason. This setup is fairly basic; I ...

Is there a way to automatically execute this function when the React component is loaded, without the need for a button click?

After spending a few days trying to figure this out, I'm hitting a wall. This ReactJS project is new for me as I usually work with C#. The goal is to create a print label component that triggers the print dialog when a link in the menu is clicked. Cu ...

Simply modifying a custom attribute source using jQuery does not result in it being refreshed

Introduction: Utilizing jQuery.elevateZoom-3.0.8 to display zoomed-in images. The SRC attribute contains the path to the smaller/normal image The DATA-ZOOM-IMAGE attribute holds the path to the larger image used for zooming. Here is the HTML Code: ...

What is the optimal method for creating and testing AJAX applications on a local server, then effortlessly deploying them online?

Exploring AJAX development is new to me. The challenge I've encountered so far is dealing with the same-origin policy, which requires modifying host information strings like absolute URLs in JavaScript files every time I deploy local files to remote s ...

creating a fresh instance of a class while in a subscribe method

Although this code is functional, it briefly displays incorrect data because a blank token is instantiated before being populated in the subscribe function. Is there a way to move the instantiation into the subscribe function or provide all necessary par ...

Hovering over triggers tooltip flickering with Mouseover/Mouseenter interaction

When the mouseover event is triggered on the parent element, there seems to be a flickering issue when moving into the tooltip (child) element. The console log indicates that the mouseover/mouseenter event is being fired rapidly. The tooltip does not stay ...

What types of shadowMapTypes are available?

Can you list the potential shadowMapTypes available for renderer? At this time, I am only aware of three: THREE. PCFShadowMap PCFSoftShadowMap PCFBasicShadowMap ...

Tips for effectively sharing content on social media from your Vuejs application

I have been using the vue-social-sharing library to enable social media sharing on my website, and overall it's been working well. However, I am facing a problem where when I click the Facebook share button, it doesn't share the title, descriptio ...

Determine how to use both the "if" and "else if" statements in

/html code/ There are 4 textboxes on this page for entering minimum and maximum budget and area values. The condition set is that the maximum value should be greater than the minimum value for both budget and area. This condition is checked when the form i ...

Having trouble loading HTML content from another file

Here is the code snippet in question: <script> var load = function load_home(){ document.getElementById("content").innerHTML='<object type="type/html" data="/linker/templates/button.html" ></object>'; } </script> ...

Obtain the identification address for a group of items

I have a JSON object containing place IDs, and I am attempting to retrieve the corresponding addresses for each ID. This is the code snippet I'm using: <div id="places" class="places"></div> <script> function initialize() { j ...

Erase all records using MongoDB by the following criteria or

I am currently working with a calendar document structured as shown below: calendar: [ {days: { 1: [], 2: ['surprise'], 3: [], ... }}, {days: { 1: [], 2: [], 3: ['test'], ... }} ] My task involves locating specific ...

When a div is clicked, the text inside the button changes. If another div is clicked, the previous text is reset

I am seeking a solution for changing the text of a button within three columns when a specific 'advice-card' div is clicked on. The text should change to 'Hide' for the clicked div, and the other buttons should switch back to 'Show ...