Zoom in on a cuboid using the three.js orthographic camera to achieve a unique perspective

In my latest project, I've created a straightforward three.js application that showcases a cube. The necessary files for this application include: index.html, viewer_style.css, and viewer.js.

The structure of the index.html file is as follows:

<!DOCTYPE html>
<html>
    <head>
        <meta charset=utf-8>
        <title>My first three.js app</title>
        <link rel='stylesheet' type='text/css' href='viewer_style.css'>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
        <script src="js/three.js"></script>
        <script src="js/controls/OrbitControls.js"></script>
        <script src="js/renderers/Projector.js"></script>
    </head>
    <body>
        <script src="viewer.js"></script>
    </body>
</html>

Furthermore, here is the content of the viewer.js file:

// Perform Scene Setup
var scene = new THREE.Scene();
...

// Display Cube with Desired Margin
...
if(aspect < 1){
    frustumHeight = 2 * bbox_radius;
}
else{
    frustumHeight = 2 * bbox_radius / aspect;
}
camera.left = - frustumHeight * aspect / 2;
...

// Implement Rendering Logic
...
function animate(){
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
}
...

<p>To illustrate how fitting the cube works, please refer to this image:<a href="https://i.sstatic.net/4OWlI.png" rel="nofollow noreferrer"></a></p>

<p>However, when modifying the camera position in <code>viewer.js to:

camera.position.x = 100;
camera.position.y = 100;
camera.position.z = 100;

We end up with a display like this:https://i.sstatic.net/E7k50.png

Unfortunately, the cube does not fit properly on the screen in this scenario. It seems like the issue lies in measuring the bounding sphere's radius within an incorrect reference system. Despite numerous attempts, I have yet to find a viable solution.

If anyone has insight into what might be amiss or could provide guidance on employing the correct approach, your input would be greatly appreciated. Thank you!

Answer №1

Your solution is accurate when you modify aspect < 1 to aspect > 1, as recommended. You can view the working example of your code in this jsfiddle link: https://jsfiddle.net/uniqueuser/example/

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

Extracting data from a selection list

I am currently tackling a project for my JavaScript class, where I need the user to input a name and select an option from a dropdown menu. They should then click a button to add this input/selection to a map and then click another button to display the ma ...

The appearance of the upload button in React using Material-UI seems off

After following the MUI document on React Button upload, I noticed that the UI results were different than expected. Instead of just showing the button UI, there was an additional UI element present. By adding the sx={{display:'none'}} property, ...

Proper technique for canceling event listeners using Next.js, React, and Flowplayer

Currently implementing TypeScript in a nextJS project and using a cdn version of flowplayer added via a custom hook. The component has a global scope variable called "video" which is loaded using the useScript hook from here. const status = useScript( ...

Automating user login with node.js and passport.js: A step-by-step guide

My login application is built using node.js and passport.js, with the session being maintained using express-session and connect-mongo. I need to ensure that users are redirected to the home page upon accessing the URL, only sending them to the login page ...

Invoking a class method in Javascriptcore on iOS

I'm currently trying to comprehend the inner workings of JavascriptCore. Initially, I attempted calling a single function. Now, my focus has shifted to invoking a function within a class. This is what my javascript code looks like: var sayHelloAlf ...

Utilize @db.Decimal within the Prisma framework for the parameters "s", "e", and "d"

When I define the schema in Prisma like this: value Decimal? @db.Decimal(7, 4) Why do I always get this format when retrieving the value from the database: "value": { "s": 1, "e": 0, & ...

What is the best method for obtaining a spring controller's object using AJAX?

Here is the AJAX call I am working with: var url = "UsersGroupReader.html?selectedCompanyName=" + selectedCompanyName + "&test="+Math.random(); req.onreadystatechange = processAccessGroupRequest; req.open("GET", url, true); req.send(null); function ...

retrieving XML information through DOM children elements

Suppose I have an XML object in the following format: <book id="01"> <author>Conner, Jim</author> <title>House Hunter</title> <genre>DIY</genre> <price>5.95</price> <publish_date>2000-1 ...

Using TypeScript to determine the week number - the value on the right side of the math operation must be of data type 'any'

I've spent a lot of time searching for code to help me calculate the week number in my Angular app according to ISO standards. It's been challenging to find JavaScript-specific code, but I believe I may have found something - however, I encounter ...

Why are my functions still being called asynchronously? I must be missing something

My task involves downloading 5 files exported from our school's database and running a query based on the first export. There will be queries for the other four files, and since there are three schools, my functions need to be scalable. I have two fu ...

TinyMCE - Utilizing selection.setContent along with getContent for the Warp Button

I am looking to implement a button that will wrap content with all tags. Snippet of Code: editor.addButton('MobileToggleArea', { text: '<M>', icon: false, onclick: function (){ editor.selection. ...

The visibility of the Google +1 button is lost during the partial postback process in ASP.NET

When trying to implement the Google Plus One button using AddThis on one of our localized pages, we encountered a strange issue. Despite retrieving data from the backend (let's assume a database), the plus button was not loading during an AJAX based p ...

Tips for avoiding the forward slash in a URL parameter

$.ajax({ url: "/api/v1/cases/annotations/" + case_id + "/" + encodeURIComponent(current_plink), When I use encodeURIComponent to escape slashes, it doesn't work as expected. The code converts the "slash" to "%2F", but Apache doesn't reco ...

AssertionError [ERR_ASSERTION]: The value of undefined is not equal to 390 in the GitLab

I'm a bit confused about the AssertionError [ERR_ASSERTION]: undefined == 390 in Gitlab. What I need is: The sumSalaries(obj) function, which should take an object obj as a parameter where the field names correspond to the employee's name and t ...

The forward button is malfunctioning after using history.pushState

I have managed to resolve issues with the back button, however, I am still unable to fix the forward button. Despite the URL changing, the page does not reload. Here is the code snippet that I am currently using: $('.anchor .wrapper').css({ &a ...

swap out a method within a publicly available npm module

Currently, I am using an API wrapper package that utilizes the request module for making API requests. While this setup works well in most cases, I am facing a situation where I need to use this package in a node-webkit environment and replace the request ...

beforeSend method in jquery ajax synchronously calling

One of my functions is called: function callAjax(url, data) { $.ajax( { url: url, // same domain data: data, cache: false, async: false, // use sync results beforeSend: function() { // show loading indicator }, ...

Encountering a JavaScript runtime error while trying to access and interpret JSON

Currently, I'm facing a challenge with converting a C# list of string types into a JSON object. The issue arises when trying to read this JSON object later in JavaScript. On the other hand, the process seems to work fine when dealing with a C# list of ...

Utilizing json_encode() while sending an array as an object

After sending an AJAX request with an ID, I am eagerly awaiting a response from PHP containing additional information. The response from PHP is structured like this: Array ( [success] => 1 [id] => 20 [fullname] => John Doe [status ...

Is my method of sending an email through angular correct?

I'm eager to expand my knowledge by learning something new. My goal is to utilize Angular and PHP to send an email. I am transforming my form data into an object and then attempting to pass this object as a variable to a PHP file. While I am successfu ...