Obtain all meshes within the bounds of a 3D cube using three.js

I am working with a fascinating system of tubes. These tubes are created using the following code:

tube_color - represents the color of the tube,
spline_points - a large collection of THREE.Vector3 objects,
segments, radiusSegments are just numerical values

    var material = new THREE.MeshLambertMaterial( { color: tube_color, shading: THREE.SmoothShading } );
    var spline = new THREE.SplineCurve3(spline_points);
    var tube = new THREE.TubeGeometry(spline, segments, 10, radiusSegments, false, false);
    var tubeMesh = new THREE.Mesh(tube, material);
    scene.add(tubeMesh);

This piece of code generates a specific mesh object within the space. By utilizing myMesh.geometry.vertices, I can obtain an array of Vector3's for each mesh.
The main issue is: when I place a cube around a certain point in 3D space, it intersects with the tubes. An example of creating such a cube is demonstrated below:

    var cube = new THREE.CubeGeometry(xSize,ySize,zSize, 5, 5, 5);
    var material = new THREE.MeshBasicMaterial({
        color: 0xff0000,
        opacity: 1,
        wireframe: true,
        transparent: false
    });
    var selectionMesh = new THREE.Mesh(cube, material);
    scene.add(selectionMesh);

Is there a way to identify meshes that intersect with this cubic area? While I could iterate through all meshes in the scene object and check if any of their points lie within the cubic area, I am hoping for a simpler method or algorithm to achieve this...

Answer №1

Following @WestLangley's advice, the solution to this problem involved implementing an octree structure.

octree=new THREE.Octree({
      undeferred:false,
      depthMax:Infinity,
      objectsThreshold:8,
      overlapPct:0.15
    } );

Once constructed, the octree was used for search operations:

var meshesSearch=octree.search( rayCaster.ray.origin, radiusSphere, true, rayCaster.ray.direction ); 

In order to achieve specific intersections, the recursive flag must be set:

raycaster.intersectOctreeObjects( objects, true )

Although it led to a more complex processing of my unique scenario, this approach successfully resolved the issue at hand.

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

Transforming a detailed JSON structure into a more simplified format with Angular 2

As a newcomer to Angular 2, I find myself encountering a hurdle in filtering unnecessary data from a JSON object that is retrieved from a REST API. Below is an example of the JSON data I am working with: { "refDataId":{ "rdk":1, "refDataTy ...

How can I access the object that created an interval from inside an interval callback function?

I am facing a challenge with JavaScript as a beginner. I am working on a React App and trying to add a timer feature to it. I have successfully created the timer functionality, but the issue lies in the timer callback (similar to this query: setInterval in ...

Determining in Express.js whether headers have been sent or not

As I develop a library that involves setting headers, I aim to provide a personalized error message in case the headers have already been sent. Rather than simply letting it fail with the default "Can't set headers after they are sent" message from No ...

Ways to hide an element when scrolling for a better user experience

I am encountering an issue with a custom icon element in my table. The icon is supposed to only be displayed when its specific row is hovered over. However, when I scroll down without moving my mouse, the hover state does not update and the icon remains on ...

Exploring the (*ngFor) Directive to Iterate Through an [object Object]

Attempting to iterate through the array using *ngFor as shown below. let geographicalArea = [{ "_id": "5e77f43e48348935b4571fa7", "name": "Latin America", "employee": { "_id": "5e77c50c4476e734d8b30dc6", "name": "Thomas", ...

Guide on executing a sequence of animations with the help of promises

In my current project, I am creating a Rubik's cube animation using three.js. The main challenge I am facing is implementing a function to shuffle the cube smoothly. So far, I have successfully developed functions to animate single moves, such as rot ...

Defining variables within a jQuery function

Within my initialization function (init), I have defined some variables and an animation that utilizes those variables. The challenge arises when I want to use the same animation/variables in my clickSlide function. http://jsfiddle.net/lollero/4WfZa/ (Un ...

Image loading failure detected in ReactJS

In my current project using Reactjs (Nextjs framework), I encountered an issue where I am unable to display an image on a page without specifying the "height" and "width" attributes in the Image tag. I attempted the following code snippet but the image is ...

Creating a captivating full-frame background video using react-vimeo or react-player: A step-by-step guide

I am currently working on embedding Vimeo videos as full-frame background videos with autoplay and loop functionalities, while hiding player controls. I managed to achieve this using a standard HTML video element, but faced challenges when attempting to im ...

Issue: Upon attempting to connect to a vsftpd server deployed on AWS using the npm module ssh2-sftp-client, all designated authentication methods have failed

Code snippet for connecting to the vsftpd server sftp.connect({ host: "3.6.75.65" port: "22" username: "ashish-ftp" password: "*******" }) .then(() => { console.log("result") }) .catch((err)=>{ ...

The issue of assigning Ajax data to jQuery inputs with identical classes is not being resolved correctly

I am currently developing an Invoicing System where the Rate(Amount) value changes automatically when the Product(Item) is changed. The issue I am encountering is that when I change the first Product, all the other Product Rates change to the Rate of the ...

Can you provide me with the accurate URL to access my Web API endpoint in asp.net?

I'm currently utilizing an API in my asp.net project and attempting to access it from my JavaScript file. However, I suspect there may be an issue with the URL I am using. Here is the controller and method I am trying to retrieve: [Route("api/[co ...

Executing a Python script to run the main.js and require.js files

Looking for assistance on executing JS files with Python code. For instance, transforming this HTML snippet: <script data-main="main.js" src="require.js"></script> into Python. ...

Validating alpha-numeric passwords with JavaScript

I created a JavaScript function to validate if a password is alphanumeric. However, I am facing an issue where the alert message is not being displayed when the password is not alphanumeric. Below is my code snippet: if (!input_string.match(/^[0-9a-z]+$ ...

Ways to prevent other users from clicking or modifying a particular row

I have a data table in my project that will be accessed by multiple users simultaneously. My requirement is that once a row is selected and edited by one user, it should become unclickable for other users who are also viewing the same page or data table. ...

Passing parameters between various components in a React application

Is it possible to pass a parameter or variable to a different component in React with react-router 3.0.0? For example, if a button is clicked and its onClick function redirects to another component where the variable should be instantly loaded to display a ...

Encountered an error while trying to create a new NestJS project using yarn: Command execution failure - yarn install --

Having trouble installing a new NestJs project using yarn. Operating system : Microsoft Windows [version 10.0.19044.3086] Node version : 18.17.1 npm version : 9.6.7 yarn version : 1.22.19 Nest cli version : 10.1.16 I attempted to install by running : npm ...

Removing exclamation points from the routing of a state URL is a key aspect of mastering full stack development

Is there a way to remove exclamation marks from the url in state routing using mean.io? Currently, my url looks like http://localhost:3000/#!/auth/register I just want to get rid of the "!" marks after the "#" symbol. Is it possible? Here is the content ...

Is it true that jQuery and JavaScript operate within separate namespaces?

Within my jQuery code, I am trying to increment a value using the following function: $(document).ready(function(){ var fieldCounter = 0; ... The issue I am facing is that I am unable to access this incremented value from a non-jQuery function. Conver ...

SequelizeIncludeError: unable to fetch data using the 'include' method

My database requests are handled using Sequelize.js, and I have set up a many-to-many relationship between two tables with a third junction table called polit_in_article. Let me walk you through my three tables: politician.js: module.exports = (sequelize ...