(Solving the Issue of Size in Three.JS Transform Controls for Translation)

I have been working on customizing the transform controls in three.js for my current project. I successfully modified the rotation part and am now focusing on the translation aspect. In the translation Gizmo, there is an XYZ octahedron at the center. I have removed all other elements and functionalities, focusing solely on that central mesh, and it is functioning well. However, I am facing a minor challenge with the size and position of the controller.

The Octahedron has been changed to a boxGeometry, and I am writing the code to adjust the size of the controller to match the selected object. To achieve this, I thought of making the controller's size equal to the size of the boxHelper acting as the object's outline. I attempted this logic in a sample code where I created a box, obtained the size of the box helper, and then created another box of the same size, which worked perfectly. But when implementing the same code in three.js transform controls, the result was not as expected.

Here is the geometry code snippet:

 XYZ: [[ new THREE.Mesh( new THREE.BoxGeometry( 0.1, 0.1, 0.1 ), pickerMaterial )]],

Next, I retrieve the size of box3 when attaching it to an object:

  this.addBoxHelper = function () {
        this.removeBoxHelper();

        if(this.object.box3) {
            **this.object.box3.getSize(selectionBoxSize);**
            console.log(selectionBoxSize)

            this.objectBoxHelper = new THREE.Box3Helper(this.object.box3, 0xffff00);
            this.objectBoxHelper.canSelect = function () {
                return false;
            }
            this.object.add(this.objectBoxHelper);
        }
    }

