What is causing the color to be replaced by the latest selection?

I'm having trouble drawing three lines with different colors. They all end up being the same color, which is the last color specified in my code snippet below:

function initObject() {
        var lineLength = 10;
        geometry = new THREE.Geometry();
        var xMat = new THREE.LineBasicMaterial({color:0xdd5246, opacity:0.2});
        var yMat = new THREE.LineBasicMaterial({color:0xfac942, opacity:0.2});
        var zMat = new THREE.LineBasicMaterial({color:0x149b5a, opacity:0.2});
        geometry.vertices.push(new THREE.Vector3(-lineLength, 0, 0));
        geometry.vertices.push(new THREE.Vector3(lineLength, 0, 0));
        var xLine = new THREE.LineSegments(geometry, xMat);
        
        geometry = new THREE.Geometry(); // Resetting geometry for next line
        geometry.vertices.push(new THREE.Vector3(0, lineLength, 0));
        geometry.vertices.push(new THREE.Vector3(0, -lineLength, 0));
        var yLine = new THREE.LineSegments(geometry, yMat);
        
        geometry = new THREE.Geometry(); // Resetting geometry for next line
        geometry.vertices.push(new THREE.Vector3(0, 0, lineLength));
        geometry.vertices.push(new THREE.Vector3(0, 0, -lineLength));
        var zLine = new THREE.LineSegments(geometry, zMat);

        scene.add(xLine);
        scene.add(yLine);
        scene.add(zLine);
    }

Can anyone help me solve this issue? Thank you so much!

Answer №1

Utilizing the same geometry object, you are incorporating all vertices and binding materials to it. As a result, previous bindings (such as xMat and yMat) are overwritten by the latest one (zMat).

My suggestion is to separate your three lines into distinct geometries.

// X line: 
geometry = new THREE.Geometry();
var xMat = new THREE.LineBasicMaterial({color:0xaabbcc, opacity:0.2});
geometry.vertices.push(new THREE.Vector3(-lineLenth, 0, 0));
geometry.vertices.push(new THREE.Vector3(lineLenth, 0, 0));
var xLine = new THREE.LineSegments(geometry, xMat);
scene.add(xLine);

// Y line:
geometry = new THREE.Geometry();
var yMat = new THREE.LineBasicMaterial({color:0x112233, opacity:0.2});
geometry.vertices.push(new THREE.Vector3(0, lineLenth, 0));
geometry.vertices.push(new THREE.Vector3(0, -lineLenth, 0));
var yLine = new THREE.LineSegments(geometry, yMat);
scene.add(yLine);

// Z line: ... to be continued

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

Alter appearance using various classes

I am seeking assistance in changing the font of text using classes. I have multiple texts with different classes and I want to be able to edit all the texts without adding another dropdown menu. I believe that the change needs to occur in a script. Pleas ...

Local storage synchronization in progress, please hold on

Currently, there seems to be a synchronization issue between the local storage and the server. Countries, cities, and users are synchronized with the server separately through different Ajax calls. The problem at hand is that other JavaScript codes (such ...

Obtaining information from a intricate string input

{JSON.stringify(walletData.transactions, null, 2)} My goal is to extract backend data and display it as a table. The response has been converted into an array as shown below. [ { "date": "Nov 07, 2023", "description" ...

Transferring form data to a PHP script with the help of JavaScript/jQuery

Struggling to figure out how to pass form values to a PHP script using JS. The PHP script is functioning correctly by saving the data into a text file with additional information such as IP and Date/Time. Despite being a simple issue, my lack of JS knowl ...

Transmit responses from PHP script

I am in the process of creating a signup form where, upon clicking the submit button, all form data is sent to a PHP file for validation. My goal is to display an error message next to each input field if there are multiple errors. How can I achieve this ...

The background failed to display (potentially due to a hovering function)

I encountered an issue with a div that has a background image. Upon loading the page, the background image fails to display initially. However, when I hover over the div and then move my mouse elsewhere (due to a specific function described below), the bac ...

What is the best way to handle promises within the context of updating state in React

Recently, I've been working on a React code snippet focused on creating a text input field. onChangeDestination(url, index) { this.setState(prevState => { const rules = [...prevState.rules]; rules[index] = { ...rules[index], url}; ...

Managing control among various child popups

In the situation I am facing, a parent window with login fields is present. In order to manage its controls, I have stored the window handle using: String parentWindow = idriver.getWindowHandle(); After entering the login credentials, a new popup (let&ap ...

Restrict input to only text characters in a textbox using AngularJS

Looking for a way to ensure that users can only input characters into a textbox in my project. Any suggestions on how I can accomplish this? ...

Unit Testing Angular: Mastering Unit Testing for the .map() Function

I am in need of testing a service method, but I am unsure about how to achieve complete coverage for the code that gets executed as a result of calling another injected service method. The Service Method to be tested: @Injectable() export class BomRevisi ...

Having trouble accessing NWJS modules with your new windows?

When I create a window using window.open in my NWJS application, it appears that the window is unable to access any nodejs or nwjs modules. How can I find a solution for this issue? I utilize document.write to add content to the page because the content m ...

Getting dynamic props from a clicked element in React involves accessing the target element's properties and

I am currently working with a React "tree" menu component that has main links with submenus generated dynamically through a JSON GET call. Upon inspecting the tree in the React Inspector, I noticed that each element has multiple props associated with it. H ...

Control Center for JavaScript Administration

When dealing with Javascript content on a larger website, what is the best way to efficiently manage it? I am facing challenges with multiple $(document).ready()'s and the need to handle numerous id strings ($('#id')). One idea was to combin ...

Can Express not use await?

Why am I encountering a SyntaxError that says "await is only valid in async function" even though I am using await inside an async function? (async function(){ 'use strict'; const express = require("express"); const bodyParser = ...

Find the sum and subtotals for two different categories in a JavaScript array

Currently, I'm in the process of aggregating three totals: totalPoints, monthlyTotals, and monthlyByType by utilizing Array.prototype.reduce. So far, I've managed to successfully calculate totalPoints and monthlyTotals, but I'm encountering ...

How to use a filtering select dropdown in React to easily sort and search options?

Recently, I started learning React and created a basic app that utilizes a countries API. The app is able to show all the countries and has a search input for filtering. Now, I want to enhance it by adding a select dropdown menu to filter countries by reg ...

Data structures of advanced complexity within HTML data attributes

I am delighted by the existence of the html5 data attribute. It's great to be able to input a plain string into html attributes and retrieve them using jquery. However, wouldn't it be wonderful to have more than just a basic string? Is there a ...

Navigate to a different directory within Cypress by utilizing the cy.exec() function

Dealing with cypress to execute a python script in another directory. However, it seems like the directory change is not functioning as expected. visitPythonProject() { cy.exec("cd /Users/**/Desktop/project/"); cy.exec("ls"); // thi ...

I encounter difficulty utilizing assets within my React application

Currently, I am in the process of developing a React application for practice purposes. However, I have encountered an issue with using images and audio files stored in the assets folder. Despite my attempts to import them into the project, I have been uns ...

Enhance your slideshows with React-slick: Integrate captivating animations

I recently built a slider using react slick, and now there is a need to adjust the transition and animation of slides when the previous and next buttons are clicked. I received some advice to add a class to the currently active slide while changing slide ...