This commit is contained in:
Piotrek
2021-04-24 20:47:04 +02:00
parent 00fd077642
commit 61ec3e5a21
8 changed files with 54 additions and 18 deletions
Binary file not shown.
+7 -7
View File
@@ -5,7 +5,7 @@
* incAxis: Last axis that was used to increment position
* addr: Number given by node, brick offset in brick array
*/
vec4 castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
HitResult castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
{
vec3 o = origin;
@@ -34,7 +34,7 @@ vec4 castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
// Normal vector (negate previous increment axis and mult by sign)
vec3 normal = -incAxis * raySign;
return vec4(solve(hitPoint, dir, normal, brick-1), 1.0);
return HitResult(brick-1, hitPoint, normal);
}
// Get new closest axis to increment
@@ -47,14 +47,14 @@ vec4 castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
if(pos.x > 31 || pos.y > 31 || pos.z > 31) { break; }
}
return vec4(1.0,0.0,0.5,0.0);
return HitResult(0, vec3(0,0,0), vec3(0,0,0));
}
/*
* origin: Camera position
* dir: Ray coming from camera position going through currently drawn pixel
*/
vec3 castNodes(vec3 origin, vec3 dir)
HitResult castNodes(vec3 origin, vec3 dir)
{
// Round input pos to nearest voxel
vec3 pos = floor(origin);
@@ -81,8 +81,8 @@ vec3 castNodes(vec3 origin, vec3 dir)
vec3 hitPoint = origin + dir * len;
// Cast brick
vec4 result = castBricks(pos, hitPoint, dir, incAxis, node);
if(result.a > 0.0) return result.rgb;
HitResult result = castBricks(pos, hitPoint, dir, incAxis, node);
if(result.data > 0) return result;
}
// Get new closest axis to increment
@@ -96,5 +96,5 @@ vec3 castNodes(vec3 origin, vec3 dir)
}
// Not found
return vec3(0.0, 0.0, 0.0);
return HitResult(0, vec3(0,0,0), vec3(0,0,0));
}
+13 -1
View File
@@ -13,6 +13,7 @@ layout(set=0,binding=0) uniform Uniforms {
mat4 _ViewMatrix;
mat4 _ProjMatrixInv;
vec3 _CamPos;
vec3 _SunDir;
float _Time;
};
layout(set=1,binding=0) uniform usampler3D _NodesTexture;
@@ -27,16 +28,27 @@ layout(set=3, binding=0) uniform Materials {
// #define NEAR 0.01
// Include
#include "structs.cginc"
#include "shading.cginc"
#include "raycasting.cginc"
void main()
{
// Black by default
out_color = vec4(0,0,0,1);
// Calculate ray direction
vec3 view_dir = (_ProjMatrixInv * vec4(vert_uv, 0, 1)).xyz;
vec3 ray_dir = normalize(_ViewMatrix * vec4(view_dir,0)).xyz;
// Raycast
out_color = vec4(castNodes(_CamPos, ray_dir), 1.0);
HitResult primary = castNodes(_CamPos, ray_dir);
if(primary.data > 0)
{
out_color = vec4(applyLighting(primary.pos, ray_dir, primary.normal, primary.data), 1.0);
HitResult shadow = castNodes(primary.pos-ray_dir*0.000001, _SunDir);
if(shadow.data > 0) out_color *= 0.1;
}
}
+3 -4
View File
@@ -3,15 +3,14 @@
view: View vector (camera ray)
normal: Normal vector
*/
vec3 solve(vec3 hitPoint, vec3 view, vec3 normal, uint brick)
vec3 applyLighting(vec3 hitPoint, vec3 view, vec3 normal, uint brick)
{
// Lighting
vec3 light_color = vec3(1.0, 1.0, 1.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);
vec3 specular = light_color * pow(max(dot(view, reflect(-light_dir, normal)), 0.0), 32);
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);
// Combine colors
vec3 object_color = _Materials[brick].albedo.rgb;//vec3(0.0, 0.1, 0.5);
+6
View File
@@ -0,0 +1,6 @@
struct HitResult {
uint data; // Voxel data
vec3 pos; // Position in world
vec3 normal; // Normal vector
};