Modify the color of the object model when clicked - Three.js

When a user clicks on a specific part of the object model, I want to display the wireframe of that part to indicate which part is being modified. The user can then choose a color for that part from a palette. However, the line child.material.color.set(selectedColor) doesn't seem to be working for some reason. Am I overlooking something? Apologies for the extensive code.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8>
    <title>Tacchhi.com Demo</title>
    <link rel="stylesheet" href="css/main.css">
</head>
<body>

    <div className="palette">>
        <span></span>
        <span></span>
        <span></span>
    </div>

    <script src="https://code.jquery.com/jquery-2.2.4.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/1.19.0/TweenMax.min.js"></script>
    <script src="js/Three-r80.js"></script>
    <script src="js/OrbitControls.js"></script>
    <script src="js/OBJLoader.js"></script>
    <script src="js/MTLLoader.js"></script>
    <script src="js/Projector.js"></script>
    <(script&gq
        //GLOBAL VARIABLES
        var renderCanvas, scene, camera, renderer, cameraControl, objModel, raycaster, vector, clickInfo, marker, modelHasLoaded;

        //VIEWPORT SIZE
        var viewportWidth = window.innerWidth;
        var viewportHeight = window.innerHeight;

        function init(){
            //SCENE
            window.scene = new THREE.Scene();

            //CAMERA
            camera = new THREE.PerspectiveCamera(75, viewportWidth/viewportHeight, 1, 1000);
            camera.position.set(0, 20, 160);
            camera.lookAt(scene.position);

            //RENDERER
            renderer = new THREE.WebGLRenderer();
            renderer.setSize(viewportWidth, viewportHeight);
            renderer.setClearColor('#ccc');

            //LIGHTING
            var ambientLight = new THREE.AmbientLight('#000');
            scene.add(ambientLight);
            var pointLight = new THREE.PointLight('#fff', 1, 2000);
            // Other lighting properties

            // Load OBJ Model
            // Set Orbit Controls 
            // Render object when loaded with mouse cursor logic
            // Raycasting and Marker implementation 

            //CONTINUE WITH OTHER FUNCTIONS AND LOGIC AS PER REQUIREMENT
            
            
          });

        </script>
    </body>
    </html>

Answer №1

Just throwing out some brainstormed ideas:

Have you confirmed that selectedColor is a valid hexadecimal value?

Try hardcoding another color using the set method instead of setHex to ensure all objects are properly colored.

After making any color changes, don't forget to call requestAnimationFrame(render); to immediately see the updates in your scene. Otherwise, it may take some time for the changes to appear.

UPDATE:

Here's a bold suggestion... although it might seem drastic, sharing materials is considered a best practice in the world of graphics. By predefining and reusing materials throughout your application, you can optimize memory usage and performance. For example, create two materials like ObjectMaterial and SelectedObjectMaterial. Whenever an object is selected, switch its material to SelectedObjectMaterial. When it's deselected, revert back to ObjectMaterial.

The concept of sharing also extends to meshes and geometries, leading to a more efficient application, improved performance, and cleaner code overall.

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

Challenges encountered when attempting to send an array in res.json with the MERN stack

