An unofficial collection of APIs used in FreeJam games and mods
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.4KB

  1. use genmesh::{generators::Cube, Quad, MapToVertices, Vertices};
  2. use obj;
  3. use crate::robocraft;
  4. /// Convert a Robocraft robot to a 3D model in Wavefront OBJ format.
  5. pub fn cubes_to_model(robot: robocraft::Cubes) -> obj::Obj {
  6. let mut positions = Vec::<[f32; 3]>::new();
  7. let mut normals = Vec::<[f32; 3]>::new();
  8. let mut objects = Vec::<obj::Object>::new();
  9. let mut last = 0;
  10. for cube in robot.into_iter() {
  11. positions.extend::<Vec::<[f32; 3]>>(
  12. Cube::new().vertex(|v|
  13. [(v.pos.x * 0.5) + (cube.x as f32), (v.pos.y * 0.5) + (cube.y as f32), (v.pos.z * 0.5) + (cube.z as f32)])
  14. .vertices()
  15. .collect()
  16. );
  17. normals.extend::<Vec::<[f32; 3]>>(
  18. Cube::new().vertex(|v|
  19. [(v.normal.x * 0.5) + (cube.x as f32), (v.normal.y * 0.5) + (cube.y as f32), (v.normal.z * 0.5) + (cube.z as f32)])
  20. .vertices()
  21. .collect()
  22. );
  23. let polys = Cube::new().vertex(|_| {last+=1; return last-1;})
  24. .map(|Quad{x: v0, y: v1, z: v2, w: v3}|
  25. obj::SimplePolygon(vec![
  26. obj::IndexTuple(v0, Some(0), Some(v0)),
  27. obj::IndexTuple(v1, Some(0), Some(v1)),
  28. obj::IndexTuple(v2, Some(0), Some(v2)),
  29. obj::IndexTuple(v3, Some(0), Some(v3))
  30. ])
  31. /*obj::SimplePolygon(vec![
  32. obj::IndexTuple(v0, None, None),
  33. obj::IndexTuple(v1, None, None),
  34. obj::IndexTuple(v2, None, None),
  35. obj::IndexTuple(v3, None, None)
  36. ])*/
  37. ).collect();
  38. objects.push(
  39. obj::Object{
  40. name: format!("Cube-ID{}-NUM{}", cube.id, objects.len()),
  41. groups: vec![
  42. obj::Group {
  43. name: format!("Cube-ID{}-NUM{}-0", cube.id, objects.len()),
  44. index: 0,
  45. material: None,
  46. polys: polys
  47. },
  48. ]
  49. }
  50. );
  51. }
  52. println!("Last (index): {}, Vertices (len): {}", last, positions.len());
  53. obj::Obj{
  54. data: obj::ObjData {
  55. position: positions,
  56. texture: vec![[0.0, 0.0]],
  57. normal: normals,
  58. objects: objects,
  59. material_libs: Vec::new(),
  60. },
  61. path: std::path::PathBuf::new(),
  62. }
  63. }