BabylonExporter.GLTFExporter.AbstractMesh.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using BabylonExport.Entities;
  2. using GLTFExport.Entities;
  3. namespace Max2Babylon
  4. {
  5. partial class BabylonExporter
  6. {
  7. private GLTFNode ExportAbstractMesh(BabylonAbstractMesh babylonAbstractMesh, GLTF gltf, GLTFNode gltfParentNode, BabylonScene babylonScene)
  8. {
  9. RaiseMessage("GLTFExporter.AbstractMesh | Export abstract mesh named: " + babylonAbstractMesh.name, 1);
  10. // Node
  11. var gltfNode = new GLTFNode();
  12. gltfNode.name = babylonAbstractMesh.name;
  13. gltfNode.index = gltf.NodesList.Count;
  14. gltf.NodesList.Add(gltfNode);
  15. // Hierarchy
  16. if (gltfParentNode != null)
  17. {
  18. RaiseMessage("GLTFExporter.AbstractMesh | Add " + babylonAbstractMesh.name + " as child to " + gltfParentNode.name, 2);
  19. gltfParentNode.ChildrenList.Add(gltfNode.index);
  20. gltfNode.parent = gltfParentNode;
  21. }
  22. else
  23. {
  24. // It's a root node
  25. // Only root nodes are listed in a gltf scene
  26. RaiseMessage("GLTFExporter.AbstractMesh | Add " + babylonAbstractMesh.name + " as root node to scene", 2);
  27. gltf.scenes[0].NodesList.Add(gltfNode.index);
  28. }
  29. // Transform
  30. gltfNode.translation = babylonAbstractMesh.position;
  31. if (babylonAbstractMesh.rotationQuaternion != null)
  32. {
  33. gltfNode.rotation = babylonAbstractMesh.rotationQuaternion;
  34. }
  35. else
  36. {
  37. // Convert rotation vector to quaternion
  38. BabylonVector3 rotationVector3 = new BabylonVector3
  39. {
  40. X = babylonAbstractMesh.rotation[0],
  41. Y = babylonAbstractMesh.rotation[1],
  42. Z = babylonAbstractMesh.rotation[2]
  43. };
  44. gltfNode.rotation = rotationVector3.toQuaternion().ToArray();
  45. }
  46. gltfNode.scale = babylonAbstractMesh.scaling;
  47. // Mesh
  48. var gltfMesh = gltf.MeshesList.Find(_gltfMesh => _gltfMesh.idGroupInstance == babylonAbstractMesh.idGroupInstance);
  49. if (gltfMesh != null)
  50. {
  51. gltfNode.mesh = gltfMesh.index;
  52. // Skin
  53. if (gltfMesh.idBabylonSkeleton.HasValue)
  54. {
  55. var babylonSkeleton = babylonScene.skeletons[gltfMesh.idBabylonSkeleton.Value];
  56. // Export a new skin and a new skeleton
  57. // TODO - Use the skeleton if already exported and only create a new skin
  58. var gltfSkin = ExportSkin(babylonSkeleton, gltf, gltfNode);
  59. gltfNode.skin = gltfSkin.index;
  60. }
  61. }
  62. // Animations
  63. ExportNodeAnimation(babylonAbstractMesh, gltf, gltfNode, babylonScene);
  64. return gltfNode;
  65. }
  66. }
  67. }