diff options
author | eug-vs <eugene@eug-vs.xyz> | 2021-10-27 13:58:46 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2021-10-27 13:58:46 +0300 |
commit | e444b6784e9e6867df3622ee986b4adbb7bcc685 (patch) | |
tree | 8033690780a552fcaf8be57f801727fb5ce3de38 | |
parent | f704384b9938215f818c1c2006ad9b233c0d7c00 (diff) | |
download | pistol-e444b6784e9e6867df3622ee986b4adbb7bcc685.tar.gz |
feat: add shadows
-rw-r--r-- | src/camera.rs | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/src/camera.rs b/src/camera.rs index 30d338e..61afe97 100644 --- a/src/camera.rs +++ b/src/camera.rs @@ -94,7 +94,7 @@ impl Camera { pub fn render(& mut self) { self.rorate_around_point(Vector { x: 4.0, y: 0.0, z: 0.0 }); - let palette = "$@B%8&WM#oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'."; + let palette = "$@B%8&WM#oahkbdpqwmZO0QLCJUYXzcvunxrjft/\\|()1{}[]?-_+~<>i!lI;:,\"^`'. "; let (screen_width, screen_height) = self.screen(); let h_step = rotate_z(self.direction, PI / 2.0).normalize() * screen_width / WIDTH as f32; @@ -145,14 +145,29 @@ impl Camera { let mut dist = 0.0; let mut count = 0; - while dist < self.brightness && count < 30 { + while dist < self.brightness && count < 50 { count += 1; dist = self.sdf(point); if dist < threshold { // Collision in point! Let's calculate lights now: + let mut res: f32 = 1.0; + let mut t = 0.001; + let k = 2.0; + + while t < 7.0 { + let h = self.sdf(point - light * t); + if h < 0.001 { + return 0.97 + } + res = res.min(k * h / t); + t += h; + + } + let normal = self.normal(point); let dot = -(normal.dot(light)); - return 1.0 - dot.max(0.01).min(0.98); + + return 1.0 - 0.5 * dot.max(0.01).min(0.98) - 0.5 * res.min(0.98) } point = point + ray * dist; } |