Utilizing the raycaster.intersectObject in three.js to target and manipulate the descendants of an Object3

I have been exploring ways to create a series of clickable cubes that can be highlighted, allowing for color changes, texture additions, or other manipulations. After reviewing the source code of various interactive examples on , I noticed that each example uses different methods to create and select objects within the scene. Since I am new to JavaScript, I might be overlooking something simple.

To achieve this, I decided to utilize an Object3D class called "blocks" to store all the cubes:

blocks = new THREE.Object3D()

Using a nested loop, I aimed to generate a 9x9 array of cubes starting at coordinates (0,0,0) with slight spacing between them. These cubes are then added to the "blocks" object and subsequently added to the scene:

function stack(mx, my, mz){
    for (var i = 0; i < 9; i++){
        line(mx, my, mz);
        mz += 3;
    }
}

function line(mx, my, mz){
    for (var i = 0; i < 9; i++){
        var block = new THREE.Mesh(Geometry, Material);
        block.position.x = mx;
        block.position.y = my;
        block.position.z = mz;

        blocks.add(block);
        mx += 3;
    }
}
stack(mx, my, mz);
scene.add(blocks);

When running this code, I successfully render the cubes. However, when utilizing raycasting to interact with the cubes and attempting to select individual objects, I encounter difficulties due to all children having the same ID as the first object created:

var intersects = raycaster.intersectObjects(blocks.children, true);

I have experimented with alternative approaches such as storing objects in regular arrays or dynamically creating unique variables for each element, but these methods still present challenges in selecting and manipulating individual objects.

My main question is: How can I efficiently create multiple Mesh objects in a manner that allows for individual selection and manipulation without treating them as a collective group?

Answer №1

The issue you're encountering may be due to sharing the same Material for multiple instances of your Mesh. When properties of the material are modified for one object, those changes can affect others using the same material in the `blocks.children` collection.

function createLine(x, y, z){
    for (var i = 0; i < 9; i++){
      var material = new THREE.MeshLambertMaterial({color: 0xffffff});
      var block = new THREE.Mesh(Geometry, material);
      block.position.x = x;
      block.position.y = y;
      block.position.z = z;

      blocks.add(block);
      x += 3;
    }
}

This solution should resolve the issue you described.

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

Sync issues observed with jQuery slide animations

I am currently utilizing a carousel slider that includes text content. When the user clicks on the 'next' button, the .carousel-text div slides up to hide the text, the carousel then moves to the next slide, and finally the .carousel-text on the ...

Edit data within an Angular table on-the-fly

Currently, I am in the process of developing a dynamic table that may have an unknown number of columns and rows. My goal is to incorporate inline editing with inline templates using ng-include. The first issue at hand: I am faced with the challenge of u ...

Generating unique spreadsheet identifiers in an HTML file

When it comes to displaying a chart using Google Sheets, I encounter an issue where the data is sourced from the same spreadsheet: function updateChart() { var ui = HtmlService.createHtmlOutputFromFile('ChartLine').setWidth(1350).setHeight(550) ...

Attempting to modify Namecheap's custom DNS field through Python Selenium

I am facing an issue while attempting to modify DNS settings for domains in Namecheap using Python Selenium Below is the HTML code: <select class="dashed-select add-margin ng-untouched ng-valid select2-offscreen ng-dirty ng-valid-parse" ng-change="nam ...

Error message in Linux for NodeJS global NPM package: "File or directory does not exist"

After setting up my Ubuntu 14.04 system, I installed nodejs and npm using the command: sudo apt-get install nodejs npm To ensure packages utilize the node interpreter instead of nodejs, I created a symlink with: sudo ln -s /usr/bin/nodejs /usr/local/bin ...

Express Session doesn't remove the variable assigned

While developing a web application using Node Express, I encountered a simple issue in my new project. Despite setting a session variable to null, the old session data is still being called. Seeking assistance to resolve this issue. I utilized express-ses ...

Issues with AngularJS Nested ng-repeat Functionality in v1.2.1

I'm currently developing a system where I need to nest two ng-repeat statements to iterate through a 2D array. I managed to achieve this successfully using version 1.1.1, as shown here: http://jsfiddle.net/skArT/1/ However, when I tried the same cod ...

Using PHP and jQuery to populate a Bootstrap modal window with data

I am looking for a way to dynamically load data into a bootstrap modal box using JavaScript. Here is the JS code snippet: $(function() { $('.push').click(function() { var id = $(this).attr('id'); $.ajax({ ...

Sending arguments from JavaScript to PHP via ajax

I am facing a challenge where I need to send a JavaScript variable to a PHP function. While I was successful in doing this with hard-coded values, I'm struggling when it comes to using variables. Here's an example of what worked for me: <butt ...

Error due to PlatformLocation's location dependency issue

My AppComponent relies on Location (from angular2/router) as a dependency. Within the AppComponent, I am using Location.path(). However, when running my Jasmine test, I encountered an error. Can you help me identify the issue with my Jasmine test and guide ...

Issue with setting a cookie on a separate domain using Express and React

My backend is hosted on a server, such as backend.vercel.app, and my frontend is on another server, like frontend.vercel.app. When a user makes a request to the /login route, I set the cookie using the following code: const setCookie = (req, res, token) = ...

Exploring FileReader and DOMParser for AngularJS applications

I have a user uploaded file using AngularJS and would like to manipulate the file contents using XML. Unfortunately, I am facing an issue with the DOMParser recognizing the text file. index.html <div ng-controller = "myCtrl"> <input type ...

Difficulty extracting value from Unix bash array when implementing functions

Attempting to retrieve the value from a bash array has been successful with the given code snippet - ### stk3.sh #!bin/bash declare -x -A my_ids my_ids[cat]=100 my_ids[dog]=200 id_key=${1:-cat} ## default setting echo "Id key is :${id_key}" id_ ...

Detecting a click outside of a div or its opening button and closing the element using Vanilla JS

When dealing with two buttons, one opens a form, and the other a dropdown. The goal is to have both elements close when clicking outside. The dropdown functionality already works as expected because it closes whenever the user clicks outside the opening b ...

The failure of invoking a batch file using custom protocol in Windows 10 JavaScript

I recently made a new entry in the Registry (following instructions from this helpful post). The entry can be seen in the snapshot below: https://i.sstatic.net/InZyg.jpg After creating this entry, I used the code snippet below to confirm that the Batch s ...

Activate a function upon the clicking of a button by utilizing a directive in Angular.js

In my directive, there is a function called "myFunction()", and in the template, I have a button. When the button is clicked, I want to execute the function without using ng-click for specific reasons. Instead, I am looking to assign a class to the button ...

Failure to achieve closure - even after thorough study of the concept

I have a countdown that I'm trying to get working during onload, but I'm running into issues with closures. Here is my code: Check out the fiddle here for (var o in myDates) { var myDate = myDates[o]; var iid = o; funcs[o] = function() { ...

Guide on implementing (_ map op) from Z3-LIB in z3py

The Z3-LIB provides support for operators in the extended Array theory like (_ map op). Could you please explain how to utilize this operator in Z3py? ...

Finding and implementing the page URL in JavaScript

Basically, I only want a certain script to be called if the URL is not "blogs.html". Here are some examples where the script should NOT be called: mydomains.com/blogs mydomains.com/blogs.html And here are some examples where the script should be called: ...

Creating an MPG4 Video from a Three.js Scene: What Are the Steps?

I am working with a three.js scene that includes numerous animated objects. I am looking to export this animation as an mpg4 video. Here are my questions: What is the best method for exporting a scene to create an mpg4 video? Is it recommended to perfo ...