Convert shaders in dali-demo to use shader compilation tool
[platform/core/uifw/dali-demo.git] / examples / particles / shaders / particle-view.vert
1 // Shader for billboarded particles, where the vertices of the particles
2 // are supplied as vec3 position (particle position) + vec2 sub-position.
3
4 #version 300 es
5
6 precision lowp float;
7 uniform mat4 uModelView; // DALi
8 uniform mat4 uProjection; // DALi
9 uniform vec3 uSize; // DALi
10 uniform vec4 uColor; // DALi
11
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;
17 uniform float uTime;
18 uniform float uFocalLength;
19 uniform float uAperture;
20 uniform float uPopulation;
21
22 struct Scatter
23 {
24   float radiusSqr;
25   float amount;
26   vec3 ray;
27 };
28
29 const int SCATTER_VARS = 6; // Must match ParticleView::mScatterProps' size.
30 uniform Scatter uScatter[SCATTER_VARS];
31
32 const int POPULATION_GRANULARITY = 128;
33 uniform float uOrderLookUp[POPULATION_GRANULARITY];
34
35 in vec3 aPosition;
36 in float aSeed;
37 in vec4 aPath;
38 in vec2 aSubPosition;
39 in float aSize;
40
41 flat out float vDepth;
42 flat out float vFocalDistance;
43 out vec2 vUvUnit;
44 flat out float vOpacity;
45 flat out vec3 vColor; // ignore alpha
46
47 float bezier(vec3 control, float alpha)
48 {
49   return mix(mix(control.x, control.y, alpha), mix(control.y, control.z, alpha), alpha);
50 }
51
52 void main() {
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)];
56
57   // Get twinkle scalar
58   float twinkle = sin(uTime * floor(uTwinkleFrequency * aSeed) + fract(aSeed * 1.17137));
59
60   // Add Motion
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);
70
71   // Model to view position
72   vec3 position3 = aPosition * uSize + motion;
73
74   vec4 position = uModelView * vec4(position3, 1.f);
75
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)
79   {
80     vec2 scatterDist = (normalizedPos - uScatter[i].ray * dot(uScatter[i].ray, normalizedPos)).xy;
81
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;
86   }
87
88   // Calculate normalised depth and distance from focal plane
89   float depth = (position.z - uDepthRange.x) * uDepthRange.y;
90   vDepth = depth;
91
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.
95
96   // Calculate expiring scale - for size and opacity.
97   float expiringScale = smoothstep(order + 1.f, order, uPopulation);
98
99   // Calculate billboard position and size
100   vec2 subPosition = aSubPosition * aSize *
101     (1.f + twinkle * aSeed * uTwinkleSizeScale) *
102     expiringScale;
103
104   // Insist on hacking the size? Do it here...
105   float sizeHack = depth + .5f;
106   // NOTE: sizeHack *= sizeHack looked slightly better.
107   subPosition *= sizeHack;
108
109   vec3 subPositionView = vec3(subPosition, 0.);
110
111   // Add billboards to view position.
112   position += vec4(subPositionView, 0.f);
113
114   // subPosition doubles as normalized (-1..1) UV.
115   vUvUnit = aSubPosition;
116
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);
120
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);
124
125   gl_Position = uProjection * position;
126 }