Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 Google LLC |
| 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 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "modules/particles/include/SkParticleAffector.h" |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkContourMeasure.h" |
| 11 | #include "include/core/SkPath.h" |
| 12 | #include "include/utils/SkParsePath.h" |
| 13 | #include "include/utils/SkRandom.h" |
| 14 | #include "include/utils/SkTextUtils.h" |
| 15 | #include "modules/particles/include/SkCurve.h" |
| 16 | #include "modules/particles/include/SkParticleData.h" |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 17 | #include "src/core/SkMakeUnique.h" |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 18 | #include "src/sksl/SkSLByteCode.h" |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 19 | #include "src/sksl/SkSLCompiler.h" |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 20 | #include "src/sksl/SkSLExternalValue.h" |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 21 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 22 | void SkParticleAffector::apply(const SkParticleUpdateParams& params, |
| 23 | SkParticleState ps[], int count) { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 24 | if (fEnabled) { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 25 | this->onApply(params, ps, count); |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 26 | } |
| 27 | } |
| 28 | |
| 29 | void SkParticleAffector::visitFields(SkFieldVisitor* v) { |
| 30 | v->visit("Enabled", fEnabled); |
| 31 | } |
| 32 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 33 | class SkLinearVelocityAffector : public SkParticleAffector { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 34 | public: |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 35 | SkLinearVelocityAffector(const SkCurve& angle = 0.0f, |
| 36 | const SkCurve& strength = 0.0f, |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 37 | bool force = true, |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 38 | SkParticleFrame frame = kWorld_ParticleFrame) |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 39 | : fAngle(angle) |
| 40 | , fStrength(strength) |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 41 | , fForce(force) |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 42 | , fFrame(frame) {} |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 43 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 44 | REFLECTED(SkLinearVelocityAffector, SkParticleAffector) |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 45 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 46 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 47 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 48 | float angle = fAngle.eval(params, ps[i]); |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 49 | SkScalar rad = SkDegreesToRadians(angle); |
| 50 | SkScalar s_local = SkScalarSin(rad), |
| 51 | c_local = SkScalarCos(rad); |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 52 | SkVector heading = ps[i].getFrameHeading(static_cast<SkParticleFrame>(fFrame)); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 53 | SkScalar c = heading.fX * c_local - heading.fY * s_local; |
| 54 | SkScalar s = heading.fX * s_local + heading.fY * c_local; |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 55 | float strength = fStrength.eval(params, ps[i]); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 56 | SkVector force = { c * strength, s * strength }; |
| 57 | if (fForce) { |
| 58 | ps[i].fVelocity.fLinear += force * params.fDeltaTime; |
| 59 | } else { |
| 60 | ps[i].fVelocity.fLinear = force; |
| 61 | } |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 62 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 66 | SkParticleAffector::visitFields(v); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 67 | v->visit("Force", fForce); |
Brian Osman | e5d532e | 2019-02-26 14:58:40 -0500 | [diff] [blame] | 68 | v->visit("Frame", fFrame, gParticleFrameMapping, SK_ARRAY_COUNT(gParticleFrameMapping)); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 69 | v->visit("Angle", fAngle); |
| 70 | v->visit("Strength", fStrength); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | private: |
| 74 | SkCurve fAngle; |
| 75 | SkCurve fStrength; |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 76 | bool fForce; |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 77 | int fFrame; |
| 78 | }; |
| 79 | |
| 80 | class SkAngularVelocityAffector : public SkParticleAffector { |
| 81 | public: |
| 82 | SkAngularVelocityAffector(const SkCurve& strength = 0.0f, bool force = true) |
| 83 | : fStrength(strength) |
| 84 | , fForce(force) {} |
| 85 | |
| 86 | REFLECTED(SkAngularVelocityAffector, SkParticleAffector) |
| 87 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 88 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 89 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 90 | float strength = fStrength.eval(params, ps[i]); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 91 | if (fForce) { |
| 92 | ps[i].fVelocity.fAngular += strength * params.fDeltaTime; |
| 93 | } else { |
| 94 | ps[i].fVelocity.fAngular = strength; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void visitFields(SkFieldVisitor* v) override { |
| 100 | SkParticleAffector::visitFields(v); |
| 101 | v->visit("Force", fForce); |
| 102 | v->visit("Strength", fStrength); |
| 103 | } |
| 104 | |
| 105 | private: |
| 106 | SkCurve fStrength; |
| 107 | bool fForce; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | class SkPointForceAffector : public SkParticleAffector { |
| 111 | public: |
| 112 | SkPointForceAffector(SkPoint point = { 0.0f, 0.0f }, SkScalar constant = 0.0f, |
| 113 | SkScalar invSquare = 0.0f) |
| 114 | : fPoint(point), fConstant(constant), fInvSquare(invSquare) {} |
| 115 | |
| 116 | REFLECTED(SkPointForceAffector, SkParticleAffector) |
| 117 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 118 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 119 | for (int i = 0; i < count; ++i) { |
| 120 | SkVector toPoint = fPoint - ps[i].fPose.fPosition; |
| 121 | SkScalar lenSquare = toPoint.dot(toPoint); |
| 122 | toPoint.normalize(); |
| 123 | ps[i].fVelocity.fLinear += |
| 124 | toPoint * (fConstant + (fInvSquare / lenSquare)) * params.fDeltaTime; |
| 125 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 129 | SkParticleAffector::visitFields(v); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 130 | v->visit("Point", fPoint); |
| 131 | v->visit("Constant", fConstant); |
| 132 | v->visit("InvSquare", fInvSquare); |
| 133 | } |
| 134 | |
| 135 | private: |
| 136 | SkPoint fPoint; |
| 137 | SkScalar fConstant; |
| 138 | SkScalar fInvSquare; |
| 139 | }; |
| 140 | |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 141 | class SkOrientationAffector : public SkParticleAffector { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 142 | public: |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 143 | SkOrientationAffector(const SkCurve& angle = 0.0f, |
| 144 | SkParticleFrame frame = kLocal_ParticleFrame) |
| 145 | : fAngle(angle) |
| 146 | , fFrame(frame) {} |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 147 | |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 148 | REFLECTED(SkOrientationAffector, SkParticleAffector) |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 149 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 150 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 151 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 152 | float angle = fAngle.eval(params, ps[i]); |
Brian Osman | 4428f2c | 2019-04-02 10:59:28 -0400 | [diff] [blame] | 153 | SkScalar rad = SkDegreesToRadians(angle); |
| 154 | SkScalar s_local = SkScalarSin(rad), |
| 155 | c_local = SkScalarCos(rad); |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 156 | SkVector heading = ps[i].getFrameHeading(static_cast<SkParticleFrame>(fFrame)); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 157 | ps[i].fPose.fHeading.set(heading.fX * c_local - heading.fY * s_local, |
| 158 | heading.fX * s_local + heading.fY * c_local); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 159 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 160 | } |
| 161 | |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 162 | void visitFields(SkFieldVisitor *v) override { |
| 163 | SkParticleAffector::visitFields(v); |
Brian Osman | e5d532e | 2019-02-26 14:58:40 -0500 | [diff] [blame] | 164 | v->visit("Frame", fFrame, gParticleFrameMapping, SK_ARRAY_COUNT(gParticleFrameMapping)); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 165 | v->visit("Angle", fAngle); |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 166 | } |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 167 | |
| 168 | private: |
| 169 | SkCurve fAngle; |
| 170 | int fFrame; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 171 | }; |
| 172 | |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 173 | class SkPositionInCircleAffector : public SkParticleAffector { |
| 174 | public: |
| 175 | SkPositionInCircleAffector(const SkCurve& x = 0.0f, const SkCurve& y = 0.0f, |
| 176 | const SkCurve& radius = 0.0f, bool setHeading = true) |
| 177 | : fX(x) |
| 178 | , fY(y) |
| 179 | , fRadius(radius) |
| 180 | , fSetHeading(setHeading) {} |
| 181 | |
| 182 | REFLECTED(SkPositionInCircleAffector, SkParticleAffector) |
| 183 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 184 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 185 | for (int i = 0; i < count; ++i) { |
| 186 | SkVector v; |
| 187 | do { |
| 188 | v.fX = ps[i].fRandom.nextSScalar1(); |
| 189 | v.fY = ps[i].fRandom.nextSScalar1(); |
| 190 | } while (v.dot(v) > 1); |
| 191 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 192 | SkPoint center = { fX.eval(params, ps[i]), fY.eval(params, ps[i]) }; |
| 193 | SkScalar radius = fRadius.eval(params, ps[i]); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 194 | ps[i].fPose.fPosition = center + (v * radius); |
| 195 | if (fSetHeading) { |
| 196 | if (!v.normalize()) { |
| 197 | v.set(0, -1); |
| 198 | } |
| 199 | ps[i].fPose.fHeading = v; |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void visitFields(SkFieldVisitor* v) override { |
| 205 | SkParticleAffector::visitFields(v); |
| 206 | v->visit("SetHeading", fSetHeading); |
| 207 | v->visit("X", fX); |
| 208 | v->visit("Y", fY); |
| 209 | v->visit("Radius", fRadius); |
| 210 | } |
| 211 | |
| 212 | private: |
| 213 | SkCurve fX; |
| 214 | SkCurve fY; |
| 215 | SkCurve fRadius; |
| 216 | bool fSetHeading; |
| 217 | }; |
| 218 | |
| 219 | class SkPositionOnPathAffector : public SkParticleAffector { |
| 220 | public: |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 221 | SkPositionOnPathAffector(const char* path = "", bool setHeading = true, |
| 222 | SkParticleValue input = SkParticleValue()) |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 223 | : fPath(path) |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 224 | , fInput(input) |
| 225 | , fSetHeading(setHeading) { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 226 | this->rebuild(); |
| 227 | } |
| 228 | |
| 229 | REFLECTED(SkPositionOnPathAffector, SkParticleAffector) |
| 230 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 231 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 232 | if (fContours.empty()) { |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 237 | float t = fInput.eval(params, ps[i]); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 238 | SkScalar len = fTotalLength * t; |
| 239 | int idx = 0; |
| 240 | while (idx < fContours.count() && len > fContours[idx]->length()) { |
| 241 | len -= fContours[idx++]->length(); |
| 242 | } |
| 243 | SkVector localXAxis; |
| 244 | if (!fContours[idx]->getPosTan(len, &ps[i].fPose.fPosition, &localXAxis)) { |
| 245 | ps[i].fPose.fPosition = { 0, 0 }; |
| 246 | localXAxis = { 1, 0 }; |
| 247 | } |
| 248 | if (fSetHeading) { |
| 249 | ps[i].fPose.fHeading.set(localXAxis.fY, -localXAxis.fX); |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | void visitFields(SkFieldVisitor* v) override { |
| 255 | SkString oldPath = fPath; |
| 256 | |
| 257 | SkParticleAffector::visitFields(v); |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 258 | v->visit("Input", fInput); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 259 | v->visit("SetHeading", fSetHeading); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 260 | v->visit("Path", fPath); |
| 261 | |
| 262 | if (fPath != oldPath) { |
| 263 | this->rebuild(); |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | private: |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 268 | SkString fPath; |
| 269 | SkParticleValue fInput; |
| 270 | bool fSetHeading; |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 271 | |
| 272 | void rebuild() { |
| 273 | SkPath path; |
| 274 | if (!SkParsePath::FromSVGString(fPath.c_str(), &path)) { |
| 275 | return; |
| 276 | } |
| 277 | |
| 278 | fTotalLength = 0; |
| 279 | fContours.reset(); |
| 280 | |
| 281 | SkContourMeasureIter iter(path, false); |
| 282 | while (auto contour = iter.next()) { |
| 283 | fContours.push_back(contour); |
| 284 | fTotalLength += contour->length(); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | // Cached |
| 289 | SkScalar fTotalLength; |
| 290 | SkTArray<sk_sp<SkContourMeasure>> fContours; |
| 291 | }; |
| 292 | |
| 293 | class SkPositionOnTextAffector : public SkParticleAffector { |
| 294 | public: |
| 295 | SkPositionOnTextAffector(const char* text = "", SkScalar fontSize = 96, bool setHeading = true, |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 296 | SkParticleValue input = SkParticleValue()) |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 297 | : fText(text) |
| 298 | , fFontSize(fontSize) |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 299 | , fInput(input) |
| 300 | , fSetHeading(setHeading) { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 301 | this->rebuild(); |
| 302 | } |
| 303 | |
| 304 | REFLECTED(SkPositionOnTextAffector, SkParticleAffector) |
| 305 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 306 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 307 | if (fContours.empty()) { |
| 308 | return; |
| 309 | } |
| 310 | |
| 311 | // TODO: Refactor to share code with PositionOnPathAffector |
| 312 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 313 | float t = fInput.eval(params, ps[i]); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 314 | SkScalar len = fTotalLength * t; |
| 315 | int idx = 0; |
| 316 | while (idx < fContours.count() && len > fContours[idx]->length()) { |
| 317 | len -= fContours[idx++]->length(); |
| 318 | } |
| 319 | SkVector localXAxis; |
| 320 | if (!fContours[idx]->getPosTan(len, &ps[i].fPose.fPosition, &localXAxis)) { |
| 321 | ps[i].fPose.fPosition = { 0, 0 }; |
| 322 | localXAxis = { 1, 0 }; |
| 323 | } |
| 324 | if (fSetHeading) { |
| 325 | ps[i].fPose.fHeading.set(localXAxis.fY, -localXAxis.fX); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | void visitFields(SkFieldVisitor* v) override { |
| 331 | SkString oldText = fText; |
| 332 | SkScalar oldSize = fFontSize; |
| 333 | |
| 334 | SkParticleAffector::visitFields(v); |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 335 | v->visit("Input", fInput); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 336 | v->visit("SetHeading", fSetHeading); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 337 | v->visit("Text", fText); |
| 338 | v->visit("FontSize", fFontSize); |
| 339 | |
| 340 | if (fText != oldText || fFontSize != oldSize) { |
| 341 | this->rebuild(); |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | private: |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 346 | SkString fText; |
| 347 | SkScalar fFontSize; |
| 348 | SkParticleValue fInput; |
| 349 | bool fSetHeading; |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 350 | |
| 351 | void rebuild() { |
| 352 | fTotalLength = 0; |
| 353 | fContours.reset(); |
| 354 | |
| 355 | if (fText.isEmpty()) { |
| 356 | return; |
| 357 | } |
| 358 | |
Kevin Lubick | 9663484 | 2019-03-05 14:09:24 -0500 | [diff] [blame] | 359 | // Use the font manager's default font |
| 360 | SkFont font(nullptr, fFontSize); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 361 | SkPath path; |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 362 | SkTextUtils::GetPath(fText.c_str(), fText.size(), SkTextEncoding::kUTF8, 0, 0, font, &path); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 363 | SkContourMeasureIter iter(path, false); |
| 364 | while (auto contour = iter.next()) { |
| 365 | fContours.push_back(contour); |
| 366 | fTotalLength += contour->length(); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // Cached |
| 371 | SkScalar fTotalLength; |
| 372 | SkTArray<sk_sp<SkContourMeasure>> fContours; |
| 373 | }; |
| 374 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 375 | class SkSizeAffector : public SkParticleAffector { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 376 | public: |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 377 | SkSizeAffector(const SkCurve& curve = 1.0f) : fCurve(curve) {} |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 378 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 379 | REFLECTED(SkSizeAffector, SkParticleAffector) |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 380 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 381 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 382 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 383 | ps[i].fPose.fScale = fCurve.eval(params, ps[i]); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 384 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 388 | SkParticleAffector::visitFields(v); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 389 | v->visit("Curve", fCurve); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | private: |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 393 | SkCurve fCurve; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 394 | }; |
| 395 | |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 396 | class SkFrameAffector : public SkParticleAffector { |
| 397 | public: |
| 398 | SkFrameAffector(const SkCurve& curve = 1.0f) : fCurve(curve) {} |
| 399 | |
| 400 | REFLECTED(SkFrameAffector, SkParticleAffector) |
| 401 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 402 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 403 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 404 | ps[i].fFrame = fCurve.eval(params, ps[i]); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 405 | } |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 409 | SkParticleAffector::visitFields(v); |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 410 | v->visit("Curve", fCurve); |
| 411 | } |
| 412 | |
| 413 | private: |
| 414 | SkCurve fCurve; |
| 415 | }; |
| 416 | |
| 417 | class SkColorAffector : public SkParticleAffector { |
| 418 | public: |
| 419 | SkColorAffector(const SkColorCurve& curve = SkColor4f{ 1.0f, 1.0f, 1.0f, 1.0f }) |
| 420 | : fCurve(curve) {} |
| 421 | |
| 422 | REFLECTED(SkColorAffector, SkParticleAffector) |
| 423 | |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 424 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 425 | for (int i = 0; i < count; ++i) { |
Brian Osman | bdcdf1a | 2019-03-04 10:55:22 -0500 | [diff] [blame] | 426 | ps[i].fColor = fCurve.eval(params, ps[i]); |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame] | 427 | } |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 431 | SkParticleAffector::visitFields(v); |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 432 | v->visit("Curve", fCurve); |
| 433 | } |
| 434 | |
| 435 | private: |
| 436 | SkColorCurve fCurve; |
| 437 | }; |
| 438 | |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 439 | static const char* kDefaultCode = |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 440 | "// float rand; Every read returns a random float [0 .. 1)\n" |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 441 | "layout(ctype=float) in uniform float dt;\n" |
| 442 | "layout(ctype=float) in uniform float effectAge;\n" |
| 443 | "\n" |
| 444 | "void main(in float age,\n" |
| 445 | " in float invLifetime,\n" |
| 446 | " inout float2 pos,\n" |
| 447 | " inout float2 dir,\n" |
| 448 | " inout float scale,\n" |
| 449 | " inout float2 vel,\n" |
| 450 | " inout float spin,\n" |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 451 | " inout float4 color) {\n" |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 452 | "}\n"; |
| 453 | |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 454 | class SkRandomExternalValue : public SkSL::ExternalValue { |
| 455 | public: |
| 456 | SkRandomExternalValue(const char* name, SkSL::Compiler& compiler) |
| 457 | : INHERITED(name, *compiler.context().fFloat_Type) |
| 458 | , fRandom(nullptr) { } |
| 459 | |
| 460 | void setRandom(SkRandom* random) { fRandom = random; } |
| 461 | bool canRead() const override { return true; } |
Brian Osman | 1a79f0b | 2019-06-24 16:32:14 -0400 | [diff] [blame] | 462 | void read(int /*unusedIndex*/, float* target) override { *target = fRandom->nextF(); } |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 463 | |
| 464 | private: |
| 465 | SkRandom* fRandom; |
| 466 | typedef SkSL::ExternalValue INHERITED; |
| 467 | }; |
| 468 | |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 469 | class SkInterpreterAffector : public SkParticleAffector { |
| 470 | public: |
| 471 | SkInterpreterAffector() : fCode(kDefaultCode) { |
| 472 | this->rebuild(); |
| 473 | } |
| 474 | |
| 475 | REFLECTED(SkInterpreterAffector, SkParticleAffector) |
| 476 | |
| 477 | void onApply(const SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 478 | for (int i = 0; i < count; ++i) { |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 479 | fRandomValue->setRandom(&ps[i].fRandom); |
Brian Osman | 08a8496 | 2019-06-14 10:17:16 -0400 | [diff] [blame] | 480 | fByteCode->run(fMain, &ps[i].fAge, nullptr, 1, ¶ms.fDeltaTime, 2); |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 481 | } |
| 482 | } |
| 483 | |
| 484 | void visitFields(SkFieldVisitor* v) override { |
| 485 | SkString oldCode = fCode; |
| 486 | |
| 487 | SkParticleAffector::visitFields(v); |
| 488 | v->visit("Code", fCode); |
| 489 | |
| 490 | if (fCode != oldCode) { |
| 491 | this->rebuild(); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | private: |
| 496 | SkString fCode; |
| 497 | |
| 498 | // Cached |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 499 | std::unique_ptr<SkSL::ByteCode> fByteCode; |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 500 | std::unique_ptr<SkRandomExternalValue> fRandomValue; |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 501 | SkSL::ByteCodeFunction* fMain; |
| 502 | |
| 503 | void rebuild() { |
| 504 | SkSL::Compiler compiler; |
| 505 | SkSL::Program::Settings settings; |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 506 | auto rand = skstd::make_unique<SkRandomExternalValue>("rand", compiler); |
| 507 | compiler.registerExternalValue(rand.get()); |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 508 | auto program = compiler.convertProgram(SkSL::Program::kGeneric_Kind, |
| 509 | SkSL::String(fCode.c_str()), settings); |
| 510 | if (!program) { |
| 511 | SkDebugf("%s\n", compiler.errorText().c_str()); |
| 512 | return; |
| 513 | } |
| 514 | |
| 515 | auto byteCode = compiler.toByteCode(*program); |
| 516 | if (compiler.errorCount()) { |
| 517 | SkDebugf("%s\n", compiler.errorText().c_str()); |
| 518 | return; |
| 519 | } |
| 520 | |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 521 | fMain = byteCode->fFunctions[0].get(); |
Brian Osman | 8016441 | 2019-06-07 13:00:23 -0400 | [diff] [blame] | 522 | fByteCode = std::move(byteCode); |
Brian Osman | 4c63a1e | 2019-05-16 15:11:46 -0400 | [diff] [blame] | 523 | fRandomValue = std::move(rand); |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 524 | } |
| 525 | }; |
| 526 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 527 | void SkParticleAffector::RegisterAffectorTypes() { |
| 528 | REGISTER_REFLECTED(SkParticleAffector); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 529 | REGISTER_REFLECTED(SkLinearVelocityAffector); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 530 | REGISTER_REFLECTED(SkAngularVelocityAffector); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 531 | REGISTER_REFLECTED(SkPointForceAffector); |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 532 | REGISTER_REFLECTED(SkOrientationAffector); |
Brian Osman | 3d76d1b | 2019-02-28 15:48:05 -0500 | [diff] [blame] | 533 | REGISTER_REFLECTED(SkPositionInCircleAffector); |
| 534 | REGISTER_REFLECTED(SkPositionOnPathAffector); |
| 535 | REGISTER_REFLECTED(SkPositionOnTextAffector); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 536 | REGISTER_REFLECTED(SkSizeAffector); |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 537 | REGISTER_REFLECTED(SkFrameAffector); |
| 538 | REGISTER_REFLECTED(SkColorAffector); |
Brian Osman | c04cadb | 2019-05-09 14:05:04 -0400 | [diff] [blame] | 539 | REGISTER_REFLECTED(SkInterpreterAffector); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 540 | } |
| 541 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 542 | sk_sp<SkParticleAffector> SkParticleAffector::MakeLinearVelocity(const SkCurve& angle, |
| 543 | const SkCurve& strength, |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 544 | bool force, |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 545 | SkParticleFrame frame) { |
| 546 | return sk_sp<SkParticleAffector>(new SkLinearVelocityAffector(angle, strength, force, frame)); |
| 547 | } |
| 548 | |
| 549 | sk_sp<SkParticleAffector> SkParticleAffector::MakeAngularVelocity(const SkCurve& strength, |
| 550 | bool force) { |
| 551 | return sk_sp<SkParticleAffector>(new SkAngularVelocityAffector(strength, force)); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 552 | } |
| 553 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 554 | sk_sp<SkParticleAffector> SkParticleAffector::MakePointForce(SkPoint point, SkScalar constant, |
| 555 | SkScalar invSquare) { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 556 | return sk_sp<SkParticleAffector>(new SkPointForceAffector(point, constant, invSquare)); |
| 557 | } |
| 558 | |
Brian Osman | 0c48681 | 2019-02-26 10:02:15 -0500 | [diff] [blame] | 559 | sk_sp<SkParticleAffector> SkParticleAffector::MakeOrientation(const SkCurve& angle, |
| 560 | SkParticleFrame frame) { |
| 561 | return sk_sp<SkParticleAffector>(new SkOrientationAffector(angle, frame)); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 562 | } |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 563 | |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 564 | sk_sp<SkParticleAffector> SkParticleAffector::MakeSize(const SkCurve& curve) { |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 565 | return sk_sp<SkParticleAffector>(new SkSizeAffector(curve)); |
| 566 | } |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 567 | |
| 568 | sk_sp<SkParticleAffector> SkParticleAffector::MakeFrame(const SkCurve& curve) { |
| 569 | return sk_sp<SkParticleAffector>(new SkFrameAffector(curve)); |
| 570 | } |
| 571 | |
| 572 | sk_sp<SkParticleAffector> SkParticleAffector::MakeColor(const SkColorCurve& curve) { |
| 573 | return sk_sp<SkParticleAffector>(new SkColorAffector(curve)); |
| 574 | } |