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