Skip to content

Commit

Permalink
#102 Fix interpolate model particle
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed Oct 8, 2020
1 parent 0b7a02c commit acf249c
Show file tree
Hide file tree
Showing 12 changed files with 137 additions and 55 deletions.
4 changes: 2 additions & 2 deletions Assets/BuiltIn/Shader/Particle/GLSL/ParticleAdditiveFS.d.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ void main(void)
{
vec4 texColor = texture(uTexture, varTexCoord0.xy);
vec3 color = mix(vec3(0.0, 0.0, 0.0), texColor.rgb, texColor.a);
vec3 result = color * varColor.rgb;
FragColor = vec4(sRGB(result * varColor.a), 1.0);
vec3 result = sRGB(color) * sRGB(varColor.rgb);
FragColor = vec4(result * varColor.a, 1.0);
}
4 changes: 2 additions & 2 deletions Assets/BuiltIn/Shader/Particle/GLSL/ParticleAdditiveFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ void main(void)
{
vec4 texColor = texture(uTexture, varTexCoord0.xy);
vec3 color = mix(vec3(0.0, 0.0, 0.0), texColor.rgb, texColor.a);
vec3 result = color * varColor.rgb;
FragColor = vec4(sRGB(result * varColor.a), 1.0);
vec3 result = sRGB(color) * sRGB(varColor.rgb);
FragColor = vec4(result * varColor.a, 1.0);
}
5 changes: 3 additions & 2 deletions Assets/BuiltIn/Shader/Particle/GLSL/ParticleFS.d.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ out vec4 FragColor;

void main(void)
{
vec4 color = texture(uTexture, varTexCoord0.xy) * varColor;
FragColor = vec4(sRGB(color.rgb), color.a);
vec4 color = texture(uTexture, varTexCoord0.xy);
vec3 result = sRGB(color.rgb) * sRGB(varColor.rgb);
FragColor = vec4(result, color.a * varColor.a);
}
5 changes: 3 additions & 2 deletions Assets/BuiltIn/Shader/Particle/GLSL/ParticleFS.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ vec3 linearRGB(vec3 color)
}
void main(void)
{
vec4 color = texture(uTexture, varTexCoord0.xy) * varColor;
FragColor = vec4(sRGB(color.rgb), color.a);
vec4 color = texture(uTexture, varTexCoord0.xy);
vec3 result = sRGB(color.rgb) * sRGB(varColor.rgb);
FragColor = vec4(result, color.a * varColor.a);
}
4 changes: 2 additions & 2 deletions Assets/BuiltIn/Shader/Particle/HLSL/ParticleAdditiveFS.d.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ float4 main(PS_INPUT input) : SV_TARGET
{
float4 texColor = uTexture.Sample(uTextureSampler, input.tex0);
float3 color = lerp(float3(0.0, 0.0, 0.0), texColor.rgb, texColor.a);
float3 result = input.color.rgb * color;
return float4(sRGB(result * input.color.a), 1.0);
float3 result = sRGB(input.color.rgb) * sRGB(color);
return float4(result * input.color.a, 1.0);
}
4 changes: 2 additions & 2 deletions Assets/BuiltIn/Shader/Particle/HLSL/ParticleAdditiveFS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ float4 main(PS_INPUT input) : SV_TARGET
{
float4 texColor = uTexture.Sample(uTextureSampler, input.tex0);
float3 color = lerp(float3(0.0, 0.0, 0.0), texColor.rgb, texColor.a);
float3 result = input.color.rgb * color;
return float4(sRGB(result * input.color.a), 1.0);
float3 result = sRGB(input.color.rgb) * sRGB(color);
return float4(result * input.color.a, 1.0);
}
6 changes: 4 additions & 2 deletions Assets/BuiltIn/Shader/Particle/HLSL/ParticleFS.d.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ struct PS_INPUT

