double-hit-balls/assets/phong_fragment.txt

50 lines
1.3 KiB
Plaintext
Raw Permalink Normal View History

2018-01-21 21:05:15 +00:00
precision mediump float;
uniform sampler2D Texture;
uniform float Transparency;
varying vec3 position;
varying vec3 normal;
varying vec2 texCoord;
uniform vec3 lightPos;
uniform vec3 cameraPos;
uniform vec3 ambientLight;
uniform vec3 diffuseLight;
uniform vec3 specularLight;
uniform float specularPower;
void phongModel( vec3 norm, out vec3 ambAndDiff, out vec3 spec )
{
vec3 lightDirection = normalize(vec3(lightPos));
vec3 cameraDirection = normalize(vec3(cameraPos) - position);
vec3 lightReflection = reflect( -lightDirection, norm );
float sDotN = max( dot(lightDirection,norm), 0.0 );
vec3 diffuse = diffuseLight * sDotN;
ambAndDiff = ambientLight + diffuse;
spec = vec3(0.0);
if(sDotN > 0.0)
{
spec = specularLight * pow(max( dot(lightReflection, cameraDirection), 0.0 ), specularPower);
// spec is NAN, if dot == 0 && specularPower <= 0
}
}
void main()
{
vec3 ambAndDiff, spec;
vec3 norm = normalize(normal);
phongModel(norm, ambAndDiff, spec);
vec4 textureColor = texture2D(Texture, texCoord).rgba;
textureColor.r = textureColor.r * ambAndDiff.r + spec.r;
textureColor.g = textureColor.g * ambAndDiff.g + spec.g;
textureColor.b = textureColor.b * ambAndDiff.b + spec.b;
gl_FragColor = textureColor;
}