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 | |
| 8 | #include "SkParticleAffector.h" |
| 9 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 10 | #include "SkCurve.h" |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 11 | #include "SkParticleData.h" |
| 12 | #include "SkRandom.h" |
| 13 | |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame^] | 14 | void SkParticleAffector::apply(SkParticleUpdateParams& params, SkParticleState ps[], int count) { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 15 | if (fEnabled) { |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame^] | 16 | this->onApply(params, ps, count); |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 17 | } |
| 18 | } |
| 19 | |
| 20 | void SkParticleAffector::visitFields(SkFieldVisitor* v) { |
| 21 | v->visit("Enabled", fEnabled); |
| 22 | } |
| 23 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 24 | class SkLinearVelocityAffector : public SkParticleAffector { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 25 | public: |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 26 | SkLinearVelocityAffector(const SkCurve& angle = 0.0f, |
| 27 | const SkCurve& strength = 0.0f, |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 28 | bool force = true, |
| 29 | bool local = false) |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 30 | : fAngle(angle) |
| 31 | , fStrength(strength) |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 32 | , fForce(force) |
| 33 | , fLocal(local) {} |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 34 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 35 | REFLECTED(SkLinearVelocityAffector, SkParticleAffector) |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 36 | |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame^] | 37 | void onApply(SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
| 38 | for (int i = 0; i < count; ++i) { |
| 39 | float angle = fAngle.eval(ps[i].fAge, ps[i].fStableRandom); |
| 40 | SkScalar c_local, s_local = SkScalarSinCos(SkDegreesToRadians(angle), &c_local); |
| 41 | SkVector heading = fLocal ? ps[i].fPose.fHeading : SkVector{ 0, -1 }; |
| 42 | SkScalar c = heading.fX * c_local - heading.fY * s_local; |
| 43 | SkScalar s = heading.fX * s_local + heading.fY * c_local; |
| 44 | float strength = fStrength.eval(ps[i].fAge, ps[i].fStableRandom); |
| 45 | SkVector force = { c * strength, s * strength }; |
| 46 | if (fForce) { |
| 47 | ps[i].fVelocity.fLinear += force * params.fDeltaTime; |
| 48 | } else { |
| 49 | ps[i].fVelocity.fLinear = force; |
| 50 | } |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 51 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 55 | SkParticleAffector::visitFields(v); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 56 | v->visit("Force", fForce); |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 57 | v->visit("Local", fLocal); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 58 | v->visit("Angle", fAngle); |
| 59 | v->visit("Strength", fStrength); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | private: |
| 63 | SkCurve fAngle; |
| 64 | SkCurve fStrength; |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 65 | bool fForce; |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 66 | bool fLocal; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | class SkPointForceAffector : public SkParticleAffector { |
| 70 | public: |
| 71 | SkPointForceAffector(SkPoint point = { 0.0f, 0.0f }, SkScalar constant = 0.0f, |
| 72 | SkScalar invSquare = 0.0f) |
| 73 | : fPoint(point), fConstant(constant), fInvSquare(invSquare) {} |
| 74 | |
| 75 | REFLECTED(SkPointForceAffector, SkParticleAffector) |
| 76 | |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame^] | 77 | void onApply(SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
| 78 | for (int i = 0; i < count; ++i) { |
| 79 | SkVector toPoint = fPoint - ps[i].fPose.fPosition; |
| 80 | SkScalar lenSquare = toPoint.dot(toPoint); |
| 81 | toPoint.normalize(); |
| 82 | ps[i].fVelocity.fLinear += |
| 83 | toPoint * (fConstant + (fInvSquare / lenSquare)) * params.fDeltaTime; |
| 84 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 88 | SkParticleAffector::visitFields(v); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 89 | v->visit("Point", fPoint); |
| 90 | v->visit("Constant", fConstant); |
| 91 | v->visit("InvSquare", fInvSquare); |
| 92 | } |
| 93 | |
| 94 | private: |
| 95 | SkPoint fPoint; |
| 96 | SkScalar fConstant; |
| 97 | SkScalar fInvSquare; |
| 98 | }; |
| 99 | |
| 100 | class SkOrientAlongVelocityAffector : public SkParticleAffector { |
| 101 | public: |
| 102 | SkOrientAlongVelocityAffector() {} |
| 103 | |
| 104 | REFLECTED(SkOrientAlongVelocityAffector, SkParticleAffector) |
| 105 | |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame^] | 106 | void onApply(SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
| 107 | for (int i = 0; i < count; ++i) { |
| 108 | SkVector heading = ps[i].fVelocity.fLinear; |
| 109 | if (!heading.normalize()) { |
| 110 | heading.set(0, -1); |
| 111 | } |
| 112 | ps[i].fPose.fHeading = heading; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 113 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 114 | } |
| 115 | |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 116 | void visitFields(SkFieldVisitor *v) override { |
| 117 | SkParticleAffector::visitFields(v); |
| 118 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 119 | }; |
| 120 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 121 | class SkSizeAffector : public SkParticleAffector { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 122 | public: |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 123 | SkSizeAffector(const SkCurve& curve = 1.0f) : fCurve(curve) {} |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 124 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 125 | REFLECTED(SkSizeAffector, SkParticleAffector) |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 126 | |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame^] | 127 | void onApply(SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
| 128 | for (int i = 0; i < count; ++i) { |
| 129 | ps[i].fPose.fScale = fCurve.eval(ps[i].fAge, ps[i].fStableRandom); |
| 130 | } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 134 | SkParticleAffector::visitFields(v); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 135 | v->visit("Curve", fCurve); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | private: |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 139 | SkCurve fCurve; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 140 | }; |
| 141 | |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 142 | class SkFrameAffector : public SkParticleAffector { |
| 143 | public: |
| 144 | SkFrameAffector(const SkCurve& curve = 1.0f) : fCurve(curve) {} |
| 145 | |
| 146 | REFLECTED(SkFrameAffector, SkParticleAffector) |
| 147 | |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame^] | 148 | void onApply(SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
| 149 | for (int i = 0; i < count; ++i) { |
| 150 | ps[i].fFrame = fCurve.eval(ps[i].fAge, ps[i].fStableRandom); |
| 151 | } |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 155 | SkParticleAffector::visitFields(v); |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 156 | v->visit("Curve", fCurve); |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | SkCurve fCurve; |
| 161 | }; |
| 162 | |
| 163 | class SkColorAffector : public SkParticleAffector { |
| 164 | public: |
| 165 | SkColorAffector(const SkColorCurve& curve = SkColor4f{ 1.0f, 1.0f, 1.0f, 1.0f }) |
| 166 | : fCurve(curve) {} |
| 167 | |
| 168 | REFLECTED(SkColorAffector, SkParticleAffector) |
| 169 | |
Brian Osman | 14a67a3 | 2019-02-25 14:30:44 -0500 | [diff] [blame^] | 170 | void onApply(SkParticleUpdateParams& params, SkParticleState ps[], int count) override { |
| 171 | for (int i = 0; i < count; ++i) { |
| 172 | ps[i].fColor = fCurve.eval(ps[i].fAge, ps[i].fStableRandom); |
| 173 | } |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void visitFields(SkFieldVisitor* v) override { |
Brian Osman | 1b20cd8 | 2019-02-25 14:15:02 -0500 | [diff] [blame] | 177 | SkParticleAffector::visitFields(v); |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 178 | v->visit("Curve", fCurve); |
| 179 | } |
| 180 | |
| 181 | private: |
| 182 | SkColorCurve fCurve; |
| 183 | }; |
| 184 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 185 | void SkParticleAffector::RegisterAffectorTypes() { |
| 186 | REGISTER_REFLECTED(SkParticleAffector); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 187 | REGISTER_REFLECTED(SkLinearVelocityAffector); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 188 | REGISTER_REFLECTED(SkPointForceAffector); |
| 189 | REGISTER_REFLECTED(SkOrientAlongVelocityAffector); |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 190 | REGISTER_REFLECTED(SkSizeAffector); |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 191 | REGISTER_REFLECTED(SkFrameAffector); |
| 192 | REGISTER_REFLECTED(SkColorAffector); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 193 | } |
| 194 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 195 | sk_sp<SkParticleAffector> SkParticleAffector::MakeLinearVelocity(const SkCurve& angle, |
| 196 | const SkCurve& strength, |
Brian Osman | d5c57fe | 2019-02-22 11:48:18 -0500 | [diff] [blame] | 197 | bool force, |
| 198 | bool local) { |
| 199 | return sk_sp<SkParticleAffector>(new SkLinearVelocityAffector(angle, strength, force, local)); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 200 | } |
| 201 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 202 | sk_sp<SkParticleAffector> SkParticleAffector::MakePointForce(SkPoint point, SkScalar constant, |
| 203 | SkScalar invSquare) { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 204 | return sk_sp<SkParticleAffector>(new SkPointForceAffector(point, constant, invSquare)); |
| 205 | } |
| 206 | |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 207 | sk_sp<SkParticleAffector> SkParticleAffector::MakeOrientAlongVelocity() { |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 208 | return sk_sp<SkParticleAffector>(new SkOrientAlongVelocityAffector()); |
| 209 | } |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 210 | |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 211 | sk_sp<SkParticleAffector> SkParticleAffector::MakeSize(const SkCurve& curve) { |
Brian Osman | 8b6283f | 2019-02-14 16:55:21 -0500 | [diff] [blame] | 212 | return sk_sp<SkParticleAffector>(new SkSizeAffector(curve)); |
| 213 | } |
Brian Osman | 125daa4 | 2019-02-20 12:25:20 -0500 | [diff] [blame] | 214 | |
| 215 | sk_sp<SkParticleAffector> SkParticleAffector::MakeFrame(const SkCurve& curve) { |
| 216 | return sk_sp<SkParticleAffector>(new SkFrameAffector(curve)); |
| 217 | } |
| 218 | |
| 219 | sk_sp<SkParticleAffector> SkParticleAffector::MakeColor(const SkColorCurve& curve) { |
| 220 | return sk_sp<SkParticleAffector>(new SkColorAffector(curve)); |
| 221 | } |