basic phong lighting
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
|
||||
vec3 raycast(vec3 origin, vec3 dir)
|
||||
{
|
||||
// Round input pos to nearest voxel
|
||||
vec3 pos = floor(origin);
|
||||
// Inverse ray direction
|
||||
vec3 rayInv = 1.0/dir;
|
||||
// Sign of the ray direction, to know where to increment pos
|
||||
vec3 raySign = sign(dir);
|
||||
// Distance to next voxel in grid
|
||||
vec3 dist = (pos-origin+0.5 + raySign*0.5) * rayInv;
|
||||
|
||||
// Closest axis to increment in grid will be stored here (x=1 or y=1 or z=1)
|
||||
vec3 incAxis = vec3(0,0,0);
|
||||
|
||||
|
||||
for(int i=0;i<50;i++)
|
||||
{
|
||||
// Get node
|
||||
uint node = texelFetch(diffuseTex, ivec3(pos), 0).r;
|
||||
if(node != 0)
|
||||
{
|
||||
// Hit point
|
||||
vec3 mini = (pos-origin+0.5 - raySign*0.5) * rayInv;
|
||||
float len = max(mini.x, max(mini.y, mini.z));
|
||||
vec3 point = origin + dir * len;
|
||||
// Normal vector (negate previous increment axis and mult by sign)
|
||||
vec3 normal = -incAxis * raySign;
|
||||
|
||||
// Lighting
|
||||
vec3 light_color = vec3(1.0, 1.0, 1.0);
|
||||
vec3 light_dir = normalize(point-vec3(0.0, 1.5, 4.0));
|
||||
|
||||
vec3 ambient = light_color * 0.1;
|
||||
vec3 diffuse = light_color * max(1.0-dot(normal, light_dir), 0.0);
|
||||
vec3 specular = light_color * pow(max(dot(dir, reflect(-light_dir, normal)), 0.0), 32);
|
||||
|
||||
// Object color
|
||||
vec3 object_color = vec3(0.0, 0.1, 0.5);
|
||||
return (ambient + diffuse + specular) * object_color;
|
||||
}
|
||||
|
||||
// Get new closest axis to increment
|
||||
incAxis = step(dist.xyz, dist.yzx) * step(dist.xyz, dist.zxy);
|
||||
dist += incAxis * raySign * rayInv;
|
||||
pos += incAxis * raySign;
|
||||
|
||||
// if(pos.x < 0 || pos.y < 0 || pos.z < 0) { break; }
|
||||
// if(pos.x > 1 || pos.y > 1 || pos.z > 1) { break; }
|
||||
}
|
||||
|
||||
// Not found
|
||||
return vec3(0.0, 0.0, 0.0);
|
||||
}
|
||||
Reference in New Issue
Block a user