From 93b24b9d01f806cf69ecee86fada9e5b9bf06182 Mon Sep 17 00:00:00 2001 From: Rosyid Haryadi Date: Sun, 2 Mar 2025 16:56:59 +0700 Subject: material --- src/material.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/material.rs (limited to 'src/material.rs') diff --git a/src/material.rs b/src/material.rs new file mode 100644 index 0000000..2b3deae --- /dev/null +++ b/src/material.rs @@ -0,0 +1,41 @@ +use crate::calculus::calculus::{Ray, Vec3}; +use crate::common::Color; +use crate::object::HitRecord; + +#[derive(Clone)] +pub enum MaterialType { + Diffuse, + Metal, +} + +pub struct Material { + pub material_type: MaterialType, + pub albedo: Color +} + +impl Material { + pub fn new(albedo: Color, material_type: MaterialType) -> Self { + Self { albedo, material_type } + } + + pub fn scatter(&self, r_in: &Ray, rec: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool { + scattered.direction = match self.material_type { + MaterialType::Diffuse => { + let scatter_direction = rec.normal.add(&Vec3::random_unit()); + if scatter_direction.is_near_zero() { + rec.normal.clone() + } else { + scatter_direction + } + } + MaterialType::Metal => { + r_in.reflect(&rec.normal) + } + }; + scattered.origin = rec.position; + attenuation.r = self.albedo.r; + attenuation.g = self.albedo.g; + attenuation.b = self.albedo.b; + true + } +} \ No newline at end of file -- cgit v1.2.3-70-g09d2