Creating repeatable texture patterns in Three.js

Hello, I have developed a basic renderer for my 3D objects that are generated using PHP.

While I am able to successfully render all the objects, I am facing some major issues with textures.

Currently, the texture I am using is sized at 512x512 pixels.

I want to implement this texture onto my object, but the result is not as expected:

I am struggling to display a grid without stretching, maintaining a nice 1:1 ratio.

I believe I need to calculate the repeats in some way. Does anyone have any ideas on how I can achieve this?

This is the code snippet where I set up the texture:

var texture = THREE.ImageUtils.loadTexture(basePath + '/images/textures/dt.jpg', new         THREE.UVMapping());
texture.wrapT = THREE.RepeatWrapping;
texture.wrapS = THREE.RepeatWrapping;
texture.repeat.set(1,1);
stairmaterials[0] = new THREE.MeshBasicMaterial(
    {
        side: THREE.DoubleSide,
        map: texture
    });

I have tried adjusting the repeat values to achieve a non-stretched 1:1 ratio, but unfortunately, I have not been successful. It only made the situation worse.

Additionally, I am utilizing the following algorithm to calculate vertex UVs:

geom.computeBoundingBox();

var max = geom.boundingBox.max;
var min = geom.boundingBox.min;

var offset = new THREE.Vector2(0 - min.x, 0 - min.z);
var range = new THREE.Vector2(max.x - min.x, max.z - min.z);

geom.faceVertexUvs[0] = [];
var faces = geom.faces;

for (i = 0; i < geom.faces.length; i++) {

    var v1 = geom.vertices[faces[i].a];
    var v2 = geom.vertices[faces[i].b];
    var v3 = geom.vertices[faces[i].c];

    geom.faceVertexUvs[0].push([
        new THREE.Vector2(( v1.x + offset.x ) / range.x, ( v1.z + offset.z ) / range.z),
        new THREE.Vector2(( v2.x + offset.x ) / range.x, ( v2.z + offset.z ) / range.z),
        new THREE.Vector2(( v3.x + offset.x ) / range.x, ( v3.z + offset.z ) / range.z)
    ]);

}

geom.uvsNeedUpdate = true;

Answer №1

Is your mesh imported or procedurally generated?

As you loop through each triangle, applying a scale/projection, it should work well if all the boxes are part of the same mesh. This way, they will be scaled uniformly and aligned properly since they share the same model space.

If the boxes are not part of the same mesh, the process may not yield accurate results.

The vertices being iterated through exist in local model space. Each box could have its own unique scale and position in this local space. To ensure consistency and align them correctly, you need to transform them into world space using the object's .modelMatrix. By applying this matrix, all vertices will be brought into a unified space. You can try omitting the bounding box calculation or use just one object's bounding box to achieve uniform scaling and alignment.

However, please note that this algorithm won't allow for a true 3D representation on the map as it involves a 2D projection. To show the map in 3D, consider analyzing each triangle's orientation and potentially selecting a different projection direction.

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

Seamless integration of Applitools with WebdriverIO

