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