Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.83 KB | None | 0 0
  1. //! Generate a flat scene, nothing more.
  2.  
  3. use terr::{heightmap::Heightmap, unbounded::Perlin};
  4. use nalgebra::{Point3, Vector3};
  5. use kiss3d::{window::Window, light::Light};
  6. use rand::thread_rng;
  7. use rand_distr::{Distribution, UnitCircle, Exp1};
  8. use ncollide3d::transformation::ToTriMesh;
  9.  
  10. fn main() {
  11. let mut window = Window::new("Terr: perlin octaves");
  12. window.set_light(Light::StickToCamera);
  13.  
  14. let mut rng = thread_rng();
  15.  
  16. let cells = 256;
  17. let mut heightmap = Heightmap::new_flat((cells, cells), (100.0, 100.0));
  18. let mut ampl = 20.0;
  19. let mut larc = 1.0 / (cells as f32);
  20. for _ in 0..7 {
  21. let sampler = || {
  22. let g: [f32; 2] = UnitCircle.sample(&mut rng);
  23. let s: f32 = Exp1.sample(&mut rng);
  24. [g[0] * s, g[1] * s]
  25. };
  26. let surface = Perlin::new(larc, 1024, sampler).unwrap();
  27. heightmap.add_surface(&surface, ampl);
  28. ampl *= 0.5;
  29. larc *= 2.0;
  30. }
  31.  
  32. let mut quad = heightmap.to_trimesh();
  33. for p in &mut quad.coords {
  34. // Quad is created with z=height, but y is up in kiss3d's camera.
  35. // We must rotate all three coords to keep the right side up.
  36. let temp = p.z;
  37. p.z = p.x - 50.0;
  38. p.x = p.y - 50.0;
  39. p.y = temp;
  40. }
  41. quad.recompute_normals();
  42.  
  43. let mut quad = window.add_trimesh(quad, Vector3::from_element(1.0));
  44. quad.enable_backface_culling(false);
  45. quad.set_color(0.75, 0.65, 0.4);
  46.  
  47. let heightfield = heightmap.to_heightfield();
  48. let mut quad = window.add_trimesh(heightfield.to_trimesh(()), Vector3::from_element(1.0));
  49. quad.enable_backface_culling(false);
  50. quad.set_color(0.6, 0.2, 0.7);
  51.  
  52. let mut camera = kiss3d::camera::ArcBall::new(Point3::new(0., 50., 50.), Point3::new(0., 0., 0.));
  53.  
  54. while window.render_with_camera(&mut camera) {
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement