Enhancing Textures in Three.js: Methods for Gradually Improving Resolution

I am currently working on a Three.js chart that displays multiple images on a 2D plane.

At the moment, each individual image is a 32px by 32px segment of larger 2048px by 2048px image atlas files. I intend to enlarge these individual images when users zoom into specific regions of the scene. For instance, if users zoom in on the images in the far right region of the space, I aim to replace the 32px by 32px individual images in that area with 64px by 64px images containing more details.

My question is: How can I achieve this goal using Three.js?

My initial idea was to load the higher-resolution assets, map them to the correct geometry coordinates, then remove the old mesh with 32px subimages and add the new mesh with 64px subimages. Initially, I thought about updating the texture/material for an existing geometry, but I learned that textures larger than 2048px by 2048px should not be used, and increasing the fidelity of the images within a geometry with n points could exceed that maximum texture size.

I would greatly appreciate any guidance from experienced Three.js developers on how they would tackle this task!

Full code:

/**
* Globals
**/

// Identify data endpoint
var dataUrl = 'https://s3.amazonaws.com/duhaime/blog/tsne-webgl/data/';

// Create global stores for image and atlas sizes
var image, atlas;

// Create a store for image position information
var imagePositions = null;

// ... (The rest of the code continues)
* {
  margin: 0;
  padding: 0;
  background: #000;
  color: #fff;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/88/three.min.js"></script>
<script src="https://s3.amazonaws.com/duhaime/blog/tsne-webgl/assets/js/texture-loader.js"></script>
<script src="https://s3.amazonaws.com/duhaime/blog/tsne-webgl/assets/js/trackball-controls.js"></script>
<div id='loader'>0%</div>

Answer №1

If you're looking to work with various materials and geometry groups (or material indices in your case), there are some possibilities.

It all hinges on ensuring that your texture dimensions scale correctly at a 1:1 ratio. For example, if your initial resolution has dimensions of 32x64, then doubling that resolution should result in dimensions of 64x128. UVs operate based on percentages, meaning transitioning from one image resolution to another of the same image should seamlessly "just work".

The pathway forward primarily involves changing the source of the texture image. However, it seems you prefer not to go down that route. Instead, you can assign all textures to the same Mesh simultaneously using Three.js in an efficient manner...

var myMesh = new THREE.Mesh(myGeometry, [ material1, material2, material3 ]);

Note how the material parameters are declared as an array. Each material corresponds to a different texture, representing the varied resolution images you have.

Now, delve into your Mesh. Within the geometry property, you'll encounter an element called faces, which comprises an array of Face3 objects. Every face possesses a property known as materialIndex, indicating its linkage to the material array.

When the moment arrives for triggering a change (like when your camera hits a certain distance from a mesh), altering the material index prompts the mesh to update its material accordingly:

var distance = camera.position.distanceTo(myMesh.position);
if(distance < 50){
  myMesh.faces.forEach(function(face){
    face.materialIndex = 2;
  });
}
else if(distance => 50 && distance < 100){
  myMesh.faces.forEach(function(face){
    face.materialIndex = 1;
  });
}
else{
  myMesh.faces.forEach(function(face){
    face.materialIndex = 0;
  });
}
myMesh.groupsNeedUpdate = true;

The final line (myMesh.groupsNeedUpdate = true;) informs the renderer about the altered material indices, prompting it to perform necessary updates for rendering.

Answer №2

If you're looking to optimize performance in your Three.js project, consider utilizing THREE.LOD. This tool allows you to specify different meshes based on distance, providing the opportunity to adjust textures and materials as needed. Check out this example of LOD in action on the official THREE.js website: LOD Example.

I hope this information proves useful for your project!

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

What is the best way to send a JSON response from a Symfony2 controller?

I am utilizing jQuery to modify my form, which is created using Symfony. The form is displayed within a jQuery dialog and then submitted. Data is successfully stored in the database. However, I am unsure if I need to send some JSON back to jQuery. I am ...

Retrieve the data value from a click event using the class identifier

The following code will display the correct class name, but not the correct data-validamount. It will only show: undefined. $(".prod_amount_add").click(function(){ var rowAmount = 0; var datavalid = $(this).attr('data-validamount'); ...

Send data to a JavaScript file

Even though this particular question has been asked and answered multiple times, I am still struggling to find a solution to my problem. I am currently working with Masterpages and JavaScript files, and as many people are aware, Masterpages can be quite t ...

I came across a forum where someone mentioned encountering a similar issue but unfortunately, no solution was provided. Currently, I am working on setting up a reaction roles system but despite embedding the message, the role is not being

I am currently working on setting up a reaction roles system for my Discord server, but I have encountered a significant issue that may seem minor to most. The problem is that although my bot successfully sends embeds with corresponding emojis for the ro ...

Facing Issues with Angular 10 Routing on an HTTP Server Deployment?

After successfully running my Angular ver-10 Ecommerce Project locally with "ng serve", I encountered an issue when trying to publish it using "ng-build" and hosting it with "http-server". The problem arises when navigating from the Home Screen (e.g. Dashb ...

Expanding a div's size with CSS to fill the remaining space

Here's the setup I'm working with: <div class="parent"> <div class="moverBoy"></div> <div class="smartBoy"></div> </div> The parent element is fixed at 100x20 indefinitely. moverBoy and smartBoy are al ...

Guide on using POST method in jQuery version 1.11.4 for tab functionality

I'm currently in the process of upgrading from jquery ui version 1.9.2 to jquery ui version 1.11.4 and I've encountered a situation where ajaxOptions has been removed from the tabs control. Originally, I was using the following code: $("#tabs"). ...

What is the process for retrieving DOM elements and JavaScript variables using PHP?

I am currently developing a PHP script that will dynamically generate tables in MySQL based on the user's input for the number of columns and column names. However, I have encountered some challenges when trying to access DOM elements and JavaScript v ...

What is the best way to maintain the position of components (such as a Card component) when one is expanded in a Material-UI and ReactJS project

Currently, I am working with an expandable Card component from Material-UI and using flex for aligning the components. However, when one card expands, it affects the positioning of the other components in the row: https://i.stack.imgur.com/vGxBU.png What ...

There are times when the `window.location.replace` function does not

I have implemented a Bootstrap Dropdown feature that allows users to select a language for site translation, append the URL with the selected language, and then reload the page. Initially, it works well for the first set of translations like en|es. Howeve ...

Using Angular to display exclusively the selected items from a list of checkboxes

Is there a way to display only the checked items in a checkbox list? I am looking for a functionality where I can select multiple items from a checkbox list and then have an option to show only the selected items when needed, toggling between showing just ...

Issues with receiving data on $.ajax POST request

If you'd like to check out my website Please use the following login details (case sensitive): Username: stack Password: stack Click on the tab labeled "yourhours." The main goal is to send all input box data to a database. At the moment, I am fo ...

Refresh the data list displayed within a specified div container

On my jsp page, I initially populate a model attribute with some data. Then, I use the following code to display this list on the jsp page: <c:forEach var="pattern" items="${patterns}"> <li class="list-group-item liitem"><st ...

Develop a nodejs script to make a request using a curl or similar method

Can anyone help me figure out how to replicate the functionality of this OpenSSL command using Node.js or curl? The command is: openssl s_client api-prd.koerich.com.br:443 2> / dev / null | openssl x509 -noout -dates. I have been unsuccessful in my at ...

Encountering CORS Error while trying to access Guest App in Virtualbox using Vue, Express, and Axios

I encountered an issue while trying to access my Vue app in Virtualbox from the host, both running on Linux Mint 20. Although I can now reach the login page from my host, I am consistently faced with a CORS error during login attempts: Cross-Origin Request ...

Error in JSON due to the presence of an unexpected token within the

I am facing a challenge with my PHP code, where I take a list of filenames or empty strings and store them in an array. This array is then converted to JSON and saved in a database successfully. However, the problem arises when this array is also stored wi ...

Avoiding unnecessary DOM updates in VueJS

Implementing an animated carousel has been my latest project, and I've been using code similar to the following: <template> <div v-for="(slides, id)" v-if="id > middle_id - 2 || id < middle_id + 2"> <div :class ...

Importing usernames and passwords from a .txt file with Javascript

I'm in the process of creating a website that includes a login feature. The usernames and passwords are currently stored within the website files as .txt documents. I am aware that this method is not secure, but for the purpose of this project, I want ...

Why is it that using "return false" does not stop the page from reloading when clicked?

I've attempted using this code, but it doesn't seem to prevent the page from reloading when the onclick function is triggered. What am I missing here? Could this be a problem related to browser compatibility? function track_specs() { $('b ...

Real-Time Chat Update Script

Recently, I've been delving into PHP and attempting to create a live chat web application. The data for the chat is stored in a MySQL database, and I have written a function called UpdateDb() that refreshes the chat content displayed in a certain div ...