BabylonExporter.Camera.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System.Collections.Generic;
  2. using Autodesk.Max;
  3. using BabylonExport.Entities;
  4. namespace Max2Babylon
  5. {
  6. partial class BabylonExporter
  7. {
  8. private void ExportCamera(IIGameNode cameraNode, BabylonScene babylonScene)
  9. {
  10. if (cameraNode.MaxNode.GetBoolProperty("babylonjs_noexport"))
  11. {
  12. return;
  13. }
  14. var gameCamera = cameraNode.IGameObject.AsGameCamera();
  15. var maxCamera = gameCamera.MaxObject as ICameraObject;
  16. var initialized = gameCamera.InitializeData;
  17. var babylonCamera = new BabylonCamera();
  18. RaiseMessage(cameraNode.Name, 1);
  19. babylonCamera.name = cameraNode.Name;
  20. babylonCamera.id = cameraNode.MaxNode.GetGuid().ToString();
  21. if (cameraNode.NodeParent != null)
  22. {
  23. babylonCamera.parentId = cameraNode.NodeParent.MaxNode.GetGuid().ToString();
  24. }
  25. babylonCamera.fov = Tools.ConvertFov(maxCamera.GetFOV(0, Tools.Forever));
  26. if (maxCamera.ManualClip == 1)
  27. {
  28. babylonCamera.minZ = maxCamera.GetClipDist(0, 1, Tools.Forever);
  29. babylonCamera.maxZ = maxCamera.GetClipDist(0, 2, Tools.Forever);
  30. }
  31. else
  32. {
  33. babylonCamera.minZ = 0.1f;
  34. babylonCamera.maxZ = 10000.0f;
  35. }
  36. if (babylonCamera.minZ == 0.0f)
  37. {
  38. babylonCamera.minZ = 0.1f;
  39. }
  40. // Control
  41. babylonCamera.speed = cameraNode.MaxNode.GetFloatProperty("babylonjs_speed", 1.0f);
  42. babylonCamera.inertia = cameraNode.MaxNode.GetFloatProperty("babylonjs_inertia", 0.9f);
  43. // Collisions
  44. babylonCamera.checkCollisions = cameraNode.MaxNode.GetBoolProperty("babylonjs_checkcollisions");
  45. babylonCamera.applyGravity = cameraNode.MaxNode.GetBoolProperty("babylonjs_applygravity");
  46. babylonCamera.ellipsoid = cameraNode.MaxNode.GetVector3Property("babylonjs_ellipsoid");
  47. // Position
  48. {
  49. var wm = cameraNode.GetLocalTM(0);
  50. var position = wm.Translation;
  51. babylonCamera.position = new float[] { position.X, position.Y, position.Z };
  52. // Target
  53. var target = gameCamera.CameraTarget;
  54. if (target != null)
  55. {
  56. babylonCamera.lockedTargetId = target.MaxNode.GetGuid().ToString();
  57. }
  58. else
  59. {
  60. var dir = wm.GetRow(3);
  61. babylonCamera.target = new float[] { position.X - dir.X, position.Y - dir.Y, position.Z - dir.Z };
  62. }
  63. }
  64. // Animations
  65. var animations = new List<BabylonAnimation>();
  66. //if (!ExportVector3Controller(cameraNode.TMController.PositionController, "position", animations))
  67. //{
  68. ExportVector3Animation("position", animations, key =>
  69. {
  70. var wm = cameraNode.GetLocalTM(key);
  71. var position = wm.Translation;
  72. return new float[] { position.X, position.Y, position.Z };
  73. });
  74. //}
  75. if (gameCamera.CameraTarget == null)
  76. {
  77. ExportVector3Animation("target", animations, key =>
  78. {
  79. var wm = cameraNode.GetLocalTM(key);
  80. var position = wm.Translation;
  81. var dir = wm.GetRow(3);
  82. return new float[] { position.X - dir.X, position.Y - dir.Y, position.Z - dir.Z };
  83. });
  84. }
  85. ExportFloatAnimation("fov", animations, key => new[] { Tools.ConvertFov((gameCamera.MaxObject as ICameraObject).GetFOV(key, Tools.Forever)) });
  86. babylonCamera.animations = animations.ToArray();
  87. if (cameraNode.MaxNode.GetBoolProperty("babylonjs_autoanimate"))
  88. {
  89. babylonCamera.autoAnimate = true;
  90. babylonCamera.autoAnimateFrom = (int)cameraNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  91. babylonCamera.autoAnimateTo = (int)cameraNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to");
  92. babylonCamera.autoAnimateLoop = cameraNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop");
  93. }
  94. babylonScene.CamerasList.Add(babylonCamera);
  95. }
  96. }
  97. }