3dexperiments/assets/shaders/terrain.fs

45 lines
1.4 KiB
Forth
Raw Normal View History

precision highp float;
varying vec2 uv;
varying vec3 toLightVector[5];
varying vec3 surfaceNormal;
varying vec3 toCameraVector;
uniform sampler2D texture0;
uniform vec3 lightColor[5];
uniform vec3 attenuation[5];
void main() {
float reflectivity = 0.0;
float shineDamper = 0.0;
vec3 unitNormal = normalize(surfaceNormal);
vec3 unitVectorToCamera = normalize(toCameraVector);
vec3 totalDiffuse = vec3(0.0);
vec3 totalSpecular = vec3(0.0);
for(int i=0;i<5;i++){
float distance = length(toLightVector[i]);
float attFactor = attenuation[i].x + (attenuation[i].y * distance) + (attenuation[i].z * distance * distance);
vec3 unitLightVector = normalize(toLightVector[i]);
float nDotl = dot(unitNormal,unitLightVector);
float brightness = max(nDotl,0.0);
vec3 lightDirection = -unitLightVector;
vec3 reflectedLightDirection = reflect(lightDirection, unitNormal);
float specularFactor = dot(reflectedLightDirection, unitVectorToCamera);
specularFactor = max(specularFactor,0.0);
float dampedFactor = pow(specularFactor,shineDamper);
totalDiffuse = totalDiffuse + (brightness * lightColor[i]) / attFactor;
totalSpecular = totalSpecular + (dampedFactor * reflectivity * lightColor[i]) / attFactor;
}
totalDiffuse = max(totalDiffuse,0.2);
vec4 textureColor = texture2D(texture0, uv * 40.0);
if(textureColor.a<0.5){
discard;
}
gl_FragColor = vec4(totalDiffuse,1.0) * textureColor + vec4(totalSpecular,1.0);
}