Display Texture Properly on Custom Mesh UV in Three.js

I've recently developed a custom Geometry function for Three.js, inspired by the Plane Geometry. While everything seems to be working well with my function, I'm struggling to get the UV mapping right. My approach involves using 4 triangles per square, contrasting with the default THREE.PlaneGeometry which uses only 2 triangles per square.

The UV code snippet from the PlaneGeometry goes like this:

var uva = new THREE.Vector2( ix / gridX, 1 - iz / gridZ );
var uvb = new THREE.Vector2( ix / gridX, 1 - ( iz + 1 ) / gridZ );
var uvc = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - ( iz + 1 ) / gridZ );
var uvd = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - iz / gridZ );

In contrast, here's the snippet in my source code:

var uva = new THREE.Vector2( a );
var uvb = new THREE.Vector2( b );
var uvc = new THREE.Vector2( c );
var uvd = new THREE.Vector2( d );
var uve = new THREE.Vector2( e );

It's clear that my approach is incorrect. Trying to replicate the UV setting from PlaneGeometry results in distorted output, leaving me unsure about the correct calculation method. For instance:

var uva = new THREE.Vector2( ix / gridX, 1 - iz / gridZ );
var uvb = new THREE.Vector2( ix / gridX, 1 - ( iz + 1 ) / gridZ );
var uvc = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - ( iz + 1 ) / gridZ );
var uvd = new THREE.Vector2( (( ix ) / gridX) + gridX, 1 - iz / gridZ );
var uve = new THREE.Vector2( ( ix + 1 ) / gridX, 1 - iz / gridZ );

This produces undesired results: BAD UV

Despite seeking help in the THREE.JS ChatRoom, the explanation regarding UV vectors and textures failed to clarify my doubts:

(Q) So.. are the Vector2 for UV's not supposed to be the same position as the verticies ?

(A) no an UV is a vector that maps into a texture if you have a 512x512 texture, and an UV with [0.25, 0.75], it would map to the pixel in the texture at 256, 768 each vertex has an uv this means that this vertex maps into the texture like explained above this is done for each face of a vertex, and all fragments in the face are then interpolated using those three uvs

Unfortunately, the discussion did little to dissipate my confusion, especially concerning the concept of vertices having textures. How can a point be associated with a texture?

If anyone can provide guidance or examples on how to properly position UVs, it would be greatly appreciated.

For reference, here's the relevant segment from my source code:

THREE.DiamondGeometry = function ( width, height, widthSegments, heightSegments ) {

    // Code for DiamondGeometry implementation...

};

THREE.DiamondGeometry.prototype = Object.create( THREE.Geometry.prototype );

Answer №1

Check out this image: As mentioned, when dealing with a single texture without repetition, the UV coordinates range from (0,0) to (1,1). If you have a quad-plane and assign UVs as shown in the image, the entire texture will be displayed on the quad. However, if you divide the quad into smaller sections by adding edge loops or tessellating it, but still want the texture to display without repeating, you'll need to calculate intermediate values for the vertices' positions in UV-space. For instance, the vertex at the center (resulting from tessellation) is now UV (0.5, 0.5), and the top-middle vertex is (0, 0.5). Take a look at how PlaneGeometry handles this and try to gain insights from it^^

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

Platform for managing web push notifications and directing users to specific URLs

I am not very familiar with coding, but I recently had a developer create a self-hosted web-push panel for my website and provided a plugin for my WordPress site. The admin panel is functioning properly, however, there seems to be an issue with it, possibl ...

What can be done to stop the event handler from executing?

My goal is to verify a user's authentication every time they click on a button. If the user happens to be logged out and tries to click on a button, they should be redirected back to the home page. The issue I'm facing is that the code after the ...

Aptana - Expose the Declaration

After hearing rave reviews about Aptana being the best IDE for JavaScript, I decided to download it. I grabbed a project from GitHub, found a method call in the code, right clicked on it, and saw 'open declaration - F3'. However, when I tried bot ...

Focus on the textbox within the repeating element

I am working with a repeater that has header and item templates. The header includes a textbox and a link button labeled "Add" to add the item entered in the textbox to the list. My goal is to set the focus back on the textbox after clicking "Add". Below i ...

Exploring the dynamic capabilities of YUI AJAX in conjunction with .NET

