}
#pragma MSP stage(fragment)
+struct IncomingLight
+{
+ vec3 direction;
+ vec3 color;
+};
+
virtual vec3 get_fragment_normal()
{
if(use_normal_map)
return normalize(world_normal);
}
-virtual vec3 get_light_direction(int index)
+virtual IncomingLight get_incoming_light(int index, vec3 world_pos)
{
vec4 light_pos = light_sources[index].position;
- return normalize(light_pos.xyz-world_vertex.xyz*light_pos.w);
+ vec3 rel_pos = light_pos.xyz-world_pos*light_pos.w;
+ float d = length(rel_pos);
+ return IncomingLight(rel_pos/d, light_sources[index].color);
}
virtual vec3 get_environment_sample(vec3 direction, float roughness)
}
/* Computes the full contribution of a single light */
-vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 light_color, vec3 base_color, float metalness, float roughness)
+vec3 cooktorrance_one_light_direct(vec3 normal, vec3 look, vec3 light, vec3 base_color, float metalness, float roughness)
{
vec3 halfway = normalize(light-look);
float ndist = normal_distribution_ggxtr(normal, halfway, roughness);
vec3 k_diff = (1.0-k_spec)*(1.0-metalness);
float spec_denom = max(4.0*max(dot(normal, -look), 0.0)*max(dot(normal, light), 0.0), 0.001);
- return max(dot(normal, light), 0.0)*light_color*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/spec_denom);
+ return max(dot(normal, light), 0.0)*(k_diff*lambert_diffuse(base_color)+k_spec*ndist*geom/spec_denom);
}
vec3 cooktorrance_environment(vec3 normal, vec3 look, vec3 base_color, float metalness, float roughness)
for(int i=0; i<max_lights; ++i)
if(light_sources[i].type!=0)
{
- vec3 light = get_light_direction(i);
+ IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
float shadow = get_shadow_factor(i);
- color += cooktorrance_one_light_direct(normal, look, light, light_sources[i].color, base_color, metalness, roughness)*shadow;
+ color += cooktorrance_one_light_direct(normal, look, incoming.direction, base_color, metalness, roughness)*incoming.color*shadow;
}
color += cooktorrance_environment(normal, look, base_color, metalness, roughness);
return ambient_color.rgb*surface_diffuse;
}
-vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 light_color, vec3 surface_diffuse, vec3 surface_specular, float shininess)
+vec3 phong_one_light(vec3 light, vec3 normal, vec3 look, vec3 surface_diffuse, vec3 surface_specular, float shininess)
{
float diffuse_intensity = max(dot(light, normal), 0.0);
- vec3 color = light_color*surface_diffuse*diffuse_intensity;
+ vec3 color = surface_diffuse*diffuse_intensity;
if(use_specular)
{
vec3 reflected = reflect(look, normal);
float specular_intensity = pow(max(dot(reflected, light), 0.0), shininess);
- color += light_color*surface_specular*specular_intensity;
+ color += surface_specular*specular_intensity;
}
return color;
}
for(int i=0; i<max_lights; ++i)
if(light_sources[i].type!=0)
{
- vec3 light = get_light_direction(i);
+ IncomingLight incoming = get_incoming_light(i, world_vertex.xyz);
float shadow = get_shadow_factor(i);
- color += phong_one_light(light, normal, look, light_sources[i].color, surface_diffuse, surface_specular, shininess)*shadow;
+ color += phong_one_light(incoming.direction, normal, look, surface_diffuse, surface_specular, shininess)*incoming.color*shadow;
}
if(use_emission)