41 lines
1.3 KiB
GLSL
41 lines
1.3 KiB
GLSL
#version 300 es
|
|
precision mediump float;
|
|
uniform sampler2D s_texture;
|
|
uniform int u_texture_env_mode;
|
|
uniform vec4 u_light_ambient;
|
|
uniform vec4 u_light_diffuse;
|
|
uniform vec4 u_light_specular;
|
|
uniform vec3 u_light_dir;
|
|
uniform float u_material_shiness;
|
|
in vec3 v_position;
|
|
in vec2 v_texcoord;
|
|
in vec3 v_normal;
|
|
out vec4 o_fragColor;
|
|
void main()
|
|
{
|
|
const int texture_env_mode_replace = 0x1E01;
|
|
const int texture_env_mode_modulate = 0x2100;
|
|
vec4 color = texture( s_texture, v_texcoord );
|
|
if (u_texture_env_mode==texture_env_mode_replace)
|
|
o_fragColor = color;
|
|
else if (u_texture_env_mode==texture_env_mode_modulate){
|
|
// ambient
|
|
vec4 ambient = u_light_ambient;
|
|
// diffuse
|
|
vec3 norm = normalize(v_normal.xyz);
|
|
vec3 lightDir = normalize(u_light_dir);
|
|
float diff = max(dot(norm, lightDir), 0.0);
|
|
vec4 diffuse = u_light_diffuse * diff;
|
|
// specular
|
|
vec4 specular = vec4(0, 0, 0, 1);
|
|
if (u_material_shiness>0.0){
|
|
vec3 viewDir = normalize(-v_position);
|
|
vec3 reflectDir = normalize(reflect(-lightDir, norm));
|
|
float spec = pow(max(dot(viewDir, reflectDir), 0.0), u_material_shiness);
|
|
specular = u_light_specular * spec;
|
|
}
|
|
o_fragColor = (ambient + diffuse)*color + specular;
|
|
}
|
|
else
|
|
o_fragColor = vec4(0, 0, 0, 1);
|
|
} |