a8f4865a6a2e7d2c24f5e0c8347ace0491ae7279
[platform/core/uifw/dali-core.git] / dali / internal / render / shader-source / mesh.txt
1 <VertexShader>
2   precision mediump float;
3
4   uniform   mediump mat4    uProjection;
5   uniform   mediump mat4    uModelView;
6   uniform   mediump mat4    uMvpMatrix;
7
8   uniform           bool    uTextureMapped;
9   uniform   mediump vec4    uCustomTextureCoords;
10   attribute mediump vec2    aTexCoord;
11   varying   mediump vec2    vTexCoord;
12
13   uniform   mat3            uModelViewIT;
14   attribute mediump vec3    aNormal;
15   varying   mediump vec3    vNormal;
16
17   attribute mediump vec3    aPosition;
18   varying   mediump vec4    vVertex;
19
20 #define MAX_BONES_PER_MESH  12
21
22 #ifdef USE_BONES
23   uniform   int             uBoneCount;
24   uniform   mediump mat4    uBoneMatrices[MAX_BONES_PER_MESH];
25   uniform   mediump mat3    uBoneMatricesIT[MAX_BONES_PER_MESH];
26   attribute mediump vec4    aBoneWeights;
27   attribute mediump vec4    aBoneIndices;
28 #endif
29
30   void main()
31   {
32     mediump vec4 vertexPosition = vec4(aPosition, 1.0);
33     mediump float lightIntensity;
34
35 #ifdef USE_BONES
36     if(uBoneCount > 0)
37     {
38       mediump vec4 boneWeights = aBoneWeights;
39       mediump ivec4 boneIndices = ivec4(aBoneIndices);
40       mediump vec3 vertexNormal;
41
42       // re-calculate the final weight
43       boneWeights.w = 1.0 - dot(boneWeights.xyz, vec3(1.0, 1.0, 1.0));
44
45       vec4 bonePos = (uBoneMatrices[boneIndices.x] * vertexPosition) * boneWeights.x;
46       bonePos     += (uBoneMatrices[boneIndices.y] * vertexPosition) * boneWeights.y;
47       bonePos     += (uBoneMatrices[boneIndices.z] * vertexPosition) * boneWeights.z;
48       bonePos     += (uBoneMatrices[boneIndices.w] * vertexPosition) * boneWeights.w;
49
50       vertexNormal  = (uBoneMatricesIT[boneIndices.x] * aNormal) * boneWeights.x;
51       vertexNormal += (uBoneMatricesIT[boneIndices.y] * aNormal) * boneWeights.y;
52       vertexNormal += (uBoneMatricesIT[boneIndices.z] * aNormal) * boneWeights.z;
53       vertexNormal += (uBoneMatricesIT[boneIndices.w] * aNormal) * boneWeights.w;
54       vertexNormal =  normalize(vertexNormal);
55
56       vertexPosition = uProjection * bonePos;
57       vVertex = bonePos;
58       vNormal = vertexNormal;
59     }
60     else
61     {
62 #endif
63       vertexPosition = uMvpMatrix * vec4(aPosition, 1.0);
64       vVertex = uModelView * vec4(aPosition, 1.0);
65       vNormal = mat3(uModelViewIT) * aNormal;
66 #ifdef USE_BONES
67     }
68 #endif
69     gl_Position = vertexPosition;
70
71     mediump vec2 start = uCustomTextureCoords.xy;
72     mediump vec2 scale = uCustomTextureCoords.zw;
73     vTexCoord = vec2(start.x + aTexCoord.x * scale.x, start.y + aTexCoord.y * scale.y);
74   }
75
76 </VertexShader>
77
78 <FragmentShader>
79   precision mediump float;
80
81   struct Material
82   {
83     mediump float mOpacity;
84     mediump float mShininess;
85     lowp    vec4  mAmbient;
86     lowp    vec4  mDiffuse;
87     lowp    vec4  mSpecular;
88     lowp    vec4  mEmissive;
89   };
90
91   uniform sampler2D     sTexture;
92   uniform sampler2D     sOpacityTexture;
93   uniform sampler2D     sNormalMapTexture;
94   uniform sampler2D     sEffect;
95   varying vec2          vTexCoord;
96
97   uniform Material      uMaterial;
98
99   uniform lowp  vec4    uColor;
100   varying highp vec4    vVertex;
101   varying highp vec3    vNormal;
102
103 #ifdef USE_LIGHTING
104   struct Light
105   {
106     int           mType;                      // 0=AMBIENT,1=DIRECTIONAL,2=SPOT,3=POINT
107     highp   vec2  mFallOff;                   // x,y = falloff start, falloff end
108     mediump vec2  mSpotAngle;                 // x,y   = inner cone and outer cone
109     mediump vec3  mLightPos;                  // position
110     mediump vec3  mLightDir;                  // directional (for direction/spot lights)
111     lowp    vec3  mAmbient;                   // ambient component of the light's color
112     lowp    vec3  mDiffuse;                   // diffuse component of the light's color
113     lowp    vec3  mSpecular;                  // specular component of the light's color
114   };
115
116   uniform         int   uNumberOfLights;
117   uniform Light         uLight0;
118
119   lowp vec3 lightColor;
120   lowp vec3 specularColor;
121
122   void calculateLight(Light light)
123   {
124     // Ensure that the varying vertex position doesn't lose precision
125     highp vec3 lightVector = light.mLightPos - vVertex.xyz;
126     vec3 N = normalize(vNormal);
127     vec3 L = normalize(lightVector);
128     // TODO: for directional light, should use mLightDir for light direction not lightVector
129     float NdotL = dot(N, L);
130
131     vec3 color = light.mAmbient * uMaterial.mAmbient.rgb;
132     color += light.mDiffuse * uMaterial.mDiffuse.rgb * abs(NdotL);
133
134     // Attenuation
135     highp float attenuation = 1.0;      // requires highp
136     if (light.mType >= 2)
137     {
138       attenuation -= smoothstep(light.mFallOff.x, light.mFallOff.y, length(lightVector));
139     }
140
141     // TODO spotlights
142
143     // add color to cumulative light total. TODO: don't attenuate directional light
144     lightColor += color * attenuation;
145
146     if (light.mType != 0 && NdotL > 0.0 && light.mType != 0)
147     {
148       // Specular highlight
149       vec3 E = normalize(vVertex.xyz);
150       vec3 R = reflect(L, N);
151       float specular = pow(max(dot(R, E), 0.0), uMaterial.mShininess);
152       specularColor += uMaterial.mSpecular.rgb * light.mSpecular * specular * attenuation;
153     }
154   }
155 #endif
156
157   void main()
158   {
159     // sample the texture for the initial color
160     vec4 fragColor = texture2D(sTexture, vTexCoord);
161
162 #ifdef USE_LIGHTING
163
164     // apply lighting and material properties
165     specularColor = vec3(0.0);
166     lightColor = vec3(0.0);
167
168     // @TODO conditionally compile different shaders for different number of lights
169     if( uNumberOfLights > 0 )
170     {
171       calculateLight(uLight0);
172     }
173
174     fragColor.rgb *= lightColor;
175     fragColor.rgb += specularColor;
176
177 #else
178
179     // apply material properties
180     fragColor.rgb *= (uMaterial.mAmbient + uMaterial.mDiffuse).rgb;
181
182 #endif
183
184     // apply material alpha/opacity to alpha channel
185     fragColor.a *= uMaterial.mOpacity * uMaterial.mDiffuse.a;
186
187     // and finally, apply Actor color
188     fragColor *= uColor;
189
190 // Next line useful for visualizing the normals
191 //    fragColor = vec4(normalize(vNormal), 1.0);
192     gl_FragColor = fragColor;
193   }
194
195 </FragmentShader>