2019-08-30 14:59:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2019 Google LLC
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SkParticleBinding_DEFINED
|
|
|
|
#define SkParticleBinding_DEFINED
|
|
|
|
|
|
|
|
#include "include/core/SkString.h"
|
|
|
|
#include "modules/particles/include/SkReflected.h"
|
|
|
|
#include "src/sksl/SkSLExternalValue.h"
|
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
|
Particles: Sub-effect spawning and some slight refactoring
* Added a new binding type, SkEffectBinding. This stores another
entire effect params structure (so the JSON is just nested).
The name is a callable value that spawns a new instance of
that effect, inheriting the parameters of the spawning effect
or particle (depending on which kind of script made the call).
* Broke up the monolithic update function into some helpers,
got some code reuse with the script calling logic.
* Unlike particle capacity, there is no upper limit on child
effects (yet), so it's easy to trigger runaway memory and
CPU consumption. Be careful.
* Added death scripts to effects and particles, which are a
common place to want to spawn sub-effects. Like spawn,
these run on each loop, but for one-shots they play at the
end. Even with loops, this is helpful for timing sub-effects
(see fireworks2.json).
* Finally, added a much more comprehensive example effect,
raincloud.json. This includes a total of three effects, to
generate a cloud, raindrops, and splashes when those drops
hit "the ground".
Change-Id: I3d7b72bcbb684642cd9723518b67ab1c7d7a538a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/242479
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2019-09-19 14:06:36 +00:00
|
|
|
class SkParticleEffect;
|
|
|
|
class SkParticleEffectParams;
|
2019-08-30 14:59:47 +00:00
|
|
|
class SkRandom;
|
|
|
|
|
2019-12-02 21:52:51 +00:00
|
|
|
namespace skresources {
|
|
|
|
class ResourceProvider;
|
|
|
|
}
|
|
|
|
|
2019-08-30 14:59:47 +00:00
|
|
|
namespace SkSL {
|
|
|
|
class Compiler;
|
|
|
|
}
|
|
|
|
|
|
|
|
class SkParticleExternalValue : public SkSL::ExternalValue {
|
|
|
|
public:
|
|
|
|
SkParticleExternalValue(const char* name, SkSL::Compiler& compiler, const SkSL::Type& type)
|
|
|
|
: SkSL::ExternalValue(name, type)
|
|
|
|
, fCompiler(compiler)
|
Particles: Sub-effect spawning and some slight refactoring
* Added a new binding type, SkEffectBinding. This stores another
entire effect params structure (so the JSON is just nested).
The name is a callable value that spawns a new instance of
that effect, inheriting the parameters of the spawning effect
or particle (depending on which kind of script made the call).
* Broke up the monolithic update function into some helpers,
got some code reuse with the script calling logic.
* Unlike particle capacity, there is no upper limit on child
effects (yet), so it's easy to trigger runaway memory and
CPU consumption. Be careful.
* Added death scripts to effects and particles, which are a
common place to want to spawn sub-effects. Like spawn,
these run on each loop, but for one-shots they play at the
end. Even with loops, this is helpful for timing sub-effects
(see fireworks2.json).
* Finally, added a much more comprehensive example effect,
raincloud.json. This includes a total of three effects, to
generate a cloud, raindrops, and splashes when those drops
hit "the ground".
Change-Id: I3d7b72bcbb684642cd9723518b67ab1c7d7a538a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/242479
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2019-09-19 14:06:36 +00:00
|
|
|
, fRandom(nullptr)
|
|
|
|
, fEffect(nullptr) {}
|
2019-08-30 14:59:47 +00:00
|
|
|
|
|
|
|
void setRandom(SkRandom* random) { fRandom = random; }
|
Particles: Sub-effect spawning and some slight refactoring
* Added a new binding type, SkEffectBinding. This stores another
entire effect params structure (so the JSON is just nested).
The name is a callable value that spawns a new instance of
that effect, inheriting the parameters of the spawning effect
or particle (depending on which kind of script made the call).
* Broke up the monolithic update function into some helpers,
got some code reuse with the script calling logic.
* Unlike particle capacity, there is no upper limit on child
effects (yet), so it's easy to trigger runaway memory and
CPU consumption. Be careful.
* Added death scripts to effects and particles, which are a
common place to want to spawn sub-effects. Like spawn,
these run on each loop, but for one-shots they play at the
end. Even with loops, this is helpful for timing sub-effects
(see fireworks2.json).
* Finally, added a much more comprehensive example effect,
raincloud.json. This includes a total of three effects, to
generate a cloud, raindrops, and splashes when those drops
hit "the ground".
Change-Id: I3d7b72bcbb684642cd9723518b67ab1c7d7a538a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/242479
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2019-09-19 14:06:36 +00:00
|
|
|
void setEffect(SkParticleEffect* effect) { fEffect = effect; }
|
2019-08-30 14:59:47 +00:00
|
|
|
|
|
|
|
protected:
|
Particles: Sub-effect spawning and some slight refactoring
* Added a new binding type, SkEffectBinding. This stores another
entire effect params structure (so the JSON is just nested).
The name is a callable value that spawns a new instance of
that effect, inheriting the parameters of the spawning effect
or particle (depending on which kind of script made the call).
* Broke up the monolithic update function into some helpers,
got some code reuse with the script calling logic.
* Unlike particle capacity, there is no upper limit on child
effects (yet), so it's easy to trigger runaway memory and
CPU consumption. Be careful.
* Added death scripts to effects and particles, which are a
common place to want to spawn sub-effects. Like spawn,
these run on each loop, but for one-shots they play at the
end. Even with loops, this is helpful for timing sub-effects
(see fireworks2.json).
* Finally, added a much more comprehensive example effect,
raincloud.json. This includes a total of three effects, to
generate a cloud, raindrops, and splashes when those drops
hit "the ground".
Change-Id: I3d7b72bcbb684642cd9723518b67ab1c7d7a538a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/242479
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2019-09-19 14:06:36 +00:00
|
|
|
SkSL::Compiler& fCompiler;
|
|
|
|
|
|
|
|
SkRandom* fRandom;
|
|
|
|
SkParticleEffect* fEffect;
|
2019-08-30 14:59:47 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class SkParticleBinding : public SkReflected {
|
|
|
|
public:
|
|
|
|
SkParticleBinding(const char* name = "name") : fName(name) {}
|
|
|
|
|
|
|
|
REFLECTED_ABSTRACT(SkParticleBinding, SkReflected)
|
|
|
|
|
|
|
|
void visitFields(SkFieldVisitor* v) override;
|
2019-12-02 21:52:51 +00:00
|
|
|
|
2019-08-30 14:59:47 +00:00
|
|
|
virtual std::unique_ptr<SkParticleExternalValue> toValue(SkSL::Compiler&) = 0;
|
2019-12-02 21:52:51 +00:00
|
|
|
virtual void prepare(const skresources::ResourceProvider*) = 0;
|
2019-08-30 14:59:47 +00:00
|
|
|
|
|
|
|
static void RegisterBindingTypes();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* All SkParticleBinding objects expose a particular native object to an effect's SkSL code.
|
|
|
|
* In all cases, the 'name' is the symbol that will be used to access the object from the SkSL.
|
|
|
|
* Each binding is a callable object, so the SkSL name behaves like a function. The behavior of
|
|
|
|
* each kind of binding is described below.
|
|
|
|
*/
|
|
|
|
|
2019-12-03 19:31:37 +00:00
|
|
|
// void name(loop) -- Creates an effect instance. Effect will loop if 'loop' is true, otherwise
|
|
|
|
// it's a one-shot. The new effect inherits all properties from the calling effect or particle.
|
|
|
|
static sk_sp<SkParticleBinding> MakeEffect(const char* name,
|
|
|
|
sk_sp<SkParticleEffectParams> effect);
|
|
|
|
|
|
|
|
// float4 name(xy) -- Fetches RGBA data from an image. 'xy' are normalized image coordinates.
|
|
|
|
static sk_sp<SkParticleBinding> MakeImage(const char* name,
|
|
|
|
const char* imagePath, const char* imageName);
|
|
|
|
|
|
|
|
// float4 name(t) -- Fetches position and normal from an SkPath. 't' is the normalized distance
|
|
|
|
// along the path. The return value contains position in .xy and normal in .zw.
|
|
|
|
static sk_sp<SkParticleBinding> MakePath(const char* name,
|
|
|
|
const char* pathPath, const char* pathName);
|
Particles: Sub-effect spawning and some slight refactoring
* Added a new binding type, SkEffectBinding. This stores another
entire effect params structure (so the JSON is just nested).
The name is a callable value that spawns a new instance of
that effect, inheriting the parameters of the spawning effect
or particle (depending on which kind of script made the call).
* Broke up the monolithic update function into some helpers,
got some code reuse with the script calling logic.
* Unlike particle capacity, there is no upper limit on child
effects (yet), so it's easy to trigger runaway memory and
CPU consumption. Be careful.
* Added death scripts to effects and particles, which are a
common place to want to spawn sub-effects. Like spawn,
these run on each loop, but for one-shots they play at the
end. Even with loops, this is helpful for timing sub-effects
(see fireworks2.json).
* Finally, added a much more comprehensive example effect,
raincloud.json. This includes a total of three effects, to
generate a cloud, raindrops, and splashes when those drops
hit "the ground".
Change-Id: I3d7b72bcbb684642cd9723518b67ab1c7d7a538a
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/242479
Reviewed-by: Michael Ludwig <michaelludwig@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
2019-09-19 14:06:36 +00:00
|
|
|
|
2019-08-30 14:59:47 +00:00
|
|
|
protected:
|
|
|
|
SkString fName;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SkParticleBinding_DEFINED
|