Added rotation and size decay support for particles

This commit is contained in:
Brandon Mak 2023-03-20 12:19:27 +08:00
parent aa0c9d08e0
commit b8a2e206f7
11 changed files with 83 additions and 10 deletions

View File

@ -157,7 +157,7 @@
Scale: {x: 1, y: 1, z: 1}
IsActive: true
classSHADE::SHParticleEmitterComponent:
Emission Count: 7
Emission Count: 15
Is Passive: true
Emission Interval: 0.0939999968
Min Life: 2
@ -166,8 +166,11 @@
Maximum Speed: 6
Minimum Size: 0
Maximum Size: 0.5
Size Decay: 0.990999997
Angular Ranges And Offset: {x: 6.19999981, y: 1.10000002, z: 0, w: 0.100000001}
Rotation Speed: 0
Texture Asset ID: 63456868
Rotation Speed: 0.0309999995
Rotation Decay: 0.0199999996
Texture Asset ID: 0
Custom Update Shader Asset ID: 0
IsActive: true
Scripts: ~

View File

@ -10,8 +10,11 @@ struct EmitterParameters
float minSpeed;
float maxSpeed;
float rotationSpeed;
uint textureIndex;
float rotationDecay;
vec4 lifeAndSizeRange; // min life, max life, min size, max size
float sizeDecay;
uint textureIndex;
float padding[2];
};
struct ParticleData
@ -165,14 +168,16 @@ void main()
// Set size of particle
particle.scaleAndDecay.x = particleSize;
particle.scaleAndDecay.z = particleSize;
particle.scaleAndDecay.y = particleSize;
particle.scaleAndDecay.z = emitterParams.data.sizeDecay;
particle.scaleAndDecay.w = emitterParams.data.sizeDecay;
// Set the texture for the particle
particle.textureIndex = emitterParams.data.textureIndex;
// Set orientation and rotation speed
if (emitterParams.data.rotationSpeed != 0.0f)
particle.orientationSpeedDecay = vec4 (rand(seed) * PI, emitterParams.data.rotationSpeed, 0.0f, 0.0f);
particle.orientationSpeedDecay = vec4 (rand(seed) * PI, emitterParams.data.rotationSpeed, emitterParams.data.rotationDecay, 0.0f);
else
particle.orientationSpeedDecay = vec4 (0.0f);

View File

