Enhance your camera control experience with seamless transitions on Three.js

I've been developing a website that allows users to explore celestial objects in the sky using VRControls and focus on their favorites with Trackball controls, which are activated by clicking on the object. Check out a demo of the project here: https://jsfiddle.net/tushuhei/4f1Lum5n/6/

function smoothTransition (obj, target) {
  var dummyCamera = camera.clone();
  controls = new THREE.TrackballControls(dummyCamera);
  controls.target.set(obj.point.x, obj.point.y, obj.point.z);
  new TWEEN.Tween(camera.position)
    .to(target, 1000)
    .onComplete(function() {
      transitioning = false;
      controls.dispose();
      controls = new THREE.TrackballControls(camera);
      controls.target.set(obj.point.x, obj.point.y, obj.point.z);
  }).start();
}

While TWEEN effectively handles transitions between WebVR and Trackball modes, I'm encountering a slight gap at the end of the transition. This discrepancy seems to stem from fluctuations in the camera's rotation during the completion phase.

Does anyone know of a method to ensure a seamless transition between two different camera controls, taking into account both position and rotation?

Thanks,

Answer №1

Your approach with dummyCamera was on the right path. To achieve the desired effect, you need to obtain the final quaternion and interpolate between the initial and final quaternions while the tween handles the position transition.

// Store the initial quaternion for slerp interpolation.
var startQuaternion = camera.quaternion.clone();

// Create a cloned dummy camera for applying tracking controls
// to get the final quaternion.
var dummyCamera = camera.clone();
dummyCamera.position.set(target.x, target.y, target.z);
var dummyControls = new THREE.TrackballControls(dummyCamera);
dummyControls.target.set(obj.point.x, obj.point.y, obj.point.z);
dummyControls.update();

// Disable VR controls to avoid conflict with camera control 
// during tween animation.
controls.dispose();
controls = null;

new TWEEN.Tween(camera.position)
.to(target, 1000)
.onUpdate(function (timestamp){
  // Interpolate the camera quaternion using slerp. 
  // timestamp represents the eased time value from the tween.
  THREE.Quaternion.slerp(
    startQuaternion, dummyCamera.quaternion, camera.quaternion, timestamp);
})
.onComplete(function() {
  controls = new THREE.TrackballControls(camera);
  controls.target.set(obj.point.x, obj.point.y, obj.point.z);
}).start();

https://jsfiddle.net/4f1Lum5n/10/

It's worth noting that your current implementation may cause discomfort for VR users due to loss of control over their point of view. Consider placing the user on a ship or platform and animating the platform instead to allow continuous camera control by the user.

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

A Guide to Dynamically Updating Page Titles Using JavaScript in a Ruby on Rails Application

Every time I use Ajax to load a blog post on the webpage, I adjust the <title> to "My Blog - BLOGPOST_TITLE". It is worth mentioning that "My Blog - " also shows up in the application layout. My dilemma is, how do I inform my Javascript about the " ...

Update a specific element within Angular framework

Just starting out with angular and facing a seemingly simple issue that I can't seem to solve despite trying various solutions found on SO. I have created a login component where upon submission, the user is redirected to their profile page. While I a ...

Is there a way to specifically execute a Mongoose validate function solely for the create user page and not the edit user page?

Exploring Different Tools In the process of developing a website using Node.js, Express, and MongoDB. Leveraging mongoose for interacting with the MongoDB server has been quite beneficial. However, I encountered an issue where a function within my Mongo ...

Having issues with Phonegap's getPhoto functionality

Even though the documentation here seems to be effective, I encountered an issue when implementing the code into my application. The getPhoto function returns 'content://media/external/images/media/16053' without loading the image into the img el ...

The functionality of my LinkButton activates a code sequence only when clicked twice during its Click event

Protected Sub lnkContractors_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lnkContractors.Click If Context.User.IsInRole("HOD") Then lnkContractors.OnClientClick = "PopupCenter('Juniors.aspx', &apo ...

