sampling in random hemisphere

This commit is contained in:
Piotrek
2021-04-25 13:50:39 +02:00
parent 8585f21c70
commit 9718ea68dd
7 changed files with 57 additions and 25 deletions
Binary file not shown.
+2 -2
View File
@@ -55,7 +55,7 @@ HitResult castBricks(vec3 origin, vec3 start, vec3 dir, vec3 incAxis, uint addr)
* origin: Camera position
* dir: Ray coming from camera position going through currently drawn pixel
*/
HitResult castNodes(Ray ray)
HitResult castNodes(Ray ray, uint maxSteps)
{
// Round input pos to nearest voxel
vec3 pos = floor(ray.origin);
@@ -70,7 +70,7 @@ HitResult castNodes(Ray ray)
vec3 incAxis = vec3(0,0,0);
// Iterate
for(int i=0;i<110;i++)
for(int i=0;i<maxSteps;i++)
{
// Get node
uint node = texelFetch(_NodesTexture, ivec3(pos), 0).r;
+39 -14
View File
@@ -37,30 +37,55 @@ float random(float s) {
return fract(sin(seed++ + s)*43758.5453123);
}
void main()
vec3 randomHemisphere(vec3 dir)
{
// Black by default
out_color = vec4(0,0,0,1);
vec3 uu = normalize(cross(dir, vec3(0.0,1.0,1.0)));
vec3 vv = cross(uu, dir);
vec2 rv2 = vec2(random(1), random(2));
float ra = sqrt(rv2.y);
float rx = ra*cos(6.2831*rv2.x);
float ry = ra*sin(6.2831*rv2.x);
float rz = sqrt(1.0-rv2.y);
vec3 rr = vec3(rx*uu + ry*vv + rz*dir );
return normalize(rr);
}
vec3 solve()
{
// Calculate ray direction
vec3 view_dir = (_ProjMatrixInv * vec4(vert_uv, 0, 1)).xyz;
vec3 rayDir = normalize(_ViewMatrix * vec4(view_dir,0)).xyz;
Ray primaryRay = Ray(_CamPos, rayDir);
// Raycast
HitResult primary = castNodes(primaryRay);
HitResult primary = castNodes(primaryRay, 100);
if(primary.data > 0)
{
out_color = vec4(applyLighting(primary.pos, primaryRay.dir, primary.normal, primary.data), 1.0);
vec3 color = applyLighting(primary.pos, primaryRay.dir, primary.normal, primary.data);
vec3 hitPos = primary.pos-primaryRay.dir*0.000001;
Ray shadowRay = Ray(primary.pos-primaryRay.dir*0.000001, _SunDir);
HitResult shadow = castNodes(shadowRay);
if(shadow.data > 0) out_color *= 0.1;
}
else
{
float up = clamp(dot(primaryRay.dir, vec3(0, 1, 0)) + 0.1, 0, 1);
vec3 sky = vec3(0.176, 0.592, 0.901) * up;
out_color = vec4(sky, 1.0);
// Shadow ray
Ray shadowRay = Ray(hitPos, _SunDir);
HitResult shadow = castNodes(shadowRay, 50);
if(shadow.data > 0) color *= 0.1;
// Secondary ray
Ray secondRay = Ray(hitPos, randomHemisphere(primary.normal));
HitResult second = castNodes(secondRay, 25);
if(second.data > 0) color += getAlbedo(second.data) * 0.8;
return color;
}
float up = clamp(dot(primaryRay.dir, vec3(0, 1, 0)) + 0.1, 0, 1);
return vec3(0.176, 0.592, 0.901) * up;
}
void main()
{
seed = 420 * vert_uv.x + 1337 * vert_uv.y + _Time * 1234;
out_color = vec4(solve(), 1);
}
+11 -4
View File
@@ -1,18 +1,25 @@
/*
* Returns material albedo for given voxel
*/
vec3 getAlbedo(uint data)
{
return _Materials[data-1].albedo.rgb;
}
/*
point: Hit point
view: View vector (camera ray)
normal: Normal vector
*/
vec3 applyLighting(vec3 hitPoint, vec3 view, vec3 normal, uint brick)
vec3 applyLighting(vec3 hitPoint, vec3 view, vec3 normal, uint data)
{
// Lighting
vec3 light_color = vec3(1.0, 1.0, 1.0);
vec3 light_color = vec3(1);
vec3 ambient = light_color * 0.1;
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-1].albedo.rgb;//vec3(0.0, 0.1, 0.5);
return (ambient + diffuse + specular) * object_color;
return (ambient + diffuse + specular) * getAlbedo(data);
}