@ -123,7 +123,15 @@ void main()
particle.velocity += particle.acceleration;
particle.life -= genericDataBuffer.data.dt;
particle.orientationSpeedDecay.x += particle.orientationSpeedDecay.y;
particle.orientationSpeedDecay.y -= particle.orientationSpeedDecay.z * genericDataBuffer.data.dt;
particle.scaleAndDecay.x *= particle.scaleAndDecay.z;
particle.scaleAndDecay.y *= particle.scaleAndDecay.w;
if (particle.orientationSpeedDecay.y > 0.0f)
{
particle.orientationSpeedDecay.y -= particle.orientationSpeedDecay.z * genericDataBuffer.data.dt;
if (particle.orientationSpeedDecay.y < 0.0f)
particle.orientationSpeedDecay.y = 0.0f;
}
if (particle.life < 0.0f || particle.scaleAndDecay.x < 0.0f || particle.scaleAndDecay.y < 0.0f)
{

View File

@ -81,7 +81,7 @@ void main()
vec3 normalized = normalize (particle.velocity.xyz);
float angle = particle.orientationSpeedDecay.x;
// float angle = atan (normalized.y, normalized.x);
vec2 particleScaleData = particle.scaleAndDecay.xz; // x and y
vec2 particleScaleData = particle.scaleAndDecay.xy; // x and y
mat3 rotate = mat3 (1.0f);
rotate[0][0] = cos(angle);

Binary file not shown.

View File

@ -830,6 +830,7 @@ namespace SHADE
SHEditorWidgets::DragFloat("Max Life", [comp = component]() {return comp->GetMaxLife(); }, [comp = component](float val) {comp->SetMaxLife(val); });
SHEditorWidgets::DragFloat("Min Size", [comp = component]() {return comp->GetMinSize(); }, [comp = component](float val) {comp->SetMinSize(val); });
SHEditorWidgets::DragFloat("Max Size", [comp = component]() {return comp->GetMaxSize(); }, [comp = component](float val) {comp->SetMaxSize(val); });
SHEditorWidgets::DragFloat("Size Decay", [comp = component]() {return comp->GetSizeDecayMult(); }, [comp = component](float val) {comp->SetSizeDecayMult(val); }, {}, 0.0001f);
SHEditorWidgets::DragVec4("Angles and Offsets", {"yaw", "bank", "yaw off", "bank off"},
[comp = component]()
@ -872,6 +873,16 @@ namespace SHADE
comp->SetRotationSpeed(val);
});
SHEditorWidgets::DragFloat("Rotation Decay",
[comp = component]()
{
return comp->GetRotationDecay();
},
[comp = component](float val)
{
comp->SetRotationDecay(val);
});
SHEditorWidgets::DragInt("Texture Index",
[comp = component]()
{

View File

@ -67,6 +67,11 @@ namespace SHADE
cpuEmitterData.rotationSpeed = speed;
}
void SHParticleEmitterComponent::SetRotationDecay(float decay) noexcept
{
cpuEmitterData.rotationDecay = decay;
}
void SHParticleEmitterComponent::SetTextureIndex(uint32_t index) noexcept
{
cpuEmitterData.textureIndex = index;
@ -92,6 +97,11 @@ namespace SHADE
cpuEmitterData.lifeAndSizeRange.w = size;
}
void SHParticleEmitterComponent::SetSizeDecayMult(float decay) noexcept
{
cpuEmitterData.sizeDecayMult = decay;
}
void SHParticleEmitterComponent::SetCustomUpdateShader(Handle<SHVkShaderModule> shaderModule) noexcept
{
customUpdateShader = shaderModule;
@ -143,6 +153,11 @@ namespace SHADE
return cpuEmitterData.rotationSpeed;
}
float SHParticleEmitterComponent::GetRotationDecay(void) const noexcept
{
return cpuEmitterData.rotationDecay;
}
uint32_t SHParticleEmitterComponent::GetTextureIndex(void) const noexcept
{
return cpuEmitterData.textureIndex;
@ -169,6 +184,11 @@ namespace SHADE
}
float SHParticleEmitterComponent::GetSizeDecayMult(void) const noexcept
{
return cpuEmitterData.sizeDecayMult;
}
Handle<SHVkShaderModule> SHParticleEmitterComponent::GetCustomUpdateShader(void) const noexcept
{
return customUpdateShader;

View File

@ -1,6 +1,7 @@
#pragma once
#include "Resource/SHHandle.h"
#include "Math/Vector/SHVec2.h"
#include "Math/Vector/SHVec4.h"
#include "ECS_Base/Components/SHComponent.h"
#include "Graphics/MiddleEnd/Interface/SHGraphicsConstants.h"
@ -32,11 +33,21 @@ namespace SHADE
//! Rotational speed of the quad
float rotationSpeed;
//! Texture used by the particle
uint32_t textureIndex;
//! Rotation decay rate
float rotationDecay;
//! Spawn lifetime and size range (min and max)
SHVec4 lifeAndSizeRange;
//! Size decay for particles
float sizeDecayMult;
//! Texture used by the particle
uint32_t textureIndex;
//! Padding for the shader struct
float padding[2];
};
struct GPUParticleStruct
@ -139,9 +150,11 @@ namespace SHADE
void SetMinSpeed (float speed) noexcept;
void SetMaxSpeed (float speed) noexcept;
void SetRotationSpeed (float speed) noexcept;
void SetRotationDecay (float decay) noexcept;
void SetTextureIndex (uint32_t index) noexcept;
void SetMinSize (float size) noexcept;
void SetMaxSize (float size) noexcept;
void SetSizeDecayMult (float decay) noexcept;
void SetCustomUpdateShader (Handle<SHVkShaderModule> shaderModule) noexcept;
uint32_t GetEmissionCount (void) const noexcept;
@ -153,9 +166,11 @@ namespace SHADE
float GetMinSpeed (void) const noexcept;
float GetMaxSpeed (void) const noexcept;
float GetRotationSpeed (void) const noexcept;
float GetRotationDecay (void) const noexcept;
uint32_t GetTextureIndex (void) const noexcept;
float GetMinSize (void) const noexcept;
float GetMaxSize (void) const noexcept;
float GetSizeDecayMult (void) const noexcept;
Handle<SHVkShaderModule> GetCustomUpdateShader (void) const noexcept;
/*-----------------------------------------------------------------------*/

View File

@ -512,9 +512,11 @@ namespace YAML
static constexpr std::string_view ANGULAR_RANGES_OFFSET_TAG = "Angular Ranges And Offset";
static constexpr std::string_view MIN_SIZE_TAG = "Minimum Size";
static constexpr std::string_view MAX_SIZE_TAG = "Maximum Size";
static constexpr std::string_view SIZE_DECAY_TAG = "Size Decay";
static constexpr std::string_view MIN_SPEED_TAG = "Minimum Speed";
static constexpr std::string_view MAX_SPEED_TAG = "Maximum Speed";
static constexpr std::string_view ROTATION_SPEED_TAG = "Rotation Speed";
static constexpr std::string_view ROTATION_DECAY_TAG = "Rotation Decay";
static constexpr std::string_view TEXTURE_ASSET_ID_TAG = "Texture Asset ID";
static constexpr std::string_view CUSTOM_UPDATE_SHADER_ASSET_ID_TAG = "Custom Update Shader Asset ID";
@ -530,8 +532,10 @@ namespace YAML
node[MAX_SPEED_TAG.data()] = rhs.GetMaxSpeed();
node[MIN_SIZE_TAG.data()] = rhs.GetMinSize();
node[MAX_SIZE_TAG.data()] = rhs.GetMaxSize();
node[SIZE_DECAY_TAG.data()] = rhs.GetSizeDecayMult();
node[ANGULAR_RANGES_OFFSET_TAG.data()] = rhs.GetAngularRangesAndOffsets();
node[ROTATION_SPEED_TAG.data()] = rhs.GetRotationSpeed();
node[ROTATION_DECAY_TAG.data()] = rhs.GetRotationDecay();
node[TEXTURE_ASSET_ID_TAG.data()] = rhs.GetTextureAssetID();
node[CUSTOM_UPDATE_SHADER_ASSET_ID_TAG.data()] = rhs.GetCustomUpdateShaderAssetID();
@ -556,6 +560,9 @@ namespace YAML
if (node[MAX_LIFE_TAG.data()].IsDefined())
rhs.SetMaxLife(node[MAX_LIFE_TAG.data()].as<float>());
if (node[SIZE_DECAY_TAG.data()].IsDefined())
rhs.SetSizeDecayMult(node[SIZE_DECAY_TAG.data()].as<float>());
if (node[ANGULAR_RANGES_OFFSET_TAG.data()].IsDefined())
rhs.SetAngularRangesAndOffsets(node[ANGULAR_RANGES_OFFSET_TAG.data()].as<SHVec4>());
@ -571,6 +578,10 @@ namespace YAML
if (node[ROTATION_SPEED_TAG.data()].IsDefined())
rhs.SetRotationSpeed(node[ROTATION_SPEED_TAG.data()].as<float>());
if (node[ROTATION_DECAY_TAG.data()].IsDefined())
rhs.SetRotationDecay(node[ROTATION_DECAY_TAG.data()].as<float>());
if (node[TEXTURE_ASSET_ID_TAG.data()].IsDefined())
{
AssetID id = node[TEXTURE_ASSET_ID_TAG.data()].as<AssetID>();