only calculate normal once

master
EmaMaker 2023-02-09 10:37:48 +01:00
parent b19ee8a02a
commit 6b9b85e662
1 changed files with 5 additions and 3 deletions

View File

@ -214,21 +214,23 @@ vec3 ray_march(in vec3 ro, in vec3 rd)
if(dist.sdf <= 0.000025 * i){ if(dist.sdf <= 0.000025 * i){
// Basic Phong illumination // Basic Phong illumination
// ambient // ambient
vec3 normal = sceneNormal(pos);
vec3 ambient = lightColor*dist.data.ambient; vec3 ambient = lightColor*dist.data.ambient;
// diffuse // diffuse
lightDir = normalize(lightPos - pos); lightDir = normalize(lightPos - pos);
float diff = max(dot(sceneNormal(pos), lightDir), 0.0); float diff = max(dot(normal, lightDir), 0.0);
vec3 diffuse = diff * dist.data.diffuse; vec3 diffuse = diff * dist.data.diffuse;
// specular // specular
vec3 viewDir = normalize(u_camorigin - pos); vec3 viewDir = normalize(u_camorigin - pos);
vec3 reflectDir = reflect(-lightDir, sceneNormal(pos)); vec3 reflectDir = reflect(-lightDir, normal);
float spec = pow(max(dot(viewDir, reflectDir), 0.0), dist.data.shininess); float spec = pow(max(dot(viewDir, reflectDir), 0.0), dist.data.shininess);
vec3 specular = lightColor * spec * dist.data.specular; vec3 specular = lightColor * spec * dist.data.specular;
vec3 color = (vec3(0.5) * ambient + (vec3(0.3) * diffuse +vec3(0.2) * specular)*shadow(pos, normalize(lightPos), 32.0))*calcAO(pos, sceneNormal(pos)); vec3 color = (vec3(0.5) * ambient + (vec3(0.3) * diffuse +vec3(0.2) * specular)*shadow(pos, normalize(lightPos), 32.0))*calcAO(pos, normal);
return color ; return color ;
} }