Skip to content
Snippets Groups Projects
Commit c2e4a92d authored by Alexander Gauggel's avatar Alexander Gauggel
Browse files

[#69] Additively blend particles and make them look rounded

parent cecf848f
No related branches found
No related tags found
1 merge request!56Resolve "Partikelsystem"
Pipeline #26059 passed
...@@ -15,6 +15,10 @@ namespace vkcv { ...@@ -15,6 +15,10 @@ namespace vkcv {
enum class PrimitiveTopology{PointList, LineList, TriangleList }; enum class PrimitiveTopology{PointList, LineList, TriangleList };
// add more as needed
// alternatively we could expose the blend factors directly
enum class BlendMode{ None, Additive };
struct PipelineConfig { struct PipelineConfig {
ShaderProgram m_ShaderProgram; ShaderProgram m_ShaderProgram;
uint32_t m_Width; uint32_t m_Width;
...@@ -25,6 +29,7 @@ namespace vkcv { ...@@ -25,6 +29,7 @@ namespace vkcv {
bool m_UseDynamicViewport; bool m_UseDynamicViewport;
bool m_UseConservativeRasterization = false; bool m_UseConservativeRasterization = false;
PrimitiveTopology m_PrimitiveTopology = PrimitiveTopology::TriangleList; PrimitiveTopology m_PrimitiveTopology = PrimitiveTopology::TriangleList;
BlendMode m_blendMode = BlendMode::None;
}; };
} }
\ No newline at end of file
No preview for this file type
No preview for this file type
float circleFactor(vec2 triangleCoordinates){
// percentage of distance from center to circle edge
float p = clamp((0.4 - length(triangleCoordinates)) / 0.4, 0, 1);
// remapping for nice falloff
return sqrt(p);
}
\ No newline at end of file
...@@ -23,6 +23,7 @@ layout( push_constant ) uniform constants{ ...@@ -23,6 +23,7 @@ layout( push_constant ) uniform constants{
mat4 projection; mat4 projection;
}; };
layout(location = 0) out vec2 passTriangleCoordinates;
layout(location = 1) out vec3 passVelocity; layout(location = 1) out vec3 passVelocity;
layout(location = 2) out float passlifeTime; layout(location = 2) out float passlifeTime;
...@@ -36,5 +37,9 @@ void main() ...@@ -36,5 +37,9 @@ void main()
// by adding the triangle position in view space the mesh is always camera facing // by adding the triangle position in view space the mesh is always camera facing
positionView.xyz += particle; positionView.xyz += particle;
// multiply with projection matrix for final position // multiply with projection matrix for final position
gl_Position = projection * positionView; gl_Position = projection * positionView;
// 0.01 corresponds to vertex position size in main
float normalizationDivider = 0.01;
passTriangleCoordinates = particle.xy / normalizationDivider;
} }
\ No newline at end of file
#version 450 #version 450
#extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_separate_shader_objects : enable
#extension GL_GOOGLE_include_directive : enable
#include "particleShading.inc"
layout(location = 0) in vec2 passTriangleCoordinates;
layout(location = 1) in vec3 passVelocity; layout(location = 1) in vec3 passVelocity;
layout(location = 2) in float passlifeTime; layout(location = 2) in float passlifeTime;
...@@ -14,6 +18,7 @@ layout(set=0,binding=1) uniform uPosition{ ...@@ -14,6 +18,7 @@ layout(set=0,binding=1) uniform uPosition{
vec2 position; vec2 position;
} Position; } Position;
void main() void main()
{ {
vec2 mouse = vec2(Position.position.x, Position.position.y); vec2 mouse = vec2(Position.position.x, Position.position.y);
...@@ -21,4 +26,9 @@ void main() ...@@ -21,4 +26,9 @@ void main()
float(passlifeTime < 2 && passlifeTime > 1) * vec4(1,passlifeTime * 0.5,0,0) + float(passlifeTime < 2 && passlifeTime > 1) * vec4(1,passlifeTime * 0.5,0,0) +
float(passlifeTime >= 2 && passlifeTime < 2.5f) * vec4(passlifeTime * 0.5,passlifeTime * 0.5,0,0) + float(passlifeTime >= 2 && passlifeTime < 2.5f) * vec4(passlifeTime * 0.5,passlifeTime * 0.5,0,0) +
float(passlifeTime >= 2.5f) * vec4(1,0,0,0); float(passlifeTime >= 2.5f) * vec4(1,0,0,0);
// make the triangle look like a circle
outColor *= circleFactor(passTriangleCoordinates);
// full color is achieved by additively blending many particles
outColor *= 0.5;
} }
\ No newline at end of file
#version 450 #version 450
#extension GL_ARB_separate_shader_objects : enable #extension GL_ARB_separate_shader_objects : enable
#extension GL_GOOGLE_include_directive : enable
#include "particleShading.inc"
layout(location = 0) in vec2 passTriangleCoordinates;
layout(location = 1) in vec3 passVelocity; layout(location = 1) in vec3 passVelocity;
layout(location = 2) in float passlifeTime; layout(location = 2) in float passlifeTime;
...@@ -22,4 +26,9 @@ void main() ...@@ -22,4 +26,9 @@ void main()
float(passlifeTime < 2 && passlifeTime > 1) * vec4(0.3, 0.7,1,0) + float(passlifeTime < 2 && passlifeTime > 1) * vec4(0.3, 0.7,1,0) +
float(passlifeTime >= 2 && passlifeTime < 4.f) * vec4(0.5,0.9,1,0) + float(passlifeTime >= 2 && passlifeTime < 4.f) * vec4(0.5,0.9,1,0) +
float(passlifeTime >= 4.f) * vec4(0.9,1,1,0); float(passlifeTime >= 4.f) * vec4(0.9,1,1,0);
// make the triangle look like a circle
outColor *= circleFactor(passTriangleCoordinates);
// full color is achieved by additively blending many particles
outColor *= 0.5;
} }
No preview for this file type
...@@ -95,7 +95,7 @@ int main(int argc, const char **argv) { ...@@ -95,7 +95,7 @@ int main(int argc, const char **argv) {
const vkcv::VertexLayout particleLayout(bindings); const vkcv::VertexLayout particleLayout(bindings);
const vkcv::PipelineConfig particlePipelineDefinition{ vkcv::PipelineConfig particlePipelineDefinition{
particleShaderProgram, particleShaderProgram,
UINT32_MAX, UINT32_MAX,
UINT32_MAX, UINT32_MAX,
...@@ -103,10 +103,11 @@ int main(int argc, const char **argv) { ...@@ -103,10 +103,11 @@ int main(int argc, const char **argv) {
{particleLayout}, {particleLayout},
{core.getDescriptorSet(descriptorSet).layout}, {core.getDescriptorSet(descriptorSet).layout},
true}; true};
particlePipelineDefinition.m_blendMode = vkcv::BlendMode::Additive;
const std::vector<glm::vec3> vertices = {glm::vec3(-0.005, 0.005, 0), const std::vector<glm::vec3> vertices = {glm::vec3(-0.01, 0.01, 0),
glm::vec3(0.005, 0.005, 0), glm::vec3(0.01, 0.01, 0),
glm::vec3(0, -0.005, 0)}; glm::vec3(0, -0.01, 0)};
vertexBuffer.fill(vertices); vertexBuffer.fill(vertices);
......
...@@ -182,8 +182,11 @@ namespace vkcv ...@@ -182,8 +182,11 @@ namespace vkcv
VK_COLOR_COMPONENT_G_BIT | VK_COLOR_COMPONENT_G_BIT |
VK_COLOR_COMPONENT_B_BIT | VK_COLOR_COMPONENT_B_BIT |
VK_COLOR_COMPONENT_A_BIT); VK_COLOR_COMPONENT_A_BIT);
// currently set to additive, if not disabled
// BlendFactors must be set as soon as additional BlendModes are added
vk::PipelineColorBlendAttachmentState colorBlendAttachmentState( vk::PipelineColorBlendAttachmentState colorBlendAttachmentState(
false, config.m_blendMode != BlendMode::None,
vk::BlendFactor::eOne, vk::BlendFactor::eOne,
vk::BlendFactor::eOne, vk::BlendFactor::eOne,
vk::BlendOp::eAdd, vk::BlendOp::eAdd,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment