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 11 #include "include/core/SkRefCnt.h" 12 #include "include/core/SkString.h" 13 #include "include/private/SkTArray.h" 14 #include "include/private/SkTemplates.h" 15 #include "include/utils/SkRandom.h" 16 #include "modules/particles/include/SkParticleData.h" 17 #include "modules/particles/include/SkReflected.h" 18 19 #include <memory> 20 21 class SkCanvas; 22 struct SkCurve; 23 struct SkColorCurve; 24 class SkParticleDrawable; 25 class SkParticleExternalValue; 26 27 namespace SkSL { 28 struct ByteCode; 29 class Compiler; 30 } 31 32 class SkParticleBinding : public SkReflected { 33 public: fName(name)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 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 63 protected: 64 SkString fName; 65 }; 66 67 class SkParticleEffectParams : public SkRefCnt { 68 public: 69 SkParticleEffectParams(); 70 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? 74 75 // What is drawn for each particle? (Image, shape, sprite sheet, etc.) 76 // See SkParticleDrawable::Make* 77 sk_sp<SkParticleDrawable> fDrawable; 78 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. 111 SkString fCode; 112 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. 115 SkTArray<sk_sp<SkParticleBinding>> fBindings; 116 117 void visitFields(SkFieldVisitor* v); 118 119 private: 120 friend class SkParticleEffect; 121 122 // Cached 123 std::unique_ptr<SkSL::ByteCode> fByteCode; 124 SkTArray<std::unique_ptr<SkParticleExternalValue>> fExternalValues; 125 126 void rebuild(); 127 }; 128 129 class SkParticleEffect : public SkRefCnt { 130 public: 131 SkParticleEffect(sk_sp<SkParticleEffectParams> params, const SkRandom& random); 132 133 void start(double now, bool looping = false); 134 void update(double now); 135 void draw(SkCanvas* canvas); 136 isAlive()137 bool isAlive() const { return fSpawnTime >= 0; } getCount()138 int getCount() const { return fCount; } 139 140 private: 141 void setCapacity(int capacity); 142 143 sk_sp<SkParticleEffectParams> fParams; 144 145 SkRandom fRandom; 146 147 bool fLooping; 148 double fSpawnTime; 149 150 int fCount; 151 double fLastTime; 152 float fSpawnRemainder; 153 154 SkParticles fParticles; 155 SkAutoTMalloc<SkRandom> fStableRandoms; 156 157 // Cached 158 int fCapacity; 159 }; 160 161 #endif // SkParticleEffect_DEFINED 162