diff --git a/src/bin/minitracer/anim.rs b/src/bin/minitracer/anim.rs index 56dbd58..33e7205 100644 --- a/src/bin/minitracer/anim.rs +++ b/src/bin/minitracer/anim.rs @@ -5,6 +5,7 @@ pub struct SphereParams { pub radius: f32, pub color: Vec3, pub alpha: f32, + pub glossiness: f32, pub origin: Vec3, pub amplitudes: Vec3, pub frequencies: Vec3, @@ -19,6 +20,7 @@ pub struct SphereParamsDistribution { pub dampl: Uniform, pub dfreq: Uniform, pub dphase: Uniform, + pub dgloss: Uniform, } impl Default for SphereParamsDistribution { @@ -31,6 +33,7 @@ impl Default for SphereParamsDistribution { dampl: Uniform::new(0.3, 0.8), dfreq: Uniform::new(0.2, 1.5), dphase: Uniform::new(0., 2. * std::f32::consts::PI), + dgloss: Uniform::new(0., 1.), } } } @@ -42,6 +45,7 @@ impl SphereParamsDistribution { radius: self.drad.sample(rgen), color: self.dcol.sample3(rgen).normalize(), alpha: self.demit.sample(rgen), + glossiness: self.dgloss.sample(rgen), amplitudes: self.dampl.sample3(rgen), frequencies: self.dfreq.sample3(rgen), phases: self.dphase.sample3(rgen), @@ -54,12 +58,14 @@ impl SphereParams { let center = self.origin + self.amplitudes * (self.frequencies * time + self.phases).map(|x| x.sin()); let radius = self.radius; let emit_color = self.alpha * self.color; + let glossiness = self.glossiness; let reflect_color = 0.6 * self.color + Vec3::splat(0.2); crate::Sphere { center, radius, emit_color, reflect_color, + glossiness, } } } diff --git a/src/bin/minitracer/trace.rs b/src/bin/minitracer/trace.rs index be57f88..177436a 100644 --- a/src/bin/minitracer/trace.rs +++ b/src/bin/minitracer/trace.rs @@ -13,12 +13,12 @@ pub struct Params { } #[derive(Debug, Clone, Copy)] -#[repr(C)] pub struct Sphere { pub center: Vec3, pub radius: f32, pub emit_color: Vec3, pub reflect_color: Vec3, + pub glossiness: f32, } #[derive(Debug, Clone, Copy, Pod, Zeroable)] @@ -37,7 +37,7 @@ struct SphereData { emit_color: Vec3, pad1: f32, reflect_color: Vec3, - pad2: f32, + glossiness: f32, } pub struct Tracer { @@ -155,7 +155,7 @@ impl TracerData { emit_color: s.emit_color, pad1: 0.0, reflect_color: s.reflect_color, - pad2: 0.0, + glossiness: s.glossiness, }) .collect(); let spheres_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { diff --git a/src/bin/minitracer/trace.wgsl b/src/bin/minitracer/trace.wgsl index 1e4d264..1275f3e 100644 --- a/src/bin/minitracer/trace.wgsl +++ b/src/bin/minitracer/trace.wgsl @@ -9,6 +9,7 @@ struct Sphere { radius: f32, emit_color: vec3f, reflect_color: vec3f, + glossiness: f32, } struct Vertex { @@ -87,7 +88,7 @@ fn trace_fragment(in: Varying) -> vec4f { color *= s.reflect_color; let diffuse = normal + rand_sphere(); let specular = reflect(ray, normal); - ray = normalize(mix(diffuse, specular, 0.8)); + ray = normalize(mix(diffuse, specular, s.glossiness)); if (length(color) < params.min_strength) { break; }