BabylonExporter.GLTFExporter.AbstractMesh.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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)
  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. }
  21. else
  22. {
  23. // It's a root node
  24. // Only root nodes are listed in a gltf scene
  25. RaiseMessage("GLTFExporter.AbstractMesh | Add " + babylonAbstractMesh.name + " as root node to scene", 2);
  26. gltf.scenes[0].NodesList.Add(gltfNode.index);
  27. }
  28. // Transform
  29. gltfNode.translation = babylonAbstractMesh.position;
  30. if (babylonAbstractMesh.rotationQuaternion != null)
  31. {
  32. gltfNode.rotation = babylonAbstractMesh.rotationQuaternion;
  33. }
  34. else
  35. {
  36. // Convert rotation vector to quaternion
  37. BabylonVector3 rotationVector3 = new BabylonVector3
  38. {
  39. X = babylonAbstractMesh.rotation[0],
  40. Y = babylonAbstractMesh.rotation[1],
  41. Z = babylonAbstractMesh.rotation[2]
  42. };
  43. gltfNode.rotation = rotationVector3.toQuaternion().ToArray();
  44. }
  45. gltfNode.scale = babylonAbstractMesh.scaling;
  46. // Mesh
  47. var gltfMesh = gltf.MeshesList.Find(_gltfMesh => _gltfMesh.idGroupInstance == babylonAbstractMesh.idGroupInstance);
  48. if (gltfMesh != null)
  49. {
  50. gltfNode.mesh = gltfMesh.index;
  51. }
  52. return gltfNode;
  53. }
  54. }
  55. }