What is the best way to write an SQL query to safely insert a record into a table with a dynamic name?

I'm working on a function that can insert a record into a table in PostgreSQL. The catch is that the table name needs to be a parameter for the function, and the column names are determined dynamically. To ensure protection against SQL Injection, I am ...

Sorting elements to the beginning of each nested array

Looking to rearrange the elements in the given nested array, moving the element with the id 'cat_spc_my_special_id' to the top of the list using the keyword 'special'. There are 4 items currently in the above array, and the goal is to ...

The validation process is malfunctioning

Whenever I attempt to submit without filling in the correct information, there should be an alert message displayed for each section, and the text should be highlighted in red. However, it seems to be linking directly to the linked file instead. Any assist ...

Methods for retrieving a file within a nodejs project from another file

Currently, my project has the following structure and I am facing an issue while trying to access db.js from CategoryController.js. https://i.sstatic.net/8Yhaw.png The code snippet I have used is as follows: var db = require('./../routes/db'); ...

Setting a value for the identifier just one time

I've been grappling with an issue that's been on my mind for some time now. There are 14 divs on my webpage, and I need each one to be given a random ID (ranging from 1 to 14) every time the page loads. These divs all share the class ".image-box ...

When removing a react component from the list, it results in the deletion of all other components within the same

Whenever I click a button, a new component is added to my list of components. Each component in the list has a delete button, which can remove the component from the list. https://i.sstatic.net/uH42y.png Each component, consisting of the text "New Item" ...

Find the frequency of a specific string within a JSON array

My current task involves working with a stringified array: JSON.stringify(arr) = [{"x":9.308,"y":6.576,"color":"yellow","restitution":0.2,"type":"static","radius":1,"shape":"square","width":0.25,"height":0.25},{"x":9.42,"y":7.488,"color":"yellow","resti ...

Local error when attempting to export a node module

As a novice in node.js, I'm looking to parse an XML file into JSON. To achieve this, I decided to use the bluebutton library available at https://github.com/blue-button/bluebutton.js. After installing the module using npm install bluebutton, a node_m ...

Updating Material-UI DataGrid Component with Fresh State Information

Hey there, We've integrated Material-UI into our project to build a DataGrid table of users where we can delete selected entries (checkbox) using a button. Whenever the button is clicked, it triggers an update in the object's state (removing th ...

Having trouble locating the CSS and JavaScript files within my Spring Maven project

I have a Spring framework application with a Maven project. In addition, I have CSS and JavaScript files under the WEB-INF folder. When attempting to link the JavaScript and CSS files like this: <link rel="stylesheet" href="../allDesign/js/vendor/anims ...

While conducting tests on a Vue single file component, Jest came across an unforeseen token

I need help with setting up unit tests for my Vue application that uses single file components. I've been trying to use Jest as mentioned in this guide, but encountered an error "Jest encountered an unexpected token" along with the details below: /so ...

How can one efficiently manage a large number of images on a website in a clever way?

I have a straightforward webpage with a large number of images. Just like this... <img src="img/photo00001.jpg"> <img src="img/photo00002.jpg"> <img src="img/photo00003.jpg"> ... and so forth I don't want to waste hours copying and ...

What is the best way to retrieve a Promise from a store.dispatch within Redux-saga in order to wait for it to resolve before rendering in SSR?

I have been experimenting with React SSR using Redux and Redux-saga. While I have managed to get the Client Rendering to work, the server store does not seem to receive the data or wait for the data before rendering the HTML. server.js ...

Mobile Size Setting

Could someone please explain to me why when I test this code on Google Chrome using the mobile emulator for Galaxy S3, it displays the correct size (640x360), but when I try to load it on my actual Galaxy S5 or any other device, the size is different from ...

How does a web worker behave when the owner is not actively engaged?

[edit: I am using Chrome 49.0.2623.112 m (64-bit) with default settings and HTTPS, but the issue occurred before the switch.] My application utilizes web workers to handle AJAX polling. While this may not provide a significant performance boost, it does e ...