1 // Shader for billboarded particles, where the vertices of the particles
2 // are supplied as vec3 position (particle position) + vec2 sub-position.
7 uniform mat4 uModelView; // DALi
8 uniform mat4 uProjection; // DALi
9 uniform vec3 uSize; // DALi
10 uniform vec4 uColor; // DALi
12 uniform vec3 uSecondaryColor;
13 uniform vec2 uDepthRange; // x is zNear, y is 1.f / (zFar - zNear)
14 uniform float uTwinkleFrequency;
15 uniform float uTwinkleSizeScale;
16 uniform float uTwinkleOpacityWeight;
18 uniform float uFocalLength;
19 uniform float uAperture;
20 uniform float uPopulation;
29 const int SCATTER_VARS = 6; // Must match ParticleView::mScatterProps' size.
30 uniform Scatter uScatter[SCATTER_VARS];
32 const int POPULATION_GRANULARITY = 128;
33 uniform float uOrderLookUp[POPULATION_GRANULARITY];
41 flat out float vDepth;
42 flat out float vFocalDistance;
44 flat out float vOpacity;
45 flat out vec3 vColor; // ignore alpha
47 float bezier(vec3 control, float alpha)
49 return mix(mix(control.x, control.y, alpha), mix(control.y, control.z, alpha), alpha);
53 // Get random order from the look-up table, based on particle ID.
54 int particleId = gl_VertexID / 6;
55 float order = uOrderLookUp[particleId & (POPULATION_GRANULARITY - 1)];
58 float twinkle = sin(uTime * floor(uTwinkleFrequency * aSeed) + fract(aSeed * 1.17137));
61 float s = sin(uTime + aSeed) * .5f + .5f; // different phase for all
62 // NOTE: you'd think that taking the bezier() calls apart would save 4 mix() calls, since
63 // the mix()es (of xy / yz / zw / wx) are all calculated twice. It turns out that the MALI
64 // compiler is already doing this; leaving it as is for readability.
65 float bx0 = bezier(aPath.xyz, s);
66 float bx1 = bezier(aPath.zwx, s);
67 float by0 = bezier(aPath.yzw, s);
68 float by1 = bezier(aPath.wxy, s);
69 vec3 motion = vec3(mix(bx0, bx1, s), mix(by0, by1, s), 0.f);
71 // Model to view position
72 vec3 position3 = aPosition * uSize + motion;
74 vec4 position = uModelView * vec4(position3, 1.f);
76 // Add scatter - calculated in view space, using view ray
77 vec3 normalizedPos = position.xyz / uSize;
78 for (int i = 0; i < SCATTER_VARS; ++i)
80 vec2 scatterDist = (normalizedPos - uScatter[i].ray * dot(uScatter[i].ray, normalizedPos)).xy;
82 // NOTE: replacing the division with a multiplication (by inverse) oddly results in more instructions (MALI).
83 float scatter = max(0.f, uScatter[i].radiusSqr - dot(scatterDist, scatterDist)) *
84 uScatter[i].amount / aSize;
85 position.xy += scatter * normalize(scatterDist) * uSize.xy;
88 // Calculate normalised depth and distance from focal plane
89 float depth = (position.z - uDepthRange.x) * uDepthRange.y;
92 float focalDist = (uFocalLength - depth) * uAperture;
93 focalDist *= focalDist;
94 vFocalDistance = max(focalDist, 1e-6f); // NOTE: was clamp(..., 1.f); side effect: out of focus particles get squashed at higher aperture values.
96 // Calculate expiring scale - for size and opacity.
97 float expiringScale = smoothstep(order + 1.f, order, uPopulation);
99 // Calculate billboard position and size
100 vec2 subPosition = aSubPosition * aSize *
101 (1.f + twinkle * aSeed * uTwinkleSizeScale) *
104 // Insist on hacking the size? Do it here...
105 float sizeHack = depth + .5f;
106 // NOTE: sizeHack *= sizeHack looked slightly better.
107 subPosition *= sizeHack;
109 vec3 subPositionView = vec3(subPosition, 0.);
111 // Add billboards to view position.
112 position += vec4(subPositionView, 0.f);
114 // subPosition doubles as normalized (-1..1) UV.
115 vUvUnit = aSubPosition;
117 // Vary opacity (actor alpha) by time as well as expiring scale.
118 vOpacity = uColor.a * expiringScale *
119 (1.0f + aSeed + twinkle * uTwinkleOpacityWeight) / (2.0f + uTwinkleOpacityWeight);
121 // Randomize RGB using seed.
122 vec3 mixColor = vec3(fract(aSeed), fract(aSeed * 16.f), fract(aSeed * 256.f));
123 vColor = mix(uColor.rgb, uSecondaryColor, mixColor);
125 gl_Position = uProjection * position;