THREE.js and TWEEN.js combine forces to achieve a seamless rotation of the camera

As I begin the program, my camera is focused on the point (0,0,0). The z-coordinate of the camera increases as I scroll downward. Once it reaches 160, I aim to rotate the camera towards the point (0,0,300) where an object is positioned. I initially tried using cameraLookAt(), but found the transition too sudden. Seeking a smoother effect, I delved into TWEEN.js for a solution. However, my implementation doesn't seem to be producing the desired outcome.

I could use some guidance on this issue.

  if (camera.position.z > 160) {

      var startRotation = new THREE.Euler().copy( camera.rotation );

      var endRotation = new THREE.Euler().copy( 0, 0, 300 );

      var tween = new TWEEN.Tween( startRotation ).to( { rotation: endRotation }, 2000 )

      tween.onUpdate(() => {
        camera.lookAt(startRotation)
      })

      tween.start()

    }

Answer №1

For this particular scenario, there is no requirement to rely on an animation library. Utilizing lookAt() will promptly adjust the camera to face a designated target. Nonetheless, you have the option to employ Quaternion.rotateTowards() for a gradual orientation adjustment. This function bears resemblance to Unity's Quaternion.RotateTowards(). An illustration showcasing this approach can be found here:

https://threejs.org/examples/webgl_math_orientation_transform

The concept involves determining a target orientation for the camera and subsequently employing rotateTowards() within the animation loop. The desired orientation is denoted by the variable endRotation in your application, represented as a quaternion. Experiment with it using:

const endRotation = new THREE.Quaternion().setFromEuler( new THREE.Euler( 0, 0, 300 ) );

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

What is the process of adjusting the anisotropy filtering value for textures in A-Frame?

Currently, I'm immersed in an A-Frame project where there are tiny textures located far away that are quite difficult to discern using the standard three.js texture anisotropy setting of 1. Does anyone know how to adjust this value on a texture within ...

The javascript code embedded within an AngularJS partial view is not functioning properly

To prevent loading all JavaScript in index.html, the focus should be on initializing scripts at the partial view level. For example, let's say there is a partial view with a date picker. The initialization of the date picker should happen within the ...

What specific flag should be included in Chrome settings to prevent displaying c:/fakepath?

Is there a way to disable this security feature? I'm seeking a solution in Chrome to obtain the full file path of an uploaded or viewed file, similar to how it can be done in Internet Explorer. Despite my efforts with flags like --allow-file-access-f ...

The texture filtering in Three.js PMREMGenerator is not properly set

In my AFrame scene in Three, I am dealing with environment mapping for lighting. When using a standard HDR cubemap, I encountered some challenges: The blurring based on roughness is correct due to mipmaps being generated and the minFilter set to LinearMip ...

Decoding JSON Array Containing Special Symbols

I am facing a challenge in parsing through an array key=>value pair that contains a .0. The JSON data I am trying to analyze is as follows: {message: "The given data was invalid.", errors: {…}} errors: step1.0: Array(1) 0: "Invalid respon ...

React application with a layout design that adjusts and responds to various

One task is to display various components based on the device's width; Two possible approaches have been considered: Create a React Component that accepts multiple components for different widths (sm, md, xl). It will automatically determine the de ...

Display the designated element upon clicking the designated link exclusively

I'm working with this specific HTML setup: <a href="#" class="dp">Click me</a> <div class="dp_div" style="display: none;"> this is the content within the div </div> My goal is to display the hidden div with a class of "dp_ ...

Retrieve the Query String Information from a Link and Generate a New Link in Another List

I am looking to extract information from a link in #list and then use that information to create a new link in #list3. The link I have is http://jsfiddle.net/4y5V6/24/. Is there a way to set it up so that when a link is clicked, it automatically gets added ...

JavaScript function to easily determine the precise position of an element across different web browsers

MY APOLOGIES - I made an incorrect assumption due to an error in another script. ** * but hey, it turns out this function is actually quite useful ;) Many thanks for the assistance. !~~ This is the function I have: function findPos(obj) { var c ...

What are the steps to incorporate a MenuItem within a div container instead of being a direct child of the Select component in Material-UI?

Embarking on my MUI journey, I am exploring how to utilize Select and MenuItem in tandem to create a checked dropdown functionality with the help of this reference link. My goal is to structure a header, body div for MenuItems, and a footer inside the sele ...

Tips for incorporating a JavaScript script into local HTML code

I am facing an issue with my code on jsfiddle. It works perfectly there, but when I try to run it locally, it doesn't seem to work. I have checked the code multiple times and even downloaded the jQuery file to link it, but still no luck. I feel like i ...

Tips for retaining the scroll position of a particular page after a postback

I am working on a grid to display data row by row. Each row is displayed using a user control. When I scroll to the bottom of the page and click on a row, it redirects me to a view page for that specific row. However, when I click on the back link, I would ...

Switching from PHP to jQuery or JavaScript can make for a

I've been attempting to convert this PHP code to jQuery or JavaScript without success. I'm still learning about jQuery and JavaScript Check out the original PHP code: <?php // Set timezone date_default_timezone_set('UTC'); ...

Creating a visual representation of data using Google Charts to display a stacked bar chart

I wrote a script to display a stacked Google chart using JSON data stored on the wwwroot directory - <html> <head> <title>DevOps Monitoring Application</title> <link rel="icon" type="image/png" hr ...

What might be causing the React useEffect hook to only trigger for the final element in the list instead of all elements?

I am currently developing a user interface in which elements should trigger custom animations when the user scrolls on the page. The structure involves a Parent component <Experiment /> with children components <Article />. To achieve this, I a ...

Three.js encountered an issue while compiling the fragment shader: The variable 'texture' was not found

Encountering a compiling issue with a fragment shader while working with Three.js. Below is the code I'm using: https://jsbin.com/cigadibeya/3/edit?html,js,output. I attempted to create an animation similar to this example: THREE.WebGLProgram: shader ...

Smooth transitions between points in animations using the D3 easing function

I'm working on an animated line chart that displays data related to play activities. I've been experimenting with changing the animation style between data points on the chart using d3.ease Currently, my code looks like this: path .attr("stro ...

Requiring a condition for each element in an array which is part of an object

Let's discuss a scenario where I have to decide whether to display a block based on a certain condition. Here is an example array structure: const data = [ { name: "item1" , values : [0,0,0,0,0]}, { name: "item2" , values : [0,0,0,0,0]}, { nam ...

Filtering Events using Radio Buttons in FullCalendar via JQuery

Currently, I am attempting to utilize a set of buttons in the JQUERY FullCalendar to filter events. My implementation successfully filters events based on the 'dog' or 'cat' category using a dropdown. However, I am struggling to achiev ...

My pathways are clearly mapped out, yet express is returning a frustrating 404 error

After exhausting all the similar posts on StackOverflow without finding a solution... Let's take a look at my app.js, which is the first file that the express library seeks when launching the app right after my server.js file responsible for setting ...