BabylonExporter.GLTFExporter.Camera.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using BabylonExport.Entities;
  2. using GLTFExport.Entities;
  3. namespace Max2Babylon
  4. {
  5. partial class BabylonExporter
  6. {
  7. private GLTFCamera ExportCamera(BabylonCamera babylonCamera, GLTF gltf, GLTFNode gltfParentNode)
  8. {
  9. RaiseMessage("GLTFExporter.Camera | Export camera named: " + babylonCamera.name, 1);
  10. // --------------------------
  11. // ---------- Node ----------
  12. // --------------------------
  13. RaiseMessage("GLTFExporter.Camera | Node", 2);
  14. // Node
  15. var gltfNode = new GLTFNode();
  16. gltfNode.name = babylonCamera.name;
  17. gltfNode.index = gltf.NodesList.Count;
  18. gltf.NodesList.Add(gltfNode);
  19. // Hierarchy
  20. if (gltfParentNode != null)
  21. {
  22. RaiseMessage("GLTFExporter.Camera | Add " + babylonCamera.name + " as child to " + gltfParentNode.name, 3);
  23. gltfParentNode.ChildrenList.Add(gltfNode.index);
  24. }
  25. else
  26. {
  27. // It's a root node
  28. // Only root nodes are listed in a gltf scene
  29. RaiseMessage("GLTFExporter.Camera | Add " + babylonCamera.name + " as root node to scene", 3);
  30. gltf.scenes[0].NodesList.Add(gltfNode.index);
  31. }
  32. // Transform
  33. gltfNode.translation = babylonCamera.position;
  34. if (babylonCamera.rotationQuaternion != null)
  35. {
  36. gltfNode.rotation = babylonCamera.rotationQuaternion;
  37. }
  38. else
  39. {
  40. // Convert rotation vector to quaternion
  41. BabylonVector3 rotationVector3 = new BabylonVector3
  42. {
  43. X = babylonCamera.rotation[0],
  44. Y = babylonCamera.rotation[1],
  45. Z = babylonCamera.rotation[2]
  46. };
  47. gltfNode.rotation = rotationVector3.toQuaternion().ToArray();
  48. }
  49. // No scaling defined for babylon camera. Use identity instead.
  50. gltfNode.scale = new float[3] { 1, 1, 1 };
  51. // Animations
  52. ExportNodeAnimation(babylonCamera, gltf, gltfNode);
  53. // --- prints ---
  54. RaiseMessage("GLTFExporter.Camera | babylonCamera data", 2);
  55. RaiseMessage("GLTFExporter.Camera | babylonCamera.type=" + babylonCamera.type, 3);
  56. RaiseMessage("GLTFExporter.Camera | babylonCamera.fov=" + babylonCamera.fov, 3);
  57. RaiseMessage("GLTFExporter.Camera | babylonCamera.maxZ=" + babylonCamera.maxZ, 3);
  58. RaiseMessage("GLTFExporter.Camera | babylonCamera.minZ=" + babylonCamera.minZ, 3);
  59. // --------------------------
  60. // ------- gltfCamera -------
  61. // --------------------------
  62. RaiseMessage("GLTFExporter.Camera | create gltfCamera", 2);
  63. // Camera
  64. var gltfCamera = new GLTFCamera { name = babylonCamera.name };
  65. gltfCamera.index = gltf.CamerasList.Count;
  66. gltf.CamerasList.Add(gltfCamera);
  67. gltfNode.camera = gltfCamera.index;
  68. gltfCamera.gltfNode = gltfNode;
  69. // Camera type
  70. switch (babylonCamera.mode)
  71. {
  72. case (BabylonCamera.CameraMode.ORTHOGRAPHIC_CAMERA):
  73. var gltfCameraOrthographic = new GLTFCameraOrthographic();
  74. gltfCameraOrthographic.xmag = 1; // Do not bother about it - still mandatory
  75. gltfCameraOrthographic.ymag = 1; // Do not bother about it - still mandatory
  76. gltfCameraOrthographic.zfar = babylonCamera.maxZ;
  77. gltfCameraOrthographic.znear = babylonCamera.minZ;
  78. gltfCamera.type = GLTFCamera.CameraType.orthographic.ToString();
  79. gltfCamera.orthographic = gltfCameraOrthographic;
  80. break;
  81. case (BabylonCamera.CameraMode.PERSPECTIVE_CAMERA):
  82. var gltfCameraPerspective = new GLTFCameraPerspective();
  83. gltfCameraPerspective.aspectRatio = null; // Do not bother about it - use default glTF value
  84. gltfCameraPerspective.yfov = babylonCamera.fov; // Babylon camera fov mode is assumed to be vertical (FOVMODE_VERTICAL_FIXED)
  85. gltfCameraPerspective.zfar = babylonCamera.maxZ;
  86. gltfCameraPerspective.znear = babylonCamera.minZ;
  87. gltfCamera.type = GLTFCamera.CameraType.perspective.ToString();
  88. gltfCamera.perspective = gltfCameraPerspective;
  89. break;
  90. default:
  91. RaiseError("GLTFExporter.Camera | camera mode not found");
  92. break;
  93. }
  94. return gltfCamera;
  95. }
  96. }
  97. }