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" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 17 | #include "modules/particles/include/SkParticleData.h" |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 18 | |
| 19 | #include <memory> |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame^] | 20 | #include <vector> |
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 | |
Brian Osman | d12f278 | 2019-11-27 10:34:18 -0500 | [diff] [blame] | 27 | namespace skresources { |
| 28 | class ResourceProvider; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 29 | } // namespace skresources |
Brian Osman | d12f278 | 2019-11-27 10:34:18 -0500 | [diff] [blame] | 30 | |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 31 | namespace SkSL { |
Brian Osman | 9b8b455 | 2019-09-30 13:23:14 -0400 | [diff] [blame] | 32 | class ByteCode; |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame^] | 33 | class ExternalValue; |
John Stiles | a6841be | 2020-08-06 14:11:56 -0400 | [diff] [blame] | 34 | } // namespace SkSL |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 35 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 36 | class SkParticleEffectParams : public SkRefCnt { |
| 37 | public: |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 38 | SkParticleEffectParams(); |
| 39 | |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 40 | // Maximum number of particles per instance of the effect |
| 41 | int fMaxCount; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 42 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 43 | // What is drawn for each particle? (Image, shape, sprite sheet, etc.) |
| 44 | // See SkParticleDrawable::Make* |
Brian Osman | 543d2e2 | 2019-02-15 14:29:38 -0500 | [diff] [blame] | 45 | sk_sp<SkParticleDrawable> fDrawable; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 46 | |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 47 | // Particle behavior is driven by two chunks of SkSL code. Effect functions are defined in |
| 48 | // fEffectCode, and get a mutable Effect struct: |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 49 | // |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 50 | // struct Effect { |
Brian Osman | d1846d8 | 2019-09-17 13:32:32 -0400 | [diff] [blame] | 51 | // float age; // Normalized age of the effect |
| 52 | // float lifetime; // Effect's duration, in seconds - script should set this in effectSpawn |
| 53 | // int loop; // Number of loops that have elapsed (0 on initial spawn) |
| 54 | // float rate; // Rate to generate new particles (particles / second) |
| 55 | // int burst; // Number of particles to emit in a single update |
| 56 | // // Set during spawn to emit that many at once on each loop |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 57 | // |
| 58 | // // Everything below this line controls the state of the effect, which is also the |
| 59 | // // default values for new particles. |
| 60 | // float2 pos = { 0, 0 }; // Local position |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 61 | // float2 dir = { 0, -1 }; // Heading. Should be a normalized vector. |
| 62 | // float scale = 1; // Size, normalized relative to the drawable's native size |
| 63 | // float2 vel = { 0, 0 }; // Linear velocity, in (units / second) |
| 64 | // float spin = 0; // Angular velocity, in (radians / second) |
| 65 | // float4 color = { 1, 1, 1, 1 }; // RGBA color |
| 66 | // float frame = 0; // Normalized sprite index for multi-frame drawables |
Brian Osman | 95c26ef | 2020-02-10 13:45:22 -0500 | [diff] [blame] | 67 | // uint flags = 0; // Arbitrary state for use by script |
| 68 | // uint seed = 0; // Random seed, used with rand() (see below) |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 69 | // }; |
| 70 | // |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 71 | // Particle functions are defined in fParticleCode, and get a mutable Particle struct, as well |
| 72 | // as a uniform copy of the current Effect, named 'effect'. |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 73 | // |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 74 | // struct Particle { |
| 75 | // float age; |
| 76 | // float lifetime; |
| 77 | // float2 pos; |
| 78 | // float2 dir; |
| 79 | // float scale; |
| 80 | // float2 vel; |
| 81 | // float spin; |
| 82 | // float4 color; |
| 83 | // float frame; |
Brian Osman | 95c26ef | 2020-02-10 13:45:22 -0500 | [diff] [blame] | 84 | // uint flags; |
| 85 | // uint seed; |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 86 | // }; |
| 87 | // |
Brian Osman | 95c26ef | 2020-02-10 13:45:22 -0500 | [diff] [blame] | 88 | // All functions have access to a global function named 'rand'. It takes a uint seed value, |
| 89 | // which it uses and updates (using a linear congruential RNG). It returns a random floating |
| 90 | // point value in [0, 1]. Typical usage is to pass the particle or effect's seed value to rand. |
| 91 | // For particle functions, the seed is rewound after each update, so calls to 'rand(p.seed)' |
| 92 | // will return consistent values from one update to the next. |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 93 | // |
| 94 | // Finally, there is one global uniform values available, 'dt'. This is a floating point |
| 95 | // number of seconds that have elapsed since the last update. |
| 96 | // |
| 97 | // Effect code should define two functions: |
| 98 | // |
| 99 | // 'void effectSpawn(inout Effect e)' is called when an instance of the effect is first |
| 100 | // created, and again at every loop point (if the effect is played with the looping flag). |
| 101 | // |
| 102 | // 'void effectUpdate(inout Effect e)' is called once per update to adjust properties of the |
| 103 | // effect (ie emitter). |
| 104 | // |
| 105 | // Particle code should also define two functions: |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 106 | // |
| 107 | // 'void spawn(inout Particle p)' is called once for each particle when it is first created, |
| 108 | // 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] | 109 | // 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] | 110 | // |
| 111 | // 'void update(inout Particle p)' is called for each particle on every call to the running |
| 112 | // SkParticleEffect's update() method. It can animate any of the particle's values. Note that |
| 113 | // 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] | 114 | |
| 115 | SkString fEffectCode; |
| 116 | SkString fParticleCode; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 117 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame] | 118 | // External objects accessible by the effect's SkSL code. Each binding is a name and particular |
| 119 | // kind of object. See SkParticleBinding::Make* for details. |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 120 | SkTArray<sk_sp<SkParticleBinding>> fBindings; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 121 | |
| 122 | void visitFields(SkFieldVisitor* v); |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 123 | |
Brian Osman | 9dac0d8 | 2019-12-02 16:52:51 -0500 | [diff] [blame] | 124 | // Load/compute cached resources |
| 125 | void prepare(const skresources::ResourceProvider*); |
| 126 | |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 127 | private: |
| 128 | friend class SkParticleEffect; |
| 129 | |
| 130 | // Cached |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 131 | struct Program { |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 132 | std::unique_ptr<SkSL::ByteCode> fByteCode; |
Brian Osman | 32d5355 | 2020-09-23 13:55:20 -0400 | [diff] [blame^] | 133 | std::vector<std::unique_ptr<SkSL::ExternalValue>> fExternalValues; |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 134 | }; |
| 135 | |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 136 | Program fEffectProgram; |
| 137 | Program fParticleProgram; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | class SkParticleEffect : public SkRefCnt { |
| 141 | public: |
Brian Osman | 95c26ef | 2020-02-10 13:45:22 -0500 | [diff] [blame] | 142 | SkParticleEffect(sk_sp<SkParticleEffectParams> params); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 143 | |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 144 | // Start playing this effect, specifying initial values for the emitter's properties |
| 145 | void start(double now, bool looping, SkPoint position, SkVector heading, float scale, |
Brian Osman | 95c26ef | 2020-02-10 13:45:22 -0500 | [diff] [blame] | 146 | SkVector velocity, float spin, SkColor4f color, float frame, uint32_t flags, |
| 147 | uint32_t seed); |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 148 | |
| 149 | // Start playing this effect, with default values for the emitter's properties |
| 150 | void start(double now, bool looping) { |
| 151 | this->start(now, looping, |
| 152 | { 0.0f, 0.0f }, // position |
| 153 | { 0.0f, -1.0f }, // heading |
| 154 | 1.0f, // scale |
| 155 | { 0.0f, 0.0f }, // velocity |
| 156 | 0.0f, // spin |
| 157 | { 1.0f, 1.0f, 1.0f, 1.0f }, // color |
Brian Osman | 559ffe4 | 2019-09-25 11:24:50 -0400 | [diff] [blame] | 158 | 0.0f, // sprite frame |
Brian Osman | 95c26ef | 2020-02-10 13:45:22 -0500 | [diff] [blame] | 159 | 0, // flags |
| 160 | 0); // seed |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 161 | } |
| 162 | |
Kevin Lubick | 269fe89 | 2019-03-06 09:32:55 -0500 | [diff] [blame] | 163 | void update(double now); |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 164 | void draw(SkCanvas* canvas); |
| 165 | |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 166 | bool isAlive(bool includeSubEffects = true) const { |
| 167 | return (fState.fAge >= 0 && fState.fAge <= 1) |
| 168 | || (includeSubEffects && !fSubEffects.empty()); |
| 169 | } |
Brian Osman | b77d502 | 2019-03-06 11:08:48 -0500 | [diff] [blame] | 170 | int getCount() const { return fCount; } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 171 | |
Brian Osman | df18296 | 2019-10-15 10:27:59 -0400 | [diff] [blame] | 172 | float getRate() const { return fState.fRate; } |
| 173 | int getBurst() const { return fState.fBurst; } |
| 174 | SkPoint getPosition() const { return fState.fPosition; } |
| 175 | SkVector getHeading() const { return fState.fHeading; } |
| 176 | float getScale() const { return fState.fScale; } |
| 177 | SkVector getVelocity() const { return fState.fVelocity; } |
| 178 | float getSpin() const { return fState.fSpin; } |
| 179 | SkColor4f getColor() const { return fState.fColor; } |
| 180 | float getFrame() const { return fState.fFrame; } |
| 181 | uint32_t getFlags() const { return fState.fFlags; } |
| 182 | |
| 183 | void setRate (float r) { fState.fRate = r; } |
| 184 | void setBurst (int b) { fState.fBurst = b; } |
| 185 | void setPosition(SkPoint p) { fState.fPosition = p; } |
| 186 | void setHeading (SkVector h) { fState.fHeading = h; } |
| 187 | void setScale (float s) { fState.fScale = s; } |
| 188 | void setVelocity(SkVector v) { fState.fVelocity = v; } |
| 189 | void setSpin (float s) { fState.fSpin = s; } |
| 190 | void setColor (SkColor4f c) { fState.fColor = c; } |
| 191 | void setFrame (float f) { fState.fFrame = f; } |
| 192 | void setFlags (uint32_t f) { fState.fFlags = f; } |
| 193 | |
Brian Osman | b08cc02 | 2020-04-02 11:38:40 -0400 | [diff] [blame] | 194 | const SkSL::ByteCode* effectCode() const { return fParams->fEffectProgram.fByteCode.get(); } |
| 195 | const SkSL::ByteCode* particleCode() const { return fParams->fParticleProgram.fByteCode.get(); } |
Brian Osman | 5b43113 | 2019-10-15 16:41:18 -0400 | [diff] [blame] | 196 | |
| 197 | float* effectUniforms() { return fEffectUniforms.data(); } |
| 198 | float* particleUniforms() { return fParticleUniforms.data(); } |
| 199 | |
Brian Osman | 2aa85df | 2019-08-30 10:59:47 -0400 | [diff] [blame] | 200 | static void RegisterParticleTypes(); |
| 201 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 202 | private: |
| 203 | void setCapacity(int capacity); |
| 204 | |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 205 | // Helpers to break down update |
| 206 | void advanceTime(double now); |
| 207 | |
| 208 | void processEffectSpawnRequests(double now); |
Brian Osman | df18296 | 2019-10-15 10:27:59 -0400 | [diff] [blame] | 209 | void runEffectScript(double now, const char* entry); |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 210 | |
| 211 | void processParticleSpawnRequests(double now, int start); |
| 212 | void runParticleScript(double now, const char* entry, int start, int count); |
| 213 | |
Brian Osman | d12f278 | 2019-11-27 10:34:18 -0500 | [diff] [blame] | 214 | sk_sp<SkParticleEffectParams> fParams; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 215 | |
Brian Osman | 5c1f8eb | 2019-02-14 14:49:55 -0500 | [diff] [blame] | 216 | bool fLooping; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 217 | int fCount; |
| 218 | double fLastTime; |
| 219 | float fSpawnRemainder; |
| 220 | |
Brian Osman | 5b43113 | 2019-10-15 16:41:18 -0400 | [diff] [blame] | 221 | // C++ version of the SkSL Effect struct. This is the inout parameter to per-effect scripts, |
| 222 | // and provided as a uniform (named 'effect') to the per-particle scripts. |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 223 | struct EffectState { |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 224 | float fAge; |
| 225 | float fLifetime; |
| 226 | int fLoopCount; |
| 227 | float fRate; |
| 228 | int fBurst; |
| 229 | |
| 230 | // Properties that determine default values for new particles |
| 231 | SkPoint fPosition; |
| 232 | SkVector fHeading; |
| 233 | float fScale; |
| 234 | SkVector fVelocity; |
| 235 | float fSpin; |
| 236 | SkColor4f fColor; |
| 237 | float fFrame; |
Brian Osman | 559ffe4 | 2019-09-25 11:24:50 -0400 | [diff] [blame] | 238 | uint32_t fFlags; |
Brian Osman | 95c26ef | 2020-02-10 13:45:22 -0500 | [diff] [blame] | 239 | uint32_t fRandom; |
Brian Osman | d46cb97 | 2019-09-12 16:25:52 -0400 | [diff] [blame] | 240 | }; |
| 241 | EffectState fState; |
| 242 | |
Brian Osman | 95c26ef | 2020-02-10 13:45:22 -0500 | [diff] [blame] | 243 | SkParticles fParticles; |
| 244 | SkAutoTMalloc<float> fStableRandoms; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 245 | |
| 246 | // Cached |
| 247 | int fCapacity; |
Brian Osman | 5b43113 | 2019-10-15 16:41:18 -0400 | [diff] [blame] | 248 | SkTArray<float, true> fEffectUniforms; |
| 249 | SkTArray<float, true> fParticleUniforms; |
Brian Osman | 9a8b846 | 2019-09-19 10:06:36 -0400 | [diff] [blame] | 250 | |
| 251 | // Private interface used by SkEffectBinding and SkEffectExternalValue to spawn sub effects |
| 252 | friend class SkEffectExternalValue; |
| 253 | struct SpawnRequest { |
| 254 | SpawnRequest(int index, bool loop, sk_sp<SkParticleEffectParams> params) |
| 255 | : fIndex(index) |
| 256 | , fLoop(loop) |
| 257 | , fParams(std::move(params)) {} |
| 258 | |
| 259 | int fIndex; |
| 260 | bool fLoop; |
| 261 | sk_sp<SkParticleEffectParams> fParams; |
| 262 | }; |
| 263 | void addSpawnRequest(int index, bool loop, sk_sp<SkParticleEffectParams> params) { |
| 264 | fSpawnRequests.emplace_back(index, loop, std::move(params)); |
| 265 | } |
| 266 | SkTArray<SpawnRequest> fSpawnRequests; |
| 267 | |
| 268 | SkTArray<sk_sp<SkParticleEffect>> fSubEffects; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 269 | }; |
| 270 | |
| 271 | #endif // SkParticleEffect_DEFINED |