BabylonExporter.GLTFExporter.Camera.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. using BabylonExport.Entities;
  2. using GLTFExport.Entities;
  3. namespace Max2Babylon
  4. {
  5. partial class BabylonExporter
  6. {
  7. // TODO - Test if ok with a gltf viewer working with custom camera (babylon loader/sandbox doesn't load them)
  8. private GLTFCamera ExportCamera(BabylonCamera babylonCamera, GLTF gltf, GLTFNode gltfParentNode)
  9. {
  10. RaiseMessage("GLTFExporter.Camera | Export camera named: " + babylonCamera.name, 1);
  11. // --------------------------
  12. // ---------- Node ----------
  13. // --------------------------
  14. RaiseMessage("GLTFExporter.Camera | Node", 2);
  15. // Node
  16. var gltfNode = new GLTFNode();
  17. gltfNode.name = babylonCamera.name;
  18. gltfNode.index = gltf.NodesList.Count;
  19. gltf.NodesList.Add(gltfNode);
  20. // Hierarchy
  21. if (gltfParentNode != null)
  22. {
  23. RaiseMessage("GLTFExporter.Camera | Add " + babylonCamera.name + " as child to " + gltfParentNode.name, 3);
  24. gltfParentNode.ChildrenList.Add(gltfNode.index);
  25. }
  26. else
  27. {
  28. // It's a root node
  29. // Only root nodes are listed in a gltf scene
  30. RaiseMessage("GLTFExporter.Camera | Add " + babylonCamera.name + " as root node to scene", 3);
  31. gltf.scenes[0].NodesList.Add(gltfNode.index);
  32. }
  33. // Transform
  34. gltfNode.translation = babylonCamera.position;
  35. // Switch from left to right handed coordinate system
  36. //gltfNode.translation[0] *= -1;
  37. if (babylonCamera.rotationQuaternion != null)
  38. {
  39. gltfNode.rotation = babylonCamera.rotationQuaternion;
  40. }
  41. else
  42. {
  43. // Convert rotation vector to quaternion
  44. BabylonVector3 rotationVector3 = new BabylonVector3
  45. {
  46. X = babylonCamera.rotation[0],
  47. Y = babylonCamera.rotation[1],
  48. Z = babylonCamera.rotation[2]
  49. };
  50. gltfNode.rotation = rotationVector3.toQuaternion().ToArray();
  51. }
  52. // No scaling defined for babylon camera. Use identity instead.
  53. gltfNode.scale = new float[3] { 1, 1, 1 };
  54. // --- prints ---
  55. RaiseMessage("GLTFExporter.Camera | babylonCamera data", 2);
  56. RaiseMessage("GLTFExporter.Camera | babylonCamera.type=" + babylonCamera.type, 3);
  57. RaiseMessage("GLTFExporter.Camera | babylonCamera.fov=" + babylonCamera.fov, 3);
  58. RaiseMessage("GLTFExporter.Camera | babylonCamera.maxZ=" + babylonCamera.maxZ, 3);
  59. RaiseMessage("GLTFExporter.Camera | babylonCamera.minZ=" + babylonCamera.minZ, 3);
  60. // --------------------------
  61. // ------- gltfCamera -------
  62. // --------------------------
  63. RaiseMessage("GLTFExporter.Camera | create gltfCamera", 2);
  64. // Camera
  65. var gltfCamera = new GLTFCamera { name = babylonCamera.name };
  66. gltfCamera.index = gltf.CamerasList.Count;
  67. gltf.CamerasList.Add(gltfCamera);
  68. gltfNode.camera = gltfCamera.index;
  69. gltfCamera.gltfNode = gltfNode;
  70. // Camera type
  71. switch (babylonCamera.mode)
  72. {
  73. case (BabylonCamera.CameraMode.ORTHOGRAPHIC_CAMERA):
  74. var gltfCameraOrthographic = new GLTFCameraOrthographic();
  75. gltfCameraOrthographic.xmag = 1; // TODO - How to retreive value from babylon? xmag:The floating-point horizontal magnification of the view
  76. gltfCameraOrthographic.ymag = 1; // TODO - How to retreive value from babylon? ymag:The floating-point vertical magnification of the view
  77. gltfCameraOrthographic.zfar = babylonCamera.maxZ;
  78. gltfCameraOrthographic.znear = babylonCamera.minZ;
  79. gltfCamera.type = GLTFCamera.CameraType.orthographic.ToString();
  80. gltfCamera.orthographic = gltfCameraOrthographic;
  81. break;
  82. case (BabylonCamera.CameraMode.PERSPECTIVE_CAMERA):
  83. var gltfCameraPerspective = new GLTFCameraPerspective();
  84. gltfCameraPerspective.aspectRatio = null; // 0.8f; // TODO - How to retreive value from babylon? The aspect ratio in babylon is computed based on the engine rather than set on a camera (aspectRatio = _gl.drawingBufferWidth / _gl.drawingBufferHeight)
  85. gltfCameraPerspective.yfov = babylonCamera.fov; // WARNING - Babylon camera fov mode is assumed to be vertical (FOVMODE_VERTICAL_FIXED)
  86. gltfCameraPerspective.zfar = babylonCamera.maxZ;
  87. gltfCameraPerspective.znear = babylonCamera.minZ;
  88. gltfCamera.type = GLTFCamera.CameraType.perspective.ToString();
  89. gltfCamera.perspective = gltfCameraPerspective;
  90. break;
  91. default:
  92. RaiseError("GLTFExporter.Camera | camera mode not found");
  93. break;
  94. }
  95. return gltfCamera;
  96. }
  97. }
  98. }