raytracers/rtiow/src/texture.rs

64 lines
1.3 KiB
Rust

use perlin::GENERATOR;
use vec3::Vec3;
pub trait Texture: Send + Sync {
fn value(&self, u: f32, v: f32, p: Vec3) -> Vec3;
}
pub struct ConstantTexture {
color: Vec3,
}
impl ConstantTexture {
pub fn new(color: Vec3) -> ConstantTexture {
ConstantTexture { color }
}
}
impl Texture for ConstantTexture {
fn value(&self, _u: f32, _v: f32, _p: Vec3) -> Vec3 {
self.color
}
}
pub struct CheckerTexture {
odd: Box<Texture>,
even: Box<Texture>,
}
impl CheckerTexture {
pub fn new(odd: Box<Texture>, even: Box<Texture>) -> CheckerTexture {
CheckerTexture { odd, even }
}
}
impl Texture for CheckerTexture {
fn value(&self, u: f32, v: f32, p: Vec3) -> Vec3 {
let sines = (10. * p.x).sin() * (10. * p.y).sin() * (10. * p.z).sin();
if sines < 0. {
self.odd.value(u, v, p)
} else {
self.even.value(u, v, p)
}
}
}
pub struct NoiseTexture {
scale: f32,
}
impl NoiseTexture {
pub fn new() -> NoiseTexture {
NoiseTexture { scale: 1. }
}
pub fn with_scale(scale: f32) -> NoiseTexture {
NoiseTexture { scale }
}
}
impl Texture for NoiseTexture {
fn value(&self, _u: f32, _v: f32, p: Vec3) -> Vec3 {
Vec3::new(1., 1., 1.) * 0.5 * (1. + GENERATOR.noise(self.scale * p))
}
}