3D objects must always be positioned to perfectly fit within the window when using an Orthographic Camera

My goal is to ensure that my 3D objects always "fit" inside the window. Here is an overview of my current code:

export function onWindowResize(camera, renderer) {
  const canvas = renderer.domElement;
  const width = window.innerWidth;
  const height = window.innerHeight;
  const connection = getBoundingBox(connectionGroup);
  const needResize = canvas.width !== width || canvas.height !== height;
  const isPortrait = connection.width < connection.height;

  if (needResize) renderer.setSize(width, height);

  const aspect = isPortrait ? width / height : height / width;
  const frustumSize = Math.max(connection.width, connection.height);

  //Front View
  if (isPortrait) {
    camera.left = (-frustumSize * aspect) / 2;
    camera.right = (frustumSize * aspect) / 2;
    camera.top = frustumSize / 2;
    camera.bottom = -frustumSize / 2;
  } else {
    camera.left = -frustumSize / 2;
    camera.right = frustumSize / 2;
    camera.top = (frustumSize * aspect) / 2;
    camera.bottom = (-frustumSize * aspect) / 2;
  }

  camera.updateProjectionMatrix();
}

Currently, everything functions as intended: the object fits within the window based on its height or width. However, a problem arises when the object's height exceeds the window height but still fits within the maximum width (or vice versa), resulting in cropping of the model.

I am only looking to fit the object on the initial display, after which the user can freely utilize orbit controls.

MAX WIDTH

MAX HEIGHT

MAX WIDTH but models height exceed window/camera height

I have experimented with manually setting camera.zoom, but struggled to determine the correct value for it. This led to malfunctions in my orbit controls.

Answer №1

When dealing with calculations, it's important to consider a specific scenario where the aspect ratio of window dimensions (referred to as aspect) is actually smaller than the aspect ratio of connection dimensions (known as frustumAspect). In this case, the value of frustumSize needs to be adjusted to accommodate this difference, denoted as frustumScaled.

As an alternative approach, you have the option to simplify camera.left and camera.bottom by flipping their corresponding properties.

const frustumAspect = isPortrait ? (connection.width / connection.height) :
  (connection.height / connection.width);

const aspectScale = Math.max(frustumAspect / aspect, 1);
const frustumScaled = frustumSize * aspectScale;

//Adjusting for Front View
if (isPortrait) {
  camera.right = (frustumScaled * aspect) / 2;
  camera.top = frustumScaled / 2;
} else {
  camera.right = frustumScaled / 2;
  camera.top = (frustumScaled * aspect) / 2;
}
camera.left = -camera.right;
camera.bottom = -camera.top;

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

Buttons and links with intricate geometries

I am currently developing a web application using Java/JSF and I am interested in finding out about technologies that would allow me to define intricate shapes as clickable buttons or links. Right now, my only option is to split an image into smaller trans ...

What is the best way to create custom shapes using an array of points in the xyz coordinates system with THREE.JS?

For instance: I have the following points [1,0,2],[2,0,2],[3,2,5] I am looking to create a geometric shape using these points by connecting them. I attempted using THREE.Shape, however it only allows me to construct the shape on the x and y axis. Is there ...

What is the best way to monitor a document variable that is set after a component has been

Is there a way to access the variable document.googleAnalytics, added by Google Tag Manager, for A/B testing on a Vue.js page? I attempted to initialize the variable in both the mounted and created methods of the component, but it returns as undefined: ex ...

When working with Next.js, I encountered a scenario where the useEffect() function was being triggered twice. I attempted to solve this issue by providing an empty array as the second argument, but to no

When working with Next-JS, I encountered an issue where the useEffect() function was running twice. While I heard that using an empty array as the second argument can fix this in React, it did not work in my case within Next-JS (which is based on React). ...

Invoke a function while rendering or utilize a getter

