Tweak ParticlesSlide's resource provider to show how to use PathBinding
Adds an example effect that spawns particles along an SkPath. Change-Id: I53f3c02fefec814bd9e16f3ac593eac4cf6a297c Reviewed-on: https://skia-review.googlesource.com/c/skia/+/341418 Commit-Queue: Brian Osman <brianosman@google.com> Reviewed-by: Brian Osman <brianosman@google.com>
This commit is contained in:
parent
b21fac2481
commit
e7f68f3644
35
resources/particles/path_spawn.json
Normal file
35
resources/particles/path_spawn.json
Normal file
@ -0,0 +1,35 @@
|
||||
{
|
||||
"MaxCount": 800,
|
||||
"Drawable": {
|
||||
"Type": "SkCircleDrawable",
|
||||
"Radius": 2
|
||||
},
|
||||
"EffectCode": [
|
||||
"void effectSpawn(inout Effect effect) {",
|
||||
" effect.lifetime = 1;",
|
||||
" effect.rate = 100;",
|
||||
"}",
|
||||
""
|
||||
],
|
||||
"Code": [
|
||||
"void spawn(inout Particle p) {",
|
||||
" float4 pos_and_normal = my_path(rand(p.seed));",
|
||||
" p.pos = pos_and_normal.xy;",
|
||||
" p.vel = pos_and_normal.zw * -5;",
|
||||
"",
|
||||
" p.lifetime = 2;",
|
||||
"}",
|
||||
"",
|
||||
"void update(inout Particle p) {",
|
||||
"}",
|
||||
""
|
||||
],
|
||||
"Bindings": [
|
||||
{
|
||||
"Type": "SkPathBinding",
|
||||
"Name": "my_path",
|
||||
"PathPath": "",
|
||||
"PathName": "star"
|
||||
}
|
||||
]
|
||||
}
|
@ -16,12 +16,44 @@
|
||||
#include "src/sksl/SkSLByteCode.h"
|
||||
#include "src/utils/SkOSPath.h"
|
||||
#include "tools/Resources.h"
|
||||
#include "tools/ToolUtils.h"
|
||||
#include "tools/viewer/ImGuiLayer.h"
|
||||
|
||||
#include "imgui.h"
|
||||
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
using namespace sk_app;
|
||||
|
||||
class TestingResourceProvider : public skresources::ResourceProvider {
|
||||
public:
|
||||
TestingResourceProvider() {}
|
||||
|
||||
sk_sp<SkData> load(const char resource_path[], const char resource_name[]) const override {
|
||||
auto it = fResources.find(resource_name);
|
||||
if (it != fResources.end()) {
|
||||
return it->second;
|
||||
} else {
|
||||
return GetResourceAsData(SkOSPath::Join(resource_path, resource_name).c_str());
|
||||
}
|
||||
}
|
||||
|
||||
sk_sp<skresources::ImageAsset> loadImageAsset(const char resource_path[],
|
||||
const char resource_name[],
|
||||
const char /*resource_id*/[]) const override {
|
||||
auto data = this->load(resource_path, resource_name);
|
||||
return skresources::MultiFrameImageAsset::Make(data);
|
||||
}
|
||||
|
||||
void addPath(const char resource_name[], const SkPath& path) {
|
||||
fResources[resource_name] = path.serialize();
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<std::string, sk_sp<SkData>> fResources;
|
||||
};
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
static int InputTextCallback(ImGuiInputTextCallbackData* data) {
|
||||
@ -171,7 +203,11 @@ ParticlesSlide::ParticlesSlide() {
|
||||
// Register types for serialization
|
||||
SkParticleEffect::RegisterParticleTypes();
|
||||
fName = "Particles";
|
||||
fResourceProvider = skresources::FileResourceProvider::Make(GetResourcePath());
|
||||
auto provider = sk_make_sp<TestingResourceProvider>();
|
||||
SkPath star = ToolUtils::make_star({ 0, 0, 100, 100 }, 5);
|
||||
star.close();
|
||||
provider->addPath("star", star);
|
||||
fResourceProvider = provider;
|
||||
}
|
||||
|
||||
void ParticlesSlide::loadEffects(const char* dirname) {
|
||||
|
Loading…
Reference in New Issue
Block a user