CameraComponentConverter.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/Serialization/SerializeContext.h>
  9. #if defined(CAMERA_EDITOR)
  10. #include "EditorCameraComponent.h"
  11. #else
  12. #include "CameraComponent.h"
  13. #endif
  14. #include "CameraComponentController.h"
  15. namespace Camera
  16. {
  17. namespace ClassConverters
  18. {
  19. bool DeprecateCameraComponentWithoutEditor(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  20. {
  21. // Capture the old values
  22. float fov = DefaultFoV;
  23. classElement.GetChildData(AZ::Crc32("Field of View"), fov);
  24. float nearDistance = DefaultNearPlaneDistance;
  25. classElement.GetChildData(AZ::Crc32("Near Clip Plane Distance"), nearDistance);
  26. float farDistance = DefaultFarClipPlaneDistance;
  27. classElement.GetChildData(AZ::Crc32("Far Clip Plane Distance"), farDistance);
  28. bool shouldSpecifyFrustum = false;
  29. classElement.GetChildData(AZ::Crc32("SpecifyDimensions"), shouldSpecifyFrustum);
  30. float frustumWidth = DefaultFrustumDimension;
  31. classElement.GetChildData(AZ::Crc32("FrustumWidth"), frustumWidth);
  32. float frustumHeight = DefaultFrustumDimension;
  33. classElement.GetChildData(AZ::Crc32("FrustumHeight"), frustumHeight);
  34. // convert to the new class
  35. #if defined(CAMERA_EDITOR)
  36. if (classElement.GetName() == AZ::Crc32("m_template"))
  37. {
  38. classElement.Convert<EditorCameraComponent>(context);
  39. }
  40. else
  41. #else
  42. classElement.Convert<CameraComponent>(context);
  43. #endif // CAMERA_EDITOR
  44. // add the new values
  45. classElement.AddElementWithData(context, "Field of View", fov);
  46. classElement.AddElementWithData(context, "Near Clip Plane Distance", nearDistance);
  47. classElement.AddElementWithData(context, "Far Clip Plane Distance", farDistance);
  48. classElement.AddElementWithData(context, "SpecifyDimensions", shouldSpecifyFrustum);
  49. classElement.AddElementWithData(context, "FrustumWidth", frustumWidth);
  50. classElement.AddElementWithData(context, "FrustumHeight", frustumHeight);
  51. return true;
  52. }
  53. bool UpdateCameraComponentToUseController(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
  54. {
  55. // Create a controller, as they now house the camera config
  56. CameraComponentController controller;
  57. CameraComponentConfig config = controller.GetConfiguration();
  58. // Migrate pre-existing configuration to the controller config
  59. classElement.GetChildData(AZ::Crc32("Field of View"), config.m_fov);
  60. classElement.GetChildData(AZ::Crc32("Near Clip Plane Distance"), config.m_nearClipDistance);
  61. classElement.GetChildData(AZ::Crc32("Far Clip Plane Distance"), config.m_farClipDistance);
  62. classElement.GetChildData(AZ::Crc32("SpecifyDimensions"), config.m_specifyFrustumDimensions);
  63. classElement.GetChildData(AZ::Crc32("FrustumWidth"), config.m_frustumWidth);
  64. classElement.GetChildData(AZ::Crc32("FrustumHeight"), config.m_frustumHeight);
  65. controller.SetConfiguration(config);
  66. // Remove old fields
  67. classElement.RemoveElementByName(AZ::Crc32("Field of View"));
  68. classElement.RemoveElementByName(AZ::Crc32("Near Clip Plane Distance"));
  69. classElement.RemoveElementByName(AZ::Crc32("Far Clip Plane Distance"));
  70. classElement.RemoveElementByName(AZ::Crc32("SpecifyDimensions"));
  71. classElement.RemoveElementByName(AZ::Crc32("FrustumWidth"));
  72. classElement.RemoveElementByName(AZ::Crc32("FrustumHeight"));
  73. // Add the controller element
  74. classElement.AddElementWithData(context, "Controller", controller);
  75. return true;
  76. }
  77. }
  78. } // Camera