2023-02-27 15:14:32 +08:00
|
|
|
#version 450
|
|
|
|
|
2023-03-16 09:34:42 +08:00
|
|
|
#define PI 3.14159265f
|
|
|
|
|
2023-02-27 15:14:32 +08:00
|
|
|
layout(local_size_x = 128) in;
|
|
|
|
|
|
|
|
struct EmitterParameters
|
|
|
|
{
|
2023-03-15 21:09:55 +08:00
|
|
|
vec4 angularRangesAndOffsets;
|
2023-03-21 15:27:42 +08:00
|
|
|
vec4 acceleration;
|
2023-03-15 21:09:55 +08:00
|
|
|
float minSpeed;
|
|
|
|
float maxSpeed;
|
2023-03-16 09:34:42 +08:00
|
|
|
float rotationSpeed;
|
2023-03-20 12:19:27 +08:00
|
|
|
float rotationDecay;
|
2023-02-27 15:14:32 +08:00
|
|
|
vec4 lifeAndSizeRange; // min life, max life, min size, max size
|
2023-03-20 20:43:50 +08:00
|
|
|
vec4 colorTint;
|
2023-03-30 15:19:54 +08:00
|
|
|
vec4 colorTintRange;
|
|
|
|
vec4 colorDecay;
|
2023-03-20 12:19:27 +08:00
|
|
|
float sizeDecay;
|
|
|
|
uint textureIndex;
|
|
|
|
float padding[2];
|
2023-03-13 11:41:23 +08:00
|
|
|
};
|
2023-02-27 15:14:32 +08:00
|
|
|
|
|
|
|
struct ParticleData
|
|
|
|
{
|
|
|
|
vec4 position;
|
2023-03-16 09:34:42 +08:00
|
|
|
vec4 orientationSpeedDecay;
|
2023-02-27 15:14:32 +08:00
|
|
|
vec4 velocity;
|
|
|
|
vec4 acceleration;
|
|
|
|
vec4 scaleAndDecay;
|
2023-03-20 20:43:50 +08:00
|
|
|
vec4 colorTint;
|
2023-03-30 15:19:54 +08:00
|
|
|
vec4 colorDecay;
|
2023-02-27 15:14:32 +08:00
|
|
|
float life;
|
|
|
|
uint textureIndex;
|
2023-03-13 11:41:23 +08:00
|
|
|
};
|
2023-02-27 15:14:32 +08:00
|
|
|
|
|
|
|
struct GenericData
|
|
|
|
{
|
|
|
|
//! Delta time
|
|
|
|
float dt;
|
|
|
|
|
|
|
|
//! Elapsed time of the application
|
|
|
|
float elapsedTime;
|
|
|
|
|
|
|
|
//! Viewport width of the scene (excluding imgui, that means smaller than window)
|
|
|
|
uint viewportWidth;
|
|
|
|
|
|
|
|
//! Ditto but for height
|
|
|
|
uint viewportHeight;
|
|
|
|
};
|
|
|
|
|
|
|
|
layout (set = 0, binding = 0) uniform GenericDataBuffer
|
|
|
|
{
|
|
|
|
GenericData data;
|
|
|
|
} genericDataBuffer;
|
|
|
|
|
|
|
|
layout (std430, set = 2, binding = 0) readonly buffer EmitterBuffer
|
|
|
|
{
|
|
|
|
EmitterParameters data;
|
|
|
|
} emitterParams;
|
|
|
|
|
|
|
|
layout (std430, set = 2, binding = 1) coherent restrict buffer ParticlesInputBuffer
|
|
|
|
{
|
|
|
|
ParticleData data[];
|
|
|
|
} inputParticles;
|
|
|
|
|
|
|
|
// output buffer not needed
|
|
|
|
// layout (std430, set = 2, binding = 2) coherent restrict buffer ParticlesOutputBuffer
|
|
|
|
// {
|
|
|
|
// ParticleData data[];
|
|
|
|
// } outputParticles;
|
|
|
|
|
|
|
|
layout (std430, set = 2, binding = 3) coherent restrict buffer ParticlesFreelistBuffer
|
|
|
|
{
|
|
|
|
int freeCount;
|
|
|
|
int freeIndices[];
|
|
|
|
|
|
|
|
} freelist;
|
|
|
|
|
|
|
|
|
|
|
|
// push constants
|
|
|
|
layout(std140, push_constant) uniform EmitterPushConstant
|
|
|
|
{
|
|
|
|
vec4 emitterPosition;
|
|
|
|
uint emissionCount;
|
|
|
|
|
|
|
|
} emitterPushConstant;
|
|
|
|
|
|
|
|
uint pcg_hash(uint seed)
|
|
|
|
{
|
|
|
|
uint state = seed * 747796405u + 2891336453u;
|
|
|
|
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
|
|
|
|
return (word >> 22u) ^ word;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Used to advance the PCG state.
|
|
|
|
uint rand_pcg(inout uint rng_state)
|
|
|
|
{
|
|
|
|
uint state = rng_state;
|
|
|
|
rng_state = rng_state * 747796405u + 2891336453u;
|
|
|
|
uint word = ((state >> ((state >> 28u) + 4u)) ^ state) * 277803737u;
|
|
|
|
return (word >> 22u) ^ word;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Advances the prng state and returns the corresponding random float.
|
|
|
|
float rand(inout uint state)
|
|
|
|
{
|
|
|
|
uint x = rand_pcg(state);
|
|
|
|
state = x;
|
|
|
|
return float(x)*uintBitsToFloat(0x2f800004u);
|
|
|
|
}
|
|
|
|
|
2023-03-13 11:41:23 +08:00
|
|
|
float map(float value, float inMin, float inMax, float outMin, float outMax)
|
|
|
|
{
|
|
|
|
return outMin + (outMax - outMin) * (value - inMin) / (inMax - inMin);
|
|
|
|
}
|
|
|
|
|
2023-02-27 15:14:32 +08:00
|
|
|
void main()
|
|
|
|
{
|
|
|
|
uint emitterInvocationIndex = gl_GlobalInvocationID.x;
|
2023-03-13 11:41:23 +08:00
|
|
|
vec4 emitterPosition = emitterPushConstant.emitterPosition;
|
2023-03-15 21:09:55 +08:00
|
|
|
vec4 angularRangesAndOffsets = emitterParams.data.angularRangesAndOffsets;
|
|
|
|
float minSpeed = emitterParams.data.minSpeed;
|
|
|
|
float maxSpeed = emitterParams.data.maxSpeed;
|
2023-02-27 15:14:32 +08:00
|
|
|
|
|
|
|
if (emitterInvocationIndex >= emitterPushConstant.emissionCount)
|
|
|
|
return;
|
|
|
|
|
2023-03-14 10:55:53 +08:00
|
|
|
// Freecount will start at max particles. Here we subtract every time we emit.
|
2023-02-27 15:14:32 +08:00
|
|
|
int freelistIndex = atomicAdd (freelist.freeCount, -1) - 1;
|
|
|
|
if (freelistIndex < 0)
|
|
|
|
atomicAdd (freelist.freeCount, 1);
|
|
|
|
|
|
|
|
ParticleData particle;
|
|
|
|
|
2023-03-13 11:41:23 +08:00
|
|
|
// Get seed for randomization
|
2023-03-24 20:30:01 +08:00
|
|
|
uint pixel_index = uint (emitterPosition.x * emitterPosition.y + floatBitsToUint(genericDataBuffer.data.elapsedTime) * (gl_GlobalInvocationID.x + 1));
|
2023-03-13 11:41:23 +08:00
|
|
|
uint seed = pcg_hash (pixel_index);
|
|
|
|
|
2023-02-27 15:14:32 +08:00
|
|
|
int index = freelist.freeIndices[freelistIndex];
|
|
|
|
|
2023-03-13 11:41:23 +08:00
|
|
|
// emit particle from emitter position
|
|
|
|
particle.position = vec4 (emitterPosition.xyz, 1.0f);
|
|
|
|
|
2023-03-22 22:52:43 +08:00
|
|
|
vec2 eulerAngles = vec2 ((rand(seed) - 0.5f) * angularRangesAndOffsets.x + angularRangesAndOffsets.z,
|
|
|
|
(rand(seed) - 0.5f) * angularRangesAndOffsets.y + angularRangesAndOffsets.w);
|
2023-03-15 21:09:55 +08:00
|
|
|
|
2023-03-14 10:55:53 +08:00
|
|
|
// Set its velocity
|
2023-03-16 10:44:03 +08:00
|
|
|
// particle.velocity.xyz = vec3 (cos(eulerAngles.x) * cos(eulerAngles.y),
|
|
|
|
// sin(eulerAngles.x) * cos(eulerAngles.y),
|
|
|
|
// sin(eulerAngles.y));
|
|
|
|
|
2023-03-22 22:52:43 +08:00
|
|
|
float heading = eulerAngles.x;
|
2023-03-16 10:44:03 +08:00
|
|
|
float bank = eulerAngles.y;
|
|
|
|
float cb = cos(bank);
|
|
|
|
float sb = sin(bank);
|
2023-03-22 22:52:43 +08:00
|
|
|
float ch = cos (heading);
|
|
|
|
float sh = sin (heading);
|
2023-03-16 10:44:03 +08:00
|
|
|
float cp = cos (0.0f);
|
|
|
|
float sp = sin (0.0f);
|
|
|
|
|
|
|
|
particle.velocity.xyz = mat3 (
|
|
|
|
(ch * cb + sh * sp * sb), (sb * cp), (-sh * cb + ch * sp * sb),
|
|
|
|
(-ch * sb + sh * sp * cb), (cb * cp), ( sb * sh + ch * sp * cb),
|
|
|
|
(sh * cp), (-sp), (ch * cp)
|
|
|
|
) * vec3 (1.0f, 0.0f, 0.0f);
|
|
|
|
|
2023-03-15 21:09:55 +08:00
|
|
|
|
|
|
|
particle.velocity *= map (rand (seed), 0.0f, 1.0f, minSpeed.x, maxSpeed.x);
|
2023-03-14 10:55:53 +08:00
|
|
|
|
2023-03-13 11:41:23 +08:00
|
|
|
// randomize life value that ranges from minLife to maxLife
|
2023-03-14 10:55:53 +08:00
|
|
|
particle.life = map (rand(seed), 0.0f, 1.0f, emitterParams.data.lifeAndSizeRange.x, emitterParams.data.lifeAndSizeRange.y);
|
|
|
|
|
2023-03-15 21:09:55 +08:00
|
|
|
float particleSize = map (rand(seed), 0.0f, 1.0f, emitterParams.data.lifeAndSizeRange.z, emitterParams.data.lifeAndSizeRange.w);
|
|
|
|
|
2023-03-20 20:43:50 +08:00
|
|
|
// Set size of and size decay of particle
|
2023-03-15 21:09:55 +08:00
|
|
|
particle.scaleAndDecay.x = particleSize;
|
2023-03-20 12:19:27 +08:00
|
|
|
particle.scaleAndDecay.y = particleSize;
|
|
|
|
particle.scaleAndDecay.z = emitterParams.data.sizeDecay;
|
|
|
|
particle.scaleAndDecay.w = emitterParams.data.sizeDecay;
|
2023-03-30 15:19:54 +08:00
|
|
|
particle.colorDecay = emitterParams.data.colorDecay;
|
2023-03-20 20:43:50 +08:00
|
|
|
|
2023-03-30 15:19:54 +08:00
|
|
|
float randRange = rand(seed) * 2.0f - 1.0f;
|
|
|
|
|
2023-03-20 20:43:50 +08:00
|
|
|
// Set particle color tint
|
2023-03-30 15:19:54 +08:00
|
|
|
particle.colorTint = emitterParams.data.colorTint + vec4 (randRange * emitterParams.data.colorTintRange.x,
|
|
|
|
randRange * emitterParams.data.colorTintRange.y,
|
|
|
|
randRange * emitterParams.data.colorTintRange.z,
|
|
|
|
randRange * emitterParams.data.colorTintRange.w);
|
|
|
|
|
|
|
|
// particle.colorTint = emitterParams.data.colorTint;
|
|
|
|
|
2023-03-13 11:41:23 +08:00
|
|
|
|
2023-03-16 09:34:42 +08:00
|
|
|
// Set the texture for the particle
|
|
|
|
particle.textureIndex = emitterParams.data.textureIndex;
|
|
|
|
|
|
|
|
// Set orientation and rotation speed
|
|
|
|
if (emitterParams.data.rotationSpeed != 0.0f)
|
2023-03-20 12:19:27 +08:00
|
|
|
particle.orientationSpeedDecay = vec4 (rand(seed) * PI, emitterParams.data.rotationSpeed, emitterParams.data.rotationDecay, 0.0f);
|
2023-03-16 09:34:42 +08:00
|
|
|
else
|
|
|
|
particle.orientationSpeedDecay = vec4 (0.0f);
|
|
|
|
|
|
|
|
|
2023-03-21 15:27:42 +08:00
|
|
|
particle.acceleration = emitterParams.data.acceleration;
|
2023-03-16 09:34:42 +08:00
|
|
|
|
2023-03-13 11:41:23 +08:00
|
|
|
|
|
|
|
inputParticles.data[index] = particle;
|
2023-02-27 15:14:32 +08:00
|
|
|
}
|