I am facing a challenge while trying to make two separate model calls using the MERN stack. The issue arises when I try to send an array in res.json; extracting the data seems to be problematic. On examining the console log: {data: "[]", status: ...

How can you trigger a 'hashchange' event regardless of whether the hash is the same or different?

Having a challenge with my event listener setup: window.addEventListener('hashchange', () => setTimeout(() => this.handleHashChange(), 0)); Within the handleHashChange function, I implemented logic for scrolling to an on-page element whil ...

The functionality of wp_script_is in WordPress seems to be malfunctioning when it comes to

I need to load a certain file only after a specific script has finished loading. Wordpress offers a useful method called 'wp_script_is' for detecting if a script has loaded or not. When I use the jquery handle with "done", it functions as expecte ...

Trigger a JavaScript function just before navigating to the next page in the background

I am trying to call a Javascript function from the code-behind in vb.net, but I'm facing an issue where the function is not being executed properly due to redirection to the next page before it runs. I do not want to trigger this function on an Onclic ...

Creating a Recursive Facebook Page Data Scraper using Selenium and Node.js

I am trying to iterate through an array of Facebook page IDs and retrieve the code from each event page. However, I am encountering a problem where I only get the code of the last page ID in the array repeated multiple times. For example, if there are 3 ID ...

Is it possible to incorporate two ng-repeat directives within a single td element in a table?

The results so far Expected outcome Please advise me on how to incorporate two ng-repeats within one td. When I use a span tag afterwards, the expected result is not achieved. I have used one ng-repeat in the td and the other in a span tag, which is why t ...

Guide to iterating through a queue of promises (sequentially handling asynchronous messages)

Currently, I am working on processing a queue of promises (which are representations of messages) in a specific order and using AngularJS for the task. Just to give you an idea, let's say that I have a method called connect() which returns a promise ...

You can update a JavaScript string by adding values using the '+=' operator

I have the following function: function generateJSONstringforuncheckedfilters(){ jsonstring = ''; jsonstring = "["; $('body').on('click', 'input', function(){ jsonstring += "[{'OrderGUID&apo ...

Issue with ng-selected when used alongside ng-options or ng-repeat in Angular

My application features a form where users can fill out their starting point and choose from 350 possible destinations to get directions using Google Maps. Users can select their destination by either clicking on a pin on the map or choosing from a drop-do ...

Attempting to integrate the Angular2 module text-mask-addon into the project

Currently, I am in the process of integrating text-mask-addons into my Angular application. You can find more information at https://github.com/text-mask/text-mask/tree/master/addons Despite attempting to follow the npm links suggestion, I am encountering ...

Retain selected elements and eliminate the rest while maintaining structure with Cheerio/jQuery

I am looking to isolate specific elements while maintaining their structure. For example: <html> <head> <meta xx> </head> <body> <div class="some1"> <div class="some1-1">< ...

Comparison between referencing the DOM and storing references to the DOM elements

Can you explain the contrast between these two statements? $("#txt").val("123"); versus var txt=$("#txt"); txt.val("123"); Which statement is considered more effective in terms of efficiency? ...

jQuery's visibility check function is not functioning properly

I am developing a web application for managing orders and finance in a restaurant. To enhance the functionality of my application, I require more screens to operate with. To facilitate this, I have implemented a small function to toggle between visibility: ...

Transform Promise-based code to use async/await

I'm attempting to rephrase this code using the async \ await syntax: public loadData(id: string): void { this.loadDataAsync() .then((data: any): void => { // Perform actions with data }) .catch((ex): v ...

Stop jquery and/or x-editable from automatically converting strings into objects

I am facing an issue where I want to display a JSON string using x-editable, but it is converting it into an object against my wishes. This results in [object Object] being displayed instead of the actual string. How can I prevent this from happening? var ...

What is the process for forming a series of arrays from one singular array?

If I have a large array consisting of numbers from 1 to 18, is there a simple method to split it into pairs like [1,2], [3,4], [5,6], [7,8], [9,10], [11,12], [13,14] for n=2? The special case of n=2 is all I need. ...

Enhance your Angularfire experience with $firebaseArray by enabling dynamic counting and summing

Is there a way to dynamically count certain nodes if they are defined? The current implementation requires explicitly calling sum(). app.factory("ArrayWithSum", function($firebaseArray) { return $firebaseArray.$extend({ sum: function() { var ...

Clicking an element to uncover more information

Currently, I am working on solving the second question within this series of problems. The task involves creating a functionality where clicking on a legislator's name displays additional information about them. You can view my progress so far by visi ...

How can I accommodate pushState routes from Backbone.js on a node.js express server?

pushState feature was added to Backbone.js in version 0.5. According to the backbone documentation: When using real URLs, your web server must be capable of rendering those pages as well. For example, if you have a route like /documents/100, your web s ...

What is the jQuery Autocomplete syntax like?

Though my experience with jQuery is limited, I have successfully implemented Autocomplete code that highlights the search characters in the dropdown rows: .autocomplete({ delay: 500, minLength: 0, source: function(request, response) { ...