basic phong lighting
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
pub const DATA: &[u32] = &[
|
pub const DATA: &[u32] = &[
|
||||||
// z0
|
// z0
|
||||||
1,0,
|
1,0,
|
||||||
0,0,
|
0,1,
|
||||||
// z1
|
// z1
|
||||||
0,0,
|
0,0,
|
||||||
0,0,
|
0,0,
|
||||||
|
|||||||
Binary file not shown.
@@ -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);
|
||||||
|
}
|
||||||
+6
-11
@@ -24,19 +24,14 @@ layout(set=1,binding=0) uniform usampler3D diffuseTex;
|
|||||||
|
|
||||||
// Include
|
// Include
|
||||||
#include "shapes.cginc"
|
#include "shapes.cginc"
|
||||||
#include "raymarching.cginc"
|
#include "raycasting.cginc"
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
// Test
|
|
||||||
uint node = texelFetch(diffuseTex, ivec3(0, 0, 0), 0).r;
|
|
||||||
if(node == 4294967295) out_color = vec4(0.0, 0.5, 1.0, 1.0);
|
|
||||||
else out_color = vec4(1.0, 0.1, 0.0, 1.0);
|
|
||||||
|
|
||||||
|
|
||||||
// Calculate ray direction
|
// Calculate ray direction
|
||||||
// vec3 view_dir = (proj_matrix_inv * vec4(vert_uv, 0, 1)).xyz;
|
vec3 view_dir = (proj_matrix_inv * vec4(vert_uv, 0, 1)).xyz;
|
||||||
// vec3 ray_dir = normalize(view_matrix * vec4(view_dir,0)).xyz;
|
vec3 ray_dir = normalize(view_matrix * vec4(view_dir,0)).xyz;
|
||||||
// // Calculate color
|
|
||||||
// out_color = vec4(raymarch(cam_pos, ray_dir), 1);
|
// Raycast
|
||||||
|
out_color = vec4(raycast(cam_pos, ray_dir), 1.0);
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user