Enhancing textures in three.js

I am currently working on a project using three.js where I am attempting to change the color texture by clicking on an image.

Below is the code I have been using:

...

var col;

document.getElementById('image3').onclick = function() { 
col=("textures/synleder/synleder_COL.png");
};


var textures = {
            color: THREE.ImageUtils.loadTexture(col),
            normal: THREE.ImageUtils.loadTexture('textures/synleder/synleder_NRM.jpg'),
            specular: THREE.ImageUtils.loadTexture('textures/synleder/synleder_SPEC.jpg'),
    };


textures.color.wrapS = textures.color.wrapT = THREE.RepeatWrapping;
textures.color.repeat.set( 2, 2);
textures.normal.wrapS = textures.normal.wrapT = THREE.RepeatWrapping;
textures.normal.repeat.set( 2, 2);
textures.specular.wrapS = textures.specular.wrapT = THREE.RepeatWrapping;
textures.specular.repeat.set( 2, 2);


            var shader = THREE.ShaderLib[ "normalmap" ];
            var uniforms = THREE.UniformsUtils.clone( shader.uniforms );

            uniforms[ "tNormal" ].value = textures.normal;
            uniforms[ "uNormalScale" ].value.y = 2;


        var material2 = new THREE.MeshPhongMaterial( {
                map: textures.color,
                specularMap: textures.specular,
                normalMap: uniforms[ "tNormal" ].value,
                normalScale: uniforms[ "uNormalScale" ].value,

            } );


            loader = new THREE.JSONLoader( true );
            document.body.appendChild( loader.statusDomElement );

            loader.load( "geo/kugel5.js", function( geometry ) { createScene( geometry, scale,  material2 ) } );


...

So far, I have tried defining a variable col to store the path of my color textures. When clicking on image3, I expected the new color texture to be applied to my geometry. However, this functionality is not working as expected. During my research, I came across this thread: Three.js texture / image update at runtime

I believe I need to add something to update the texture: textures.color.needsUpdate=true;

When I attempted to add this to my code like so:

 document.getElementById('image3').onclick = function() { 
    col=("textures/synleder/synleder_COL.png");
    textures.color.needsUpdate=true;
    };

my geometry disappears. Can someone please advise on what I might be doing wrong?

Thank you in advance!

Answer №1

document.getElementById('photo3').addEventListener('click', function() {
  const texture = new THREE.TextureLoader().load("textures/paintings/painting_col.png", function() {
    material.color = texture;
    texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
    texture.repeat.set(3, 3);
    texture.needsUpdate = true;
  });
});

This should work perfectly

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

Ways to emphasize search outcomes in Flask/HTML?

I am currently working on creating a search box using HTML and the Flask framework in Python. Below is the layout I am working with: Layout My goal is to input text into the search box and have it highlighted within the text area on the same HTML page. F ...

The utilization of ui-view resulted in an error message stating "Failed to instantiate the ui.router

Recently, I've been diving into Angularjs and attempting to organize this plunker by splitting it across different files using ui-view. Let's take a look at my app.js 'use strict'; angular.module('Main', []); angular.modul ...

Having trouble rendering the response text from the API fetched in Next.js on a webpage

I've written some code to back up a session border controller (SBC) and it seems to be working well based on the output from console.log. The issue I'm facing is that the response comes in as a text/ini file object and I'm unable to display ...

What is the reason behind the Placeholder not functioning in IE8?

Whenever I trigger the onblur event on an input of type "password", it will hide both the placeholder text and the input itself. Check out the GitHub link for this plugin ...

Include a photo in the notification when utilizing the sendToTopic function

I am looking to utilize the sendToTopic method for sending notifications to a topic. Is there a way to include an image in the notification? It seems that notification.imageUrl is not available as an option. ...

Error in Typescript: The type 'Element' does not have a property named 'contains'

Hey there, I'm currently listening for a focus event on an HTML dialog and attempting to validate if the currently focused element is part of my "dialog" class. Check out the code snippet below: $(document).ready(() => { document.addEventListe ...

Unleash the full potential of React and material-ui with the Raised Button feature - find out how to effortlessly keep all

This snippet showcases the code for my custom Buttons component: import React from 'react'; import RaisedButton from 'material-ui/RaisedButton'; const style = { button: { margin: 2, padding: 0, minWidth: 1, }, }; cons ...

blurring out of an input field and a division element

I am currently working on creating a dropdown suggestion box that hides when the input field and dropdown box are no longer in focus. My HTML code: <input type="text" id="user_address"> <div id="user_address_sg">SUGGESTION</div> <di ...

The data retrieved from the API call is outdated

I am struggling with a weather web API that is only showing old data when called in the code. However, when I enter the API URL directly into the browser, it displays the most up-to-date information for the current city. Can anyone help me troubleshoot why ...

How can I center the window vertically based on the middle of an element?

Hey guys, I need some help with my code. I've been trying to create a button that will shift the window's Y position to center the "Box - 5" div vertically through an onclick event. Basically, I want to make sure the "Box - 5" div is positioned i ...

What is the method of updating content on a webpage without altering the page itself or using hashed URLs?

I have a good understanding of how to update webpage content through AJAX and loading remote content. However, I recently came across UStream's innovative new layout. When you click on any video, not only does the content change seamlessly without rel ...

The Three.js obj/mtl model unexpectedly appears in a dark hue

My three.js application was functioning perfectly, but today when I opened it, the obj/mtl model appeared completely black. It used to look like this: https://i.sstatic.net/hIDwR.png Now, without any code changes, it looks like this: https://i.sstatic.ne ...

Creating interactive dropdown menus with PHP and Catalyst using Jquery

Currently, I am working on incorporating cascading dropdown menus into a catalyst web app. The main goal is to allow users to select a database table from the first dropdown menu and have the columns of that table populate the second dropdown menu. To achi ...

"Mastering the art of traversing through request.body and making necessary updates on an object

As I was reviewing a MERN tutorial, specifically focusing on the "update" route, I came across some interesting code snippets. todoRoutes.route('/update/:id').post(function(req, res) { Todo.findById(req.params.id, function(err, todo) { ...

Utilizing class-validator for conditional validation failure

Implementing conditional validation in the class-validator library using the given example, I need to ensure that validation fails if the woodScrews property is assigned a value when the tool property is set to Tool.TapeMeasure. I've searched extensiv ...

A guide to correctly importing a Json File into Three.js

I've been working on some cool projects in Blender and wanted to showcase one using threejs. However, I'm facing an issue where the object isn't displaying properly. Can someone guide me on how to correctly load a JSON file with keyframe ani ...

Saving an incremented number using Vue JS in Firebase database

I have a VueJS app integrated with Firebase JSON database where I'm trying to implement a feature that allows users to update the vote count of a comment by clicking an arrow (similar to upvotes on this website). The function works properly, but the V ...

Creating interactive panorama hotspots with THREE.js SphereGeometry and DOMElements

After creating a WebGL 3D Panorama application using a SphereGeometry, PerspectiveCamera, and CanvasTexture, I am now trying to enhance the scene by adding "HotSpots" over specific areas of the SphereGeometry. However, I am facing difficulty in updating th ...

Storing information in Firebase using React.js

When storing an object in Firebase, I expected the structure to be as shown in the image below. However, what I received was a generated running number as a key. This is the code I used to store the object in Firebase: var location = []; location.push({ ...

Is there a way to retrieve data from a sealed JSON object using JavaScript?

The data is being fetched from the API and here is the response object: { "abc": [{ "xyz": "INFO 1", "pqr": "INFO 2" }, { "xyz": "INFO 3", "pqr": "INFO 4" } ] } We are lookin ...