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