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
|
|
|
{
|
|
|
|
"MaxCount": 128,
|
|
|
|
"Drawable": {
|
|
|
|
"Type": "SkCircleDrawable",
|
|
|
|
"Radius": 2
|
|
|
|
},
|
|
|
|
"EffectCode": [
|
|
|
|
"void effectSpawn(inout Effect effect) {",
|
|
|
|
" if (effect.loop == 0) {",
|
|
|
|
" cloud(true);",
|
|
|
|
" }",
|
|
|
|
" effect.color = float4(0.1, 0.1, 1.0, 1.0);",
|
|
|
|
" effect.rate = 10;",
|
|
|
|
"}",
|
|
|
|
""
|
|
|
|
],
|
|
|
|
"Code": [
|
|
|
|
"void spawn(inout Particle p) {",
|
|
|
|
" p.lifetime = 4;",
|
|
|
|
" p.pos.x = mix(-50, 50, rand);",
|
|
|
|
" p.vel.y = 50;",
|
|
|
|
"}",
|
|
|
|
"",
|
2019-09-25 15:24:50 +00:00
|
|
|
"bool once(bool cond, inout uint flags, uint flag) {",
|
|
|
|
" bool result = false;",
|
|
|
|
" if (cond && (flags & flag) == 0) {",
|
|
|
|
" flags |= flag;",
|
|
|
|
" result = true;",
|
|
|
|
" }",
|
|
|
|
" return result;",
|
|
|
|
"}",
|
|
|
|
"",
|
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 update(inout Particle p) {",
|
2019-10-07 13:38:30 +00:00
|
|
|
" p.vel.y += 20 * dt;",
|
2019-09-25 15:24:50 +00:00
|
|
|
" if (once(p.pos.y > 150, p.flags, 0x1)) {",
|
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
|
|
|
" p.scale = 0;",
|
|
|
|
" splash(false);",
|
|
|
|
" }",
|
|
|
|
"}",
|
|
|
|
""
|
|
|
|
],
|
|
|
|
"Bindings": [
|
|
|
|
{
|
|
|
|
"Type": "SkEffectBinding",
|
|
|
|
"Name": "cloud",
|
|
|
|
"MaxCount": 60,
|
|
|
|
"Drawable": {
|
|
|
|
"Type": "SkCircleDrawable",
|
|
|
|
"Radius": 16
|
|
|
|
},
|
|
|
|
"EffectCode": [
|
|
|
|
"void effectSpawn(inout Effect effect) {",
|
|
|
|
" effect.color = float4(1, 1, 1, 1);",
|
|
|
|
" effect.rate = 30;",
|
|
|
|
"}",
|
|
|
|
""
|
|
|
|
],
|
|
|
|
"Code": [
|
|
|
|
"float2 circle() {",
|
|
|
|
" float2 xy;",
|
|
|
|
" do {",
|
|
|
|
" xy.x = 2 * rand - 1;",
|
|
|
|
" xy.y = 2 * rand - 1;",
|
|
|
|
" } while (dot(xy, xy) > 1);",
|
|
|
|
" return xy;",
|
|
|
|
"}",
|
|
|
|
"",
|
|
|
|
"void spawn(inout Particle p) {",
|
|
|
|
" p.lifetime = 2.5;",
|
|
|
|
" p.pos = circle() * float2(50, 10);",
|
|
|
|
" p.vel.x = mix(-10, 10, rand);",
|
|
|
|
" p.vel.y = mix(-10, 10, rand);",
|
|
|
|
"}",
|
|
|
|
"",
|
|
|
|
"void update(inout Particle p) {",
|
|
|
|
" p.color.a = 1 - (length(p.pos) / 150);",
|
|
|
|
"}",
|
|
|
|
""
|
|
|
|
],
|
|
|
|
"Bindings": []
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"Type": "SkEffectBinding",
|
|
|
|
"Name": "splash",
|
|
|
|
"MaxCount": 8,
|
|
|
|
"Drawable": {
|
|
|
|
"Type": "SkCircleDrawable",
|
|
|
|
"Radius": 1
|
|
|
|
},
|
|
|
|
"EffectCode": [
|
|
|
|
"void effectSpawn(inout Effect effect) {",
|
|
|
|
" effect.burst = 8;",
|
|
|
|
" effect.scale = 1;",
|
|
|
|
"}",
|
|
|
|
""
|
|
|
|
],
|
|
|
|
"Code": [
|
|
|
|
"void spawn(inout Particle p) {",
|
|
|
|
" p.lifetime = rand;",
|
|
|
|
" float a = radians(mix(-80, 80, rand) - 90);",
|
|
|
|
" p.vel.x = cos(a) * 20;",
|
|
|
|
" p.vel.y = sin(a) * 20;",
|
|
|
|
"}",
|
|
|
|
"",
|
|
|
|
"void update(inout Particle p) {",
|
|
|
|
" p.vel.y += dt * 20;",
|
|
|
|
"}",
|
|
|
|
""
|
|
|
|
],
|
|
|
|
"Bindings": []
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|