Add turbulence

master
max.nuding 2022-07-07 08:57:53 +02:00
parent d5d24cee03
commit 0e72e21f92
Failed to extract signature
3 changed files with 44 additions and 4 deletions

View File

@ -9,8 +9,6 @@ pub struct NoiseTexture {
impl Texture for NoiseTexture {
fn value(&self, _u: f64, _v: f64, point: &Point3) -> Color {
Color::new(1.0, 1.0, 1.0)
* 0.5
* (1.0 + self.noise.noise(&(*point * self.scale)))
Color::new(1.0, 1.0, 1.0) * self.noise.default_turbulence(&(*point * self.scale))
}
}

View File

@ -22,6 +22,22 @@ impl Perlin {
}
}
pub fn default_turbulence(&self, point: &Point3) -> f64 {
self.turbulence(point, 7)
}
pub fn turbulence(&self, point: &Point3, depth: i32) -> f64 {
let mut accum = 0.0;
let mut temp_p = point.clone();
let mut weight = 1.0;
for _i in 0..depth {
accum += weight * self.noise(&temp_p);
weight *= 0.5;
temp_p *= 2.0;
}
accum.abs()
}
pub fn noise(&self, point: &Point3) -> f64 {
let u = point.x() - point.x().floor();
let v = point.y() - point.y().floor();

View File

@ -1,5 +1,5 @@
use std::fmt::{Display, Formatter};
use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, Index};
use std::ops::{Add, AddAssign, Div, Mul, Neg, Sub, Index, MulAssign};
use rand::distributions::{Distribution, Uniform};
#[derive(Debug, Clone, Copy)]
@ -228,6 +228,32 @@ impl Mul<Vec3> for f64 {
}
}
impl Mul<f64> for &Vec3 {
type Output = Vec3;
fn mul(self, other: f64) -> Vec3 {
Vec3 {
x: self.x * other,
y: self.y * other,
z: self.z * other,
}
}
}
impl Mul<&Vec3> for f64 {
type Output = Vec3;
fn mul(self, other: &Vec3) -> Vec3 {
other * self
}
}
impl MulAssign<f64> for Vec3 {
fn mul_assign(&mut self, rhs: f64) {
*self = *self * rhs;
}
}
impl Div<Vec3> for Vec3 {
type Output = Vec3;