sampling in random hemisphere
This commit is contained in:
@@ -10,7 +10,7 @@ pub struct UniformValues {
|
|||||||
cam_pos: [f32; 3],
|
cam_pos: [f32; 3],
|
||||||
padding: f32,
|
padding: f32,
|
||||||
sun_dir: [f32; 3],
|
sun_dir: [f32; 3],
|
||||||
padding2: f32,
|
//padding2: f32,
|
||||||
time: f32
|
time: f32
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ impl Camera {
|
|||||||
|
|
||||||
pub fn new(aspect: f32, fov: f32) -> Self {
|
pub fn new(aspect: f32, fov: f32) -> Self {
|
||||||
Self {
|
Self {
|
||||||
position: (2.0, 1.0, -1.0).into(),
|
position: (1.0, 2.0, 1.0).into(),
|
||||||
forward: (-0.66, -0.33, 0.66).into(),
|
forward: (0.66, -0.25, 0.66).into(),
|
||||||
up: Vector3::unit_y(),
|
up: Vector3::unit_y(),
|
||||||
aspect,
|
aspect,
|
||||||
fovy: fov,
|
fovy: fov,
|
||||||
@@ -33,6 +33,6 @@ impl Camera {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn view_matrix(&self) -> Matrix4<f32> {
|
pub fn view_matrix(&self) -> Matrix4<f32> {
|
||||||
return Matrix4::look_to_rh((0.0,0.0,0.0).into(), self.forward, self.up);
|
return Matrix4::look_to_rh((0.0,0.0,0.0).into(), self.forward.normalize(), self.up);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+1
-1
@@ -33,7 +33,7 @@ impl Renderer {
|
|||||||
format: adapter.get_swap_chain_preferred_format(&surface),
|
format: adapter.get_swap_chain_preferred_format(&surface),
|
||||||
width: size.width,
|
width: size.width,
|
||||||
height: size.height,
|
height: size.height,
|
||||||
present_mode: wgpu::PresentMode::Mailbox, // Fifo = Vsync
|
present_mode: wgpu::PresentMode::Fifo, // Fifo = Vsync
|
||||||
};
|
};
|
||||||
let swapchain = device.create_swap_chain(&surface, &swapchain_desc);
|
let swapchain = device.create_swap_chain(&surface, &swapchain_desc);
|
||||||
(swapchain_desc, swapchain)
|
(swapchain_desc, swapchain)
|
||||||
|
|||||||
Binary file not shown.
@@ -55,7 +55,7 @@ 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(Ray ray)
|
HitResult castNodes(Ray ray, uint maxSteps)
|
||||||
{
|
{
|
||||||
// Round input pos to nearest voxel
|
// Round input pos to nearest voxel
|
||||||
vec3 pos = floor(ray.origin);
|
vec3 pos = floor(ray.origin);
|
||||||
@@ -70,7 +70,7 @@ HitResult castNodes(Ray ray)
|
|||||||
vec3 incAxis = vec3(0,0,0);
|
vec3 incAxis = vec3(0,0,0);
|
||||||
|
|
||||||
// Iterate
|
// Iterate
|
||||||
for(int i=0;i<110;i++)
|
for(int i=0;i<maxSteps;i++)
|
||||||
{
|
{
|
||||||
// Get node
|
// Get node
|
||||||
uint node = texelFetch(_NodesTexture, ivec3(pos), 0).r;
|
uint node = texelFetch(_NodesTexture, ivec3(pos), 0).r;
|
||||||
|
|||||||
+37
-12
@@ -37,30 +37,55 @@ float random(float s) {
|
|||||||
return fract(sin(seed++ + s)*43758.5453123);
|
return fract(sin(seed++ + s)*43758.5453123);
|
||||||
}
|
}
|
||||||
|
|
||||||
void main()
|
vec3 randomHemisphere(vec3 dir)
|
||||||
{
|
{
|
||||||
// Black by default
|
vec3 uu = normalize(cross(dir, vec3(0.0,1.0,1.0)));
|
||||||
out_color = vec4(0,0,0,1);
|
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
|
// Calculate ray direction
|
||||||
vec3 view_dir = (_ProjMatrixInv * vec4(vert_uv, 0, 1)).xyz;
|
vec3 view_dir = (_ProjMatrixInv * vec4(vert_uv, 0, 1)).xyz;
|
||||||
vec3 rayDir = normalize(_ViewMatrix * vec4(view_dir,0)).xyz;
|
vec3 rayDir = normalize(_ViewMatrix * vec4(view_dir,0)).xyz;
|
||||||
Ray primaryRay = Ray(_CamPos, rayDir);
|
Ray primaryRay = Ray(_CamPos, rayDir);
|
||||||
|
|
||||||
// Raycast
|
// Raycast
|
||||||
HitResult primary = castNodes(primaryRay);
|
HitResult primary = castNodes(primaryRay, 100);
|
||||||
if(primary.data > 0)
|
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);
|
// Shadow ray
|
||||||
HitResult shadow = castNodes(shadowRay);
|
Ray shadowRay = Ray(hitPos, _SunDir);
|
||||||
if(shadow.data > 0) out_color *= 0.1;
|
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;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
float up = clamp(dot(primaryRay.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;
|
return vec3(0.176, 0.592, 0.901) * up;
|
||||||
out_color = vec4(sky, 1.0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void main()
|
||||||
|
{
|
||||||
|
seed = 420 * vert_uv.x + 1337 * vert_uv.y + _Time * 1234;
|
||||||
|
out_color = vec4(solve(), 1);
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,25 @@
|
|||||||
|
/*
|
||||||
|
* Returns material albedo for given voxel
|
||||||
|
*/
|
||||||
|
vec3 getAlbedo(uint data)
|
||||||
|
{
|
||||||
|
return _Materials[data-1].albedo.rgb;
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
point: Hit point
|
point: Hit point
|
||||||
view: View vector (camera ray)
|
view: View vector (camera ray)
|
||||||
normal: Normal vector
|
normal: Normal vector
|
||||||
*/
|
*/
|
||||||
vec3 applyLighting(vec3 hitPoint, vec3 view, vec3 normal, uint brick)
|
vec3 applyLighting(vec3 hitPoint, vec3 view, vec3 normal, uint data)
|
||||||
{
|
{
|
||||||
// Lighting
|
// Lighting
|
||||||
vec3 light_color = vec3(1.0, 1.0, 1.0);
|
vec3 light_color = vec3(1);
|
||||||
|
|
||||||
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);
|
return (ambient + diffuse + specular) * getAlbedo(data);
|
||||||
return (ambient + diffuse + specular) * object_color;
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user