mapbox / mapbox-gl-js

Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
https://docs.mapbox.com/mapbox-gl-js/
Other
11.19k stars 2.22k forks source link

Camera animation #10875

Open marcibk opened 3 years ago

marcibk commented 3 years ago

mapbox-gl-js version: 2.3.1

Question

Hi there :). I am using terrain 3d and have a route animation. I want to achieve sth. like https://www.relive.cc/view/vAOZDD5poqL with the camera. Tried a lot with the free camera path example but I don´t want to follow the route 1:1 and have the "laggy" follow animation. I tried with turf.simplify but it´s still following always over the track. I want to have a follow animation like in this video. Can you give me some hints on how to achieve this? Thank you so much for your help! Kind regards

Links to related documentation

https://docs.mapbox.com/mapbox-gl-js/example/free-camera-path/

karimnaaji commented 3 years ago

Hey @traingethermarc , this tool might not exactly be what you're looking for but have you tried using https://developmentseed.org/gl-director/?

Concerning having an API for simplifying camera animation, we've been considering adding ways to facilitate path following so any early feedback on what you're trying to achieve or how you would use it would be great.

I consider that they are still two difficult things with the current free camera API, what we'd like to facilitate is for a given path:

A challenge of (1), and possibly in the video that you mentioned above, is finding the sweet spot between a camera path crafted manually (e.g. editing each keyframe location for where the camera should be placed) compared to one generated by code while still having something that looks natural.

An API of that sort could allow as input:

marcibk commented 3 years ago

Hi @karimnaaji - thanks for your reply! This tool is super nice, but the route is always different in my case, so it´s hard to adapt a similar pattern. In my opinion it´s a math/logic problem. There are 2 approaches I guess:

  1. Follow the track (smooth) from above, with a high camera looking directly at a location. - but this looks pretty static/bad and is not nice to look at, especially with dynamic routes, this can lead to problems pretty quickly.
  2. This is the approach I would like to go for: Calculate a camera path next to the track with turning camera etc. (like in my reference) I tried it with a bbox already, but it´s still hard to have a good looking result (or even WORKING result) for every track, because sometimes the camera is stuck in the mountains or just looks super bad (because the track is not visible). Just trying to find some ideas on how to achieve this.
rreusser commented 3 years ago

I've only so far tested it in 2D, but I implemented a proof of concept for a sort of camera controller. The key feature of this controller is that it's a PI controller (proportional, integral), which makes it not deterministic in the same sense that it may not always yield the exact same final state, depending on the particular timing between frames. By sacrificing this, I think we'd gain a huge amount of flexibility, power, and value. Its inputs are lookAt point, camera pitch, bearing, and distance (in units of km). You provide them as often as you want, and it moves the camera smoothly to attain them (approximately as they animate; exactly once steady state is achieved).

Proportional means it moves the camera and look at point proportional to the distance between the current and target state.

Integral means that it integrates the accumulated state error (distance between target look at point and current look at point), and feeds that into the lookAt point controller. That allows it to track moving points without the excessive lag that tends to plague strictly proportional controllers.

Live example: https://observablehq.com/d/fb32bbf9ea0f2e41

When the camera and lookAt point are panned together, orientation of the camera is maintained: (see end of video for response to abrupt changes)

https://user-images.githubusercontent.com/572717/132393890-b21aeee2-73bd-4812-9fb3-48f4c268bdf5.mp4

When only the lookAt point is moved, the camera tends to follow behind:

https://user-images.githubusercontent.com/572717/132394037-201e034b-9e32-41eb-9dfc-1728db305d0d.mp4

Some particular details regarding the above discussion:

Downsides

Possible design

map.follow(): a function you may call as often as you like. Inputs are:

rreusser commented 3 years ago

Also, from https://github.com/mapbox/mapbox-gl-js/issues/981#issuecomment-281212219:

In the pursuit of making GL JS the smallest & sharpest tool possible, we're going to punt on this feature and encourage it to be written as a plugin.

I'd fully support this as a standalone plugin (it's the way I've prototyped it). Inclusion as an API method would benefit from some API coherence, but one way or another, I mainly just think it's useful functionality people would benefit from.