diff --git a/src/renderer/buffers/uniform_buffer.rs b/src/renderer/buffers/uniform_buffer.rs index f115ee0..ef12dc7 100644 --- a/src/renderer/buffers/uniform_buffer.rs +++ b/src/renderer/buffers/uniform_buffer.rs @@ -10,7 +10,7 @@ pub struct UniformValues { cam_pos: [f32; 3], padding: f32, sun_dir: [f32; 3], - padding2: f32, + //padding2: f32, time: f32 } diff --git a/src/renderer/camera.rs b/src/renderer/camera.rs index 5650760..8bbb243 100644 --- a/src/renderer/camera.rs +++ b/src/renderer/camera.rs @@ -14,8 +14,8 @@ impl Camera { pub fn new(aspect: f32, fov: f32) -> Self { Self { - position: (2.0, 1.0, -1.0).into(), - forward: (-0.66, -0.33, 0.66).into(), + position: (1.0, 2.0, 1.0).into(), + forward: (0.66, -0.25, 0.66).into(), up: Vector3::unit_y(), aspect, fovy: fov, @@ -33,6 +33,6 @@ impl Camera { } pub fn view_matrix(&self) -> Matrix4 { - 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); } } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index c82f271..fb1619d 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -33,7 +33,7 @@ impl Renderer { format: adapter.get_swap_chain_preferred_format(&surface), width: size.width, 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); (swapchain_desc, swapchain) diff --git a/src/shaders/bin/shader.frag.spv b/src/shaders/bin/shader.frag.spv index d625b25..541d11c 100644 Binary files a/src/shaders/bin/shader.frag.spv and b/src/shaders/bin/shader.frag.spv differ diff --git a/src/shaders/raycasting.cginc b/src/shaders/raycasting.cginc index 1c271ae..a420473 100644 --- a/src/shaders/raycasting.cginc +++ b/src/shaders/raycasting.cginc @@ -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 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); } \ No newline at end of file diff --git a/src/shaders/shading.cginc b/src/shaders/shading.cginc index 2d8c021..6208bba 100644 --- a/src/shaders/shading.cginc +++ b/src/shaders/shading.cginc @@ -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); } \ No newline at end of file