Challenges with mapping UVs in THREE.js Geometry

I've been attempting to apply a textured mesh with unique UV coordinates for each face, but despite following all the necessary steps, I haven't achieved the desired results.

To start, I define the texture coordinates:

top_texture_uv = [new THREE.Vector2(0,0), new THREE.Vector2(8,0), new THREE.Vector2(8,8), new THREE.Vector2(0,8)];
side_texture_uv = [new THREE.Vector2(0,0), new THREE.Vector2(4,0), new THREE.Vector2(4,2), new THREE.Vector2(0,2)];

I want the faces to repeat different times.

Next, after loading the texture, I update the geometry's face vertexes as mentioned in the documentation:

var txLoader = new THREE.TextureLoader();
  txLoader.load(
    'textures/checker.gif',
    function ( texture ) {
      ground_segment.mesh.material.map = texture;
  
  ground_segment.mesh.geometry.faceVertexUvs[0] = [];

  // Mapping UV coordinates to individual face vertices
  // Repeat this process for required number of face repeats
  ...

  ground_segment.mesh.geometry.uvsNeedUpdate = true;
  ground_segment.mesh.material.needsUpdate = true;
    }
  );

Despite my efforts, mapping face vertices doesn't seem to have any impact. Instead, it simply loads the texture without repeating on every face of the object. How can I adjust the geometry to reflect the specific UV coordinate changes I'm aiming for?

Answer №1

If you are working with THREE.Geometry and need to update the UVs after rendering the geometry, avoid reassigning the array directly:

mesh.geometry.faceVertexUvs[ 0 ][ 0 ][ 0 ] = new THREE.Vector2( u, v ); // Don't do this

Instead, follow this recommended approach:

mesh.geometry.faceVertexUvs[ 0 ][ 0 ][ 0 ].copy( uv ); // or .set( u, v )

mesh.geometry.uvsNeedUpdate = true; // Make sure to set this if the geometry has been rendered

This advice applies specifically to three.js r.84

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

Unable to insert a JSON object into an array using JavaScript and MongoDB

I'm encountering an issue when trying to push data into my Student model with the following schema: var StudentSchema = new Schema({ firstName: { type: String, trim: true, default: '' //validate: [validat ...

emissive material textures in three.js

Recently, I've been delving into the world of three.js and I am keen on utilizing an emissive map. My initial attempt at loading a texture into the emissive property of a phong material did not yield the desired result. Here's the snippet of code ...

Populating a DataTable with JSON data using the rows.add method

Currently, I have a functioning Datatable that successfully adds all 87 Star Wars characters from the swapi.co API. The data is retrieved in JSON format structured like this: { "count": 87, "next": "https://swapi.co/api/people/?page=2", "previous": null ...

Is there a way to sort through an array based on a nested value?

Within an array of objects, I have a structure like this: "times": [{ "id" : "id", "name" : "place", "location" : "place", "hours" : [ {"day": " ...

Issue with updating the vertices in three.js EdgesGeometry is causing the Line Segments to not be updated as expected

I have created multiple three.js objects. I have observed that the 'other' objects, designed as Mesh/Geometry/Material, update as expected after calling verticesNeedUpdate() Furthermore, I have two wireframe objects that were designed in this m ...

Issue with AJAX POST request, values not being passed to variables despite being displayed in the echo output

I am facing an issue with my Ajax post request, where the server seems to be receiving two date variables as empty even though they are sending correctly based on what I can see in the echo statement. The gettype function is returning NULL when trying to c ...

Prevent the Esc key from closing panel in jQuery Mobile

As per the jQuery Mobile documentation: "If you click the link that opened the panel, swipe left or right, or tap the Esc key, the panel will close. By default, panels can also be closed by clicking outside of the panel onto the page contents." Visit t ...

The information I am trying to send to my node.js server is not getting returned to the client

As a newcomer to node.js, I wanted to sharpen my skills by working on a simple program. The goal was to assess the connection between the client and server using node.js, express, and socket.io. Let's take a look at server.js // Setting up an HTTP se ...

Iterating through an object in Javascript using dynamically changing property names

Is there an efficient way in Javascript to iterate through the property names of objects in an array? I have objects with properties from guest1 to guest100. Instead of manually looping through each property, is there a method to loop through all the gues ...

deleting a aircraft upon the addition of another in three.js

I am striving to implement a button that toggles between different terrain-generating functions and removes the previous terrain. The current issue I am facing is that the planes stack on top of each other. You can see an image of the problem here. There ...

What do you prefer: defining properties with the JSON object or with objectName.property in JavaScript

Can you tell me which approach is considered the best practice? Is it better to use the "this" statement in the following way: var obj = { x: 20, y: 10, width: this.x, height: this.y, render: function () { // renders object on canvas ctx.fi ...

Issue with horizontal scrolling in ng-scrollbars occurs when scrolling from right to left

We are currently developing a single page application that supports two languages, one being right to left and the other left to right. For scrolling functionality, we have implemented ng-scrollbars, an Angularjs wrapper for the malihu-custom-scrollbar-pl ...

Looking to implement a star rating feature in Vue.js 2?

My perspective is as follows : <div class="col-md-8"> ... <star-rating :value="3"></star-rating> ... </div> This is how my star-rating component looks like : <template> <span class="rating"> &l ...

`The universal functionality of body background blur is inconsistent and needs improvement.`

I've developed a modal that blurs the background when the modal window is opened. It's functioning flawlessly with one set of HTML codes, but encountering issues with another set of HTML codes (which seems odd considering the identical CSS and Ja ...

Issues have been encountered with the texture when utilizing the MTLLoader in three.js

Having an issue with the MTLloader in the three.js library as the texture is not loading properly, resulting in a completely white mesh. Strangely, using the OBJ file from the examples in the library works fine and loads the object with its texture correct ...

What distinguishes the use of a list of Embedded Documents from a list of ObjectIds?

While exploring the Mongoose documentation, I stumbled upon this example: var Comments = new Schema({ title : String , body : String , date : Date }); var BlogPost = new Schema({ author : ObjectId , title : String , bo ...

What is the limitation of including a string constant with "</script>" inside a <script> block?

I am genuinely curious about this: I thought that the following code would be valid within an HTML document: <script> var test = "<script>why? </script>"; </script> However, it turns out that this leads to an "unterminated str ...

Loss of value in .net Jquery CheckBox onchange event

My JQuery code for CheckBoxes is causing some unexpected behavior: $(document).ready(function () { $('#chk_AGVS').change(function () { if ($(this).is(":checked")) { '<%Session["chkAGVS"] ...

Press the delete button to remove both the box and text dynamically with JavaScript

My task involves managing a shopping list with delete buttons next to each item. I am trying to use JavaScript to remove both the button and the corresponding text when the button is clicked. So far, my attempts have been unsuccessful. I used document.getE ...

Disabled button is set, yet I am still able to click on it

Below is the HAML code snippet: %a.btn.btn-primary.personlistbtn{href:patients_path, :target => "_blank", :disabled => 'disabled'} Person List This is how it appears in the Inspect Element: <a class="btn btn-primary personlistbtn" di ...