Skip to content

Commit 3236e96

Browse files
committed
Add reflection
1 parent d4003ac commit 3236e96

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/main.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ const N: u32 = 64;
1212
const MAX_STEP: u32 = 64;
1313
const MAX_DISTANCE: f64 = 2.0;
1414
const EPSILON: f64 = 1e-6;
15+
const BIAS: f64 = 1e-4;
16+
const MAX_DEPTH: u32 = 3;
1517

1618
struct Res {
1719
sd: f64,
1820
emissive: f64,
21+
reflectivity: f64,
1922
}
2023

2124
impl std::ops::Add<Res> for Res {
@@ -62,14 +65,17 @@ fn scene(x: f64, y: f64) -> Res {
6265
let a = Res {
6366
sd: circle_sdf(x, y, 0.4, 0.2, 0.1),
6467
emissive: 2.0,
68+
reflectivity: 0.0,
6569
};
6670
let b = Res {
6771
sd: box_sdf(x, y, 0.5, 0.8, 2.0 * PI / 16.0, 0.1, 0.1),
6872
emissive: 0.0,
73+
reflectivity: 0.9,
6974
};
7075
let c = Res {
7176
sd: box_sdf(x, y, 0.8, 0.5, 2.0 * PI / 16.0, 0.1, 0.1),
7277
emissive: 0.0,
78+
reflectivity: 0.9,
7379
};
7480
a + b + c
7581
}
@@ -133,13 +139,21 @@ fn gradient(x: f64, y: f64) -> (f64, f64) {
133139
(nx, ny)
134140
}
135141

136-
fn trace(ox: f64, oy: f64, dx: f64, dy: f64) -> f64 {
142+
fn trace(ox: f64, oy: f64, dx: f64, dy: f64, depth: u32) -> f64 {
137143
let mut t = 0.0;
138144
let mut i = 0;
139145
while i < MAX_STEP && t < MAX_DISTANCE {
140-
let r = scene(ox + dx * t, oy + dy * t);
146+
let x = ox + dx * t;
147+
let y = oy + dy * t;
148+
let r = scene(x, y);
141149
if r.sd < EPSILON {
142-
return r.emissive;
150+
let mut sum = r.emissive;
151+
if depth < MAX_DEPTH && r.reflectivity > 0.0 {
152+
let (nx, ny) = gradient(x, y);
153+
let (rx, ry) = reflect(dx, dy, nx, ny);
154+
sum += r.reflectivity * trace(x + nx * BIAS, y + ny * BIAS, rx, ry, depth + 1);
155+
}
156+
return sum;
143157
}
144158
i += 1;
145159
t += r.sd;
@@ -151,7 +165,7 @@ fn sample(rng: &mut ThreadRng, x: f64, y: f64) -> f64 {
151165
let mut sum = 0.0;
152166
for i in 0..N {
153167
let a = 2.0 * PI * (i as f64 + rng.gen_range(0.0, 1.0)) / N as f64;
154-
sum += trace(x, y, a.cos(), a.sin());
168+
sum += trace(x, y, a.cos(), a.sin(), 0);
155169
}
156170
sum / N as f64
157171
}
@@ -163,11 +177,8 @@ fn main() {
163177
for y in 0..H {
164178
let xx = x as f64 / W as f64;
165179
let yy = y as f64 / H as f64;
166-
let (nx, ny) = gradient(xx, yy);
167-
let r = ((nx.min(0.5).max(-0.5) + 0.5) * 255.0) as u8;
168-
let g = ((ny.min(0.5).max(-0.5) + 0.5) * 255.0) as u8;
169-
// let brightness = min((sample(&mut rng, xx, yy) * 255.0) as u32, 255) as u8;
170-
img.put_pixel(x, y, Rgb([r, g, 0]));
180+
let brightness = min((sample(&mut rng, xx, yy) * 255.0) as u32, 255) as u8;
181+
img.put_pixel(x, y, Rgb([brightness, brightness, brightness]));
171182
}
172183
}
173184
img.save("out.png").unwrap();

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy