HitResult and Ray structs. random function.

This commit is contained in:
Piotrek
2021-04-24 23:33:33 +02:00
parent e390b6ed9b
commit 8585f21c70
5 changed files with 26 additions and 14 deletions
Binary file not shown.
+9 -8
View File
@@ -1,6 +1,7 @@
/* /*
* origin: Point hit on the node * origin: Point hit on the node
* start: Camera pos
* dir: Camera view direction (ray direction) * dir: Camera view direction (ray direction)
* incAxis: Last axis that was used to increment position * incAxis: Last axis that was used to increment position
* addr: Number given by node, brick offset in brick array * addr: Number given by node, brick offset in brick array
@@ -54,16 +55,16 @@ HitResult castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
* origin: Camera position * origin: Camera position
* dir: Ray coming from camera position going through currently drawn pixel * dir: Ray coming from camera position going through currently drawn pixel
*/ */
HitResult castNodes(vec3 origin, vec3 dir) HitResult castNodes(Ray ray)
{ {
// Round input pos to nearest voxel // Round input pos to nearest voxel
vec3 pos = floor(origin); vec3 pos = floor(ray.origin);
// Inverse ray direction // Inverse ray direction
vec3 rayInv = 1.0/dir; vec3 rayInv = 1.0/ray.dir;
// Sign of the ray direction, to know where to increment pos // Sign of the ray direction, to know where to increment pos
vec3 raySign = sign(dir); vec3 raySign = sign(ray.dir);
// Distance to next voxel in grid // Distance to next voxel in grid
vec3 dist = (pos-origin+0.5 + raySign*0.5) * rayInv; vec3 dist = (pos-ray.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) // Closest axis to increment in grid will be stored here (x=1 or y=1 or z=1)
vec3 incAxis = vec3(0,0,0); vec3 incAxis = vec3(0,0,0);
@@ -76,12 +77,12 @@ HitResult castNodes(vec3 origin, vec3 dir)
if(node != 0) if(node != 0)
{ {
// Hit point // Hit point
vec3 mini = (pos-origin+0.5 - raySign*0.5) * rayInv; vec3 mini = (pos-ray.origin+0.5 - raySign*0.5) * rayInv;
float len = max(0.0, max(mini.x, max(mini.y, mini.z))); float len = max(0.0, max(mini.x, max(mini.y, mini.z)));
vec3 hitPoint = origin + dir * len; vec3 hitPoint = ray.origin + ray.dir * len;
// Cast brick // Cast brick
HitResult result = castBricks(pos, hitPoint, dir, incAxis, node); HitResult result = castBricks(pos, hitPoint, ray.dir, incAxis, node);
if(result.data > 0) return result; if(result.data > 0) return result;
} }
+11 -5
View File
@@ -32,6 +32,10 @@ layout(set=3, binding=0) uniform Materials {
#include "shading.cginc" #include "shading.cginc"
#include "raycasting.cginc" #include "raycasting.cginc"
float seed;
float random(float s) {
return fract(sin(seed++ + s)*43758.5453123);
}
void main() void main()
{ {
@@ -40,20 +44,22 @@ void main()
// Calculate ray direction // Calculate ray direction
vec3 view_dir = (_ProjMatrixInv * vec4(vert_uv, 0, 1)).xyz; vec3 view_dir = (_ProjMatrixInv * vec4(vert_uv, 0, 1)).xyz;
vec3 ray_dir = normalize(_ViewMatrix * vec4(view_dir,0)).xyz; vec3 rayDir = normalize(_ViewMatrix * vec4(view_dir,0)).xyz;
Ray primaryRay = Ray(_CamPos, rayDir);
// Raycast // Raycast
HitResult primary = castNodes(_CamPos, ray_dir); HitResult primary = castNodes(primaryRay);
if(primary.data > 0) if(primary.data > 0)
{ {
out_color = vec4(applyLighting(primary.pos, ray_dir, primary.normal, primary.data), 1.0); out_color = vec4(applyLighting(primary.pos, primaryRay.dir, primary.normal, primary.data), 1.0);
HitResult shadow = castNodes(primary.pos-ray_dir*0.000001, _SunDir); Ray shadowRay = Ray(primary.pos-primaryRay.dir*0.000001, _SunDir);
HitResult shadow = castNodes(shadowRay);
if(shadow.data > 0) out_color *= 0.1; if(shadow.data > 0) out_color *= 0.1;
} }
else else
{ {
float up = clamp(dot(ray_dir, vec3(0, 1, 0)) + 0.1, 0, 1); float up = clamp(dot(primaryRay.dir, vec3(0, 1, 0)) + 0.1, 0, 1);
vec3 sky = vec3(0.176, 0.592, 0.901) * up; vec3 sky = vec3(0.176, 0.592, 0.901) * up;
out_color = vec4(sky, 1.0); out_color = vec4(sky, 1.0);
} }
+1 -1
View File
@@ -10,7 +10,7 @@ vec3 applyLighting(vec3 hitPoint, vec3 view, vec3 normal, uint brick)
vec3 ambient = light_color * 0.1; vec3 ambient = light_color * 0.1;
vec3 diffuse = light_color * max(1.0-dot(normal, _SunDir), 0.0); vec3 diffuse = light_color * max(1.0-dot(normal, _SunDir), 0.0);
vec3 specular = light_color * pow(max(dot(view, reflect(-_SunDir, normal)), 0.0), 32); vec3 specular = light_color * pow(max(dot(view, reflect(_SunDir, normal)), 0.0), 32);
// Combine colors // Combine colors
vec3 object_color = _Materials[brick-1].albedo.rgb;//vec3(0.0, 0.1, 0.5); vec3 object_color = _Materials[brick-1].albedo.rgb;//vec3(0.0, 0.1, 0.5);
+5
View File
@@ -1,4 +1,9 @@
struct Ray {
vec3 origin;
vec3 dir;
};
struct HitResult { struct HitResult {
uint data; // Voxel data uint data; // Voxel data
vec3 pos; // Position in world vec3 pos; // Position in world