Problem with UV mapping when adjusting texture size

I am currently working on an application to modify minecraft models. To display the drawn texture mapped on the 3D player model, I am using ThreeJS. However, I'm facing a challenge related to changing the texture size.

Initially, the texture is mapped on the model like this. When I change the texture height to 64, it transforms into this.

The texture creation process looks like this:

/* Texture */
var texture = new THREE.Texture(canvas);

texture.minFilter = texture.magFilter = THREE.NearestFilter;

Here, the variable canvas refers to HTMLCanvasElement. Whenever the content of the canvas changes, I refresh the texture using the following steps:

this.skin.render(canvas.getContext("2d"), this.skin.w, this.skin.h);

this.limbs.forEach(function (limb) {
    limb.mapUV(self.skin.w, self.skin.h);
});

this.material.map.needsUpdate = true;
this.material.needsUpdate = true;

In this code snippet, this.material represents a double sided transparent material with an attached texture. The UV's appear to be correct.

In a previous application, I solved a similar issue by recreating the entire mesh. However, in this scenario, that approach seems costly. How can I resolve this issue? It appears as though the texture might be cached or something similar. If my assumption is correct, how can I clear the cache?

Thank you for your attention!

P.S.: Apologies for not being able to share images due to restrictions on my reputation.

Answer №1

After spending numerous hours fine-tuning my code, I was able to achieve the desired outcome and successfully resolve my issue. It turns out that the elements of the UV array in geometry.faceVertexUvs were being cached within ThreeJS core.

In my initial UV mapping code, I was directly assigning the array of Vector2 to geometry.faceVertexUvs:

geo.faceVertexUvs[0][side] = [faces[1], faces[2], faces[0]];

While this worked the first time, subsequent attempts retrieved the cached values, despite marking UVs as dirty (geo.uvsNeedUpdate = true). So, instead of assigning the array, I opted to copy each Vector2 from the faces array to every triangle vertex in geo.faceVertexUvs[0][side]:

geo.faceVertexUvs[0][side][0].copy(faces[1]);
geo.faceVertexUvs[0][side][1].copy(faces[2]);
geo.faceVertexUvs[0][side][2].copy(faces[0]);

I believe proper UV mapping techniques like this should be included in the official documentation of ThreeJS for better clarity.

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

Can you guide me on implementing AWS SDK interfaces in TypeScript?

Attempting to create an SES TypeScript client using AWS definitions file downloaded from this link My approach so far: /// <reference path="../typings/aws-sdk.d.ts" /> var AWS = require('aws-sdk'); var ses:SES = new AWS.SES(); The error ...

Tips for successfully parsing JSON data during an Ajax call

After making an Ajax call, the response.responseText I receive looks like this: . "[ columns :[ { "id":"name", "header":"User name" }, { "id":"birth", "header":"Date of birth" } ], ...

The function window.location.reload(true) is failing to properly refresh the page

Currently, I have a PHP page that retrieves data from a MYSQL database and creates HTML content based on the returned rows. I recently implemented a feature where clicking a button adds a new row to the database using AJAX, jQuery, and PHP. However, after ...

Learn the ins and outs of utilizing *ngIf in index.html within Angular 8

Can anyone explain how I can implement the *ngIf condition in index.html for Angular2+? I need to dynamically load tags based on a condition using the *ngIf directive, and I'm trying to retrieve the value from local storage. Below is my code snippet b ...

The useParams() method results in a null value

When attempting to utilize the useParams() hook in nextjs, I am encountering an issue where it returns null despite following the documentation. Here is my current directory structure: pages ├── [gameCode] │ └── index.tsx Within index.tsx ...

Displaying an image in AngularJS using a byte array received in the response

Dealing with a service that adds properties to a file and returns it as a byte array in the response. I'm struggling to display it properly since it's in byte form. Attempted converting it to base64 but still showing raw bytes. PNG IHDR&L ...

What is the method for identifying the name of a CSS Variable currently in use?

Consider this code snippet: /* css */ :root { --text-color: #666666; } input { color: var(--text-color); } In Javascript, how can I determine the name of the CSS custom property (variable) being utilized? // javascript console.log(document.querySel ...

A versatile function that displays a loading icon on top of a specified div

Is it possible to pass an identifier for a particular div element to a function, where the function will display a loading image that covers the entire div, and then pass the same identifier to another function to hide the loading image? I am looking to cr ...

Sending a message to an iframe from a different origin using JavaScript

Just starting out with JavaScript and I'm attempting to send a message to my iframe in order to scroll it. Here is the code I am using: scroll(i) { var src = $("#iframe").attr("src"); $("#iframe").contentWindow.postMe ...

Tips for effectively grouping a JavaScript object array with the help of Lodash

I have an array of objects in JavaScript that looks like this: [{ day : "August 30th", id: 1, message : "lorem ipsum1" },{ day : "August 30th", id: 2, message : "lorem ipsum2" },{ day : "August 31th", id: 3, message : " ...

Prevent redundant Webpack chunk creation

I am currently working on integrating a new feature into my Webpack project, and I have encountered a specific issue. Within the project, there are two entry points identified as about and feedback. The about entry point imports feedback, causing both abo ...

Using TypeScript to define callback functions within the Cordova.exec method

I'm encountering an issue with the TypeScript definition for Cordova. The codrova.d.ts file doesn't allow for any function arguments in the success-callback and error-callback. To better illustrate my problem, here's a small example: Here ...

software tool for managing state transitions

Can anyone recommend a superior javascript library for workflows? Right now, I'm utilizing Joint JS, but I require something with a more visually appealing interface (users tend to prefer that). ...

analyzing strings by comparing their characters to another string

In a scenario where I have two strings; let's call them string a="a-b-c" and string b="a-b". I am looking to determine whether every letter in string b is present within string a. ...

Can I receive a PHP echo/response in Ajax without using the post method in Ajax?

Is it feasible to use Ajax for posting a form containing text and images through an HTML form, and receiving the response back via Ajax? Steps 1.) Include the form in HTML with a submit button. 2.) Submit the form to PHP, where it will process and upload ...

Present the value of an object within an array in an HTML format

I have organized an array containing information about different video games: let games = [{ title: 'Fortnite', price: 20, img: "./assets/images/Fortnite.jpg" }, { title: 'Valorant', price: 0, img: "./asse ...

Avoid losing any entered form information when leaving the page

As I work on creating a datagrid with hundreds of rows, each row features a checkbox that allows users to select items from the grid. I've noticed that users might spend considerable time filtering and searching through the grid, ticking checkboxes a ...

Tips for serializing form inputs within a .change event using javascript, PHP, and CURL

I have a small project underway that involves using Ajax to retrieve data from a database and update a page. The user is able to build a dynamic query, creating a chain of query strings where each item in the chain affects the next one. This results in a l ...

Using npm as a build tool to merge files together

Upon discovering the flexibility of using npm as a task runner instead of gulp or grunt, I have successfully implemented various tasks such as linting, stylus processing, jade compilation, uglifying, and even watching files. However, I am facing difficulti ...

mention the element to display on the pagination

I find the logic here quite puzzling. This particular code block seems to be related to pagination, as it involves a function that is triggered upon clicking. componentDidUpdate() { const { location } = this.context; const { query } = this; if ...