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