Seeking assistance to integrate Applitools with my WebdriverIO project. Find the specifications below: Node Version: v12.18.2 NPM Version: 6.14.5 WebdriverIO Version: 6.3.6 The service in my wdio file is configured as follows: services: ['selenium-s ...

Propagating numerical values through iterative iterations

I am currently facing an issue with passing values as props to a component using the forEach method in JavaScript. In addition to passing the existing values from an array, I also want to send another value that needs to be incremented by 1 for each iterat ...

Internet Explorer 11 Freezes Unexpectedly with Dynamic SVG Components

Lately, I developed a unique SVG Icon control for the new html application at my workplace. However, our quality department started noticing that IE 11 would intermittently "crash" while using the application after its implementation. I'm not convinc ...

The background image of my bootstrap carousel is not responsive to changes in the browser window size

Hey there, I'm new to the world of programming and currently working on a project to create the front end of my personal website. I've opted to utilize a bootstrap carousel background image slider in my index.html file. However, I've noticed ...

Learn how to dynamically incorporate multiple Bootstrap collapse elements in PHP

I've encountered an issue with my code that contains both HTML and PHP. The problem arises when there are multiple elements in a collapse, as only the first element works properly. This is due to the fact that each element has the same id named "colla ...

How can I make sure that my function returns a mutated object that is an instance of the same class in

export const FilterUndefined = <T extends object>(obj: T): T => { return Object.entries(obj).reduce((acc, [key, value]) => { return value ? { ...acc, [key]: value } : acc; }, {}) as T; }; During a database migration process, I encounte ...

What are the best practices for effectively managing jQuery UI sliders?

I am currently developing a web application that involves updating jQuery UI sliders using JavaScript. While I have managed to resolve most of the issues related to updating the slider after initialization, there is one particular issue that remains unreso ...

Why is receiving input value in React not successful?

Attempted to retrieve input value when user clicks search, but encountered difficulties. var Login = React.createClass({ getInitialState: function () { return {name: ''}; }, handleSearch(){ alert(this.state.name); // Why isn't ...

Tips for building an API using an app router in Next.js

After setting up my new project and using the app router as recommended in the latest version of Next.js, I am now looking to create an API. How can I go about creating and utilizing this API within my page app/user/page.tsx? I have already created an API ...

Tips for adjusting the size of a ThreeJS Renderer in Firefox and Edge

I am facing an issue with my simple web application that includes a three.js webgl view. The page is designed to be responsive, so I need the 3D view to resize accordingly when the window size changes. The resizing functionality is working correctly in Chr ...

a guide on utilizing knockout js to bind json data within a datalist

Below is the code I have written for my web services: public class SympsService : System.Web.Services.WebService { [WebMethod] public symps GetSymptoms(string organ_name) { symps Symptoms = new symps(); string CS = Configurati ...

Guide to excluding all subdependencies using webpack-node-externals

My current setup involves using webpack to bundle both server assets and client code by specifying the target property. While this configuration has been working well, I encountered an issue where webpack includes all modules from node_modules even for ser ...

React Select only enabling single selection at a time

Currently, I am in the process of updating my react app to version 16.8 and also updating all associated libraries. One issue that has come up is with two drop-down selects from Material UI. Since the update, the multi-select option no longer allows multip ...

Get access to the file upload field using ajax

I'm encountering an issue with accessing the file upload field //HTML <input type='file' name='images' id="images" multiple> ////////////////// $('.bt-add-com').click(function(){ var formdata = new For ...

Having trouble with the "next" pagination button not loading cards, even though the numbered buttons are working perfectly

My pagination numbers are functioning correctly, but I'm having trouble with my "next" or "previous" buttons. I am using Bootstrap 5 and jQuery to load cards from an array. The issue seems to be with the currentPage variable in my displayList function ...

Error: [$controller:ctrlreg] - The controller registration has failed

I am currently exploring the world of AngularJs and attempting to display options from a json file. However, I keep encountering the following error message: "Error: [$controller:ctrlreg]" Below is the code snippet I am working with: var sj = angular. ...

What methods can be used to disable the back button functionality within an iframe?

I am facing an issue with embedding YouTube links on my webpage using iframes. When a user clicks on the link, it plays the video in the iframe. Below is the HTML and jQuery code I am using: HTML: <a href="http://www.youtube.com/embed/Q5im0Ssyyus"> ...

Connect user input to a predefined value within an object

I am currently working on developing a timesheet application that allows users to input the number of hours they work daily. The user data is stored in an object, and I aim to display each user's hours (duration) in an input field within a table. An i ...

What flaws are present in this authentication system?

As a developer with a passion for coding, rather than a security expert, I came across The definitive guide to form-based website authentication, which highlighted the importance of SSL or complex algorithms in safeguarding login data from eavesdropping. D ...

How can I clear the div styling once the onDismiss handler has been triggered

Seeking assistance with resetting a div on a Modal after it has been closed. The issue I am facing with my pop-up is that the div retains the previous styling of display: none instead of reverting to display: flex. I have searched for a solution without su ...