Flutter Engine
The Flutter Engine
Camera.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2020 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
14#include "src/utils/SkJSON.h"
15
16#include <algorithm>
17#include <cmath>
18#include <utility>
19
20namespace skottie {
21namespace internal {
22
23namespace {
24
25SkM44 ComputeCameraMatrix(const SkV3& position,
26 const SkV3& poi,
27 const SkV3& rotation,
28 const SkSize& viewport_size,
29 float zoom) {
30
31 // Initial camera vector.
32 const auto cam_t = SkM44::Rotate({0, 0, 1}, SkDegreesToRadians(-rotation.z))
33 * SkM44::Rotate({0, 1, 0}, SkDegreesToRadians( rotation.y))
34 * SkM44::Rotate({1, 0, 0}, SkDegreesToRadians( rotation.x))
35 * SkM44::LookAt({ position.x, position.y, -position.z },
36 { poi.x, poi.y, poi.z },
37 { 0, 1, 0 })
38 * SkM44::Scale(1, 1, -1);
39
40 // View parameters:
41 //
42 // * size -> composition size (TODO: AE seems to base it on width only?)
43 // * distance -> "zoom" camera attribute
44 //
45 const auto view_size = std::max(viewport_size.width(), viewport_size.height()),
46 view_distance = zoom,
47 view_angle = std::atan(sk_ieee_float_divide(view_size * 0.5f, view_distance));
48
49 const auto persp_t = SkM44::Scale(view_size * 0.5f, view_size * 0.5f, 1)
50 * SkM44::Perspective(0, view_distance, 2 * view_angle);
51
52 return SkM44::Translate(viewport_size.width() * 0.5f,
53 viewport_size.height() * 0.5f,
54 0)
55 * persp_t * cam_t;
56}
57
58} // namespace
59
61 const skjson::ObjectValue& jtransform,
62 const AnimationBuilder& abuilder,
63 const SkSize& viewport_size)
64 : INHERITED(jtransform, abuilder)
65 , fViewportSize(viewport_size)
66 // The presence of an anchor point property ('a') differentiates
67 // one-node vs. two-node cameras.
68 , fType(jtransform["a"].is<skjson::NullValue>() ? CameraType::kOneNode
69 : CameraType::kTwoNode) {
70 // 'pe' (perspective?) corresponds to AE's "zoom" camera property.
71 this->bind(abuilder, jlayer["pe"], fZoom);
72}
73
75
77 // Camera parameters:
78 //
79 // * location -> position attribute
80 // * point of interest -> anchor point attribute (two-node camera only)
81 // * orientation -> rotation attribute
82 //
83 const auto position = this->position();
84
85 return ComputeCameraMatrix(position,
86 this->poi(position),
87 this->rotation(),
88 fViewportSize,
89 fZoom);
90}
91
92SkV3 CameraAdaper::poi(const SkV3& pos) const {
93 // AE supports two camera types:
94 //
95 // - one-node camera: does not auto-orient, and starts off perpendicular
96 // to the z = 0 plane, facing "forward" (decreasing z).
97 //
98 // - two-node camera: has a point of interest (encoded as the anchor point),
99 // and auto-orients to point in its direction.
100
101 if (fType == CameraType::kOneNode) {
102 return { pos.x, pos.y, -pos.z - 1};
103 }
104
105 const auto ap = this->anchor_point();
106
107 return { ap.x, ap.y, -ap.z };
108}
109
111 const auto center = SkVector::Make(viewport_size.width() * 0.5f,
112 viewport_size.height() * 0.5f);
113
114 static constexpr float kDefaultAEZoom = 879.13f;
115
116 const SkV3 pos = { center.fX, center.fY, -kDefaultAEZoom },
117 poi = { pos.x, pos.y, -pos.z - 1 },
118 rot = { 0, 0, 0 };
119
121 ComputeCameraMatrix(pos, poi, rot, viewport_size, kDefaultAEZoom));
122}
123
125 const skjson::ObjectValue& jtransform,
127 const SkSize& viewport_size) const {
128 auto adapter = sk_make_sp<CameraAdaper>(jlayer, jtransform, *this, viewport_size);
129
130 if (adapter->isStatic()) {
131 adapter->seek(0);
132 } else {
133 fCurrentAnimatorScope->push_back(adapter);
134 }
135
136 return sksg::Transform::MakeConcat(adapter->node(), std::move(parent));
137}
138
139} // namespace internal
140} // namespace skottie
SkPoint pos
static constexpr float sk_ieee_float_divide(float numer, float denom)
#define SkDegreesToRadians(degrees)
Definition: SkScalar.h:77
Definition: SkM44.h:150
static SkM44 LookAt(const SkV3 &eye, const SkV3 &center, const SkV3 &up)
Definition: SkM44.cpp:331
static SkM44 Rotate(SkV3 axis, SkScalar radians)
Definition: SkM44.h:239
static SkM44 Translate(SkScalar x, SkScalar y, SkScalar z=0)
Definition: SkM44.h:225
static SkM44 Perspective(float near, float far, float angle)
Definition: SkM44.cpp:343
static SkM44 Scale(SkScalar x, SkScalar y, SkScalar z=1)
Definition: SkM44.h:232
bool bind(const AnimationBuilder &, const skjson::ObjectValue *, T *)
sk_sp< sksg::Transform > attachCamera(const skjson::ObjectValue &jlayer, const skjson::ObjectValue &jtransform, sk_sp< sksg::Transform >, const SkSize &) const
Definition: Camera.cpp:124
CameraAdaper(const skjson::ObjectValue &jlayer, const skjson::ObjectValue &jtransform, const AnimationBuilder &abuilder, const SkSize &viewport_size)
Definition: Camera.cpp:60
SkM44 totalMatrix() const override
Definition: Camera.cpp:76
static sk_sp< sksg::Transform > DefaultCameraTransform(const SkSize &viewport_size)
Definition: Camera.cpp:110
static sk_sp< Matrix > Make(const T &m)
Definition: SkSGTransform.h:70
static sk_sp< Transform > MakeConcat(sk_sp< Transform > a, sk_sp< Transform > b)
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
Definition: SkSize.h:52
SkScalar width() const
Definition: SkSize.h:76
SkScalar height() const
Definition: SkSize.h:77
Definition: SkM44.h:56
float y
Definition: SkM44.h:57
float z
Definition: SkM44.h:57
float x
Definition: SkM44.h:57