Convert shaders in dali-demo to use shader compilation tool
[platform/core/uifw/dali-demo.git] / examples / deferred-shading / shaders / deferred-shading-mainpass.frag
1 #version 300 es
2
3 precision mediump float;
4
5 const int kMaxLights = 32;
6
7 const float kAttenuationConst = .05f;
8 const float kAttenuationLinear = .1f;
9 const float kAttenuationQuadratic = .15f;
10
11 // G-buffer
12 uniform sampler2D uTextureNormal;
13 uniform sampler2D uTexturePosition;
14 uniform sampler2D uTextureColor;
15
16 uniform mat4 uInvProjection;
17 uniform vec3 uDepth_InvDepth_Near;
18
19 #define DEPTH uDepth_InvDepth_Near.x
20 #define INV_DEPTH uDepth_InvDepth_Near.y
21 #define NEAR uDepth_InvDepth_Near.z
22
23 // Light source uniforms
24 struct Light
25 {
26   vec3 position;    // view space
27   float radius;
28   vec3 color;
29 };
30
31 uniform Light uLights[kMaxLights];
32
33 in vec2 vUv;
34 out vec4 oColor;
35
36 vec4 Unmap(vec4 m)  // texture -> projection
37 {
38   m.w = m.w * DEPTH + NEAR;
39   m.xyz = (m.xyz - vec3(.5)) * (2.f * m.w);
40   return m;
41 }
42
43 vec3 CalculateLighting(vec3 pos, vec3 normal)
44 {
45   vec3 viewDir = normalize(pos);
46   vec3 viewDirRefl = -reflect(viewDir, normal);
47
48   vec3 light = vec3(0.04f); // fake ambient term
49   for (int i = 0; i < kMaxLights; ++i)
50   {
51     vec3 rel = pos - uLights[i].position;
52     float distance = length(rel);
53     rel /= distance;
54
55     float a = uLights[i].radius / (kAttenuationConst + kAttenuationLinear * distance +
56       kAttenuationQuadratic * distance * distance);     // attenuation
57
58     float l = max(0.f, dot(normal, rel));   // lambertian
59     float s = pow(max(0.f, dot(viewDirRefl, rel)), 256.f);  // specular
60
61     light += (uLights[i].color * (l + s)) * a;
62   }
63
64   return light;
65 }
66
67 void main()
68 {
69   vec3 normSample = texture(uTextureNormal, vUv).xyz;
70   if (dot(normSample, normSample) == 0.f)
71   {
72     discard;  // if we didn't write this texel, don't bother lighting it.
73   }
74
75   vec3 normal = normalize(normSample - .5f);
76
77   vec4 posSample = texture(uTexturePosition, vUv);
78   vec3 pos = (uInvProjection * Unmap(posSample)).xyz;
79
80   vec3 color = texture(uTextureColor, vUv).rgb;
81   vec3 finalColor = color * CalculateLighting(pos, normal);
82
83   oColor = vec4(finalColor, 1.f);
84 }