BabylonExporter.GLTFExporter.AbstractMesh.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. // TODO - Choose between this method and the extra root node
  31. // Switch from left to right handed coordinate system
  32. //gltfNode.translation[0] *= -1;
  33. if (babylonAbstractMesh.rotationQuaternion != null)
  34. {
  35. gltfNode.rotation = babylonAbstractMesh.rotationQuaternion;
  36. }
  37. else
  38. {
  39. // Convert rotation vector to quaternion
  40. BabylonVector3 rotationVector3 = new BabylonVector3
  41. {
  42. X = babylonAbstractMesh.rotation[0],
  43. Y = babylonAbstractMesh.rotation[1],
  44. Z = babylonAbstractMesh.rotation[2]
  45. };
  46. gltfNode.rotation = rotationVector3.toQuaternion().ToArray();
  47. }
  48. gltfNode.scale = babylonAbstractMesh.scaling;
  49. // Mesh
  50. var gltfMesh = gltf.MeshesList.Find(_gltfMesh => _gltfMesh.idGroupInstance == babylonAbstractMesh.idGroupInstance);
  51. if (gltfMesh != null)
  52. {
  53. gltfNode.mesh = gltfMesh.index;
  54. }
  55. return gltfNode;
  56. }
  57. }
  58. }