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 | |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 11 | #include "include/core/SkColor.h" |
| 12 | #include "include/core/SkPoint.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 13 | #include "include/core/SkRefCnt.h" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 14 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 15 | #include "include/private/SkTArray.h" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 16 | #include "include/private/SkTemplates.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/utils/SkRandom.h" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 18 | #include "modules/particles/include/SkParticleData.h" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 19 | |
| 20 | #include <memory> |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 21 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 22 | class SkCanvas; |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 23 | class SkFieldVisitor; |
| 24 | class SkParticleBinding; |
Brian Osman | 543d2e2 | 2019-02-15 14:29:38 -0500 | [diff] [blame] | 25 | class SkParticleDrawable; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 26 | class SkParticleExternalValue; |
| 27 | |
| 28 | namespace SkSL { |
| 29 | struct ByteCode; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 30 | } |
| 31 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 32 | class SkParticleEffectParams : public SkRefCnt { |
| 33 | public: |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 34 | SkParticleEffectParams(); |
| 35 | |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 36 | // Maximum number of particles per instance of the effect |
| 37 | int fMaxCount; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 38 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 39 | // What is drawn for each particle? (Image, shape, sprite sheet, etc.) |
| 40 | // See SkParticleDrawable::Make* |
Brian Osman | 543d2e2 | 2019-02-15 14:29:38 -0500 | [diff] [blame] | 41 | sk_sp<SkParticleDrawable> fDrawable; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 42 | |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 43 | // Particle behavior is driven by two chunks of SkSL code. Effect functions are defined in |
| 44 | // fEffectCode, and get a mutable Effect struct: |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 45 | // |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 46 | // struct Effect { |
Brian Osman | d1846d8 | 2019-09-17 13:32:32 -0400 | [diff] [blame^] | 47 | // float age; // Normalized age of the effect |
| 48 | // float lifetime; // Effect's duration, in seconds - script should set this in effectSpawn |
| 49 | // int loop; // Number of loops that have elapsed (0 on initial spawn) |
| 50 | // float rate; // Rate to generate new particles (particles / second) |
| 51 | // int burst; // Number of particles to emit in a single update |
| 52 | // // Set during spawn to emit that many at once on each loop |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 53 | // |
| 54 | // // Everything below this line controls the state of the effect, which is also the |
| 55 | // // default values for new particles. |
| 56 | // float2 pos = { 0, 0 }; // Local position |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 57 | // float2 dir = { 0, -1 }; // Heading. Should be a normalized vector. |
| 58 | // float scale = 1; // Size, normalized relative to the drawable's native size |
| 59 | // float2 vel = { 0, 0 }; // Linear velocity, in (units / second) |
| 60 | // float spin = 0; // Angular velocity, in (radians / second) |
| 61 | // float4 color = { 1, 1, 1, 1 }; // RGBA color |
| 62 | // float frame = 0; // Normalized sprite index for multi-frame drawables |
| 63 | // }; |
| 64 | // |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 65 | // Particle functions are defined in fParticleCode, and get a mutable Particle struct, as well |
| 66 | // as a uniform copy of the current Effect, named 'effect'. |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 67 | // |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 68 | // struct Particle { |
| 69 | // float age; |
| 70 | // float lifetime; |
| 71 | // float2 pos; |
| 72 | // float2 dir; |
| 73 | // float scale; |
| 74 | // float2 vel; |
| 75 | // float spin; |
| 76 | // float4 color; |
| 77 | // float frame; |
| 78 | // }; |
| 79 | // |
| 80 | // All functions have access to a global variable named 'rand'. Every read of 'rand' returns a |
| 81 | // random floating point value in [0, 1). For particle functions, the state is rewound after |
| 82 | // each update, so calls to 'rand' will return consistent values from one update to the next. |
| 83 | // |
| 84 | // Finally, there is one global uniform values available, 'dt'. This is a floating point |
| 85 | // number of seconds that have elapsed since the last update. |
| 86 | // |
| 87 | // Effect code should define two functions: |
| 88 | // |
| 89 | // 'void effectSpawn(inout Effect e)' is called when an instance of the effect is first |
| 90 | // created, and again at every loop point (if the effect is played with the looping flag). |
| 91 | // |
| 92 | // 'void effectUpdate(inout Effect e)' is called once per update to adjust properties of the |
| 93 | // effect (ie emitter). |
| 94 | // |
| 95 | // Particle code should also define two functions: |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 96 | // |
| 97 | // 'void spawn(inout Particle p)' is called once for each particle when it is first created, |
| 98 | // to set initial values. At a minimum, this should set 'lifetime' to the number of seconds |
Brian Osman | d1846d8 | 2019-09-17 13:32:32 -0400 | [diff] [blame^] | 99 | // that the particle will exist. Other parameters will get default values from the effect. |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 100 | // |
| 101 | // 'void update(inout Particle p)' is called for each particle on every call to the running |
| 102 | // SkParticleEffect's update() method. It can animate any of the particle's values. Note that |
| 103 | // the 'lifetime' field has a different meaning in 'update', and should not be used or changed. |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 104 | |
| 105 | SkString fEffectCode; |
| 106 | SkString fParticleCode; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 107 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 108 | // External objects accessible by the effect's SkSL code. Each binding is a name and particular |
| 109 | // kind of object. See SkParticleBinding::Make* for details. |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 110 | SkTArray<sk_sp<SkParticleBinding>> fBindings; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 111 | |
| 112 | void visitFields(SkFieldVisitor* v); |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 113 | |
| 114 | private: |
| 115 | friend class SkParticleEffect; |
| 116 | |
| 117 | // Cached |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 118 | struct Program { |
| 119 | std::unique_ptr<SkSL::ByteCode> fByteCode; |
| 120 | SkTArray<std::unique_ptr<SkParticleExternalValue>> fExternalValues; |
| 121 | }; |
| 122 | |
| 123 | Program fEffectProgram; |
| 124 | Program fParticleProgram; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 125 | |
| 126 | void rebuild(); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | class SkParticleEffect : public SkRefCnt { |
| 130 | public: |
Brian Osman | 5c1f8eb | 2019-02-14 14:49:55 -0500 | [diff] [blame] | 131 | SkParticleEffect(sk_sp<SkParticleEffectParams> params, const SkRandom& random); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 132 | |
Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 133 | void start(double now, bool looping = false); |
Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 134 | void update(double now); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 135 | void draw(SkCanvas* canvas); |
| 136 | |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 137 | bool isAlive() const { return fState.fAge >= 0 && fState.fAge <= 1; } |
Brian Osman | b77d502 | 2019-03-06 11:08:48 -0500 | [diff] [blame] | 138 | int getCount() const { return fCount; } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 139 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 140 | static void RegisterParticleTypes(); |
| 141 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 142 | private: |
| 143 | void setCapacity(int capacity); |
| 144 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 145 | sk_sp<SkParticleEffectParams> fParams; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 146 | |
Brian Osman | 5c1f8eb | 2019-02-14 14:49:55 -0500 | [diff] [blame] | 147 | SkRandom fRandom; |
| 148 | |
| 149 | bool fLooping; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 150 | int fCount; |
| 151 | double fLastTime; |
| 152 | float fSpawnRemainder; |
| 153 | |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 154 | // Effect-associated values exposed to script. They are some mix of uniform and inout, |
| 155 | // depending on whether we're executing per-feffect or per-particle scripts. |
| 156 | struct EffectState { |
| 157 | float fDeltaTime; |
| 158 | |
| 159 | // Above this line is always uniform. Below is uniform for particles, inout for effect. |
| 160 | |
| 161 | float fAge; |
| 162 | float fLifetime; |
| 163 | int fLoopCount; |
| 164 | float fRate; |
| 165 | int fBurst; |
| 166 | |
| 167 | // Properties that determine default values for new particles |
| 168 | SkPoint fPosition; |
| 169 | SkVector fHeading; |
| 170 | float fScale; |
| 171 | SkVector fVelocity; |
| 172 | float fSpin; |
| 173 | SkColor4f fColor; |
| 174 | float fFrame; |
| 175 | }; |
| 176 | EffectState fState; |
| 177 | |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 178 | SkParticles fParticles; |
| 179 | SkAutoTMalloc<SkRandom> fStableRandoms; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 180 | |
| 181 | // Cached |
| 182 | int fCapacity; |
| 183 | }; |
| 184 | |
| 185 | #endif // SkParticleEffect_DEFINED |