Is it better to use a class method or a getter when a render method needs to return a calculated value? class User extends Component { getFullName () { const { fname, lname } = this.props return `${lname}, ${fname}` } render () { return ...

What is the proper way to update a dropdown value in a React JS component?

Can you please guide me on how to assign a value in a dropdown in react js? I am retrieving the dropdown data after a delay of 3000 milliseconds and then I need to set a value in the dropdown. const App = ({ children }) => { const val = "ax"; const ...

What is the best method for fetching data using jQuery to be used in a flot pie chart?

I am in the process of fetching data for a flot pie chart and want to ensure that I am doing it correctly. Any input would be greatly appreciated. I will also include the JSON data that is being returned so that I can confirm it adheres to the correct synt ...

Text color animation effect for menu

Here is the layout of my menu: <nav> <a href="#"> <span class="black">Home</span> <span class="red active">Home</span> </a> <a href="#"> <span class="black">Longer m ...

Angular identifies when a user navigates away from a page

Currently, I am utilizing Angular 1.5 along with ui-router. My goal is to identify when a user exits a route. The code snippet I have at the moment looks like this: $scope.$on("$stateChangeSuccess", function () { if (!$scope.flag) { //... ...

Press Button to create cookie and store it in Drupal 7

I am currently working on my Drupal 7 local website, which features an article and a popup on the homepage leading to that article. Within the article, there is a button that I want to serve as a way for users to dismiss the initial popup permanently. My i ...

Node.js express version 4.13.3 is experiencing an issue where the serveStatic method is not properly serving mp3 or

I am currently utilizing Express 4.13.3 along with the serve-static npm module to serve static assets successfully, except for files with mp3 or ogg extensions. Despite reviewing the documentation, I have not come across any information indicating that thi ...

Capturing mouse coordinates from hover events in Highcharts Gantt using the highcharts-react library

Currently, I am utilizing the official React highcharts wrapper to create a Gantt chart. My goal is to obtain precise mouse coordinates from a mouse-over event and utilize them for a custom tooltip. For instance: https://stackblitz.com/edit/react-c5mivs ...

Guide on incorporating dxTreeView with AngularJS

Hello there, I am trying to utilize the dx-tree-view component. In my Home.html file, I have included the following HTML code: <div dx-tree-view="treeViewOptions"></div> And in my HomeController.js: $scope.treeViewOptions = { binding ...

Why is the text returned by the Angular Response body missing the JSON brackets? Strange, isn't it

As a newcomer to Angular 2, I should mention that some information is internal and will be replaced with placeholders when needed. My current task involves making a simple post request and retrieving the contents of the request body. Below is my existing ...

Tips on developing a limited directive?

Currently, I am experimenting with AngularJS and NG-Table and facing a challenge that I cannot seem to resolve: My issue involves displaying a collection of User objects from a Django application in an NG-Table. One of the attributes of the model is a boo ...

What is the best way to set up configurations for node modules?

I have a module called mymodule which includes configuration options such as foo, bar, and two functions named f1 and f2. My goal is to initialize this module with default values in one client application, while passing different defaults in another clien ...

What is the best way to transfer an object property to an event handler function for executing DOM manipulation tasks?

I am working on a React-rendered HTML page that displays a list of objects representing websites. I have successfully stored these objects in memory and can show them in a long list on the page without any issues. Recently, I added a button for each objec ...

Determine the possible width of a concealed element

I am currently customizing the lavalamp plugin to be compatible with dropdown menus, but I have come across a minor issue. I am trying to determine the offsetWidth of a hidden element. Obviously, this question is a bit nonsensical. What I actually need is ...

What is the best way to declare a TypeScript type with a repetitive structure?

My data type is structured in the following format: type Location=`${number},${number};${number},${number};...` I am wondering if there is a utility type similar to Repeat<T> that can simplify this for me. For example, could I achieve the same resul ...

Is the use of div:after content really affecting the width? I am having trouble getting my transition to work

Here is a simple code snippet that represents my actual code: #myDiv { background: black; color:white; float:left; min-width:45px; max-width:450px; -webkit-transition: all 1s ease-in-out; transition: all 1s ease-in-out; } #myDiv:hover:after { width ...