Incorporating geometric shapes into THREE.Object3D

I have made some edits to this post for clarity.

My goal here is to create a tile made up of 8 different triangles, each capable of changing color independently.

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

The issue I am facing is that when I change the color of one triangle, it also changes the color of the lines in between, as shown in the second image.

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

This is the code used to create a tile:

var tile=[];
var n=0;
for(var i=0; i<4; i++){
    for(var j=0; j<2; j++){
        var triangle = new THREE.Object3D();
        var lineMaterial = new THREE.LineBasicMaterial({color:0xffffff, transparent:true, opacity:0.5});
        var triangleMaterial = new THREE.MeshPhongMaterial({color:COLOR_OFF ,emissive:EMISSIVE_OFF ,side:THREE.DoubleSide, shading:THREE.FlatShading});

        var geometry = new THREE.Geometry();
        geometry.vertices.push( triangleVectors[j][0], triangleVectors[j][1], triangleVectors[j][2] );
        var face = new THREE.Face3(0, 1, 2);
        geometry.faces.push(face);

        triangle.add(new THREE.LineSegments(new THREE.Geometry(), lineMaterial));
        triangle.add( new THREE.Mesh( new THREE.Geometry(), triangleMaterial));

        triangle.children[ 0 ].geometry = new THREE.WireframeGeometry(geometry);
        triangle.children[ 1 ].geometry = geometry;

        triangle.rotation.z = Math.PI*i/2;
        triangle.position.x = TILE_LENGTH*x;
        triangle.position.y = TILE_LENGTH*y;

        n++;                        
        tile.push({'triangle':triangle,'number':n,'state':"OFF"});                      
        scene.add(triangle);
     }
}

To update the state of a triangle on the tile, I use the following code:

for(var j=0;j<tile.length; j++){

    tile[j].triangle.children[0].material.color.set(COLOR_OFF);
    tile[j].triangle.children[1].material.color.set(COLOR_OFF);
    tile[j].state="OFF";

    for(var k=0; k<pathUpdates[step].length; k++){
        if(pathUpdates[step][k].number == tile[j].number){
           tile[j].triangle.children[0].material.color.set(COLOR_ON);               
           tile[j].triangle.children[1].material.color.set(COLOR_ON);
           floor[i].tile[j].state="ON";
        }
    }
}

Is this the correct way to modify a material?

I have simplified the code slightly to illustrate my issue better. The full code can be found in this repository https://github.com/tul1/Tile.git. If you'd like to see it in action, I have deployed it at .

Answer №1

My current issue revolves around the fact that altering the color of a single triangle also changes the color of the connecting lines, as depicted in the second image.

After examining your source code, it seems that if you wish to maintain the white color of the lines while only adjusting the triangles' colors, you can simply eliminate the line responsible for modifying the color of lineMaterial:

for(var j=0;j<tile.length; j++){
    // The line below will alter all line colors to 0x156289:
    // tile[j].triangle.children[0].material.color.set(COLOR_OFF);

    tile[j].triangle.children[1].material.color.set(COLOR_OFF);
    tile[j].state="OFF";

    for(var k=0; k<pathUpdates[step].length; k++){
        if(pathUpdates[step][k].number == tile[j].number){
           // The line below will change the active line color to 0x891528
           // tile[j].triangle.children[0].material.color.set(COLOR_ON); 

            tile[j].triangle.children[1].material.color.set(COLOR_ON);
            floor[i].tile[j].state="ON";
        }
    }
}

Evaluation

To maintain the original white color on the lines, refrain from altering their color during modification: https://i.sstatic.net/ASlyT.jpg

Grouping

Should you desire grouping all tiles into one entity, consider introducing a THREE.Group object within your global scope (outside of any for() loops). Subsequently, add each triangle individually during every iteration of the for() loop. Here's a pseudocode example illustrating this process:

// Establish tileGroup outside the loop
var tileGroup = new THREE.Group();

for(var x=0; x<nX; x++){
    for(var y=0; y<nY; y++){
        var tile=[];
        var n=0;
        for(var i=0; i<4; i++){
            for(var j=0; j<2; j++){
                // All existing triangle-creation code belongs here

                // ...

                // Once triangle is formed, include it into the group
                tileGroup.add(triangle);
            }
        }
    }
}

// At last, append tileGroup to scene
scene.add(tileGroup);

// Consequently, manage all tiles simultaneously inside the group
tileGroup.position.set(0, 15, 0);

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

"Adjust the placement of a personalized marker on a Google Map using google-map-react

I have incorporated google-map-react into my React project and I have designed custom markers. The markers are displayed at the correct location but from the top left corner: https://i.sstatic.net/e6lGN.png The red dot indicates the exact location. Howe ...

Searching by object id from the front-end: A step-by-step guide

