Tips for applying consistent border textures to varying sized rectangles without distorting the corners

This is a request about textures. I am interested in having a variety of rectangles with different aspect ratios.

When I apply a texture

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

If I place the texture on a Plane, it gets distorted, especially at the corners. (The middle part doesn't matter as much since it's just a gradient).

The result looks like this.

https://i.sstatic.net/5TzEN.png

If possible, I would like to have differently sized rectangles all use one texture without stretching the corners. Similar to this.

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

Answer №1

I developed a unique BufferGeometry approach for implementing 9-slicing in Three.js. This technique involves creating 9 planes with specific UVs to preserve the corners' aspect ratio when stretching.

const basePlaneVertices = [0, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 0];

class NineSlicePlane extends THREE.Mesh {
    constructor(material, dimensions = { width: 100, height: 100, border: 25 }, uvBorder = 0.25) {

        let geometry = new THREE.BufferGeometry();

        let vertices = [];
        let uvs = [];

        const vertexOffsetsX = [0, dimensions.border, dimensions.width - dimensions.border, dimensions.width];
        const vertexOffsetsY = [0, dimensions.border, dimensions.height - dimensions.border, dimensions.height];
        const uvOffsets = [0, uvBorder, 1 - uvBorder, 1];

        for (let i = 0; i < 3; i++) {
            for (let j = 0; j < 3; j++) {
                for (let k = 0; k < 6; k++) {
                    vertices.push(
                        basePlaneVertices[k * 2] * (vertexOffsetsX[i + 1] - vertexOffsetsX[i]) + vertexOffsetsX[i] - dimensions.width * 0.5,
                        basePlaneVertices[k * 2 + 1] * (vertexOffsetsY[j + 1] - vertexOffsetsY[j]) + vertexOffsetsY[j] - dimensions.height * 0.5,
                        0
                    );
                    uvs.push(uvOffsets[i + basePlaneVertices[k * 2]], uvOffsets[j + basePlaneVertices[k * 2 + 1]]);
                }
            }
        }

        geometry.setAttribute('position', new THREE.BufferAttribute(new Float32Array(vertices), 3));
        geometry.setAttribute('uv', new THREE.BufferAttribute(new Float32Array(uvs), 2));

        super(geometry, material);
    }
}

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

Incorporate a 3D rendering into an established THREE.Scene() for an enhanced visual experience

I love creating 3D scenes, adding objects and exploring different camera movements using the keyboard. Recently, I came across a tutorial that introduced me to loading 3D models using JSONLoader in Three.js: var jsonLoader = new THREE.JSONLoader(); jsonL ...

Using ASP.NET Server Controls to Toggle Textbox Visibility Based on Dropdown Selection

What is the optimal method for displaying or hiding a textbox or an entire div section based on a user's selection from a dropdown menu? I am uncertain if this can be achieved with server controls, so would it require using standard client-side HTML c ...

Utilizing numerous occurrences of my personalized Angular JS Directive within the identical form

I've been struggling to find a solution for this particular issue. On my webpage, I have two instances of a directive that are supposed to set values for two different text fields. However, when I select one value, the other field automatically chang ...

Unexpectedly, the child component within the modal in React Native has been resetting to its default state

In my setup, there is a parent component called LeagueSelect and a child component known as TeamSelect. LeagueSelect functions as a React Native modal that can be adjusted accordingly. An interesting observation I've made is that when I open the Lea ...

The Ajax call is ineffective in executing the query

Welcome to my first post on this forum! I have been delving into the world of Ajax, jQuery, and Php lately and trying to create a 'Favorite' feature. This feature allows users to click on an icon, triggering an Ajax function that links to a .php ...

Why does jQuery constantly add a cache buster to my CDN script URLs?

My current script utilizes $.get( ... ) to retrieve HTML content from my server and inject it into a <div> using $.html. I have noticed that when the pulled HTML contains <script> tags, jQuery automatically adds a cache buster parameter to the ...

The "if" statement carries out the identical action each time it is executed

Despite my efforts to toggle the value of the turn variable every time the if statement runs, I keep encountering the same outcome. It appears that turn consistently evaluates as 2. Below is the code snippet in question: $(function() { var turn = 2; ...

What is the best method for users to add a div element and its contents?

As someone new to the world of web development, I am currently learning and working on a project. I have encountered an issue with creating a custom div that includes a link and user-defined text. I want the div to be generated automatically when the use ...

Refresh the view seamlessly using Ajax without reloading the page

When a button is clicked, the following code will run: $.ajax({ type: "POST", url: base_url+"/controller/method", data: {val: value}, success: function(data){ data = jQuery.parseJSON(dat ...

Exploring the asynchronous for loop functionality in the express.js framework

I'm attempting to use a for loop to display all images. I have stored the paths of the images in an array called Cubeimage. However, when trying to display them using <img>, I encountered an error. How can I write asynchronous code to make it wo ...

What is the best way to share models across different node.js projects?

In my setup, I have two node.js projects - project A and project B. Project A serves as the main project, while project B is more of an "ad-hoc" project with a specific purpose. The challenge lies in the fact that project B requires access to project A&apo ...

Utilizing jQuery to format HTML code and set it as the text in a Textarea

I am currently working on populating the text for my Textarea using JavaScript (jQuery), as I need to include a variable from JS in it. This is what I have been doing: $("#txt_banner1").text( '<a href="'+producturl+'">< ...

Retrieve the complete document from a specific value in MongoDB

I am trying to extract all values of a MongoDB document based on a single value. Example: "id": "id", "name": "name", "description": "description", "invite": "invi ...

Change the color of a table cell on two consecutive clicks using jQuery

Hey there! I've got this super simple code that does the job of changing the background color of a selected cell in a table to red. It's pretty awesome and works like a charm. Here's the CSS: .fixture-table td.team.on { background-colo ...

How come my button function is yielding output just once?

Currently, I am exploring the functionalities of a basic VSCode extension webview. My main goal is to trigger a message display every time a button is clicked, but I have encountered an issue where the message only appears the first time. Below is my HTML ...

Update the dropdown menu using jQuery when resizing

I am seeking a solution to adjust the behavior of my dropdown menu based on the screen resolution, utilizing jQuery. Currently, I have set it up so that above 1024px, the ul.level-2 menu is triggered by hovering over the button, and below 1024px, it respo ...

What is the process for inserting JavaScript into a submit button within a pop-up form?

I am looking to implement Ajax on a submit button within a pop-up form. Thank you for your assistance! ...

Tips for increasing the number of pixels in the current position of an element?

I need to shift an image to the right by adding pixels to its current left position. The challenge arises when the image, positioned absolutely, goes back to its nearest relative parent (the container) and creates a strange visual effect. Within my flex c ...

Locate a deeply nested element within an array of objects using a specific string identifier

Trying to search for an object in an array with a matching value as a string can be achieved with the following code snippet. Is there an alternative method to optimize this process without utilizing map? Code: const arr = [{ label: 'A', ...

Identifying Disconnected Sockets in Socket.IO

Running a socket.io server/client setup, I encounter an issue where a client safely disconnects, the server-side code snippet socket.on('disconnect', function() { }); is triggered as expected. However, in case of a client server crash, the ev ...