working bricks ray traversing

This commit is contained in:
Piotrek
2021-04-23 15:27:04 +02:00
parent 8368545e6c
commit af5c2f2532
5 changed files with 66 additions and 15 deletions
+60 -12
View File
@@ -4,11 +4,11 @@
view: View vector (camera ray)
normal: Normal vector
*/
vec3 solve(vec3 point, vec3 view, vec3 normal)
vec3 solve(vec3 hitPoint, vec3 view, vec3 normal)
{
// Lighting
vec3 light_color = vec3(1.0, 1.0, 1.0);
vec3 light_dir = normalize(point-vec3(0.0, 1.5, 4.0));
vec3 light_dir = normalize(hitPoint-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);
@@ -19,11 +19,59 @@ vec3 solve(vec3 point, vec3 view, vec3 normal)
return (ambient + diffuse + specular) * object_color;
}
vec3 castBricks(vec3 origin, vec3 dir)
/*
* origin: Point hit on the node
* dir: Camera view direction (ray direction)
* addr: Number given by node, afaik brick offset in brick array
*/
vec3 castBricks(vec3 origin, vec3 start, vec3 dir, uint addr)
{
// Change scale
origin = start - origin;
origin += dir*0.00001;
origin *= 32;
// Same as in casting nodes
vec3 pos = floor(origin);
vec3 rayInv = 1.0/dir;
vec3 raySign = sign(dir);
vec3 dist = (pos-origin+0.5 + raySign*0.5) * rayInv;
ivec3 offset = ivec3(0, 0, (addr-1)*32);
vec3 incAxis = vec3(0,0,0);
for(int i=0;i<110;i++)
{
// Get brick
uint brick = texelFetch(bricksTexture, offset+ivec3(pos), 0).r;
if(brick != 0)
{
// Hit point
vec3 mini = (pos-origin+0.5 - raySign*0.5) * rayInv;
float len = max(mini.x, max(mini.y, mini.z));
vec3 hitPoint = (origin + dir * len) + start;
// Normal vector (negate previous increment axis and mult by sign)
vec3 normal = -incAxis * raySign;
return solve(hitPoint, dir, normal);
}
// Get new closest axis to increment
incAxis = step(dist.xyz, dist.yzx) * step(dist.xyz, dist.zxy);
dist += incAxis * raySign * rayInv;
pos += incAxis * raySign;
// Outside bounds
if(pos.x < 0 || pos.y < 0 || pos.z < 0) { break; }
if(pos.x > 32 || pos.y > 32 || pos.z > 32) { break; }
}
return vec3(0.5,0.5,0.5);
}
/*
* origin: Camera position
* dir: Ray coming from camera position going through currently drawn pixel
*/
vec3 castNodes(vec3 origin, vec3 dir)
{
// Round input pos to nearest voxel
@@ -38,8 +86,8 @@ vec3 castNodes(vec3 origin, vec3 dir)
// Closest axis to increment in grid will be stored here (x=1 or y=1 or z=1)
vec3 incAxis = vec3(0,0,0);
float level = 1;
for(int i=0;i<50;i++)
// Iterate
for(int i=0;i<56;i++)
{
// Get node
uint node = texelFetch(nodesTexture, ivec3(pos), 0).r;
@@ -48,14 +96,13 @@ vec3 castNodes(vec3 origin, vec3 dir)
// 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;
vec3 hitPoint = origin + dir * len;
// Normal vector (negate previous increment axis and mult by sign)
vec3 normal = -incAxis * raySign;
// Get subnode
//return step(vec3(0.5, 0.5, 0.5), point - pos);
//vec3 normal = -incAxis * raySign;
//return solve(hitPoint, dir, normal);
return solve(point, dir, normal);
// Cast brick
return castBricks(pos, hitPoint, dir, node); //todo: continue if not hit
}
// Get new closest axis to increment
@@ -63,6 +110,7 @@ vec3 castNodes(vec3 origin, vec3 dir)
dist += incAxis * raySign * rayInv;
pos += incAxis * raySign;
// Outside bounds
// if(pos.x < 0 || pos.y < 0 || pos.z < 0) { break; }
// if(pos.x > 1 || pos.y > 1 || pos.z > 1) { break; }
}