| 1 |
#version 440 |
| 2 |
|
| 3 |
layout(std140, binding = 0) uniform buf { |
| 4 |
mat4 qt_modelMatrix; |
| 5 |
mat4 qt_viewMatrix; |
| 6 |
mat4 qt_projectionMatrix; |
| 7 |
#ifdef QSSG_PARTICLES_ENABLE_VERTEX_LIGHTING |
| 8 |
// ParticleLightData struct |
| 9 |
vec4 qt_pointLightPosition[4]; |
| 10 |
vec4 qt_pointLightConstantAtt; |
| 11 |
vec4 qt_pointLightLinearAtt; |
| 12 |
vec4 qt_pointLightQuadAtt; |
| 13 |
vec4 qt_pointLightColor[4]; |
| 14 |
vec4 qt_spotLightPosition[4]; |
| 15 |
vec4 qt_spotLightConstantAtt; |
| 16 |
vec4 qt_spotLightLinearAtt; |
| 17 |
vec4 qt_spotLightQuadAtt; |
| 18 |
vec4 qt_spotLightColor[4]; |
| 19 |
vec4 qt_spotLightDirection[4]; |
| 20 |
vec4 qt_spotLightConeAngle; |
| 21 |
vec4 qt_spotLightInnerConeAngle; |
| 22 |
#endif |
| 23 |
vec4 qt_spriteConfig; |
| 24 |
vec3 qt_light_ambient_total; |
| 25 |
vec2 qt_oneOverParticleImageSize; |
| 26 |
uint qt_countPerSlice; |
| 27 |
uint qt_lineSegmentCount; |
| 28 |
float qt_billboard; |
| 29 |
float qt_opacity; |
| 30 |
float qt_alphaFade; |
| 31 |
float qt_sizeModifier; |
| 32 |
float qt_texcoordScale; |
| 33 |
#ifdef QSSG_PARTICLES_ENABLE_VERTEX_LIGHTING |
| 34 |
bool qt_pointLights; |
| 35 |
bool qt_spotLights; |
| 36 |
#endif |
| 37 |
} ubuf; |
| 38 |
|
| 39 |
#ifdef QSSG_PARTICLES_ENABLE_VERTEX_LIGHTING |
| 40 |
/* Simplified lighting for particles |
| 41 |
* |
| 42 |
* Treat particles as points -> do not take normal into account |
| 43 |
* -> directional lights become ambient |
| 44 |
* -> point lights only account for attenuation |
| 45 |
* -> spotlights only account for attenuation and spot angles |
| 46 |
*/ |
| 47 |
vec4 qt_attenuate4(in vec4 dist, in vec4 catt, in vec4 latt, in vec4 qatt) |
| 48 |
{ |
| 49 |
vec4 factors = catt + dist * latt + dist * dist * qatt; |
| 50 |
return vec4(1.0) / factors; |
| 51 |
} |
| 52 |
vec3 qt_calcPointLights(in vec3 position) |
| 53 |
{ |
| 54 |
vec4 lengths = vec4(length(position - ubuf.qt_pointLightPosition[0].xyz), |
| 55 |
length(position - ubuf.qt_pointLightPosition[1].xyz), |
| 56 |
length(position - ubuf.qt_pointLightPosition[2].xyz), |
| 57 |
length(position - ubuf.qt_pointLightPosition[3].xyz)); |
| 58 |
vec4 attenuations = qt_attenuate4(lengths, ubuf.qt_pointLightConstantAtt, ubuf.qt_pointLightLinearAtt, ubuf.qt_pointLightQuadAtt); |
| 59 |
|
| 60 |
return ubuf.qt_pointLightColor[0].rgb * attenuations.x + ubuf.qt_pointLightColor[1].rgb * attenuations.y + ubuf.qt_pointLightColor[2].rgb * attenuations.z + ubuf.qt_pointLightColor[3].rgb * attenuations.w; |
| 61 |
} |
| 62 |
vec3 qt_calcSpotLights(in vec3 position) |
| 63 |
{ |
| 64 |
vec3 lightVector0 = position - ubuf.qt_spotLightPosition[0].xyz; |
| 65 |
vec3 lightVector1 = position - ubuf.qt_spotLightPosition[1].xyz; |
| 66 |
vec3 lightVector2 = position - ubuf.qt_spotLightPosition[2].xyz; |
| 67 |
vec3 lightVector3 = position - ubuf.qt_spotLightPosition[3].xyz; |
| 68 |
vec4 lengths = vec4(length(lightVector0), length(lightVector1), length(lightVector2), length(lightVector3)); |
| 69 |
vec4 attenuations = qt_attenuate4(lengths, ubuf.qt_spotLightConstantAtt, ubuf.qt_spotLightLinearAtt, ubuf.qt_spotLightQuadAtt); |
| 70 |
vec4 spots = vec4(acos(dot(normalize(lightVector0), normalize(ubuf.qt_spotLightDirection[0].xyz))), |
| 71 |
acos(dot(normalize(lightVector1), normalize(ubuf.qt_spotLightDirection[1].xyz))), |
| 72 |
acos(dot(normalize(lightVector2), normalize(ubuf.qt_spotLightDirection[2].xyz))), |
| 73 |
acos(dot(normalize(lightVector3), normalize(ubuf.qt_spotLightDirection[3].xyz)))); |
| 74 |
vec4 cones = vec4(1.0 - smoothstep(ubuf.qt_spotLightInnerConeAngle, ubuf.qt_spotLightConeAngle, spots)); |
| 75 |
attenuations *= cones; |
| 76 |
return ubuf.qt_spotLightColor[0].rgb * attenuations.x + ubuf.qt_spotLightColor[1].rgb * attenuations.y + ubuf.qt_spotLightColor[2].rgb * attenuations.z + ubuf.qt_spotLightColor[3].rgb * attenuations.w; |
| 77 |
} |
| 78 |
vec3 qt_calcLightColor(in vec3 position) |
| 79 |
{ |
| 80 |
vec3 color = vec3(0.0); |
| 81 |
|
| 82 |
if (ubuf.qt_pointLights) |
| 83 |
color += qt_calcPointLights(position); |
| 84 |
if (ubuf.qt_spotLights) |
| 85 |
color += qt_calcSpotLights(position); |
| 86 |
return color + ubuf.qt_light_ambient_total; |
| 87 |
} |
| 88 |
#endif // QSSG_PARTICLES_ENABLE_VERTEX_LIGHTING |
| 89 |
|
| 90 |
layout(binding = 2) uniform sampler2D qt_particleTexture; |
| 91 |
|
| 92 |
out gl_PerVertex { |
| 93 |
vec4 gl_Position; |
| 94 |
}; |
| 95 |
|
| 96 |
const uint particleSize = 4; |
| 97 |
|
| 98 |
struct Particle |
| 99 |
{ |
| 100 |
vec3 position; |
| 101 |
float size; |
| 102 |
vec4 color; |
| 103 |
vec3 binormal; |
| 104 |
float animationFrame; |
| 105 |
float age; |
| 106 |
float segmentLength; |
| 107 |
vec2 unusedPadding; |
| 108 |
}; |
| 109 |
|
| 110 |
vec2 qt_indexToUV(in uint index) |
| 111 |
{ |
| 112 |
uint v = index / ubuf.qt_countPerSlice; |
| 113 |
uint u = index - ubuf.qt_countPerSlice * v; |
| 114 |
return (vec2(u * particleSize, v) + vec2(0.5)) * ubuf.qt_oneOverParticleImageSize; |
| 115 |
} |
| 116 |
|
| 117 |
Particle qt_loadParticle(in uint index) |
| 118 |
{ |
| 119 |
Particle p; |
| 120 |
vec2 offset = qt_indexToUV(index); |
| 121 |
vec4 p0 = texture(qt_particleTexture, offset); |
| 122 |
vec4 p1 = texture(qt_particleTexture, vec2(offset.x + ubuf.qt_oneOverParticleImageSize.x, offset.y)); |
| 123 |
vec4 p2 = texture(qt_particleTexture, vec2(offset.x + 2.0 * ubuf.qt_oneOverParticleImageSize.x, offset.y)); |
| 124 |
vec4 p3 = texture(qt_particleTexture, vec2(offset.x + 3.0 * ubuf.qt_oneOverParticleImageSize.x, offset.y)); |
| 125 |
|
| 126 |
p.position = p0.xyz; |
| 127 |
p.size = p0.w; |
| 128 |
p.color = p1; |
| 129 |
p.binormal = p2.xyz; |
| 130 |
p.animationFrame = p2.w; |
| 131 |
p.age = p3.x; |
| 132 |
p.segmentLength = p3.y; |
| 133 |
return p; |
| 134 |
} |
| 135 |
|
| 136 |
mat3 qt_fromEulerRotation(in vec3 rotation) |
| 137 |
{ |
| 138 |
float a = cos(rotation.x); |
| 139 |
float b = sin(rotation.x); |
| 140 |
float c = cos(rotation.y); |
| 141 |
float d = sin(rotation.y); |
| 142 |
float e = cos(rotation.z); |
| 143 |
float f = sin(rotation.z); |
| 144 |
mat3 ret; |
| 145 |
float bd = b*d; |
| 146 |
float ad = a*d; |
| 147 |
ret[0][0] = c * e; |
| 148 |
ret[0][1] = -c * f; |
| 149 |
ret[0][2] = d; |
| 150 |
ret[1][0] = bd*e + a*f; |
| 151 |
ret[1][1] = a*e - bd*f; |
| 152 |
ret[1][2] = -b*c; |
| 153 |
ret[2][0] = b*f - ad*e; |
| 154 |
ret[2][1] = ad*f + b*e; |
| 155 |
ret[2][2] = a*c; |
| 156 |
return ret; |
| 157 |
} |
| 158 |
|
| 159 |
float qt_ageToSpriteFactor(in float age) |
| 160 |
{ |
| 161 |
// Age should be [0, 1], but just in case take the fraction |
| 162 |
return fract(age); |
| 163 |
} |
| 164 |
|
| 165 |
float qt_calcAlphaFade(in uint segmentIndex) |
| 166 |
{ |
| 167 |
return pow(ubuf.qt_alphaFade, float(segmentIndex)); |
| 168 |
} |
| 169 |
|
| 170 |
float qt_calcSizeFactor(in uint segmentIndex) |
| 171 |
{ |
| 172 |
return pow(ubuf.qt_sizeModifier, float(segmentIndex)); |
| 173 |
} |
| 174 |
|
| 175 |
layout(location = 0) out vec4 color; |
| 176 |
layout(location = 1) out vec2 spriteData; |
| 177 |
layout(location = 2) out vec2 texcoord; |
| 178 |
|
| 179 |
void main() |
| 180 |
{ |
| 181 |
uint segmentIndex = gl_VertexIndex / 2; |
| 182 |
uint particleIndex = segmentIndex + gl_InstanceIndex * ubuf.qt_lineSegmentCount; |
| 183 |
|
| 184 |
Particle p = qt_loadParticle(particleIndex); |
| 185 |
|
| 186 |
float side = float(mod(gl_VertexIndex, 2)) - 0.5; |
| 187 |
float size = p.size * qt_calcSizeFactor(segmentIndex); |
| 188 |
vec3 o = p.binormal * side * size; |
| 189 |
vec4 worldPos = ubuf.qt_modelMatrix * vec4(p.position + (1.0 - ubuf.qt_billboard) * o, 1.0); |
| 190 |
vec4 viewPos = ubuf.qt_viewMatrix * worldPos; |
| 191 |
vec3 viewBN = (transpose(inverse(ubuf.qt_viewMatrix * ubuf.qt_modelMatrix)) * vec4(p.binormal, 0.0)).xyz; |
| 192 |
viewBN.z = 0; |
| 193 |
viewBN = normalize(viewBN) * side * size; |
| 194 |
viewPos = viewPos + vec4(viewBN, 0.0) * ubuf.qt_billboard; |
| 195 |
texcoord.x = p.segmentLength * ubuf.qt_texcoordScale; |
| 196 |
texcoord.y = float(mod(gl_VertexIndex, 2)); |
| 197 |
color = p.color; |
| 198 |
#ifdef QSSG_PARTICLES_ENABLE_VERTEX_LIGHTING |
| 199 |
color.rgb *= qt_calcLightColor(worldPos.xyz); |
| 200 |
#endif |
| 201 |
color.a *= ubuf.qt_opacity * qt_calcAlphaFade(segmentIndex); |
| 202 |
gl_Position = ubuf.qt_projectionMatrix * viewPos; |
| 203 |
#ifdef QSSG_PARTICLES_ENABLE_MAPPED |
| 204 |
spriteData.x = qt_ageToSpriteFactor(p.age); |
| 205 |
#endif |
| 206 |
#ifdef QSSG_PARTICLES_ENABLE_ANIMATED |
| 207 |
spriteData.y = p.animationFrame; |
| 208 |
#endif |
| 209 |
} |
| 210 |
|