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 | #ifndef SkParticleEffect_DEFINED |
| 9 | #define SkParticleEffect_DEFINED |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkRefCnt.h" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 12 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/private/SkTArray.h" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 14 | #include "include/private/SkTemplates.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/utils/SkRandom.h" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 16 | #include "modules/particles/include/SkParticleData.h" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 17 | |
| 18 | #include <memory> |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 19 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 20 | class SkCanvas; |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame^] | 21 | class SkFieldVisitor; |
| 22 | class SkParticleBinding; |
Brian Osman | 543d2e2 | 2019-02-15 14:29:38 -0500 | [diff] [blame] | 23 | class SkParticleDrawable; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 24 | class SkParticleExternalValue; |
| 25 | |
| 26 | namespace SkSL { |
| 27 | struct ByteCode; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 28 | } |
| 29 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 30 | class SkParticleEffectParams : public SkRefCnt { |
| 31 | public: |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 32 | SkParticleEffectParams(); |
| 33 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 34 | int fMaxCount; // Maximum number of particles per instance of the effect |
| 35 | float fEffectDuration; // How long does the effect last after being played, in seconds? |
| 36 | float fRate; // How many particles are emitted per second? |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 37 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 38 | // What is drawn for each particle? (Image, shape, sprite sheet, etc.) |
| 39 | // See SkParticleDrawable::Make* |
Brian Osman | 543d2e2 | 2019-02-15 14:29:38 -0500 | [diff] [blame] | 40 | sk_sp<SkParticleDrawable> fDrawable; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 41 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 42 | // Particle behavior is driven by two SkSL functions defined in the fCode string. |
| 43 | // Both functions get a mutable Particle struct: |
| 44 | // |
| 45 | // struct Particle { |
| 46 | // float age; |
| 47 | // float lifetime; |
| 48 | // float2 pos = { 0, 0 }; // Local position, relative to the effect. |
| 49 | // float2 dir = { 0, -1 }; // Heading. Should be a normalized vector. |
| 50 | // float scale = 1; // Size, normalized relative to the drawable's native size |
| 51 | // float2 vel = { 0, 0 }; // Linear velocity, in (units / second) |
| 52 | // float spin = 0; // Angular velocity, in (radians / second) |
| 53 | // float4 color = { 1, 1, 1, 1 }; // RGBA color |
| 54 | // float frame = 0; // Normalized sprite index for multi-frame drawables |
| 55 | // }; |
| 56 | // |
| 57 | // In addition, both functions have access to a global variable named 'rand'. Every read of |
| 58 | // 'rand' returns a random floating point value in [0, 1). The random generator is stored |
| 59 | // per-particle, and the state is rewound after each update, so calls to 'rand' will return |
| 60 | // consistent values from one update to the next. |
| 61 | // |
| 62 | // Finally, there are two global uniform values available. The first is 'dt', a floating point |
| 63 | // number of seconds that have elapsed since the last update. The second is 'effectAge', which |
| 64 | // is the normalized age of the effect (not particle). For looping effects, this will wrap |
| 65 | // back to zero when the effect's age exceeds its duration. |
| 66 | // |
| 67 | // 'void spawn(inout Particle p)' is called once for each particle when it is first created, |
| 68 | // to set initial values. At a minimum, this should set 'lifetime' to the number of seconds |
| 69 | // that the particle will exist. Other parameters have defaults shown above. |
| 70 | // |
| 71 | // 'void update(inout Particle p)' is called for each particle on every call to the running |
| 72 | // SkParticleEffect's update() method. It can animate any of the particle's values. Note that |
| 73 | // the 'lifetime' field has a different meaning in 'update', and should not be used or changed. |
Brian Osman | e59acb7 | 2019-07-25 16:58:46 -0400 | [diff] [blame] | 74 | SkString fCode; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 75 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 76 | // External objects accessible by the effect's SkSL code. Each binding is a name and particular |
| 77 | // kind of object. See SkParticleBinding::Make* for details. |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 78 | SkTArray<sk_sp<SkParticleBinding>> fBindings; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 79 | |
| 80 | void visitFields(SkFieldVisitor* v); |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | friend class SkParticleEffect; |
| 84 | |
| 85 | // Cached |
Brian Osman | e59acb7 | 2019-07-25 16:58:46 -0400 | [diff] [blame] | 86 | std::unique_ptr<SkSL::ByteCode> fByteCode; |
| 87 | SkTArray<std::unique_ptr<SkParticleExternalValue>> fExternalValues; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 88 | |
| 89 | void rebuild(); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | class SkParticleEffect : public SkRefCnt { |
| 93 | public: |
Brian Osman | 5c1f8eb | 2019-02-14 14:49:55 -0500 | [diff] [blame] | 94 | SkParticleEffect(sk_sp<SkParticleEffectParams> params, const SkRandom& random); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 95 | |
Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 96 | void start(double now, bool looping = false); |
Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 97 | void update(double now); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 98 | void draw(SkCanvas* canvas); |
| 99 | |
Brian Osman | b77d502 | 2019-03-06 11:08:48 -0500 | [diff] [blame] | 100 | bool isAlive() const { return fSpawnTime >= 0; } |
| 101 | int getCount() const { return fCount; } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 102 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame^] | 103 | static void RegisterParticleTypes(); |
| 104 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 105 | private: |
| 106 | void setCapacity(int capacity); |
| 107 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 108 | sk_sp<SkParticleEffectParams> fParams; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 109 | |
Brian Osman | 5c1f8eb | 2019-02-14 14:49:55 -0500 | [diff] [blame] | 110 | SkRandom fRandom; |
| 111 | |
| 112 | bool fLooping; |
| 113 | double fSpawnTime; |
| 114 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 115 | int fCount; |
| 116 | double fLastTime; |
| 117 | float fSpawnRemainder; |
| 118 | |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 119 | SkParticles fParticles; |
| 120 | SkAutoTMalloc<SkRandom> fStableRandoms; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 121 | |
| 122 | // Cached |
| 123 | int fCapacity; |
| 124 | }; |
| 125 | |
| 126 | #endif // SkParticleEffect_DEFINED |