Files
o3de/Gems/Camera/Code/Source/EditorCameraComponent.cpp
T
Esteban Papp 1f9b284de2 Merge branch 'development' into cmake/SPEC-7179
Signed-off-by: Esteban Papp <81431996+amznestebanpapp@users.noreply.github.com>

# Conflicts:
#	Code/Editor/Plugins/ComponentEntityEditorPlugin/ComponentEntityEditorPlugin_precompiled.h
#	Code/Editor/Plugins/EditorCommon/EditorCommon_precompiled.h
#	Code/Editor/Plugins/EditorCommon/stdafx.cpp
#	Code/Editor/Plugins/FFMPEGPlugin/FFMPEGPlugin_precompiled.h
#	Code/Editor/Plugins/PerforcePlugin/PerforcePlugin_precompiled.h
#	Code/Editor/Plugins/ProjectSettingsTool/ProjectSettingsTool_precompiled.h
#	Code/Framework/AzToolsFramework/AzToolsFramework/AzToolsFramework_precompiled.h
#	Code/Tools/AssetProcessor/native/precompiled.h
#	Code/Tools/Standalone/StandaloneTools_precompiled.h
#	Gems/AssetMemoryAnalyzer/Code/Source/AssetMemoryAnalyzer_precompiled.h
#	Gems/Atom/Asset/ImageProcessingAtom/Code/Source/ImageProcessing_precompiled.h
#	Gems/Atom/RHI/DX12/Code/Source/RHI/Atom_RHI_DX12_precompiled.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/Mac/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Include/Platform/iOS/Atom_RHI_Metal_precompiled_Platform.h
#	Gems/Atom/RHI/Metal/Code/Source/Atom_RHI_Metal_precompiled.h
#	Gems/Atom/RHI/Metal/Code/atom_rhi_metal_common_files.cmake
#	Gems/Atom/RHI/Null/Code/Source/Atom_RHI_Null_precompiled.h
#	Gems/Atom/RHI/Null/Code/atom_rhi_null_common_files.cmake
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Android/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Linux/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Mac/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Include/Platform/Windows/Atom_RHI_Vulkan_precompiled_Platform.h
#	Gems/Atom/RHI/Vulkan/Code/Source/Atom_RHI_Vulkan_precompiled.h
#	Gems/Atom/RHI/Vulkan/Code/Source/RHI/SwapChain.cpp
#	Gems/Atom/RHI/Vulkan/Code/atom_rhi_vulkan_common_files.cmake
#	Gems/AtomLyIntegration/AtomFont/Code/Include/AtomLyIntegration/AtomFont/AtomFont_precompiled.h
#	Gems/Blast/Code/Source/StdAfx.cpp
#	Gems/Camera/Code/Source/Camera_precompiled.h
#	Gems/EMotionFX/Code/Source/EMotionFX_precompiled.h
#	Gems/FastNoise/Code/Source/FastNoise_precompiled.h
#	Gems/Gestures/Code/Source/Gestures_precompiled.h
#	Gems/GradientSignal/Code/Source/GradientSignal_precompiled.h
#	Gems/GraphCanvas/Code/precompiled.h
#	Gems/ImGui/Code/Source/ImGui_precompiled.h
#	Gems/InAppPurchases/Code/Source/InAppPurchases_precompiled.h
#	Gems/LmbrCentral/Code/Source/LmbrCentral_precompiled.h
#	Gems/LmbrCentral/Code/Tests/ShapeGeometryUtilTest.cpp
#	Gems/LyShine/Code/Editor/UiCanvasEditor_precompiled.h
#	Gems/LyShine/Code/Source/Animation/LyShine_precompiled.h
#	Gems/LyShine/Code/Source/LyShine_precompiled.h
#	Gems/LyShineExamples/Code/Source/LyShineExamples_precompiled.h
#	Gems/Maestro/Code/Source/Cinematics/Maestro_precompiled.h
#	Gems/Maestro/Code/Source/Maestro_precompiled.h
#	Gems/MessagePopup/Code/Source/MessagePopup_precompiled.h
#	Gems/Metastream/Code/Source/Metastream_precompiled.h
#	Gems/Microphone/Code/Source/Microphone_precompiled.h
#	Gems/Multiplayer/Code/Source/Multiplayer_precompiled.h
#	Gems/PhysX/Code/NumericalMethods/Source/NumericalMethods_precompiled.h
#	Gems/PhysX/Code/Source/PhysXUnsupported_precompiled.h
#	Gems/PhysX/Code/Source/PhysX_precompiled.h
#	Gems/PhysX/Code/physx_unsupported_files.cmake
#	Gems/PhysXDebug/Code/Source/PhysXDebugUnsupported_precompiled.h
#	Gems/PhysXDebug/Code/Source/PhysXDebug_precompiled.h
#	Gems/ScriptCanvas/Code/Editor/precompiled.h
#	Gems/ScriptCanvas/Code/Source/precompiled.h
#	Gems/ScriptCanvasDeveloper/Code/Source/precompiled.h
#	Gems/ScriptCanvasPhysics/Code/Source/ScriptCanvasPhysics_precompiled.h
#	Gems/ScriptEvents/Code/Source/precompiled.h
#	Gems/ScriptEvents/Code/Tests/Editor/EditorTests.cpp
#	Gems/ScriptedEntityTweener/Code/Source/ScriptedEntityTweener_precompiled.h
#	Gems/SliceFavorites/Code/Source/SliceFavorites_precompiled.h
#	Gems/StartingPointCamera/Code/Source/StartingPointCamera_precompiled.h
#	Gems/StartingPointInput/Code/Source/StartingPointInput_precompiled.h
#	Gems/StartingPointMovement/Code/Source/StartingPointMovement_precompiled.h
#	Gems/SurfaceData/Code/Source/SurfaceData_precompiled.h
#	Gems/TextureAtlas/Code/Source/TextureAtlas_precompiled.h
#	Gems/TickBusOrderViewer/Code/Source/TickBusOrderViewer_precompiled.h
#	Gems/Twitch/Code/Source/Twitch_precompiled.h
#	Gems/VirtualGamepad/Code/Source/VirtualGamepad_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBoxUnsupported_precompiled.h
#	Gems/WhiteBox/Code/Source/WhiteBox_precompiled.h
2021-07-16 15:42:37 -07:00

