| 1 |
#version 300 es |
| 2 |
|
| 3 |
precision highp float; |
| 4 |
|
| 5 |
uniform mat4 viewMatrix; |
| 6 |
|
| 7 |
uniform vec3 lightPosition; |
| 8 |
uniform vec3 lightIntensity; |
| 9 |
|
| 10 |
uniform vec3 ka; // Ambient reflectivity |
| 11 |
uniform vec3 kd; // Diffuse reflectivity |
| 12 |
uniform vec3 ks; // Specular reflectivity |
| 13 |
uniform float shininess; // Specular shininess factor |
| 14 |
|
| 15 |
uniform highp sampler2DShadow shadowMapTexture; |
| 16 |
|
| 17 |
in vec4 positionInLightSpace; |
| 18 |
|
| 19 |
in vec3 position; |
| 20 |
in vec3 normal; |
| 21 |
|
| 22 |
out vec4 fragColor; |
| 23 |
|
| 24 |
vec3 dsModel(const in vec3 pos, const in vec3 n) |
| 25 |
{ |
| 26 |
// Calculate the vector from the light to the fragment |
| 27 |
vec3 s = normalize(vec3(viewMatrix * vec4(lightPosition, 1.0)) - pos); |
| 28 |
|
| 29 |
// Calculate the vector from the fragment to the eye position |
| 30 |
// (origin since this is in "eye" or "camera" space) |
| 31 |
vec3 v = normalize(-pos); |
| 32 |
|
| 33 |
// Reflect the light beam using the normal at this fragment |
| 34 |
vec3 r = reflect(-s, n); |
| 35 |
|
| 36 |
// Calculate the diffuse component |
| 37 |
float diffuse = max(dot(s, n), 0.0); |
| 38 |
|
| 39 |
// Calculate the specular component |
| 40 |
float specular = 0.0; |
| 41 |
if (dot(s, n) > 0.0) |
| 42 |
specular = pow(max(dot(r, v), 0.0), shininess); |
| 43 |
|
| 44 |
// Combine the diffuse and specular contributions (ambient is taken into account by the caller) |
| 45 |
return lightIntensity * (kd * diffuse + ks * specular); |
| 46 |
} |
| 47 |
|
| 48 |
void main() |
| 49 |
{ |
| 50 |
float shadowMapSample = textureProj(shadowMapTexture, positionInLightSpace); |
| 51 |
|
| 52 |
vec3 ambient = lightIntensity * ka; |
| 53 |
|
| 54 |
vec3 result = ambient; |
| 55 |
if (shadowMapSample > 0.0) |
| 56 |
result += dsModel(position, normalize(normal)); |
| 57 |
|
| 58 |
fragColor = vec4(result, 1.0); |
| 59 |
} |