| 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/SkPath.h" |
| 9 |
#include "include/core/SkPathBuilder.h" |
| 10 |
#include "include/core/SkPoint.h" |
| 11 |
#include "include/core/SkRect.h" |
| 12 |
#include "include/core/SkRefCnt.h" |
| 13 |
#include "include/core/SkScalar.h" |
| 14 |
#include "include/private/base/SkAssert.h" |
| 15 |
#include "modules/jsonreader/SkJSONReader.h" |
| 16 |
#include "modules/skottie/src/Adapter.h" |
| 17 |
#include "modules/skottie/src/SkottiePriv.h" |
| 18 |
#include "modules/skottie/src/SkottieValue.h" |
| 19 |
#include "modules/skottie/src/layers/shapelayer/ShapeLayer.h" |
| 20 |
#include "modules/sksg/include/SkSGGeometryEffect.h" |
| 21 |
#include "modules/sksg/include/SkSGGeometryNode.h" |
| 22 |
#include "modules/sksg/include/SkSGNode.h" |
| 23 |
#include "src/core/SkGeometry.h" |
| 24 |
|
| 25 |
#include <utility> |
| 26 |
#include <vector> |
| 27 |
|
| 28 |
class SkMatrix; |
| 29 |
|
| 30 |
namespace skottie::internal { |
| 31 |
|
| 32 |
namespace { |
| 33 |
|
| 34 |
static SkPoint lerp(const SkPoint& p0, const SkPoint& p1, SkScalar t) { |
| 35 |
return p0 + (p1 - p0) * t; |
| 36 |
} |
| 37 |
|
| 38 |
// Operates on the cubic representation of a shape. Pulls vertices towards the shape center, |
| 39 |
// and cubic control points away from the center. The general shape center is the vertex average. |
| 40 |
class PuckerBloatEffect final : public sksg::GeometryEffect { |
| 41 |
public: |
| 42 |
explicit PuckerBloatEffect(sk_sp<sksg::GeometryNode> geo) : INHERITED({std::move(geo)}) {} |
| 43 |
|
| 44 |
// Fraction of the transition to center. I.e. |
| 45 |
// |
| 46 |
// 0 -> no effect |
| 47 |
// 1 -> vertices collapsed to center |
| 48 |
// |
| 49 |
// Negative values are allowed (inverse direction), as are extranormal values. |
| 50 |
SG_ATTRIBUTE(Amount, float, fAmount) |
| 51 |
|
| 52 |
private: |
| 53 |
SkPath onRevalidateEffect(const sk_sp<GeometryNode>& geo, const SkMatrix&) override { |
| 54 |
struct CubicInfo { |
| 55 |
SkPoint ctrl0, ctrl1, pt; // corresponding to SkPath::cubicTo() params, respectively. |
| 56 |
}; |
| 57 |
|
| 58 |
const auto input = geo->asPath(); |
| 59 |
if (SkScalarNearlyZero(fAmount)) { |
| 60 |
return input; |
| 61 |
} |
| 62 |
|
| 63 |
const auto input_bounds = input.computeTightBounds(); |
| 64 |
const SkPoint center{input_bounds.centerX(), input_bounds.centerY()}; |
| 65 |
|
| 66 |
SkPathBuilder builder; |
| 67 |
|
| 68 |
SkPoint contour_start = {0, 0}; |
| 69 |
std::vector<CubicInfo> cubics; |
| 70 |
|
| 71 |
auto commit_contour = [&]() { |
| 72 |
builder.moveTo(lerp(contour_start, center, fAmount)); |
| 73 |
for (const auto& c : cubics) { |
| 74 |
builder.cubicTo(lerp(c.ctrl0, center, -fAmount), |
| 75 |
lerp(c.ctrl1, center, -fAmount), |
| 76 |
lerp(c.pt , center, fAmount)); |
| 77 |
} |
| 78 |
builder.close(); |
| 79 |
|
| 80 |
cubics.clear(); |
| 81 |
}; |
| 82 |
|
| 83 |
// Normalize all verbs to cubic representation. |
| 84 |
SkPath::Iter iter(input, true); |
| 85 |
while (auto rec = iter.next()) { |
| 86 |
SkSpan<const SkPoint> pts = rec->fPoints; |
| 87 |
switch (rec->fVerb) { |
| 88 |
case SkPathVerb::kMove: |
| 89 |
commit_contour(); |
| 90 |
contour_start = pts[0]; |
| 91 |
break; |
| 92 |
case SkPathVerb::kLine: { |
| 93 |
// Empirically, straight lines are treated as cubics with control points |
| 94 |
// located length/100 away from extremities. |
| 95 |
static constexpr float kCtrlPosFraction = 1.f / 100; |
| 96 |
const auto line_start = pts[0], |
| 97 |
line_end = pts[1]; |
| 98 |
cubics.push_back({ |
| 99 |
lerp(line_start, line_end, kCtrlPosFraction), |
| 100 |
lerp(line_start, line_end, 1 - kCtrlPosFraction), |
| 101 |
line_end |
| 102 |
}); |
| 103 |
} break; |
| 104 |
case SkPathVerb::kQuad: { |
| 105 |
SkPoint quad[4]; |
| 106 |
SkConvertQuadToCubic(pts.data(), quad); |
| 107 |
cubics.push_back({quad[1], quad[2], quad[3]}); |
| 108 |
} break; |
| 109 |
case SkPathVerb::kConic: { |
| 110 |
// We should only ever encounter conics from circles/ellipses. |
| 111 |
SkASSERT(SkScalarNearlyEqual(rec->conicWeight(), SK_ScalarRoot2Over2)); |
| 112 |
|
| 113 |
// http://spencermortensen.com/articles/bezier-circle/ |
| 114 |
static constexpr float kCubicCircleCoeff = 1 - 0.551915024494f; |
| 115 |
|
| 116 |
const auto conic_start = cubics.empty() ? contour_start |
| 117 |
: cubics.back().pt, |
| 118 |
conic_end = pts[2]; |
| 119 |
|
| 120 |
cubics.push_back({ |
| 121 |
lerp(pts[1], conic_start, kCubicCircleCoeff), |
| 122 |
lerp(pts[1], conic_end , kCubicCircleCoeff), |
| 123 |
conic_end |
| 124 |
}); |
| 125 |
} break; |
| 126 |
case SkPathVerb::kCubic: |
| 127 |
cubics.push_back({pts[1], pts[2], pts[3]}); |
| 128 |
break; |
| 129 |
case SkPathVerb::kClose: |
| 130 |
commit_contour(); |
| 131 |
break; |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
return builder.detach(); |
| 136 |
} |
| 137 |
|
| 138 |
float fAmount = 0; |
| 139 |
|
| 140 |
using INHERITED = sksg::GeometryEffect; |
| 141 |
}; |
| 142 |
|
| 143 |
class PuckerBloatAdapter final : public DiscardableAdapterBase<PuckerBloatAdapter, |
| 144 |
PuckerBloatEffect> { |
| 145 |
public: |
| 146 |
PuckerBloatAdapter(const skjson::ObjectValue& joffset, |
| 147 |
const AnimationBuilder& abuilder, |
| 148 |
sk_sp<sksg::GeometryNode> child) |
| 149 |
: INHERITED(sk_make_sp<PuckerBloatEffect>(std::move(child))) { |
| 150 |
this->bind(abuilder, joffset["a" ], fAmount); |
| 151 |
} |
| 152 |
|
| 153 |
private: |
| 154 |
void onSync() override { |
| 155 |
// AE amount is percentage-based. |
| 156 |
this->node()->setAmount(fAmount / 100); |
| 157 |
} |
| 158 |
|
| 159 |
ScalarValue fAmount = 0; |
| 160 |
|
| 161 |
using INHERITED = DiscardableAdapterBase<PuckerBloatAdapter, PuckerBloatEffect>; |
| 162 |
}; |
| 163 |
|
| 164 |
} // namespace |
| 165 |
|
| 166 |
std::vector<sk_sp<sksg::GeometryNode>> ShapeBuilder::AttachPuckerBloatGeometryEffect( |
| 167 |
const skjson::ObjectValue& jround, const AnimationBuilder* abuilder, |
| 168 |
std::vector<sk_sp<sksg::GeometryNode>>&& geos) { |
| 169 |
std::vector<sk_sp<sksg::GeometryNode>> bloated; |
| 170 |
bloated.reserve(geos.size()); |
| 171 |
|
| 172 |
for (auto& g : geos) { |
| 173 |
bloated.push_back(abuilder->attachDiscardableAdapter<PuckerBloatAdapter> |
| 174 |
(jround, *abuilder, std::move(g))); |
| 175 |
} |
| 176 |
|
| 177 |
return bloated; |
| 178 |
} |
| 179 |
|
| 180 |
} // namespace skottie::internal |