mesh_generation/mesh/
mod.rs

1pub mod polymesh;
2pub use self::polymesh::PolyMesh;
3
4use noise::{NoiseFn, Perlin};
5
6/// Generates noise
7///
8///
9pub fn generate_noise_map(noise_generator: Perlin, image_width: u32, image_height: u32, divider: f64, num_layers: u32) -> Vec<f64> {
10    let mut chunk = vec![0.0; (image_height * image_width) as usize];
11    let mut max_val = 0.0;
12    for x in 0..image_width {
13        for y in 0..image_height {
14            let mut fractal = 0.0;
15            let mut amplitude = 1.0;
16            let mut xy = [(x as f64+0.5)/divider, (y as f64 + 0.5)/divider, 0.0];
17            for _ in 0..num_layers {
18                fractal+= 0.5 * amplitude as f64 *  (1.0+noise_generator.get(xy));
19                xy[0]*=2.0;
20                xy[1]*=2.0;
21                amplitude *= 0.5; 
22            }
23            if fractal > max_val {
24                max_val = fractal
25            };
26            chunk[(y * image_width + x) as usize] = fractal; 
27        }
28    }
29    for i in 0..(image_height*image_width) {
30        chunk[i as usize] /= max_val; 
31    }
32    chunk
33
34} 
35
36fn _displace_polymesh_with_generator(noise_generator: Perlin, mesh: &mut PolyMesh) {
37    for i in  0..mesh.num_vertices {
38        let x = mesh.vertices[i as usize].x; 
39        let y = mesh.vertices[i as usize].z; 
40        let xy = [x as f64 + 0.5, 0.0, y as f64 + 0.5];
41        let noise = noise_generator.get(xy);
42        mesh.vertices[i as usize].y = noise; 
43    }
44}