What is the best way to find the dimensions of an unrotated Object3D?

To calculate the height of an Object3D, I use the following method:

let obj = ... ; // An instance of Object3D, such as Mesh or Group
let boundingBox = new THREE.Box3().setFromObject(obj);
let height = Math.abs(boundingBox.min.y - boundingBox.max.y);

However, when the obj is rotated on the X and/or Z axis, the difference between boundingBox.min.y and boundingBox.max.y changes, leading to a different height compared to when it's not rotated.

I aim to determine the height of obj as if there was no rotation involved. How can I achieve this?

My assumption is that I must adjust the dimensions of boundingBox based on the rotation angles, but I'm uncertain about the exact process.


Before Rotation:

After Rotation:

(red = obj, blue = boundingBox)

Answer №1

When using THREE.Box3().setFromObject(obj)
, you will receive the "world-axis-aligned bounding box" of the object, which computes the world-coordinates including rotation, position, and scale of the object along with its parents for all vertices.

If you are interested in obtaining the bounding-box of the geometry without considering the object's position, rotation, and scale, simply utilize obj.geometry.boundingBox after executing computeBoundingBox():


obj.geometry.computeBoundingBox(); 
let boundingBox = obj.geometry.boundingBox;

To calculate an aggregated bounding box for object hierarchies, you can employ the following method:


function getCombinedBoundingBox(object) {
  const result = new THREE.Box();
  object.traverse(child => {
    // exclude elements without geometry
    if (!child.geometry) { return; }

    child.geometry.computeBoundingBox();
    result.union(child.geometry.boundingBox);
  });

  return result;
}

Keep in mind that this approach only functions properly when the child-objects have not been transformed in any way.

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

Retrieve all populated fields on the second tier using Node.js and Mongoose

Do you have any insights to share on Node.js and mongoose? I've defined a mongoose schema and when I use findOne(), it returns a document with multiple elements under the "resource" key. Below is an example of how the document looks like: { "met ...

What is the best way to ensure that jQuery AJAX calls are executed in a specific

I am currently dealing with an input field that triggers an ajax call to process.php every time the text in the input field changes. The challenge I'm facing is that the responses from the server can arrive out of order due to varying processing time ...

What is the technique to make a *ngFor render items in a random order?

I'm working on creating an application that needs to display elements in a random order. However, due to restrictions within the application, I am unable to modify the ngFor directive. How can I achieve displaying ngFor content randomly? ...

How to create a custom hover effect for IconButtons in Material-UI

Customizing Hover Effects on IconButton I am currently using an IconButton component from Material-UI and have noticed a subtle grey border that appears when hovering over the icon. I am looking for a way to disable this hover effect, as I cannot seem to ...

Is there a way to repurpose a function to work with both ids and classes?

My current code is affecting all elements instead of just the intended one. I've experimented with classes and ids, ruling out those as potential issues. I'm hoping for my JavaScript to target only the selected element, not all of them. Check ou ...

Achieve accurate mouseover interaction in a ThreeJS VueJS application by dynamically altering the viewport

I managed to accomplish an amazing feat: I designed a menu consisting of 4 unique shapes. As you hover over each shape, it undergoes a color change, grows in size, and shifts the other shapes while slowing down its rotation. To achieve this, I delved into ...

Attempting to retrieve all elements... held within a parent object in JavaScript

Currently diving into JS, Jest testing, and React - quite the ride! { '1': { accountName: 'Dance Party', balance: 200, key: 1 }, '2': { accountName: 'Whiskey Party', balance: 69, key: 2 }, '3& ...

The argument type does not match the parameter type partial<>

While attempting to validate my Ionic React form, I encountered an error when calling the validationSchema within the useForm method. The specific error message received is as follows: Argument of type '{ validationSchema: ......' is not assignab ...

A proven method for distinguishing between desktop and mobile browsers

Similar Question: Exploring Browser Detection Methods in Javascript I am interested in finding an efficient way to differentiate between desktop and mobile browsers, either using JavaScript or PHP. if (desktop browser) { do x; } else { // mobi ...

Is Angular Module Lazy Loading functioning properly in Chrome?

Is there a way to verify if the JavaScript files are lazy loaded for the currently opened module using Chrome developer tools? ...

Insert the ng-if directive into an element using a directive

I am working on an AngularJS directive that involves looking up col-width, hide-state, and order properties for a flexbox element based on its ID. I want to dynamically add an ng-if=false attribute to the element if its hide-state is true. Is there a way ...

I am having trouble getting my website to work offline using caching

My cache file is displayed below: CACHE MANIFEST # 2013-11-22 14:38:54735779 CACHE: ../../../../assets/img/background_01.jpg ../../../../assets/img/background_02.jpg ../../../../assets/img/background_03.jpg ../../../../assets/img/datepicker_icon.png .. ...

Experiencing issues with Panolens.js failing to load images

Currently, I am in the process of setting up a basic 3D image viewer using Panolens.js. Despite following the example provided in the documentation, I am encountering console errors during the loading process. This is my initial attempt at working with equ ...

Exploring the inner workings of this JavaScript jQuery function within a snippet of code

My knowledge of JavaScript is limited, so I'm struggling to grasp the concept behind this code snippet: 1 (function () { 2 $(function () { 3 //Global ajax progress dialog box 4 //Simply run $("#ajax-progress-d ...

The reduce function is displaying an undefined result

Check out this code snippet: const filterByType = (target , ...element) => { return element.reduce((start, next) =>{ if(typeof next === target){ start.push(next) } } , []) } I'm trying to achieve a specific g ...

Changing the state within a slice by utilizing an object as the payload

Here is the code snippet for a standard boilerplate slice: import { createSlice, PayloadAction } from '@reduxjs/toolkit'; import { IAuthState } from '../../types'; const initialState: IAuthState = { isAuthenticated: false, profile: ...

Including Fullcalendar functionality on a Wordpress site

I'm attempting to incorporate FullCalendar into a page on my WordPress website using the native page editor. Below is the code snippet: <code> <!DOCTYPE html> <html> <head> <link href='../fullcalendar-2.0.2/fullcalend ...

How to Exclude Box Geometry from Merged Geometry in THREE JS

I have over 100,000 boxes that I've added to a merged geometry. Occasionally, I need to remove some geometries from this merged structure. Is it possible for me to iterate through the position attributes in increments of either 108 or 72 vertices per ...

Is it possible for me to input text into html while the page is loading?

Is there a way to preload external files while a web page is loading? I'm looking to incorporate a JavaScript templating engine into my website and prefer to store my templates in separate files. Instead of loading these templates asynchronously, I&ap ...

Understanding Express.js API Function Parameters

The API documentation for Express mentions that the format for a function is "app.use([path,] function [, function...])". However, in the app.js file generated by the Express generator, there is a line of code like this: "app.use('/', routes);", ...