Within my collection of meals, there is a specific document I am working with: # Meals collection { name: "burger", tag: ObjectId(63d6bb22972f5f2fa97f1506), } Now, as I navigate through my React application, I find myself needing to send a ...

How can I make the menu item become highlighted as I scroll through the page

I'm looking to enhance the menu on my single-page website so that the active state changes as you scroll through different sections. Can anyone provide a solution based on the demo code I've shared? Thanks for your help! Check out the demo here. ...

How can I enhance the functionality of AJAX in Symfony 1.4 when using jQuery, specifically in cases where a select box's options depend on the selection of another select box?

In my Symfony 1.4 application, I have three interconnected tables outlined below. Table 1: Conflictos1: connection: doctrine tableName: conflictos_1 columns: id: type: integer(4) fixed: false unsigned: false primary: tru ...

Validating passwords using Vuetify

I am implementing password validation on my form using Vuetify validation rules: passwordRules: [ v => (v && v.length >= 1 && v.length <= 10) || 'Password length should be between 1 and 10', v => (v && v.l ...

Tips on retrieving stored data from asynchronous d3 calls within the document.ready event and passing it to another function

Attempting to save data from an asynchronous call to a variable or object outside of the function has been a challenge. Several methods have been explored to make calls synchronous, such as using callbacks, but none seem to be effective for my particular ...

Simultaneous appearance of multiple jQuery dialogs

As I work on creating a team page, each team member has their own div that contains a photo and employee information. My goal is to have a dialog box pop up when the photo is clicked, using the $this context to retrieve the employee data from the same div. ...

Struggling to conceal the code with a Jasmine Spy

I have been creating test cases to ensure code coverage using TypeScript. Despite using Jasmine's spyOn, I have encountered an issue where some methods are not covered, although no errors are being reported. Your guidance on resolving this matter woul ...

Is it possible to retrieve a list of controllers that are connected to an object in Three.js?

Is there a method in Three.js to retrieve the list of controllers linked to an object? Or perhaps just one controller linked to that object. I have connected a controller to an object, but I do not want the transformController to always be visible on it. E ...

Steps for passing $scope to an AngularJS 1.5 component or directive

Exploring the utilization of the new .component() method in angular 1.5 has been a challenge. There is limited information available on its usage, and even the angular documentation does not provide clear guidance. Attempting to pass the scope or an objec ...

Tips for adjusting the font size according to the varying number of characters entered

I am currently facing a challenge with adjusting the font size based on the dynamic number of characters. For example, I have a set of characters with a font-size of 40px, and as more characters are added, the font size should decrease to fit within the av ...

Ways to dynamically update the content of an HTML table without the need to reload the page

I'm currently working on an HTML table that retrieves data from a database and includes a button for deleting selected records. Here is what the table layout looks like: name phone links john 6562 link1 link2 link3 ___________ ...

Tips for sending information to a JavaScript variable through AJAX requests

Hello there, I'm currently working on a project that involves posting data stored in a JavaScript variable using AJAX. Can anyone assist me with the correct syntax for this process? <div class="container-fluid"> <div class="card shadow m ...

The HTML document is unable to locate the Angular framework

I'm encountering an issue with the code below on my HTML page - every time I run it, I receive an error message saying "angular is not defined". I've already included angular in the head section of the page so I'm puzzled as to why it's ...

Ways to make all elements appear darker with the exception of one specific element

I am currently developing a Google Chrome extension and working on the right side, specifically in my friend's stories section. I am looking for a particular friend's story by searching for their name entered in the input field after clicking the ...

Watchman encountered an error upon initialization

Hi there, I'm encountering an error when trying to start my app with 'react-native start'. Can anyone provide guidance on how to resolve this issue? I attempted changing the permissions of the watchman folder and project folder using chmod - ...

Learn how to successfully place a draggable object into a sortable container while ensuring that the dropped item is a personalized helper element rather than the original object

The jQuery draggable/sortable demo showcases the process of dropping a clone of the draggable item (both draggable and sortable elements share the same structure). However, I am interested in dropping a different DOM structure. For example, when dragging a ...

Chargebee encountered an error of type TypeError when attempting to create a subscription. The action

Every time I attempt to send a request with Chargebee, an error appears. Error: Failed to create subscription async createSubscriptionForTeamMember(customerId, options) { try { // Proceed with subscription creation using Chargebee API ...

Selenium's `getEval` function is unexpectedly returning null, despite the fact that the corresponding JavaScript

While using Selenium, I am trying to execute a getEval function with the following javascript: document.getElementById("j_id0:j_id3:mainBlock:j_id40").children[0].children[0].children[0].children[0].children[0].children[0] .children[2].children[0].childre ...

Using a $watch on a directive that has an isolated scope to monitor changes in a nested object property retrieved from

I have developed a custom directive with an isolated scope and element. I am utilizing the values passed into the directive to construct d3/dc charts. The data goes through crossfilter on the $scope so that the directive attributes can access it. Despite s ...