Convert shaders in dali-demo to use shader compilation tool
[platform/core/uifw/dali-demo.git] / examples / refraction-effect / shaders / refraction-effect-refraction.frag
1 precision mediump float;
2 uniform   mediump float  uEffectStrength;
3 uniform   mediump vec3   uLightPosition;
4 uniform   mediump vec2   uLightXYOffset;
5 uniform   mediump vec2   uLightSpinOffset;
6 uniform   mediump float  uLightIntensity;
7 uniform   lowp    vec4   uColor;
8 uniform   sampler2D      sTexture;
9 varying   mediump vec4   vVertex;
10 varying   mediump vec3   vNormal;
11 varying   mediump vec2   vTexCoord;
12 varying   mediump vec2   vTextureOffset;
13
14 vec3 rgb2hsl(vec3 rgb)
15 {
16   float epsilon = 1.0e-10;
17   vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
18   vec4 P = mix(vec4(rgb.bg, K.wz), vec4(rgb.gb, K.xy), step(rgb.b, rgb.g));
19   vec4 Q = mix(vec4(P.xyw, rgb.r), vec4(rgb.r, P.yzx), step(P.x, rgb.r));
20
21   // RGB -> HCV
22   float value = Q.x;
23   float chroma = Q.x - min(Q.w, Q.y);
24   float hue = abs(Q.z + (Q.w-Q.y) / (6.0*chroma+epsilon));
25   // HCV -> HSL
26   float lightness = value - chroma*0.5;
27   return vec3( hue, chroma/max( 1.0-abs(lightness*2.0-1.0), 1.0e-1 ), lightness );
28 }
29
30 vec3 hsl2rgb( vec3 hsl )
31 {
32   // pure hue->RGB
33   vec4 K = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
34   vec3 p = abs(fract(hsl.xxx + K.xyz) * 6.0 - K.www);
35   vec3 RGB = clamp(p - K.xxx, 0.0, 1.0);
36
37   float chroma = ( 1.0 - abs( hsl.z*2.0-1.0 ) ) * hsl.y;
38   return ( RGB - 0.5 ) * chroma + hsl.z;
39 }
40
41 void main()
42 {
43   vec3 normal = normalize( vNormal);
44
45   vec3 lightPosition = uLightPosition + vec3(uLightXYOffset+uLightSpinOffset, 0.0);
46   mediump vec3 vecToLight = normalize( (lightPosition - vVertex.xyz) * 0.01 );
47   mediump float spotEffect = pow( max(0.05, vecToLight.z ) - 0.05, 8.0);
48
49   spotEffect = spotEffect * uEffectStrength;
50   mediump float lightDiffuse = ( ( dot( vecToLight, normal )-0.75 ) *uLightIntensity  ) * spotEffect;
51
52   lowp vec4 color = texture2D( sTexture, vTexCoord + vTextureOffset * spotEffect );
53   vec3 lightedColor =  hsl2rgb( rgb2hsl(color.rgb) + vec3(0.0,0.0,lightDiffuse) );
54
55   gl_FragColor = vec4( lightedColor, color.a ) * uColor;
56 }