Add trilinear smoothing

This commit is contained in:
max.nuding 2022-07-06 13:28:06 +02:00
parent de64b56b38
commit 9564ee92b6
Failed to extract signature

View File

@ -26,11 +26,32 @@ impl Perlin {
} }
pub fn noise(&self, point: &Point3) -> f64 { pub fn noise(&self, point: &Point3) -> f64 {
/*
let i = ((4.0 * point.x()) as i32) & 255; let i = ((4.0 * point.x()) as i32) & 255;
let j = ((4.0 * point.y()) as i32) & 255; let j = ((4.0 * point.y()) as i32) & 255;
let k= ((4.0 * point.z()) as i32) & 255; let k= ((4.0 * point.z()) as i32) & 255;
let idx = self.perm_x[i as usize] ^ self.perm_y[j as usize] ^ self.perm_z[k as usize]; let idx = self.perm_x[i as usize] ^ self.perm_y[j as usize] ^ self.perm_z[k as usize];
self.ranfloat[idx] self.ranfloat[idx]
*/
let u = point.x() - point.x().floor();
let v = point.y() - point.y().floor();
let w = point.z() - point.z().floor();
let i = point.x().floor() as i32;
let j = point.y().floor() as i32;
let k = point.z().floor() as i32;
let mut c: [[[f64; 2]; 2]; 2] = [[[0.0; 2]; 2]; 2];
for di in 0..2 {
for dj in 0..2 {
for dk in 0..2 {
let idx = self.perm_x[((i+di) & 255) as usize] ^
self.perm_y[((j+dj) & 255) as usize] ^
self.perm_z[((k+dk) & 255) as usize];
c[di as usize][dj as usize][dk as usize] = self.ranfloat[idx];
}
}
}
Perlin::trilinerar_interpolation(c, u, v, w)
} }
fn perlin_generate_perm() -> Vec<usize> { fn perlin_generate_perm() -> Vec<usize> {
@ -43,4 +64,22 @@ impl Perlin {
}); });
p p
} }
fn trilinerar_interpolation(c: [[[f64; 2]; 2]; 2], u: f64, v: f64, w: f64) -> f64 {
let mut accum = 0.0;
for i in 0..2 {
for j in 0..2 {
for k in 0..2 {
let ifl = i as f64;
let jfl = j as f64;
let kfl = k as f64;
accum += (ifl*u + (1.0-ifl)*(1.0-u)) *
(jfl*v + (1.0-jfl)*(1.0-v)) *
(kfl*w + (1.0-kfl)*(1.0-w)) *
c[i][j][k];
}
}
}
accum
}
} }