What is the process for updating data for THREE materials?

Is it possible to update the material of an object based on a radio button selection? I have managed to change the color in my current example, but the entire material does not get updated. How can I instruct THREE to adjust its internal data accordingly?

Materials

  var lederfaserMap = THREE.ImageUtils.loadTexture( 'textures/lederfaser.jpg' );
  lederfaserMap.anisotropy = 16;
  lederfaserMap.wrapS = lederfaserMap.wrapT = THREE.RepeatWrapping;
  lederfaserMap.repeat.set( 5, 5 );


  var feinleinenMap = THREE.ImageUtils.loadTexture( 'textures/feinleinen.jpg' );
  feinleinenMap.anisotropy = 16;
  feinleinenMap.wrapS = feinleinenMap.wrapT = THREE.RepeatWrapping;
  feinleinenMap.repeat.set( 8, 8 );


  var material = {     
  "feinleinen-schwarz":     new THREE.MeshPhongMaterial( { color: 0x222222, map: feinleinenMap, combine: THREE.MixOperation} ),
  "feinleinen-weiss":   new THREE.MeshPhongMaterial( { color: 0xffffff, map: feinleinenMap, combine: THREE.MixOperation } ),
  "lederfaser-schwarz":     new THREE.MeshPhongMaterial( {color: 0x222222, map: lederfaserMap, combine: THREE.MixOperation } ),
  }

Update the Material here

  var group = new THREE.Group();

loader.load('/models/object.js', function(geometry){
  var materialchange= new THREE.Mesh(geometry, material[ "feinleinen-schwarz" ]);
  materialchange.name = "materialchange";
  group.add( materialchange);
});  

  scene.add( group );



function animate() {

  requestAnimationFrame(animate);
  TWEEN.update();
  renderer.render(scene, camera);

}

Function to adjust and apply the material

function colorTo (target, value){
      var target = scene.getObjectByName(target);
      var initial = new THREE.Color(target.material.color.getHex());
      var value = new THREE.Color(value.color.getHex());
      var tween = new TWEEN.Tween(initial).to(value, 500)
          .easing(TWEEN.Easing.Cubic.InOut)
          .onUpdate(function(){
              target.material.color = initial;
          })
          .start();
  }  

Function to retrieve the attribute of the selected radio button

$("input[name=material]").change(function(event){
    if ($(this.checked)) {
        var targetColor = $(this).data("visual");
          colorTo("materialchange", material[targetColor]);
      }
  });

Answer №1

To update the color of all components within the object, you should iterate through each part and assign the new color:

