aboutsummaryrefslogtreecommitdiff
path: root/src/material/isotropic.rs
diff options
context:
space:
mode:
authorlamp2023-03-05 21:45:56 +0000
committerlamp2023-03-05 21:45:56 +0000
commit78ddaff5855bf8446adef9e18eb0d7b7ddcee52a (patch)
tree0d0e93cfa28751a2f96518eeb231cf715958e1fa /src/material/isotropic.rs
init
Diffstat (limited to 'src/material/isotropic.rs')
-rw-r--r--src/material/isotropic.rs33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/material/isotropic.rs b/src/material/isotropic.rs
new file mode 100644
index 0000000..f59fa7d
--- /dev/null
+++ b/src/material/isotropic.rs
@@ -0,0 +1,33 @@
+use std::sync::Arc;
+
+use super::Material;
+use crate::{hittable::HitRecord, texture::Texture, vec3::Vec3};
+use crate::vec3::Color;
+use crate::texture::SolidColor;
+use crate::ray::Ray;
+
+pub struct Isotropic {
+ albedo: Arc<dyn Texture>,
+}
+
+impl Isotropic {
+ pub fn from_color(color: Color) -> Self {
+ Self {
+ albedo: Arc::new(SolidColor::from_color(color)),
+ }
+ }
+
+ pub fn from_texture(texture: Arc<dyn Texture>) -> Self {
+ Self {
+ albedo: texture,
+ }
+ }
+}
+
+impl Material for Isotropic {
+ fn scatter(&self, ray_in: &Ray, hit_record: &HitRecord, attenuation: &mut Color, scattered: &mut Ray) -> bool {
+ *scattered = Ray { origin: hit_record.p.clone(), direction: Vec3::random_in_unit_sphere(), time: ray_in.time };
+ *attenuation = self.albedo.value(hit_record.u, hit_record.v, &hit_record.p);
+ true
+ }
+}