Adjust the object size to match the Viewport dimensions

I've been developing a VR world where the camera can be turned and tilted in the center but not moved around freely.

Within this virtual reality environment, you have the ability to attach a video to the background. Users can either play it by clicking on it or hovering the crosshair over the element.

The video has two states: a "normal" state and a "zoomed" state, each with their own unique appearance as seen here: Example normal and Example zoom

In the zoomed state, I want to ensure that the video does not extend beyond the current viewport size. I already know the width and height of the video. How can I calculate the correct sizing or depth to make sure it fits within the viewport?

I'm exploring both options of scaling the object on its current position or moving it closer to the camera. Any advice or suggestions are welcome.

I've attempted using the approach mentioned in this discussion thread: Link

let vFOV = this._engine.getCurrentCamera().fov * Math.PI / 180;
let ratio = 2 * Math.tan(vFOV / 2);
let screen = ratio * (window.innerWidth / window.innerHeight);
let size = this._contentMesh.geometry.boundingBox.max.y;

return (size/screen) / 4;

However, the result from the example above places the object too close to the camera. Is there something I might be missing or overlooking?

Thank you for any assistance you can provide!

Answer №1

When determining the appropriate Z position for an object to fit the screen, you can use the following formula:
let vFOV = this._engine.getCurrentCamera().fov * Math.PI / 180;
let ratio = 2 * Math.tan( vFOV / 2 );
let size = this._contentMesh.geometry.boundingBox.max.y * 2;
return size / ratio;

It is important to note that the width and height of the Viewport are not necessary for this calculation.

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

Changing from using GET to employing POST

My current Ajax request function is as follows: // JavaScript function myFunc(pid) { $.ajax({ type : "GET", url : "testback.php", contentType : "application/json; charset=utf-8", dataType : "json", data : { ...

Creating gateways using ThreeJS

While experimenting with ThreeJS, I came across a helpful page on making portals using OpenGL called Mini-Portals. I decided to try replicating the portal view function in ThreeJS. Here is my current result: https://i.sstatic.net/GjDwt.gif On the left po ...

Converting JavaScript variable values to PHP variables by extracting the content of a textarea

Currently, I'm developing a data filtering mask which triggers when the button <input type="button" /> is clicked. In this process, I have: School classes (1, 2, 3, 4, 5) Sections (A, B, C....Z) Checkboxes for male and female sexes. This ma ...

Issue with Angular 2 - Struggling with Routing

My objective is to create a menu with four links, each leading to a different HTML page: <p> <a routerLink="/function">Business Function</a> <a routerLink="/process">Business Process</a> <a routerLink="/appsystem"> ...

Transferring data between modules in nodejs

Within my custom module, there is a method designed to query the database and check if a given username exists. I need certain values to be returned in order to determine the query result at a higher level. var findUserbyUsername=function(username) { ...

Steps to prevent submission of input field until all necessary fields and checkboxes are filled, disabled the submit button

Check out my basic web application on this sandbox link: codesandbox.io/s/eager-kalam-v1lpg I need assistance with how to prevent the submit button from being enabled until all required fields and checkboxes are filled in. I am fairly new to working with ...

Ways to create a noticeable effect on an image button when hovering without causing a shift in the position

Check out my simple script here: http://jsfiddle.net/PA9Sf/ I am looking to make a specific button stand out when hovered over without affecting the position of other buttons on the page. The current implementation in the provided jsfiddle moves the butto ...

I currently have an array of strings and wish to print only the lines that include a specific substring

Here i want to showcase lines that contain the following strings: Object.< anonymous > These are multiple lines: Discover those lines that have the substring Object . < anonymous > Error: ER_ACCESS_DENIED_ERROR: Access denied for user ' ...

Looking to confirm client-side text in NodeJS?

As I work on constructing a to-do list, one challenge I am encountering is confirming that the correct task has been checked off. While considering using unique IDs for each individual task may seem like a solution, there is still the risk of users manipul ...

What could be causing the User object in the auth0/nextjs useUser hook to have missing properties?

The user object retrieved using the useUser hook from auth0/nextjs package seems to be missing some important properties: { "nickname": "example", "name": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedbc6dfd3ced2dbfec7 ...

Adaptive Website displayed within an iframe

Currently, I have a responsive website that can be viewed here. The main objective is to embed this site into an iframe while maintaining its responsiveness. I attempted embedding my site in JSFiddle for testing purposes, which you can see here. However ...

Avoid running multiple YouTube views simultaneously within an AngularJS application

Currently, I have an Angularjs application that displays a list of Youtube videos utilizing the videogular node module. An issue has arisen where users can play multiple Youtube videos simultaneously, leading to potential violations of Youtube's poli ...

Using Javascript to group elements by JSON data

I have an array of JSON objects and I'm attempting to organize it by a specific element. I came across some code example on grouping JSON format from this link const groupBy = prop => data => { return data.reduce((dict, item) => { ...

Errors have popped up unexpectedly in every test file after importing the store into a particular file

Using Jest and Enzyme to test a React application has been successful, but encountering failures when importing redux store in a utility file. The majority of tests fail with the following error: FAIL app/containers/Login/LoginContainer.test.js ● Te ...

Modify three.js material without obscuring the object

How can I update a three.js material without making the object it is applied to disappear? The only workaround I've found is to clone both the object and the material, make changes to the cloned material using material.needsUpdate = true, and then re ...

Error encountered while trying to authenticate user through post request

I have written a post route request function below to handle user login. However, I keep encountering 401 unauthorized errors when making the request. Can anyone suggest any modifications or refactorings that could potentially fix this issue? Thank you i ...

The functionality of the delete button in Datatables is only operational on the initial page, failing to

My datatable is giving me trouble. The delete button in the action column only works on the first page, but not on the other pages. Here is the code for my delete button: <table id="example" class="table table-striped table-bordered" cellspacing="0" wi ...

Having trouble customizing the HTML range input?

I am looking to customize the appearance of a range input slider. Specifically, I want the slider to be green for the lower portion (where the thumb has moved) and grey for the remaining section. I have successfully changed the default styles from blue and ...

Troubleshooting issues with debouncing a Redux dispatch and encountering unexpected results

I've been attempting to implement debounce for an API action that involves a dispatch call to a reducer. The goal is for the API call in the browser to debounce after a specific delay, but instead, it's resulting in multiple API calls after the d ...

Seeking out a particular key within a JSON object and then finding a match based on the id within that key's array - how can it be

Recently, I've been exploring JavaScript and encountering challenges when trying to apply array methods on objects. For instance, I received a fetch response and my goal is to extract the 'entries' key and then utilize the native Array.find( ...