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