BabylonExporter.Camera.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. // Type
  41. babylonCamera.type = cameraNode.MaxNode.GetStringProperty("babylonjs_type", "FreeCamera");
  42. // Control
  43. babylonCamera.speed = cameraNode.MaxNode.GetFloatProperty("babylonjs_speed", 1.0f);
  44. babylonCamera.inertia = cameraNode.MaxNode.GetFloatProperty("babylonjs_inertia", 0.9f);
  45. // Collisions
  46. babylonCamera.checkCollisions = cameraNode.MaxNode.GetBoolProperty("babylonjs_checkcollisions");
  47. babylonCamera.applyGravity = cameraNode.MaxNode.GetBoolProperty("babylonjs_applygravity");
  48. babylonCamera.ellipsoid = cameraNode.MaxNode.GetVector3Property("babylonjs_ellipsoid");
  49. // Position
  50. var wm = cameraNode.GetLocalTM(0);
  51. var position = wm.Translation;
  52. babylonCamera.position = new float[] { position.X, position.Y, position.Z };
  53. // Target
  54. var target = gameCamera.CameraTarget;
  55. if (target != null)
  56. {
  57. babylonCamera.lockedTargetId = target.MaxNode.GetGuid().ToString();
  58. }
  59. else
  60. {
  61. var dir = wm.GetRow(3);
  62. babylonCamera.target = new float[] { position.X - dir.X, position.Y - dir.Y, position.Z - dir.Z };
  63. }
  64. // Animations
  65. var animations = new List<BabylonAnimation>();
  66. ExportVector3Animation("position", animations, key =>
  67. {
  68. var tm = cameraNode.GetLocalTM(key);
  69. var translation = tm.Translation;
  70. return new float[] { translation.X, translation.Y, translation.Z };
  71. });
  72. if (gameCamera.CameraTarget == null)
  73. {
  74. ExportVector3Animation("target", animations, key =>
  75. {
  76. var tm = cameraNode.GetLocalTM(key);
  77. var translation = tm.Translation;
  78. var dir = tm.GetRow(3);
  79. return new float[] { translation.X - dir.X, translation.Y - dir.Y, translation.Z - dir.Z };
  80. });
  81. }
  82. ExportFloatAnimation("fov", animations, key => new[] { Tools.ConvertFov((gameCamera.MaxObject as ICameraObject).GetFOV(key, Tools.Forever)) });
  83. babylonCamera.animations = animations.ToArray();
  84. if (cameraNode.MaxNode.GetBoolProperty("babylonjs_autoanimate"))
  85. {
  86. babylonCamera.autoAnimate = true;
  87. babylonCamera.autoAnimateFrom = (int)cameraNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_from");
  88. babylonCamera.autoAnimateTo = (int)cameraNode.MaxNode.GetFloatProperty("babylonjs_autoanimate_to");
  89. babylonCamera.autoAnimateLoop = cameraNode.MaxNode.GetBoolProperty("babylonjs_autoanimateloop");
  90. }
  91. babylonScene.CamerasList.Add(babylonCamera);
  92. }
  93. }
  94. }