Added checkered texture
This commit is contained in:
parent
a278969bbf
commit
6037fe2802
|
@ -1,7 +1,7 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
materials::{Dielectric, Lambertian, Metal},
|
materials::{Dielectric, Lambertian, Metal},
|
||||||
shapes::{MovingSphere, Sphere},
|
shapes::{MovingSphere, Sphere},
|
||||||
texture::Solid,
|
texture::{Checker, Solid},
|
||||||
types::{Ray, Vec3},
|
types::{Ray, Vec3},
|
||||||
BvhNode, Camera, Hitable, HORIZONTAL_PARTITION, VERTICAL_PARTITION,
|
BvhNode, Camera, Hitable, HORIZONTAL_PARTITION, VERTICAL_PARTITION,
|
||||||
};
|
};
|
||||||
|
@ -55,7 +55,10 @@ impl Demo {
|
||||||
world.push(Arc::new(Sphere::new(
|
world.push(Arc::new(Sphere::new(
|
||||||
Vec3::new(0.0, -1000.0, 0.0),
|
Vec3::new(0.0, -1000.0, 0.0),
|
||||||
1000.0,
|
1000.0,
|
||||||
Lambertian::new(Solid::new(Vec3::new(0.5, 0.5, 0.5))),
|
Lambertian::new(Checker::new(
|
||||||
|
Solid::new(Vec3::new(0.2, 0.3, 0.1)),
|
||||||
|
Solid::new(Vec3::new(0.9, 0.9, 0.9)),
|
||||||
|
)),
|
||||||
)));
|
)));
|
||||||
|
|
||||||
let radius = 0.2;
|
let radius = 0.2;
|
||||||
|
|
|
@ -21,6 +21,9 @@ impl<T: Texture + Send + Sync> Material for Lambertian<T> {
|
||||||
let scatter_direction = hit_rec.normal + random_point_in_unit_sphere(rng);
|
let scatter_direction = hit_rec.normal + random_point_in_unit_sphere(rng);
|
||||||
let scattered_ray = Ray::new(hit_rec.p, scatter_direction, ray.time());
|
let scattered_ray = Ray::new(hit_rec.p, scatter_direction, ray.time());
|
||||||
|
|
||||||
(self.albedo.value(hit_rec.u, hit_rec.v), Some(scattered_ray))
|
let mut p = hit_rec.p;
|
||||||
|
self.albedo.value(hit_rec.u, hit_rec.v, &mut p);
|
||||||
|
|
||||||
|
(p, Some(scattered_ray))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
24
src/texture/checker.rs
Normal file
24
src/texture/checker.rs
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
use crate::{types::Vec3, Texture};
|
||||||
|
|
||||||
|
pub struct Checker<T: Texture> {
|
||||||
|
odd: T,
|
||||||
|
even: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Texture> Checker<T> {
|
||||||
|
pub fn new(even: T, odd: T) -> Self {
|
||||||
|
Self { odd, even }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: Texture> Texture for Checker<T> {
|
||||||
|
fn value(&self, u: f64, v: f64, p: &mut Vec3) {
|
||||||
|
let sine_wave = f64::sin(10.0 * p.x()) * f64::sin(10.0 * p.y()) * f64::sin(10.0 * p.z());
|
||||||
|
|
||||||
|
if sine_wave < 0.0 {
|
||||||
|
self.odd.value(u, v, p)
|
||||||
|
} else {
|
||||||
|
self.even.value(u, v, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
|
mod checker;
|
||||||
mod solid;
|
mod solid;
|
||||||
|
|
||||||
|
pub use checker::Checker;
|
||||||
pub use solid::Solid;
|
pub use solid::Solid;
|
||||||
|
|
||||||
use crate::types::Vec3;
|
use crate::types::Vec3;
|
||||||
|
|
||||||
pub trait Texture {
|
pub trait Texture {
|
||||||
fn value(&self, u: f64, v: f64) -> Vec3;
|
fn value(&self, u: f64, v: f64, p: &mut Vec3);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ impl Solid {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Texture for Solid {
|
impl Texture for Solid {
|
||||||
fn value(&self, _u: f64, _v: f64) -> Vec3 {
|
fn value(&self, _u: f64, _v: f64, p: &mut Vec3) {
|
||||||
self.color
|
*p = self.color
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user