2018-11-25 17:44:32 +00:00
|
|
|
precision highp float;
|
|
|
|
|
|
|
|
varying vec2 uv;
|
2019-12-31 20:09:10 +00:00
|
|
|
varying vec3 toLightVector[4];
|
2018-11-25 17:44:32 +00:00
|
|
|
varying vec3 surfaceNormal;
|
|
|
|
varying vec3 toCameraVector;
|
|
|
|
|
|
|
|
uniform sampler2D texture0;
|
2019-12-31 20:09:10 +00:00
|
|
|
uniform vec3 lightColor[4];
|
|
|
|
uniform vec3 attenuation[4];
|
2018-11-25 17:44:32 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2019-12-31 20:09:10 +00:00
|
|
|
for(int i=0;i<4;i++){
|
2018-11-25 17:44:32 +00:00
|
|
|
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);
|
|
|
|
|
2020-04-09 16:37:35 +00:00
|
|
|
vec4 textureColor = texture2D(texture0, uv);
|
2018-11-25 17:44:32 +00:00
|
|
|
if(textureColor.a<0.5){
|
|
|
|
discard;
|
|
|
|
}
|
|
|
|
|
|
|
|
gl_FragColor = vec4(totalDiffuse,1.0) * textureColor + vec4(totalSpecular,1.0);
|
|
|
|
}
|