Preserve aspect ratio when applying a texture map to a threejs object

I have a project idea to develop a 3D shoe design tool that allows users to create patterns and apply them to the shoes.

I am facing an issue with adding an image to a Three.js material. The texture appears blurry after updating it. As a beginner in Three.js, I'm uncertain if the aspect ratio is causing the problem or if it's something else.

This is how I am loading the texture:

var texture_loader = new THREE.TextureLoader();

var texture = texture_loader.load( 'https://ik.imagekit.io/toesmith/pexels-photo-414612_D4wydSedY.jpg', function ( texture ) {

    texture.wrapS = texture.wrapT = THREE.RepeatWrapping;
    texture.offset.set( 0, 0 );
    texture.repeat.set( 1, 1 );
    vamp.material = new THREE.MeshPhongMaterial({
      map: texture,
      color: new THREE.Color('#f2f2f2'),
      shininess: 20,
    });
});

The current result looks like this:

https://i.sstatic.net/vhQ5B.png

However, the expected outcome should resemble this:

https://i.sstatic.net/fisUm.jpg

If anyone has insights or can provide assistance, I would greatly appreciate it. Thank you.

You can view the code on Codepen.

Answer №1

Your UVs are currently occupying a very small area in texture coordinates, causing your texture to appear blurry. To fix this issue, you should adjust your UV mapping to take up more space, as shown below:

https://i.sstatic.net/6xuKp.jpg

There are two approaches to achieving this:

  1. Scaling Up UVs: Import your model into Blender and modify the UV mapping to cover a larger portion of the [0, 1] range.
  2. Scaling Down Texture: Utilize the texture.repeat property creatively to resize your texture to match your current UV layout. Additionally, adjust the offset for proper centering. Consider using the following code snippet:
texture.repeat = new THREE.Vector2(10, 10);
texture.offset = new THREE.Vector2(xx, yy);

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

Retrieve the location within the parent mesh

In my scenario, I have a series of meshes organized in a hierarchy as follows: Scene -scene.add(SpaceMesh) -SpaceMesh.add(ShipMesh) SpaceMesh is currently in motion within the scene. However, ShipMesh remains stationary. When I request th ...

What is the process for removing a mesh that has been uploaded using a function and then added to an Object3D?

// Defining variables for 3 obj models: cushion, backrest, and frame, and a group variable chair01 to include all 3 obj models var cushion; var backrest; var frame; var chair01 = new THREE.Object3D(); ...

Display an AJAX div when the image is hovered over and have it track the movement of

I am seeking assistance with my website project, which involves providing users with download links to movies. However, I am struggling to make the preview_block div(id) appear when the mouse hovers over the movie_block div(id). Additionally, I am having ...

I am struggling to grasp the concept of the named controller in Angularjs and how the keyword "this" is utilized

I'm trying to grasp the concept of using named controllers with the controller as syntax in Angular. Within my application, I have assigned the same controller to different divs, always naming the controller as myCtrl as C in the HTML. Inside the cont ...

Adding AngularJS data to JSON file for update

I'm feeling lost on where to begin with this. I am working with an AngularJS form and I need it to add the data it sends to a json file. I understand that AngularJS is client-side, so my main issue lies in figuring out how to manage the data being sen ...

Please make sure to only choose one checkbox at a time

I have designed a list-type menu using the bootstrap list group. My goal is to have only one main category active at a time. For example, if "Main Category One" is selected and then I click on another category, "Main Category Two" should become activated w ...

methods for retrieving data from a Laravel controller within an imported JavaScript file

My current approach involves using ajax to update a table based on the user's input. $('#user').change(function(){ var user= $("#user").val(); if (user !='None'){ $.ajax({ type: "GET", url: '/getUserAccounts/&a ...

Using Javascript to access the variables of a parent function

I have the following simplified code snippet: function(req, res) { var path = 'login'; req.on('end', function(data) { var post = parsevars(rawpost, '&'); if (typeof post.username !== 'undefined' && type ...

How can I utilize npm with the original source code instead of minified or bundled code?

I am looking to access npm and JavaScript (or TypeScript) 3rd party libraries directly from the source code. Similar to how I can make changes in Python libraries by going into their source code, I want to have the same capability with my JavaScript depen ...

The CSS and JS codes are not successfully integrating into the webpage

I am encountering an issue with loading CSS and JS files onto my page. My project involves PHP and Xampp. The file structure is as follows: My Site - CSS - index.css - JS - index.js - Index.php (Apologies for the lack of a folder tre ...

Executing simultaneous asynchronous queries with Mongoose results in an error: `SyntaxError: Illegal return statement` due to

My goal is to make multiple asynchronous requests to Mongo using Mongoose in parallel. To achieve this, I created a helper function that handles these queries dynamically without knowing the exact number beforehand. While my current implementation works ...

What is the best way to establish a default selection in Angular?

After retrieving JSON data from the server, it looks something like this: $scope.StateList = {"States": [ { "Id": 1, "Code": "AL", "Name": "Alabama" }, { "Id": 2, "Code": "AK", "Name": "Alask ...

Error in Typescript index: iterating over properties of a typed object

My scenario involves an interface that extends multiple other interfaces from an external library: interface LabeledProps extends TextProps, ComponentProps { id: string; count: number; ... } In a different section of the codebase, there is an ...

"Ionic with Angular is facing an issue where ion-radio element cannot be set as checked by default

Having trouble selecting a radio button in a radio list. Can anyone help? Any assistance would be greatly appreciated. This is how I've been attempting it: <div class="list"> <ion-radio ng-repeat="item in goalTypeList" ...

Is there an efficient way to quickly verify if a large number of elements have a specific class using a query parameter or string

My goal is to create a filtering feature where users can choose from categories "a", "b", "c", "d", and "e". Users can come to the page with a specific query string like http://example.com?cate=a, http://example.com?cate=b, etc. The categories sorting butt ...

methods to retrieve value from a javascript function

Just a quick inquiry - what's the best way to pass or return a value from a function? Here is my code: function numberOfDivs(){ var numberOfElements = $("#div").children().length; // Counting the number of existing divs return numberOfElements; } ...

Look at the image to see why the color of this three.js MeshBasicMaterial is unique

click here to view image What is the reason for this variation in color? ...

The "Go" button on iPhone triggers an error before the page is sent back

I am facing an issue with a web page that has a form containing multiple submit buttons with different functionalities like clearing the form, performing a calculation, or adding another entry line. The problem arises only on iPhone devices (tested on bot ...

Creating protected routes in ReactJs using Typescript by utilizing a Higher Order Component (HOC) as

I'm struggling to create a basic HOC that can protect a component, ensuring that the user is logged in before rendering the component. Below is my attempt at building the protective HOC (not functional yet). export default function ProtectedRoute(Com ...

What causes the element to load with a transparent appearance? And why is my JavaScript code overriding the hover effect on it?

My goal is to have an element fade out when clicked and then fade back in when its space is clicked again. I also want the opacity of the element to be 0.9 instead of 1 when visible. I've encountered two issues with my code. First, the hover selector ...