How do I determine the dimensions of an object in Three.js?

Seeking advice on determining the size of a Three.js object. I am currently loading various objects from files and wish to find a way to automatically adjust scale or camera position to ensure the entire object fits within the frame. Since different files will be loaded, detecting the size is crucial:

Here's a snippet of code for context:

    var group;

    var loader = new THREE.STLLoader();
    var group = new THREE.Object3D();
    loader.load("stl_file.stl", function (geometry) {
        console.log(geometry);
        var mat = new THREE.MeshLambertMaterial({color: 0x999999});
        group = new THREE.Mesh(geometry, mat);

        group.scale.set(0.02, 0.02, 0.02);
        scene.add(group); 

    });

I'm looking for a solution within this code that could help determine the object's size before setting the scale. Alternatively, I can adjust the camera position in a subsequent part of the script. What are your thoughts?

Jay

Answer №1

There are two methods you can use:

geometry.calculateBoundingBox();
var box = geometry.boundingBox;

or

var box = new THREE.Box3().setFromObject( object );

The latter is recommended for objects with child elements.

Using three.js version r.64

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

It is not possible to assign values to a typed array buffer

Why is the value not being assigned as anticipated without any errors being thrown? const fs = require('fs'); let file = "data.dat"; let readStream = fs.createReadStream(file,{highWaterMark:1024*1024*128}) let stats = fs.statSync(file); let stre ...

Ensure that the setTimeout() function continues to function even after closing and reopening an application

I'm currently working on a Discord.js bot that includes a banning command. The challenge I'm facing is that when I restart the bot, all the setTimeouts get reset. For example, if I ban someone for a week and then restart the bot during that time ...

Sending state properties to components within a route

In my React structure, I have the following setup: <Provider store={ store }> <Router> <Switch> <Route path="/how-to" component={ Help } /> <Route path="/start" c ...

Unload Aframe assets

In my aframe animation, I have incorporated numerous external png files that are loaded sequentially. While this works smoothly on desktops, it causes smartphones with limited RAM to crash. Is there a feature similar to "asset unload" in aframe like the o ...

What is the best way to activate a JavaScript function once a page has finished loading?

Hey there! I have a webpage with 2 text input fields and a DIV element. The first input field is for user input, while the second one is readonly. When the user hits Enter in the first input field, a new page loads into the DIV based on the query result f ...

AngularJS checkbox feature to tally the number of items that are currently selected or unselected

How can I use angularjs to count the number of selected and unselected checkbox items? My HTML <label class="col-xs-5 pull-left" style="font-weight: bold; margin-left: 4px; margin-top: -17px;" >You have selected <font size="3" color="green" ...

Eclipse Content Assist feature appears to be malfunctioning as it fails to display

Having an issue with Eclipse on Win 10 (64bit) when working with JavaScript projects. I've tested this problem on both Eclipse Photon and version 2018-09 (64 bit versions) and encountered the same problem on both instances: After creating a new JavaS ...

Invoke a Node.js script from a Spring Boot application to pass a Java object to the script. The script will modify the object and then return it back to the originating class

class Services { Address address = new Address(....); /* Invoke NodeJs script and pass address object In the js script modify address object var address = getAddress() Modify address object Return address obj ...

JavaScript for loop similar to Python'sIn JavaScript, the

As someone who is new to programming, I primarily use Python but have now encountered a situation where I need to work with JavaScript for a project utilizing Phonegap. The query retrieved from the server looks like this: [["CompanyName", "lat", "long", ...

When using Selenium WebDriver in Java, we noticed that despite initially failing with JavascriptExecutor, the element click method with WebElement performed successfully

Within the code snippet below, it is evident that using the WebElement.click() method successfully triggers an element, while the JavascriptExecutor.executeScript method encounters issues (although it works in most cases). WebElement e = driver.findElemen ...

Adjust the camera's focal point in threejs once the objects have finished loading

After loading multiple gltf files, I am adjusting their names and attempting to reposition the camera so that it aligns with the centroid of the new objects and captures the entire scene within its view. However, the centering process does not always yiel ...

Do not delete blank notes upon saving

Issue: When multiple notes are created simultaneously, and the same text is entered in the first note and saved, the blank remaining notes are not removed upon page reload or refresh. How can I ensure that empty notes are deleted and only notes with conten ...

Is there a way to invoke a method on an object stored in an array index?

Attempting to generate infowindows for markers within a Google Map, I utilized an array to create "disposable objects" within a for loop. Unfortunately, my approach is not yielding the desired results. Clicking on the markers has no effect, and upon checki ...

Looking for a way to access the source code of a QML method in C++?

I'm currently working on serializing objects to QML and I am looking for a way to retrieve the source code of functions defined within a QML object. Let's consider the following example in QML (test.qml): import QtQml 2.2 QtObject { functio ...

The lightbox fails to display in IE when a synchronous ajax request is triggered

I have a piece of code that displays a lightbox with the message 'Please Wait', followed by a synchronous ajax request that removes the lightbox once it's completed. This setup works perfectly in most browsers, but in Internet Explorer, the ...

JavaScript - overriding or halting the execution of code

I'm currently working on a project for a client where I need to incorporate their header and footer, along with some essential JavaScript files. The issue I've run into is that one of the core JS files they provided is poorly written - it fails t ...

employing async/await in the function of a backend API

I'm facing an issue with the following code snippet: service.js module.exports = { getUser }; async function getUser({ data }) { return new Promise((resolve, reject) => { const id = data["id"]; const doc = await db.colle ...

"Cross-origin resource sharing problem while working with NodeJs, Express, and React on

Currently, I am working on a small project where I am using NodeJs and Express for the backend and React for the client side. In order to tackle CORS policy issues, I have implemented the "cors" npm package on the server side, but unfortunately, it doesn& ...

What could be causing this JS file to not impact the DOM as expected?

Currently, I am delving into the world of JavaScript and trying to gain a better understanding of how to manipulate the DOM on my own. Throughout this learning process, I have encountered a puzzling situation that I am seeking assistance with. The followi ...

Modifying the appearance of Bootstrap button colors

I recently came across this code on the Bootstrap website. I am curious about how to change the text color from blue to black in this specific code snippet. Currently, the text is appearing as blue by default. For reference and a live example, you can vis ...