File size: 2,604 Bytes
a54e6a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""Camera tracks for combat shots: third-person follow behind the player facing the monster (the BOSSV2V training
view), and a transition that orbits from any start camera (e.g. the first-frame view) into the follow camera."""
import numpy as np
from .geometry import Camera, UP
from .motion import ease

flat = lambda v: np.array([v[0], v[1], 0.0])


def follow_behind_raw(scene, player, subject, t, dist=450.0, height=210.0, shoulder=60.0, look=0.5, look_z=120.0):
    p = scene.entities[player]["track"].poses[t, 0]; s = scene.entities[subject]["track"].poses[t, 0]
    d = flat(s - p); d = d / np.linalg.norm(d)
    right = np.array([-d[1], d[0], 0.0])                                  # UE: +Y is right of +X
    pos = flat(p) - d * dist + right * shoulder + UP * height
    target = flat(p) * (1 - look) + flat(s) * look + UP * look_z
    return pos, target


def _smooth(xs, alpha):
    out = np.array(xs, float)
    for i in range(1, len(out)):
        out[i] = out[i - 1] + alpha * (out[i] - out[i - 1])
    return out


def orbit_into_follow(scene, start_cam, t0, t1, player, subject, smooth=0.25, fov=None, **follow_kw):
    """Frames < t0 keep start_cam; [t0, t1) orbit around the player (angle, radius, height and aim eased) from the
    start view to the follow view; frames >= t1 follow behind the player (exponentially smoothed)."""
    T = scene.frames
    raw = [follow_behind_raw(scene, player, subject, t, **follow_kw) for t in range(T)]
    pos = _smooth([r[0] for r in raw], smooth); tgt = _smooth([r[1] for r in raw], smooth)
    fov = fov if fov is not None else start_cam.fov
    p_track = scene.entities[player]["track"]
    s_pos = np.asarray(start_cam.position, float)
    s_dist = np.linalg.norm(flat(p_track.poses[t0, 0] - s_pos))
    s_tgt = s_pos + start_cam.R[:, 0] * s_dist
    cams = []
    for t in range(T):
        if t < t0:
            cams.append(start_cam); continue
        if t >= t1:
            cams.append(Camera.look_at(pos[t], tgt[t], fov)); continue
        u = ease((t - t0) / max(1, t1 - t0))
        c = flat(p_track.poses[t, 0])
        a0 = s_pos - c; a1 = pos[t] - c
        th0, th1 = np.arctan2(a0[1], a0[0]), np.arctan2(a1[1], a1[0])
        dth = (th1 - th0 + np.pi) % (2 * np.pi) - np.pi
        th = th0 + dth * u
        r = np.linalg.norm(a0[:2]) * (1 - u) + np.linalg.norm(a1[:2]) * u
        z = s_pos[2] * (1 - u) + pos[t][2] * u
        p = c + np.array([np.cos(th) * r, np.sin(th) * r, 0.0]); p[2] = z
        aim = s_tgt * (1 - u) + tgt[t] * u
        cams.append(Camera.look_at(p, aim, fov))
    scene.cameras = cams
    return cams