Managing the size of personalized shapes in Three.js

I need help with a custom object that represents a frame created by subtracting two meshes.

generateFrame (width, height, depth) {
    const frameMesh = new THREE.Mesh(new THREE.BoxGeometry(1, 1, 1));
    frameMesh.scale.set(width, height, depth);
    const innerFrameMesh = frameMesh.clone();
    innerFrameMesh.scale.set(width - 4, height - 4, depth);

    // subtract inner frame from outer frame
    const frameGeometry = fromCSG(toCSG(frameGeometry).subtract(toCSG(innerFrameMesh)));

    const frame = new THREE.Mesh(frameGeometry, new THREE.MeshStandardMaterial(0xffffff));
    frame.geometry.computeVertexNormals();
    frame.userData.isFrame = true;

    return frame;
}

I want to scale this frame dynamically as its size changes. However, using the .scale method causes the object to stretch instead of preserving the width of the frame (which is 4 units). Is there a way to customize the scaling behavior or is there a different property/method to use to maintain the ratio of "white space" vs. "object" portion? Any help is appreciated. Thank you.

Answer №1

Using the scale() method, you can uniformly transform each vertex by multiplying their coordinates. For instance, if the inner edge of a frame is at x=6 and the outer edge at x=10, scaling it by 2 would result in the inner edge at x=12 and the outer edge at x=20, increasing the frame width to 8 units.

One way to tackle this issue is by breaking down the frame into four separate sections: top, bottom, left, and right. When scaling along the x-axis, you can elongate the top and bottom sections while adjusting the position of the left and right edges. This technique helps maintain the thickness of 4 units.

Conversely, when scaling along the y-axis, you can stretch the left and right sections and then reposition the top and bottom sections to ensure they align properly.

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

Using npm-installed cesium for the web browser is a straightforward process that involves importing

Exciting news - Cesium is now available on npm! Once I've run npm install cesium in my project, all the codes are placed inside the node_modules folder. In the introductory hello world example of Cesium, it imports cesium similar to this: <script ...

Phantom.js WebDriver.io Error: Issue with Syntax - DOM Exception 12

Currently conducting tests using webdriver.io and phantom.js. The first test works successfully, providing a list of elements: return client .url(config.host) .waitForVisible('#myvenuelist', 2000) .click('#myvenuelist') ...

Tips for validating a react select dropdown list without relying on hooks

Is it possible to validate the selection made in a dropdown without using react-hook? I need to make sure that the selection is never empty or null when the user clicks on the submit button. handleChange = (option) => { this.setState({option: option.tar ...

Angular JS presents an exciting feature called Multiple Filters, which allows

I have a data representation application that displays information in table format with columns id, name, price, quantity The data is presented using ng-repeat. You can view it on this Plunker <body ng-controller="myController"> <h1>Data< ...

Using jQuery functions on inputs within a Bootstrap Modal

I've encountered an issue with my jQuery functions that validate input fields based on a regex pattern. Everything works smoothly when the form is displayed on a regular page, but as soon as I try to implement it within a Bootstrap Modal, the validati ...

Error: Interface declaration for _.split is missing in the Lodash.d.ts file

For my current typescript project that heavily relies on Lodash with lodash.d.ts, I've encountered an issue with the _.split function not being implemented yet. It's listed under the 'Later' section in the .ts file. I need to find a wo ...

What is the best way to assign default values when destructuring interfaces within interfaces in TypeScript?

My goal here is to create a function that can be used with or without arguments. If arguments are provided, it should work with those values; if not, default values should be used. The issue I'm facing is that although there are no TypeScript errors ...

What is the best way to transfer information between two views in Aurelia?

I have two different perspectives, one called logo and the other called folder. The logo view should display something from the folder view if the folder is empty. logo.html <template> <require from="company-assets/folders"></require> ...

Avoiding unnecessary re-renders of a parent component caused by a child component

I'm facing rendering problems and would greatly appreciate any assistance. I attempted to use useMemo and useCallback, but it resulted in the checkbox breaking. Within a component, I am displaying information from an object. Let's consider the fo ...

issue encountered when trying to deploy a React application on GitHub

I successfully created a todolist on localhost. Now, I am facing issues while deploying it on GitHub. When I run '$ npm run deploy' in the command prompt, I encounter errors. To troubleshoot, I have added 'homepage', 'predeploy&ap ...

How to Build Node 0.4.7 on Ubuntu 11.10?

Having trouble compiling 0.4.7 for Heroku support as I am unable to enable ssl support required by express. I've tried installing libssl-dev and even attempted manual installation of openssl, but no luck so far. Any suggestions on how to get node up a ...

Retrieve data that resets to undefined upon reloading from an array

Encountering an unusual error while working with TypeScript for the first time. Initially, when I use console.log(data), it displays an array with objects. However, upon reloading the webpage without making any changes, the console log shows undefined. con ...

Customizing Passport JS Bearer strategy for various endpoints and HTTP REST operations

I'm currently using the BearerStrategy and I am attempting to configure different strategies for each endpoint or method within the same router. After reviewing the documentation, I have not come across any references to this particular scenario othe ...

Struggling to navigate to a specific div element using a hash in an Angular application

I came across some information here and here stating that a page can be scrolled to a specific element using just a hash selector and an id attribute. However, I am facing difficulties implementing this in my Angular application. Could this be because of ...

Guide to choosing an option in a select dropdown using Vue?

<span class="input-group-btn" style="min-width: 100px"> <select class="form-control" v-model="field.related_field"> <option value="null"></option> <option v-for="f in fields" v-bind:val ...

Leveraging material elements in React applications

I have been studying the guide on Material UI Beta for react and I am interested in creating a simple component using the Drawer element. Here is an example code from the official documentation that demonstrates how to build such a Component. import React ...

Encountering issues with Visual Studio Code following the integration of the MongoDB API Mongoose into my code

As I delve into the world of web development, I have been exploring databases with MongoDB Atlas and mongoose. Interestingly, my debugging process has hit a bump when using the node.js(legacy) debugger in VS code after importing mongoose with const mongoos ...

Executing a JavaScript function from the form attribute value in HTML: Steps to follow

<html> <head> <script type="text/javascript"> function add() { num1 = 20; num2 = 30; total = num1 + num2; return total; } </script> & ...

Is there a way to detect esbuild's build errors and execute a script in response?

Does anyone know how to handle esbuild's build error and trigger a script afterward? I'm integrating it into my workflow with npm, VSCode, and pure JavaScript. I've searched everywhere but haven't found any information on this specific ...

Tips for correctly implementing JavaScript conditional statements

Upon running this jQuery (1.8.3) code, I consistently receive the "in" alert message even when the length is greater than 1. The purpose of my actions is to dynamically add elements to a menu while ensuring that the element does not already exist. Even a ...