The following is my update function for transform controls:

    this.update = function () {

        if ( scope.object === undefined ) return;

        scope.object.updateMatrixWorld();
        worldPosition.setFromMatrixPosition( scope.object.matrixWorld );
        worldRotation.setFromRotationMatrix( tempMatrix.extractRotation( scope.object.matrixWorld ) );
        scope.object.box3.getSize(selectionBoxSize);
        scope.object.getWorldPosition(selectionBoxPos);

        camera.updateMatrixWorld();
        camPosition.setFromMatrixPosition( camera.matrixWorld );
        camRotation.setFromRotationMatrix( tempMatrix.extractRotation( camera.matrixWorld ) );

        **scaleT = selectionBoxSize;** 

         // For dynamic size change based on camera position
         //scaleT.x = worldPosition.distanceTo( camPosition ) / 6 * selectionBoxSize.x;
         //scaleT.y = worldPosition.distanceTo( camPosition ) / 6 * selectionBoxSize.y;
         //scaleT.z = worldPosition.distanceTo( camPosition ) / 6    * selectionBoxSize.z;

        this.position.copy( selectionBoxPos );

        this.scale.set( scaleT.x, scaleT.y, scaleT.z);
        this.updateMatrixWorld();

Console log output:

TransformControls.js:526 Vector3 {x: 10.020332336425781, y: 2.621583938598633, z: 3.503500819206238}

TransformControls.js:601 Vector3 {x: 10.020332336425781, y: 2.621583938598633, z: 3.503500819206238}

The scale remains the same, but the results are different. The images below illustrate this disparity:

https://i.sstatic.net/I2BbB.png https://i.sstatic.net/dSeFK.png

As shown in the images, the red box at the bottom represents the translation controller, which is smaller than the selection box. Additionally, the pivot points of my objects are at the bottom, and I aim to have this controller positioned at the center of the selection box, but using the getCenter method of box3 has not yielded the desired outcome.

I would greatly appreciate help and guidance. Please let me know if my explanation of the issue is unclear.

Answer №1

Incorrectly obtaining the bounding box of an object from its geometry can lead to inaccurate results since it doesn't consider the object's transform. It is recommended to utilize box3.setFromObject (yourObject) instead. Would this information be beneficial to you?

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

The local server for handling HTTP requests has ceased to operate

Recently, I set up the NPM package along with the http server within the "server" directory. Initially, everything was functioning smoothly; however, the server abruptly ceased operating. Upon attempting to launch the local http server, an error message a ...

When comparing the values of two arrays with undefined property values

Struggling with sorting an array of arrays that works perfectly except when the property value is undefined. Take this example: posts array = {id: "1", content: "test", "likes":[{"user_id":"2","user_name":"test"}] }, {id: "2", content: "test", "likes": ...

Navigating a controller variable to access a property of a service

I've been struggling to access a property of a service through a bound controller variable in a template. Controller: app.controller('VictoryController', function($scope, AlertStatisticsService) { $scope.AlertStats = AlertStatisticsSer ...

What is the procedure to change a matter body's isStatic property to false in matter.js upon pressing a key?

During my recent project, I encountered a challenge in trying to set the isStatic property of a Matter Body in matter.js to false when a key is pressed. if (keyIsPressed(UP_ARROW)) { this.body.isStatic(false) } Could you provide guidance on the correct ...

What is the best way to create separate arrays for every element in my object, containing a total of four arrays to efficiently produce a weekly forecast?

Hey there, I'm back with an update on my ongoing project. I've encountered a major challenge while trying to incorporate a 7-day forecast display in my app. Something along these lines: https://i.stack.imgur.com/xJA4M.png https://i.stack.imgur. ...

Access a portion of a file located on a distant server

Is it possible to read part of a remote file using Ajax without relying on server-side technologies like PHP? I believe the HTTP Range header could be utilized for this purpose, but how can we set it with Ajax? Can we even set HTTP headers in Ajax request ...

Struggling to accurately capture the values from checkboxes and dropdown selections to ensure the correct data is displayed. Assistance is needed in this

I am facing challenges in retrieving the accurate data for display from Mat-Select and Mat-Checkbox components. My goal is to capture the selected values from users and perform if-else statements to validate conditions, displaying the correct data view if ...

Unable to replicate the functionality of the tab key using jQuery for the enter key

How can I focus on the first input ('Qtd on the table') after pressing enter on the 'Buscar' input? I've tried various solutions like $(this).nextAll('input').first().focus(); $(this).next('input:text').focus ...

Steer clear of excessive stretching when using Three.js

I am working on wrapping a 3D model with textures in three js. However, I have noticed that in certain areas of the model, the image appears to be stretching even though the texture resolution is high. https://i.sstatic.net/9hcoA.png ...

Retrieve information from an express server using the fetch API

I am attempting to use the alert function to display a variable string in Express's .get() method and then send it using res. I want the alert to show "I am working fetch". This is my server.js var express = require('express'); var app = e ...

Tips for excluding files in a webpack configuration for a Vue application during the production build

I am attempting to remove an Html file named "dev.html" from the final product build. What configurations do I need to make in webpack for this? I understand that rules need to be applied, but where exactly do I need to configure them? Below is a snippe ...

Is this filter selector in jQuery correct?

It appears to function correctly, but I am unsure if there is room for improvement. I aim to target any HTML element with a class of edit-text-NUM or edit-html-NUM and adjust its color. This is the code snippet I am currently utilizing... jQuery(document ...

Explore button that gradually decreases max-height

I have a "Show More" button that expands a div by removing the css attribute max-height, but I want to add an animation similar to jQuery's slideToggle() function to smoothly reveal the rest of the content. This is the code I am using: <div id="P ...

The PHP function is failing to communicate with jQuery and Ajax

Having trouble with PHP return to jQuery/Ajax functionality, When I try to edit an item, the error message displays even though the success function is executed. On the other hand, when attempting to delete an item, nothing is displayed despite the succes ...

Determining the emptiness of an array in Postman using node.js

When I receive a response, it is in the following format: { "test1": [], "test2": [], "test3": [], "test4": null, "test5": [] } This is the response that I get after making a request. I need to verify whether test1 is empty or not. ...

Quick method to assign child iframe title as index.html <title>title</title>

Is there a simple method to dynamically update the <title> of my index.html based on the <title> of a child iframe? Take a look at my website for reference If the content within the iframe changes, will the title automatically update along wi ...

What is the best way to switch from using the three.js JSONLoader to the ObjectLoader?

I successfully loaded multiple files using the code below for three.js JSONLoader: <!DOCTYPE html> <html> <head> <title>Rotating Sphere</title> <meta charset="utf-8"> <!-- https://cdnjs.com/libra ...

The Vue app utilizing Flickr API encounters an issue: "Unable to assign value to 'data' property as it is undefined."

Beginning the development of a simple one-page photo stream app using the public Flickr stream, I've encountered an error in my code: 'Cannot set property 'data' of undefined'. Here is a snippet of my code: <b-container> ...

issue encountered while passing a callback in a res.render() function

Currently, I am working on a small application where I am fetching data from remote JSON files to generate statistics that will be displayed in an EJS file later. My objective is to pass separate values for rendering and then utilize them within the EJS d ...

Unable to store cookie using jQuery on Internet Explorer 9

Having trouble setting a cookie on IE9 and can't figure out why. My objective is to create a cookie that expires after a year, using the code below: $.cookie( name, value, { expires:days } ) where days equals 365. However, the cookie disappears as s ...