Skip to content

Commit

Permalink
Add flame particle
Browse files Browse the repository at this point in the history
  • Loading branch information
Ravbug committed Jun 13, 2024
1 parent 9d42e98 commit 90b25c2
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 5 deletions.
2 changes: 1 addition & 1 deletion RavEngine
1 change: 1 addition & 0 deletions Samples/Rendering/Attributions.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
### Asset attributions
- Helmet model and textures from [KhronosGroup/glTF-Sample-Assets](https://github.com/KhronosGroup/glTF-Sample-Assets)
- smoke particle texture by Freepik: https://www.freepik.com/free-vector/cartoon-smoke-element-animation-frames_13763535.htm
- fire particle texture by macrovector: https://www.freepik.com/free-vector/light-fire-flames-animation-collection_10271988.htm
26 changes: 22 additions & 4 deletions Samples/Rendering/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,21 @@ struct RenderingApp : public RavEngine::App {


struct SmokeParticleMaterial : public RavEngine::BillboardParticleMaterial {
SmokeParticleMaterial() : BillboardParticleMaterial("SmokeParticleInit", "SmokeParticleUpdate","default_billboard_particle","default_billboard_particle") {} //TODO: fill with shader names
SmokeParticleMaterial() : BillboardParticleMaterial("SmokeParticleInit", "SmokeParticleUpdate","default_billboard_particle","default_billboard_particle") {}

uint16_t ParticleUserDataSize() const final {
return 0; // needs no extra data
}
};

struct FireParticleMaterial : public RavEngine::BillboardParticleMaterial {
FireParticleMaterial() : BillboardParticleMaterial("FireParticleInit", "FireParticleUpdate", "default_billboard_particle", "default_billboard_particle") {};

uint16_t ParticleUserDataSize() const final {
return 0;
}
};

struct Level : public RavEngine::World {

GameObject camRoot, camHeadUD;
Expand Down Expand Up @@ -104,17 +112,27 @@ struct Level : public RavEngine::World {

lightsEntity.GetTransform().LocalRotateDelta(vector3{ deg_to_rad(45), deg_to_rad(45),0 });

auto continuousParticleEntity = Instantiate<GameObject>();
auto smokeParticleEntity = Instantiate<GameObject>();
auto smokeMat = RavEngine::New<SmokeParticleMaterial>();
smokeMat->spriteTex = Texture::Manager::Get("smoke.png");
smokeMat->spriteDim = {
.numSpritesWidth = 3,
.numSpritesHeight = 3
};
auto& smokeEmitter = continuousParticleEntity.EmplaceComponent<ParticleEmitter>(8192, smokeMat); // number of particles we want
auto& smokeEmitter = smokeParticleEntity.EmplaceComponent<ParticleEmitter>(8192, smokeMat); // number of particles we want
//smokeEmitter.mode = ParticleEmitter::Mode::Burst;
smokeEmitter.Play();
smokeParticle = { continuousParticleEntity };
smokeParticle = { smokeParticleEntity };

auto fireParticleEntity = Instantiate<GameObject>();
auto fireMat = New<FireParticleMaterial>();
fireMat->spriteTex = Texture::Manager::Get("fire.png");
fireMat->spriteDim = {
.numSpritesWidth = 11,
.numSpritesHeight = 1
};
auto& fireEmitter = fireParticleEntity.EmplaceComponent<ParticleEmitter>(8192, fireMat);
fireEmitter.Play();

SetupInputs();

Expand Down
43 changes: 43 additions & 0 deletions Samples/Rendering/shaders/FireParticleInit.csh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
struct ParticleData{
vec3 pos;
vec2 scale;
uint animationFrame;
};

float rand(in vec2 ip) {
const float seed = 12345678;
uvec2 p = uvec2(floatBitsToUint(ip.x), floatBitsToUint(ip.y));
uint s = floatBitsToUint(seed);
s ^= (s ^ ((~p.x << s) + (~p.y << s)));

p ^= (p << 17U);
p ^= ((p ^ s) >> 13U);
p ^= (p << 5U);
p ^= ((s + (s&(p.x^p.y))) >> 3U);
uint n = (p.x*s+p.y)+((p.x ^ p.y) << ~p.x ^ s) + ((p.x ^ p.y) << ~p.y ^ s);
return float(n*50323U) / float(0xFFFFFFFFU);
}
ParticleData init(ParticleInitData initData)
{
uint particleID = initData.particleID;
ParticleData data;
data.scale = vec2(0.1,1);
vec4 pos = vec4(
rand(vec2(particleID,particleID)),
rand(vec2(particleID * 2,particleID * 2)),
rand(vec2(particleID / 2,particleID / 2)),
1
);
pos = initData.emitterModel * pos;
data.pos = pos.xyz;
data.animationFrame = 0;
return data;
}
5 changes: 5 additions & 0 deletions Samples/Rendering/shaders/FireParticleInit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"shader": "FireParticleInit.csh",
"type": "particle-init",
"stage": "compute"
}
59 changes: 59 additions & 0 deletions Samples/Rendering/shaders/FireParticleUpdate.csh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include "ravengine_shader.glsl"

layout(push_constant, std430) uniform UniformBufferObject{
float fpsScale;
} ubo;


struct ParticleData{
vec3 pos;
vec2 scale;
uint animationFrame;
};


float rand(in vec2 ip) {
const float seed = 12345678;
uvec2 p = uvec2(floatBitsToUint(ip.x), floatBitsToUint(ip.y));
uint s = floatBitsToUint(seed);
s ^= (s ^ ((~p.x << s) + (~p.y << s)));

p ^= (p << 17U);
p ^= ((p ^ s) >> 13U);
p ^= (p << 5U);
p ^= ((s + (s&(p.x^p.y))) >> 3U);
uint n = (p.x*s+p.y)+((p.x ^ p.y) << ~p.x ^ s) + ((p.x ^ p.y) << ~p.y ^ s);
return float(n*50323U) / float(0xFFFFFFFFU);
}
const float maxLife = 20;
void update(inout ParticleData data, inout float newLife, uint particleID)
{
data.scale += ubo.fpsScale * 0.05;
data.pos.y += ubo.fpsScale * 0.01;
vec2 movevec = vec2(
rand(vec2(particleID, particleID)) * 2 - 1,
rand(vec2(particleID * 2, particleID * 2)) * 2 - 1
);
float life = maxLife;
movevec *= 0.02;
data.pos.x += movevec.x;
data.pos.z += movevec.y;
newLife += ubo.fpsScale;
data.animationFrame = uint(remap(newLife,0,life,0,11));
if (newLife > life){
// destroy the particle
newLife = 0;
}
}
5 changes: 5 additions & 0 deletions Samples/Rendering/shaders/FireParticleUpdate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"shader": "FireParticleUpdate.csh",
"type": "particle-update",
"stage": "compute"
}
Binary file added Samples/Rendering/textures/fire.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 90b25c2

Please sign in to comment.