float4 main(PS_INPUT input) : SV_TARGET
{
float4 result = input.color * uTexture.Sample(uTextureSampler, input.tex0);
return float4(sRGB(result.rgb), result.a);
float4 color = uTexture.Sample(uTextureSampler, input.tex0);
float3 result = sRGB(color.rgb) * sRGB(input.color.rgb);

return float4(result, color.a * input.color.a);
}
5 changes: 3 additions & 2 deletions Assets/BuiltIn/Shader/Particle/HLSL/ParticleFS.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ float3 linearRGB(float3 color)
}
float4 main(PS_INPUT input) : SV_TARGET
{
float4 result = input.color * uTexture.Sample(uTextureSampler, input.tex0);
return float4(sRGB(result.rgb), result.a);
float4 color = uTexture.Sample(uTextureSampler, input.tex0);
float3 result = sRGB(color.rgb) * sRGB(input.color.rgb);
return float4(result, color.a * input.color.a);
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,13 @@ namespace Skylicht
{
// interpolate
y = modelInterpolators[j]->interpolate(x);
params[t] = y;
}
else
{
// update param value
params[t] = p->StartValue[t] + (p->EndValue[t] - p->StartValue[t]) * y;
}

// update param value
params[t] = p->StartValue[t] + (p->EndValue[t] - p->StartValue[t]) * y;

if (t == Scale)
{
Expand Down
142 changes: 107 additions & 35 deletions Samples/ParticlesExplosion/Source/SampleParticlesExplosion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,16 @@ void SampleParticlesExplosion::onInitApp()
CGameObject *grid = zone->createEmptyObject();
grid->addComponent<CGridPlane>();

// particles pool
// particles pool
for (int i = 0; i < MAX_POOL; i++)
{
m_particlePool[i] = zone->createEmptyObject()->addComponent<Particle::CParticleComponent>();
initParticleSystem(m_particlePool[i]);
m_particlePool[i]->getGameObject()->setVisible(false);
}

// initFireSystem(zone->createEmptyObject()->addComponent<Particle::CParticleComponent>());

// init font
CGlyphFreetype *freetypeFont = CGlyphFreetype::getInstance();
freetypeFont->initFont("Segoe UI Light", "BuiltIn/Fonts/segoeui/segoeuil.ttf");
Expand Down Expand Up @@ -133,6 +135,76 @@ bool SampleParticlesExplosion::OnEvent(const SEvent& event)
return false;
}

void SampleParticlesExplosion::initFireSystem(Particle::CParticleComponent *ps)
{
ITexture *texture = NULL;
Particle::CFactory *factory = ps->getParticleFactory();

// GROUP: FIRE
Particle::CGroup *fireGroup = ps->createParticleGroup();

Particle::CQuadRenderer *fire = factory->createQuadRenderer();

texture = CTextureManager::getInstance()->getTexture("Particles/Textures/fire2.png");
fire->setMaterialType(Particle::Addtive, Particle::Camera);
fire->setAtlas(2, 2);
fire->SizeX = 0.3f;
fire->SizeY = 0.3f;
fire->getMaterial()->setTexture(0, texture);
fire->getMaterial()->applyMaterial();

fireGroup->setRenderer(fire);
fireGroup->createModel(Particle::ColorR)->setStart(0.8f, 0.9f)->setEnd(0.8f, 0.9f);
fireGroup->createModel(Particle::ColorG)->setStart(0.5f, 0.6f)->setEnd(0.5f, 0.6f);
fireGroup->createModel(Particle::ColorB)->setStart(0.3f);
fireGroup->createModel(Particle::ColorA)->setStart(0.4)->setEnd(0.0f);
fireGroup->createModel(Particle::RotateZ)->setStart(0.0f, 2.0f * core::PI);
fireGroup->createModel(Particle::RotateSpeedZ)->setStart(1.1f, 2.2f);
fireGroup->createModel(Particle::FrameIndex)->setStart(0.0f, 3.0f);
fireGroup->LifeMin = 1.0f;
fireGroup->LifeMax = 1.5f;
fireGroup->Gravity.set(0.0f, 3.0f, 0.0f);

Particle::CInterpolator *fireSizeInterpolate = fireGroup->createInterpolator();
fireSizeInterpolate->addEntry(0.0f, 0.0f);
fireSizeInterpolate->addEntry(0.5f, 3.0f);
fireSizeInterpolate->addEntry(1.0f, 0.0f);
fireGroup->createModel(Particle::Scale)->setInterpolator(fireSizeInterpolate);

// Emitters
// The emitters are arranged so that the fire looks realistic
Particle::CStraightEmitter* fireEmitter1 = factory->createStraightEmitter(core::vector3df(0.0f, 1.0f, 0.0f));
fireEmitter1->setZone(factory->createSphereZone(core::vector3df(0.0f, -1.0f, 0.0f), 0.5f));
fireEmitter1->setFlow(40);
fireEmitter1->setForce(1.0f, 2.5f);

Particle::CStraightEmitter* fireEmitter2 = factory->createStraightEmitter(core::vector3df(1.0f, 0.6f, 0.0f));
fireEmitter2->setZone(factory->createSphereZone(core::vector3df(0.15f, -1.2f, 0.075f), 0.1f));
fireEmitter2->setFlow(15);
fireEmitter2->setForce(0.5f, 1.5f);

Particle::CStraightEmitter* fireEmitter3 = factory->createStraightEmitter(core::vector3df(-0.6f, 0.8f, -0.8f));
fireEmitter3->setZone(factory->createSphereZone(core::vector3df(-0.375f, -1.15f, -0.375f), 0.3f));
fireEmitter3->setFlow(15);
fireEmitter3->setForce(0.5f, 1.5f);

Particle::CStraightEmitter* fireEmitter4 = factory->createStraightEmitter(core::vector3df(-0.8f, 0.5f, 0.2f));
fireEmitter4->setZone(factory->createSphereZone(core::vector3df(-0.255f, -1.2f, 0.225f), 0.2f));
fireEmitter4->setFlow(10);
fireEmitter4->setForce(0.5f, 1.5f);

Particle::CStraightEmitter* fireEmitter5 = factory->createStraightEmitter(core::vector3df(0.1f, 0.8f, -1.0f));
fireEmitter5->setZone(factory->createSphereZone(core::vector3df(-0.075f, -1.2f, -0.3f), 0.2f));
fireEmitter5->setFlow(10);
fireEmitter5->setForce(0.5f, 1.5f);

fireGroup->addEmitter(fireEmitter1);
fireGroup->addEmitter(fireEmitter2);
fireGroup->addEmitter(fireEmitter3);
fireGroup->addEmitter(fireEmitter4);
fireGroup->addEmitter(fireEmitter5);
}

void SampleParticlesExplosion::initParticleSystem(Particle::CParticleComponent *ps)
{
ITexture *texture = NULL;
Expand Down Expand Up @@ -171,7 +243,7 @@ void SampleParticlesExplosion::initParticleSystem(Particle::CParticleComponent *
interpolatorSmokeAlpha->addEntry(0.4f, 0.6f);
interpolatorSmokeAlpha->addEntry(0.6f, 0.6f);
interpolatorSmokeAlpha->addEntry(1.0f, 0.0f);
smokeGroup->createModel(Particle::ColorA)->setStart(0.0f)->setEnd(1.0f)->setInterpolator(interpolatorSmokeAlpha);
smokeGroup->createModel(Particle::ColorA)->setInterpolator(interpolatorSmokeAlpha);

// create emitter
Particle::CRandomEmitter *smokeEmitter = NULL;
Expand All @@ -183,6 +255,37 @@ void SampleParticlesExplosion::initParticleSystem(Particle::CParticleComponent *
smokeEmitter->setZone(sphere);
smokeGroup->addEmitter(smokeEmitter);

// GROUP: FLASH
Particle::CGroup *flashGroup = ps->createParticleGroup();

Particle::CQuadRenderer *flash = factory->createQuadRenderer();
flashGroup->setRenderer(flash);

texture = CTextureManager::getInstance()->getTexture("Particles/Textures/flash.png");
flash->setMaterialType(Particle::Addtive, Particle::Camera);
flash->getMaterial()->setTexture(0, texture);
flash->getMaterial()->applyMaterial();

flashGroup->createModel(Particle::RotateZ)->setStart(0.0f, 2.0f * core::PI);
flashGroup->LifeMin = 0.5f;
flashGroup->LifeMax = 0.5f;

Particle::CInterpolator *flashSizeInterpolator = flashGroup->createInterpolator();
flashSizeInterpolator->addEntry(0.0f, 0.25f);
flashSizeInterpolator->addEntry(0.1f, 1.5f);
flashGroup->createModel(Particle::Scale)->setInterpolator(flashSizeInterpolator);

Particle::CInterpolator *flashAlphaInterpolator = flashGroup->createInterpolator();
flashAlphaInterpolator->addEntry(0.0f, 1.0f);
flashAlphaInterpolator->addEntry(0.4f, 0.0f);
flashGroup->createModel(Particle::ColorA)->setInterpolator(flashAlphaInterpolator);

Particle::CRandomEmitter* randomEmitter = factory->createRandomEmitter();
randomEmitter->setZone(factory->createSphereZone(core::vector3df(), 0.1f));
randomEmitter->setFlow(-1);
randomEmitter->setTank(3);
flashGroup->addEmitter(randomEmitter);

// GROUP: FLAME
Particle::CGroup *flameGroup = ps->createParticleGroup();

Expand All @@ -207,12 +310,12 @@ void SampleParticlesExplosion::initParticleSystem(Particle::CParticleComponent *
flameSizeInterpolator->addEntry(0.0f, 0.25f);
flameSizeInterpolator->addEntry(0.02f, 0.8f);
flameSizeInterpolator->addEntry(1.0f, 1.4f);
flameGroup->createModel(Particle::Scale)->setStart(0.0f)->setEnd(1.0f)->setInterpolator(flameSizeInterpolator);
flameGroup->createModel(Particle::Scale)->setInterpolator(flameSizeInterpolator);

Particle::CInterpolator *flameAlphaInterpolator = flameGroup->createInterpolator();
flameAlphaInterpolator->addEntry(0.5f, 1.0f);
flameAlphaInterpolator->addEntry(1.0f, 0.0f);
flameGroup->createModel(Particle::ColorA)->setStart(0.0f)->setEnd(1.0f)->setInterpolator(flameAlphaInterpolator);
flameGroup->createModel(Particle::ColorA)->setInterpolator(flameAlphaInterpolator);

Particle::CNormalEmitter *flameEmitter = factory->createNormalEmitter(false);
flameEmitter->setFlow(-1.0f);
Expand All @@ -221,37 +324,6 @@ void SampleParticlesExplosion::initParticleSystem(Particle::CParticleComponent *
flameEmitter->setZone(sphere);
flameGroup->addEmitter(flameEmitter);

// GROUP: FLASH
Particle::CGroup *flashGroup = ps->createParticleGroup();

Particle::CQuadRenderer *flash = factory->createQuadRenderer();
flashGroup->setRenderer(flash);

texture = CTextureManager::getInstance()->getTexture("Particles/Textures/flash.png");
flash->setMaterialType(Particle::Addtive, Particle::Camera);
flash->getMaterial()->setTexture(0, texture);
flash->getMaterial()->applyMaterial();

flashGroup->createModel(Particle::RotateZ)->setStart(0.0f, 2.0f * core::PI);
flashGroup->LifeMin = 0.5f;
flashGroup->LifeMax = 0.5f;

Particle::CInterpolator *flashSizeInterpolator = flashGroup->createInterpolator();
flashSizeInterpolator->addEntry(0.0f, 0.25f);
flashSizeInterpolator->addEntry(0.1f, 2.0f);
flashGroup->createModel(Particle::Scale)->setStart(0.0f)->setEnd(1.0f)->setInterpolator(flashSizeInterpolator);

Particle::CInterpolator *flashAlphaInterpolator = flashGroup->createInterpolator();
flashAlphaInterpolator->addEntry(0.0f, 1.0f);
flashAlphaInterpolator->addEntry(0.4f, 0.0f);
flashGroup->createModel(Particle::ColorA)->setStart(0.0f)->setEnd(1.0f)->setInterpolator(flashAlphaInterpolator);

Particle::CRandomEmitter* randomEmitter = factory->createRandomEmitter();
randomEmitter->setZone(factory->createSphereZone(core::vector3df(), 0.1f));
randomEmitter->setFlow(-1);
randomEmitter->setTank(3);
flashGroup->addEmitter(randomEmitter);

// GROUP: LINE SPARK
Particle::CGroup *lineSparkGroup = ps->createParticleGroup();

Expand Down
2 changes: 2 additions & 0 deletions Samples/ParticlesExplosion/Source/SampleParticlesExplosion.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class SampleParticlesExplosion :

void initParticleSystem(Particle::CParticleComponent *ps);

void initFireSystem(Particle::CParticleComponent *ps);

virtual void onUpdate();

virtual void onRender();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void SampleParticlesMagicSkill::initTower(Particle::CParticleComponent *ps)
alphaInterpolator->addEntry(0.4f, 0.6f);
alphaInterpolator->addEntry(0.6f, 1.0f);
alphaInterpolator->addEntry(1.0f, 0.0f);
ringGroup->createModel(Particle::ColorA)->setStart(0.0f)->setEnd(1.0f)->setInterpolator(alphaInterpolator);
ringGroup->createModel(Particle::ColorA)->setInterpolator(alphaInterpolator);

ringGroup->OrientationNormal.set(0.0f, 1.0f, 0.0f);
ringGroup->OrientationUp.set(0.0f, 0.0f, 1.0f);
Expand Down

0 comments on commit acf249c

Please sign in to comment.