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" |
| 17 | #include "modules/particles/include/SkReflected.h" |
| 18 | |
| 19 | #include <memory> |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 20 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 21 | class SkCanvas; |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame^] | 22 | struct SkCurve; |
| 23 | struct SkColorCurve; |
Brian Osman | 543d2e2 | 2019-02-15 14:29:38 -0500 | [diff] [blame] | 24 | class SkParticleDrawable; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 25 | class SkParticleExternalValue; |
| 26 | |
| 27 | namespace SkSL { |
| 28 | struct ByteCode; |
| 29 | class Compiler; |
| 30 | } |
| 31 | |
| 32 | class SkParticleBinding : public SkReflected { |
| 33 | public: |
| 34 | SkParticleBinding(const char* name = "name") : fName(name) {} |
| 35 | |
| 36 | REFLECTED_ABSTRACT(SkParticleBinding, SkReflected) |
| 37 | |
| 38 | void visitFields(SkFieldVisitor* v) override; |
| 39 | virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0; |
| 40 | |
| 41 | static void RegisterBindingTypes(); |
| 42 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame^] | 43 | /* |
| 44 | * All SkParticleBinding objects expose a particular native object to an effect's SkSL code. |
| 45 | * In all cases, the 'name' is the symbol that will be used to access the object from the SkSL. |
| 46 | * Each binding is a callable object, so the SkSL name behaves like a function. The behavior of |
| 47 | * each kind of binding is described below. |
| 48 | */ |
| 49 | |
| 50 | // Binds an SkCurve to an effect's SkSL. The curve is a one-dimensional function, described |
| 51 | // in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a single float value. |
| 52 | static sk_sp<SkParticleBinding> MakeCurve(const char* name, const SkCurve& curve); |
| 53 | |
| 54 | // Binds an SkColorCurve to an effect's SkSL. The curve is a one-dimensional, function, |
| 55 | // described in SkCurve.h. It is called in the SkSL as 'name(t)', and returns a float4 value. |
| 56 | static sk_sp<SkParticleBinding> MakeColorCurve(const char* name, const SkColorCurve& curve); |
| 57 | |
| 58 | // Binds an SkPath to an effect's SkSL. The path is specified using SVG syntax. It is called |
| 59 | // in the SkSL as 'name(t)'. 't' is a normalized distance along the path. This returns a float4 |
| 60 | // value, containing the position in .xy, and the normal in .zw. |
| 61 | static sk_sp<SkParticleBinding> MakePathBinding(const char* name, const char* path); |
| 62 | |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 63 | protected: |
| 64 | SkString fName; |
| 65 | }; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 66 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 67 | class SkParticleEffectParams : public SkRefCnt { |
| 68 | public: |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 69 | SkParticleEffectParams(); |
| 70 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame^] | 71 | int fMaxCount; // Maximum number of particles per instance of the effect |
| 72 | float fEffectDuration; // How long does the effect last after being played, in seconds? |
| 73 | float fRate; // How many particles are emitted per second? |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 74 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame^] | 75 | // What is drawn for each particle? (Image, shape, sprite sheet, etc.) |
| 76 | // See SkParticleDrawable::Make* |
Brian Osman | 543d2e2 | 2019-02-15 14:29:38 -0500 | [diff] [blame] | 77 | sk_sp<SkParticleDrawable> fDrawable; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 78 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame^] | 79 | // Particle behavior is driven by two SkSL functions defined in the fCode string. |
| 80 | // Both functions get a mutable Particle struct: |
| 81 | // |
| 82 | // struct Particle { |
| 83 | // float age; |
| 84 | // float lifetime; |
| 85 | // float2 pos = { 0, 0 }; // Local position, relative to the effect. |
| 86 | // float2 dir = { 0, -1 }; // Heading. Should be a normalized vector. |
| 87 | // float scale = 1; // Size, normalized relative to the drawable's native size |
| 88 | // float2 vel = { 0, 0 }; // Linear velocity, in (units / second) |
| 89 | // float spin = 0; // Angular velocity, in (radians / second) |
| 90 | // float4 color = { 1, 1, 1, 1 }; // RGBA color |
| 91 | // float frame = 0; // Normalized sprite index for multi-frame drawables |
| 92 | // }; |
| 93 | // |
| 94 | // In addition, both functions have access to a global variable named 'rand'. Every read of |
| 95 | // 'rand' returns a random floating point value in [0, 1). The random generator is stored |
| 96 | // per-particle, and the state is rewound after each update, so calls to 'rand' will return |
| 97 | // consistent values from one update to the next. |
| 98 | // |
| 99 | // Finally, there are two global uniform values available. The first is 'dt', a floating point |
| 100 | // number of seconds that have elapsed since the last update. The second is 'effectAge', which |
| 101 | // is the normalized age of the effect (not particle). For looping effects, this will wrap |
| 102 | // back to zero when the effect's age exceeds its duration. |
| 103 | // |
| 104 | // 'void spawn(inout Particle p)' is called once for each particle when it is first created, |
| 105 | // to set initial values. At a minimum, this should set 'lifetime' to the number of seconds |
| 106 | // that the particle will exist. Other parameters have defaults shown above. |
| 107 | // |
| 108 | // 'void update(inout Particle p)' is called for each particle on every call to the running |
| 109 | // SkParticleEffect's update() method. It can animate any of the particle's values. Note that |
| 110 | // 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] | 111 | SkString fCode; |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 112 | |
Brian Osman | 3da607e | 2019-07-26 15:11:46 -0400 | [diff] [blame^] | 113 | // External objects accessible by the effect's SkSL code. Each binding is a name and particular |
| 114 | // kind of object. See SkParticleBinding::Make* for details. |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 115 | SkTArray<sk_sp<SkParticleBinding>> fBindings; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 116 | |
| 117 | void visitFields(SkFieldVisitor* v); |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 118 | |
| 119 | private: |
| 120 | friend class SkParticleEffect; |
| 121 | |
| 122 | // Cached |
Brian Osman | e59acb7 | 2019-07-25 16:58:46 -0400 | [diff] [blame] | 123 | std::unique_ptr<SkSL::ByteCode> fByteCode; |
| 124 | SkTArray<std::unique_ptr<SkParticleExternalValue>> fExternalValues; |
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 | b77d502 | 2019-03-06 11:08:48 -0500 | [diff] [blame] | 137 | bool isAlive() const { return fSpawnTime >= 0; } |
| 138 | int getCount() const { return fCount; } |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 139 | |
| 140 | private: |
| 141 | void setCapacity(int capacity); |
| 142 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 143 | sk_sp<SkParticleEffectParams> fParams; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 144 | |
Brian Osman | 5c1f8eb | 2019-02-14 14:49:55 -0500 | [diff] [blame] | 145 | SkRandom fRandom; |
| 146 | |
| 147 | bool fLooping; |
| 148 | double fSpawnTime; |
| 149 | |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 150 | int fCount; |
| 151 | double fLastTime; |
| 152 | float fSpawnRemainder; |
| 153 | |
Brian Osman | fe49163 | 2019-07-25 15:14:50 -0400 | [diff] [blame] | 154 | SkParticles fParticles; |
| 155 | SkAutoTMalloc<SkRandom> fStableRandoms; |
Brian Osman | 7c979f5 | 2019-02-12 13:27:51 -0500 | [diff] [blame] | 156 | |
| 157 | // Cached |
| 158 | int fCapacity; |
| 159 | }; |
| 160 | |
| 161 | #endif // SkParticleEffect_DEFINED |