| 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 |
|
| 8 |
#include "include/core/SkPoint.h" |
| 9 |
#include "include/core/SkScalar.h" |
| 10 |
#include "include/private/base/SkFloatingPoint.h" |
| 11 |
#include "modules/jsonreader/SkJSONReader.h" |
| 12 |
#include "modules/skottie/src/Camera.h" |
| 13 |
#include "modules/skottie/src/SkottiePriv.h" |
| 14 |
#include "modules/sksg/include/SkSGTransform.h" |
| 15 |
|
| 16 |
#include <algorithm> |
| 17 |
#include <cmath> |
| 18 |
#include <utility> |
| 19 |
|
| 20 |
namespace skottie { |
| 21 |
namespace internal { |
| 22 |
|
| 23 |
namespace { |
| 24 |
|
| 25 |
SkM44 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 |
|
| 60 |
CameraAdaper::CameraAdaper(const skjson::ObjectValue& jlayer, |
| 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 |
|
| 74 |
CameraAdaper::~CameraAdaper() = default; |
| 75 |
|
| 76 |
SkM44 CameraAdaper::totalMatrix() const { |
| 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 |
|
| 92 |
SkV3 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 |
|
| 110 |
sk_sp<sksg::Transform> CameraAdaper::DefaultCameraTransform(const SkSize& viewport_size) { |
| 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 |
|
| 120 |
return sksg::Matrix<SkM44>::Make( |
| 121 |
ComputeCameraMatrix(pos, poi, rot, viewport_size, kDefaultAEZoom)); |
| 122 |
} |
| 123 |
|
| 124 |
sk_sp<sksg::Transform> AnimationBuilder::attachCamera(const skjson::ObjectValue& jlayer, |
| 125 |
const skjson::ObjectValue& jtransform, |
| 126 |
sk_sp<sksg::Transform> parent, |
| 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 |