233 lines
11 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#include <AzCore/Serialization/EditContext.h>
#include <AzCore/Component/TransformBus.h>
#include <AzFramework/Entity/EntityDebugDisplayBus.h>
#include "EditorCameraComponent.h"
#include "ViewportCameraSelectorWindow.h"
#include <MathConversion.h>
#include <IRenderer.h>
#include <AzCore/RTTI/BehaviorContext.h>
#include <AzToolsFramework/API/ToolsApplicationAPI.h>
namespace Camera
{
namespace ClassConverters
{
extern bool UpdateCameraComponentToUseController(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement);
}
void EditorCameraComponent::Activate()
{
// Ensure our Editor Entity ID is up-to-date to sync camera configurations between Edit & Game mode.
CameraComponentConfig controllerConfig = m_controller.GetConfiguration();
controllerConfig.m_editorEntityId = GetEntityId().operator AZ::u64();
m_controller.SetConfiguration(controllerConfig);
// Call base class activate, which in turn calls Activate on our controller.
EditorCameraComponentBase::Activate();
AzFramework::EntityDebugDisplayEventBus::Handler::BusConnect(GetEntityId());
EditorCameraNotificationBus::Handler::BusConnect();
EditorCameraViewRequestBus::Handler::BusConnect(GetEntityId());
AZ::EntityId currentViewEntity;
EditorCameraRequests::Bus::BroadcastResult(currentViewEntity, &EditorCameraRequests::GetCurrentViewEntityId);
if (currentViewEntity == GetEntityId())
{
m_controller.ActivateAtomView();
m_isActiveEditorCamera = true;
}
}
void EditorCameraComponent::Deactivate()
{
if (m_isActiveEditorCamera)
{
m_controller.DeactivateAtomView();
m_isActiveEditorCamera = false;
}
EditorCameraViewRequestBus::Handler::BusDisconnect(GetEntityId());
EditorCameraNotificationBus::Handler::BusDisconnect();
AzFramework::EntityDebugDisplayEventBus::Handler::BusDisconnect();
EditorCameraComponentBase::Deactivate();
}
AZ::u32 EditorCameraComponent::OnConfigurationChanged()
{
bool isActiveEditorCamera = m_isActiveEditorCamera;
AZ::u32 configurationHash = EditorCameraComponentBase::OnConfigurationChanged();
// If we were the active editor camera before, ensure we get reactivated after our controller gets disabled then re-enabled
if (isActiveEditorCamera)
{
EditorCameraRequests::Bus::Broadcast(&EditorCameraRequests::SetViewFromEntityPerspective, GetEntityId());
}
return configurationHash;
}
static bool UpdateEditorCameraComponentToUseController(AZ::SerializeContext& context, AZ::SerializeContext::DataElementNode& classElement)
{
if (!ClassConverters::UpdateCameraComponentToUseController(context, classElement))
{
return false;
}
classElement.Convert<EditorCameraComponent>(context);
return true;
}
void EditorCameraComponent::Reflect(AZ::ReflectContext* reflection)
{
EditorCameraComponentBase::Reflect(reflection);
AZ::SerializeContext* serializeContext = azrtti_cast<AZ::SerializeContext*>(reflection);
if (serializeContext)
{
serializeContext->ClassDeprecate("EditorCameraComponent", "{B99EFE3D-3F1D-4630-8A7B-31C70CC1F53C}", &UpdateEditorCameraComponentToUseController);
serializeContext->Class<EditorCameraComponent, EditorCameraComponentBase>()
->Version(0)
->Field("FrustumLengthPercent", &EditorCameraComponent::m_frustumViewPercentLength)
->Field("FrustumDrawColor", &EditorCameraComponent::m_frustumDrawColor)
;
AZ::EditContext* editContext = serializeContext->GetEditContext();
if (editContext)
{
editContext->Class<EditorCameraComponent>("Camera", "The Camera component allows an entity to be used as a camera")
->ClassElement(AZ::Edit::ClassElements::EditorData, "")
->Attribute(AZ::Edit::Attributes::Category, "Camera")
->Attribute(AZ::Edit::Attributes::Icon, "Editor/Icons/Components/Camera.svg")
->Attribute(AZ::Edit::Attributes::ViewportIcon, "Editor/Icons/Components/Viewport/Camera.png")
->Attribute(AZ::Edit::Attributes::AutoExpand, true)
->Attribute(AZ::Edit::Attributes::AppearsInAddComponentMenu, AZ_CRC("Game", 0x232b318c))
->Attribute(AZ::Edit::Attributes::HelpPageURL, "https://o3de.org/docs/user-guide/components/reference/camera/")
->UIElement(AZ::Edit::UIHandlers::Button,"", "Sets the view to this camera")
->Attribute(AZ::Edit::Attributes::ChangeNotify, &EditorCameraComponent::OnPossessCameraButtonClicked)
->Attribute(AZ::Edit::Attributes::ButtonText, &EditorCameraComponent::GetCameraViewButtonText)
->ClassElement(AZ::Edit::ClassElements::Group, "Debug")
->Attribute(AZ::Edit::Attributes::AutoExpand, false)
->DataElement(AZ::Edit::UIHandlers::Default, &EditorCameraComponent::m_frustumViewPercentLength, "Frustum length", "Frustum length percent .01 to 100")
->Attribute(AZ::Edit::Attributes::Min, 0.01f)
->Attribute(AZ::Edit::Attributes::Max, 100.f)
->Attribute(AZ::Edit::Attributes::Suffix, " percent")
->Attribute(AZ::Edit::Attributes::Step, 1.f)
->DataElement(AZ::Edit::UIHandlers::Color, &EditorCameraComponent::m_frustumDrawColor, "Frustum color", "Frustum draw color RGB")
;
}
}
if (AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(reflection))
{
behaviorContext->Class<EditorCameraComponent>()->RequestBus("CameraRequestBus");
behaviorContext->EBus<EditorCameraViewRequestBus>("EditorCameraViewRequestBus")
->Attribute(AZ::Script::Attributes::Scope, AZ::Script::Attributes::ScopeFlags::Automation)
->Attribute(AZ::Script::Attributes::Module, "camera")
->Event("ToggleCameraAsActiveView", &EditorCameraViewRequests::ToggleCameraAsActiveView)
;
}
}
void EditorCameraComponent::OnViewportViewEntityChanged([[maybe_unused]] const AZ::EntityId& newViewId)
{
if (newViewId == GetEntityId())
{
if (!m_isActiveEditorCamera)
{
m_controller.ActivateAtomView();
m_isActiveEditorCamera = true;
AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestRefresh, AzToolsFramework::PropertyModificationRefreshLevel::Refresh_AttributesAndValues);
}
}
else if (m_isActiveEditorCamera)
{
m_controller.DeactivateAtomView();
m_isActiveEditorCamera = false;
AzToolsFramework::PropertyEditorGUIMessages::Bus::Broadcast(&AzToolsFramework::PropertyEditorGUIMessages::RequestRefresh, AzToolsFramework::PropertyModificationRefreshLevel::Refresh_AttributesAndValues);
}
}
AZ::Crc32 EditorCameraComponent::OnPossessCameraButtonClicked()
{
AZ::EntityId currentViewEntity;
EditorCameraRequests::Bus::BroadcastResult(currentViewEntity, &EditorCameraRequests::GetCurrentViewEntityId);
AzToolsFramework::EditorRequestBus::Broadcast(&AzToolsFramework::EditorRequestBus::Events::ShowViewPane, s_viewportCameraSelectorName);
if (currentViewEntity != GetEntityId())
{
EditorCameraRequests::Bus::Broadcast(&EditorCameraRequests::SetViewFromEntityPerspective, GetEntityId());
}
else
{
// set the view entity id back to Invalid, thus enabling the editor camera
EditorCameraRequests::Bus::Broadcast(&EditorCameraRequests::SetViewFromEntityPerspective, AZ::EntityId());
}
return AZ::Edit::PropertyRefreshLevels::AttributesAndValues;
}
AZStd::string EditorCameraComponent::GetCameraViewButtonText() const
{
AZ::EntityId currentViewEntity;
EditorCameraRequests::Bus::BroadcastResult(currentViewEntity, &EditorCameraRequests::GetCurrentViewEntityId);
if (currentViewEntity == GetEntityId())
{
return "Return to default editor camera";
}
else
{
return "Be this camera";
}
}
void EditorCameraComponent::DisplayEntityViewport(
[[maybe_unused]] const AzFramework::ViewportInfo& viewportInfo,
AzFramework::DebugDisplayRequests& debugDisplay)
{
AZ::Transform transform = AZ::Transform::CreateIdentity();
AZ::TransformBus::EventResult(transform, GetEntityId(), &AZ::TransformInterface::GetWorldTM);
EditorDisplay(debugDisplay, transform);
}
void EditorCameraComponent::EditorDisplay(
AzFramework::DebugDisplayRequests& debugDisplay, const AZ::Transform& world)
{
const CameraComponentConfig& config = m_controller.GetConfiguration();
const float distance = config.m_farClipDistance * m_frustumViewPercentLength * 0.01f;
float tangent = static_cast<float>(tan(0.5f * AZ::DegToRad(config.m_fov)));
float height = distance * tangent;
float width = height * debugDisplay.GetAspectRatio();
AZ::Vector3 farPoints[4];
farPoints[0] = AZ::Vector3( width, distance, height);
farPoints[1] = AZ::Vector3(-width, distance, height);
farPoints[2] = AZ::Vector3(-width, distance, -height);
farPoints[3] = AZ::Vector3( width, distance, -height);
AZ::Vector3 start(0, 0, 0);
AZ::Vector3 nearPoints[4];
nearPoints[0] = farPoints[0].GetNormalizedSafe() * config.m_nearClipDistance;
nearPoints[1] = farPoints[1].GetNormalizedSafe() * config.m_nearClipDistance;
nearPoints[2] = farPoints[2].GetNormalizedSafe() * config.m_nearClipDistance;
nearPoints[3] = farPoints[3].GetNormalizedSafe() * config.m_nearClipDistance;
debugDisplay.PushMatrix(world);
debugDisplay.SetColor(m_frustumDrawColor.GetAsVector4());
debugDisplay.DrawLine(nearPoints[0], farPoints[0]);
debugDisplay.DrawLine(nearPoints[1], farPoints[1]);
debugDisplay.DrawLine(nearPoints[2], farPoints[2]);
debugDisplay.DrawLine(nearPoints[3], farPoints[3]);
debugDisplay.DrawPolyLine(nearPoints, AZ_ARRAY_SIZE(nearPoints));
debugDisplay.DrawPolyLine(farPoints, AZ_ARRAY_SIZE(farPoints));
debugDisplay.PopMatrix();
}
} //namespace Camera