Add variable glossiness

This commit is contained in:
numzero 2024-12-29 18:25:41 +03:00
parent abf5702fdd
commit 62fea44949
3 changed files with 11 additions and 4 deletions

View File

@ -5,6 +5,7 @@ pub struct SphereParams {
pub radius: f32, pub radius: f32,
pub color: Vec3, pub color: Vec3,
pub alpha: f32, pub alpha: f32,
pub glossiness: f32,
pub origin: Vec3, pub origin: Vec3,
pub amplitudes: Vec3, pub amplitudes: Vec3,
pub frequencies: Vec3, pub frequencies: Vec3,
@ -19,6 +20,7 @@ pub struct SphereParamsDistribution {
pub dampl: Uniform<f32>, pub dampl: Uniform<f32>,
pub dfreq: Uniform<f32>, pub dfreq: Uniform<f32>,
pub dphase: Uniform<f32>, pub dphase: Uniform<f32>,
pub dgloss: Uniform<f32>,
} }
impl Default for SphereParamsDistribution { impl Default for SphereParamsDistribution {
@ -31,6 +33,7 @@ impl Default for SphereParamsDistribution {
dampl: Uniform::new(0.3, 0.8), dampl: Uniform::new(0.3, 0.8),
dfreq: Uniform::new(0.2, 1.5), dfreq: Uniform::new(0.2, 1.5),
dphase: Uniform::new(0., 2. * std::f32::consts::PI), 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), radius: self.drad.sample(rgen),
color: self.dcol.sample3(rgen).normalize(), color: self.dcol.sample3(rgen).normalize(),
alpha: self.demit.sample(rgen), alpha: self.demit.sample(rgen),
glossiness: self.dgloss.sample(rgen),
amplitudes: self.dampl.sample3(rgen), amplitudes: self.dampl.sample3(rgen),
frequencies: self.dfreq.sample3(rgen), frequencies: self.dfreq.sample3(rgen),
phases: self.dphase.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 center = self.origin + self.amplitudes * (self.frequencies * time + self.phases).map(|x| x.sin());
let radius = self.radius; let radius = self.radius;
let emit_color = self.alpha * self.color; let emit_color = self.alpha * self.color;
let glossiness = self.glossiness;
let reflect_color = 0.6 * self.color + Vec3::splat(0.2); let reflect_color = 0.6 * self.color + Vec3::splat(0.2);
crate::Sphere { crate::Sphere {
center, center,
radius, radius,
emit_color, emit_color,
reflect_color, reflect_color,
glossiness,
} }
} }
} }

View File

@ -13,12 +13,12 @@ pub struct Params {
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
#[repr(C)]
pub struct Sphere { pub struct Sphere {
pub center: Vec3, pub center: Vec3,
pub radius: f32, pub radius: f32,
pub emit_color: Vec3, pub emit_color: Vec3,
pub reflect_color: Vec3, pub reflect_color: Vec3,
pub glossiness: f32,
} }
#[derive(Debug, Clone, Copy, Pod, Zeroable)] #[derive(Debug, Clone, Copy, Pod, Zeroable)]
@ -37,7 +37,7 @@ struct SphereData {
emit_color: Vec3, emit_color: Vec3,
pad1: f32, pad1: f32,
reflect_color: Vec3, reflect_color: Vec3,
pad2: f32, glossiness: f32,
} }
pub struct Tracer { pub struct Tracer {
@ -155,7 +155,7 @@ impl TracerData {
emit_color: s.emit_color, emit_color: s.emit_color,
pad1: 0.0, pad1: 0.0,
reflect_color: s.reflect_color, reflect_color: s.reflect_color,
pad2: 0.0, glossiness: s.glossiness,
}) })
.collect(); .collect();
let spheres_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor { let spheres_buf = device.create_buffer_init(&wgpu::util::BufferInitDescriptor {

View File

@ -9,6 +9,7 @@ struct Sphere {
radius: f32, radius: f32,
emit_color: vec3f, emit_color: vec3f,
reflect_color: vec3f, reflect_color: vec3f,
glossiness: f32,
} }
struct Vertex { struct Vertex {
@ -87,7 +88,7 @@ fn trace_fragment(in: Varying) -> vec4f {
color *= s.reflect_color; color *= s.reflect_color;
let diffuse = normal + rand_sphere(); let diffuse = normal + rand_sphere();
let specular = reflect(ray, normal); 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) { if (length(color) < params.min_strength) {
break; break;
} }