Looking for some help on a problem I'm experiencing with YUI's AJAX and a YUI Datatable integration. The AJAX request is functioning properly, returning the correct data formatted as follows: {NoteId:'" + result.NoteId + "', CreatedOn: ...

What is the best method to store large amounts of coordinate data efficiently in JavaScript for easy searching by value?

When dealing with two-dimensional coordinates like (0, 1), (0, 2), and (2, 3), I initially considered using arrays such as [[0, 1], [0, 2], [2, 3]]. However, I quickly realized that this approach presented challenges when it came to efficiently looking up ...

Issues arise when attempting to play audio files following the loading of contents using AJAX

As a newcomer to jQuery, I've spent hours trying to troubleshoot my issue with no success. Therefore, I'm reaching out for help here. In the code below, I have successfully retrieved song details from a database and loaded them into a div using ...

What is the best way to clear the contents of the `<Form>` field after submitting, while still maintaining it as a server component?

Utilizing a server component for the Search form, I encountered an issue when trying to empty the input field upon clicking the cancel button. Despite importing a <SearchReset/> client component, I struggled to reset or clear the form. SearchForm.ts ...

What is the best way to set the first radio button as the default selection in Material UI?

The challenge I am facing involves a set of radio buttons representing dates from today to 30 days in the future. When a radio button is selected or active, it changes the background color. However, I would like the default selection to be the first radio ...

What is the best way to properly save a pdf and docx file in React JS?

My challenge is to efficiently download a docx or pdf file in React JS (based on server response) once the server responds. The backend is written in Python/Flask. Although the file downloads successfully, it appears corrupted and damaged when I try to op ...

What might be the reason for the random reset of the views counter to zero?

I am the proud owner of a small news website that boasts an impressive collection of thousands of articles. For my frontend, I have opted for NextJS (version 14 with pages directory), while Strapi powers my backend. Each article comes equipped with a "view ...

Is there a problem with the string comparison in my JavaScript code?

I am dealing with various XML files specific to different operating systems. Here is an excerpt from the SunOS XML: <osname>SunOS </osname> This data is extracted using jQuery: var osname = $(this).find('osname').text(); However ...

Are 'const' and 'let' interchangeable in Typescript?

Exploring AngularJS 2 and Typescript led me to create something using these technologies as a way to grasp the basics of Typescript. Through various sources, I delved into modules, Typescript concepts, with one particularly interesting topic discussing the ...

Error message: 'firebase/app' does not export 'app' (imported as 'firebase') - Import attempt failed

Encountered a strange issue today. As I tried to import firebase, an error popped up: ./node_modules/firebaseui/dist/esm.js Attempted import error: 'app' is not exported from 'firebase/app' (imported as 'firebase'). The setup ...

Effective method for obtaining the URL from a Node.js application

I'm curious if there is a method to extract a url such as http://whatever:3000/somemethod/val1/val2/val3 Is there an alternative to using .split after obtaining the path name? For example, I attempted to acquire /somemethod/val1/val2/val3 and then ...

What is the process of transitioning from jQuery to plain JS for converting selectors and event capturing?

Looking for assistance in converting this code to vanilla JavaScript: document.addEventListener("DOMContentLoaded", function() { document.querySelector("textarea").addEventListener("keydown", function(event) { var textarea = this; ...

Query string representation of a JSON object

Utilizing Maxmind.Com's GeoIP2 (Omni) Webservice, my Drupal 7 website can fetch geographic data using the visitor's IP address. To obtain a JSON document, I use the following code: <script type="text/javascript" src="//js.maxmind.com/js/apis ...

React hook form submit not being triggered

import React, { useState } from "react"; import FileBase64 from "react-file-base64"; import { useDispatch } from "react-redux"; import { makeStyles } from "@material-ui/core/styles"; import { TextField, Select, Input ...

Retrieve the attribute value from an HTML element in a JSON response using JavaScript

I received a JSON request result containing a blogger post. // API callback posts( { "version": "1.0", "encoding": "UTF-8", "entry": { "title": { "type": "text", "$t": "Vimeo Embed Video Post" ...

Debugging on the server side of Meteor application is crucial for

I am attempting to debug a Meteor app on Windows using node-inspector. I am following these steps: First, install node-inspector by running: npm install -g node-inspector Next, start Meteor in debug mode by running: NODE_OPTIONS='--debug' meteo ...