object.iterate ( function (element) {

    if (element instanceof THREE.Mesh)
        element.material.color.new = updated_color;

}

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

Choosing the Angular5 model

I'm currently working with an Angular5 project that has a <select> element bound to an array of customers. Here's the code snippet: <select class="form-control" [ngModel]="record.customer_id" (ngModelChange)="setCustomer($event)" name=" ...

Eliminating an SVG component from the webpage with the help of jQuery

Utilizing jQuery to insert an element into an embedded SVG can be achieved with the following code: var rect = SVG('rect'); $(rect).attr( { x: left, y: top, width: right - lef ...

Is it possible to merge several blobs into one zip file using JSzip?

When attempting to download multiple images as a single ZIP file, the result is receiving separate zip files instead. The console log shows that the 'urls[]' array contains different arrays. const fileURLs = window.URL.createObjectURL(result);// ...

The hyperlink tag within the overlay div is unresponsive

I'm facing an issue with my overlay div (rb-overlay) that pops up when users click on a page option. The overlay takes up the entire page and includes a close button in the top right corner. However, I've noticed that the link at the end of the t ...

What are the steps to prevent Phonegap InAppBrowser from caching the URL content?

I have implemented the InAppBrowser plugin to display a web page: window.open(encodeURI('http://app.website.com'), '_self', 'location=no,toolbar=no,hidden=yes'); After changing the content of the URL, I noticed that when I r ...

Mapping textures from a single Face4 to two separate Face3 in Three.js

Currently, I am experimenting with Three.js on a project that relies on an older version of the library. As part of my work, I am in the process of upgrading to the newest version (r71) of Three.js. While most aspects of the update are progressing smoothly ...

Achieve the hidden div scroll effect in React by initially hiding the div and then allowing

I am currently working on a web chat application using next.js. One of the features is an emoji picker button that, when clicked, displays a menu of emojis. The issue I am facing is that in order for the user to see the emoji menu, they have to scroll down ...

The challenge of harmonizing variables in PHP and JavaScript

I have a PHP script that generates an HTML table displaying data from a MySQL database. <?php include 'connexion.php'; session_start(); $idfirm = $_REQUEST['idfirm']; $namefirm = $_REQUEST['namefirm']; The table rows are ...

Verify whether an item exists within a group of objects, and if so, eliminate it from the group; otherwise, include it in the group

I am working on a function to create an array of objects where only specific attributes are kept from the original object. The goal is to check if the selected object is already in the array - if it is, remove it; if not, add it. However, I'm facing ...

Unexpected character error occurs when using VSCode with Vue: ''‌'

Recently, I've encountered some strange errors while working on a Vuejs project in VSCode. Whenever I try to write arrow functions or use brackets, I get unexpected character errors. For example, if I insert an empty computed section between methods a ...

VueJS - Iterating over a list within a vue component causes the list to be empty

Having encountered an issue with the answers provided to my question from yesterday, I have decided to create a new query with additional details. To review the original question, please visit: VueJS - using mustache template strings inside href attribute ...

Incorporating Earth Engine scripts into my AngularJS project to showcase NDVI data layer on a Google Map

Is there anyone who has successfully integrated the Earth Engine API into their front-end JavaScript code? I've been attempting to follow the demo on the earth-engine repository to add a layer to a map, but I haven't had any success. It seems lik ...

Is sending cookies the sole method to convey a directive from a server to a client for the execution of JavaScript code?

Simply put: How can a server send a message to a client to run JavaScript code on a page? It's not an Ajax request, but a basic GET browser-server request. Imagine this code in my app's front-end JavaScript file: if( condition_1 ) { FB.get ...

Thee.JSONLoader object as a publicly accessible variable

Is it possible to declare an object loaded via THREE.JSONLoader as a public variable in JavaScript? I come from a strong 3D background but am new to JS and WebGL. I would like all my loaded textures, material creation, and geometry to be easily manipulab ...

Positioning the close button on the top right edge of a Material-UI Dialog: A step-by-step guide

https://i.sstatic.net/ARTtq.png How can I include a close icon in the top right corner of the header section? I'm using the Material UI Dialog and everything works well, but I need a close button in the top section. Can anyone assist me with this? ...

Is it possible to simultaneously employ two asynchronous functions while handling two separate .json files?

Is it possible to work with 2 .json files simultaneously? My attempt did not succeed, so I am looking for an alternative method. If you have a suggestion or know the correct syntax to make this work, please share. And most importantly, does the second .j ...

Unlocking the potential of # to craft interactive web pages

Lately, I've been seeing more and more websites like Twitter that have integrated AJAX into their layouts. One thing that has really piqued my interest is the inclusion of #! in URLs. I'm curious about how I can implement this on my own site or w ...

Issue with three.js memory leak due to malfunctioning .dispose() method or incorrect usage

Greetings! I'm currently facing a memory handling issue. Despite researching various forums, I am still unable to identify the problem in my code. My project involves combining d3.js with three.js to create a visualization of nodes resembling planet ...

Refreshing the dropdown selection to a specific option using AngularJS and either JavaScript or jQuery

Currently, I am facing an issue with resetting the select tag to its first option. I have implemented Materialize CSS for styling purposes. Despite my efforts, the code snippet below is not achieving the desired outcome. Here is the JavaScript within an ...

JavaScript Equivalent of Declaration in TypeScript

In my Next JS application, I encountered a situation where a line of code relies on a variable from a script src in the app.tsx page. Here's how it looks: app.tsx: <script src="https://js.stripe.com/v3/"></script> config.ts: de ...