#pragma once #include "Core/IEditorContext.h" #include "Core/ISceneManager.h" #include "Core/ISelectionManager.h" #include "IViewportHostService.h" #include "SceneViewportPicker.h" #include "SceneViewportCameraController.h" #include "ViewportHostRenderFlowUtils.h" #include "ViewportHostRenderTargets.h" #include "ViewportObjectIdPicker.h" #include "UI/ImGuiBackendBridge.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace XCEngine { namespace Editor { namespace { constexpr bool kDebugSceneSelectionMask = false; Math::Vector3 GetSceneViewportOrientationAxisVector(SceneViewportOrientationAxis axis) { switch (axis) { case SceneViewportOrientationAxis::PositiveX: return Math::Vector3::Right(); case SceneViewportOrientationAxis::NegativeX: return Math::Vector3::Left(); case SceneViewportOrientationAxis::PositiveY: return Math::Vector3::Up(); case SceneViewportOrientationAxis::NegativeY: return Math::Vector3::Down(); case SceneViewportOrientationAxis::PositiveZ: return Math::Vector3::Forward(); case SceneViewportOrientationAxis::NegativeZ: return Math::Vector3::Back(); default: return Math::Vector3::Zero(); } } } // namespace class ViewportHostService : public IViewportHostService { public: void Initialize(UI::ImGuiBackendBridge& backend, RHI::RHIDevice* device) { Shutdown(); m_backend = &backend; m_device = device; } void Shutdown() { for (ViewportEntry& entry : m_entries) { DestroyViewportRenderTargets(m_backend, entry.renderTargets); entry = {}; } m_sceneViewCamera = {}; m_sceneViewLastRenderContext = {}; m_device = nullptr; m_backend = nullptr; m_sceneRenderer.reset(); } void BeginFrame() override { for (ViewportEntry& entry : m_entries) { entry.requestedThisFrame = false; entry.requestedWidth = 0; entry.requestedHeight = 0; } } EditorViewportFrame RequestViewport(EditorViewportKind kind, const ImVec2& requestedSize) override { ViewportEntry& entry = GetEntry(kind); entry.requestedThisFrame = requestedSize.x > 1.0f && requestedSize.y > 1.0f; entry.requestedWidth = entry.requestedThisFrame ? static_cast(requestedSize.x) : 0u; entry.requestedHeight = entry.requestedThisFrame ? static_cast(requestedSize.y) : 0u; if (entry.requestedThisFrame && m_backend != nullptr && m_device != nullptr) { EnsureViewportResources(entry); } EditorViewportFrame frame = {}; frame.textureId = entry.renderTargets.textureId; frame.requestedSize = requestedSize; frame.renderSize = ImVec2( static_cast(entry.renderTargets.width), static_cast(entry.renderTargets.height)); frame.hasTexture = entry.renderTargets.textureId != ImTextureID{}; frame.wasRequested = entry.requestedThisFrame; frame.statusText = entry.statusText; return frame; } void UpdateSceneViewInput(IEditorContext& context, const SceneViewportInput& input) override { if (!EnsureSceneViewCamera()) { return; } if (input.focusSelectionRequested) { FocusSceneView(context); } SceneViewportCameraInputState controllerInput = {}; controllerInput.viewportHeight = input.viewportSize.y; controllerInput.zoomDelta = input.hovered ? input.mouseWheel : 0.0f; controllerInput.flySpeedDelta = input.hovered ? input.flySpeedDelta : 0.0f; controllerInput.deltaTime = input.deltaTime; controllerInput.moveForward = input.moveForward; controllerInput.moveRight = input.moveRight; controllerInput.moveUp = input.moveUp; controllerInput.fastMove = input.fastMove; if (input.looking) { controllerInput.lookDeltaX = input.mouseDelta.x; controllerInput.lookDeltaY = input.mouseDelta.y; } if (input.orbiting) { controllerInput.orbitDeltaX = input.mouseDelta.x; controllerInput.orbitDeltaY = input.mouseDelta.y; } if (input.panning) { controllerInput.panDeltaX = input.mouseDelta.x; controllerInput.panDeltaY = input.mouseDelta.y; } m_sceneViewCamera.controller.ApplyInput(controllerInput); ApplySceneViewCameraController(); } uint64_t PickSceneViewEntity( IEditorContext& context, const ImVec2& viewportSize, const ImVec2& viewportMousePosition) override { if (!EnsureSceneViewCamera()) { return 0; } const Components::Scene* scene = context.GetSceneManager().GetScene(); if (scene == nullptr) { return 0; } ViewportEntry& entry = GetEntry(EditorViewportKind::Scene); uint64_t objectIdEntity = 0; if (TryPickSceneViewEntityWithObjectId( entry, viewportSize, viewportMousePosition, objectIdEntity)) { return objectIdEntity; } SceneViewportPickRequest request = {}; request.scene = scene; request.overlay = GetSceneViewOverlayData(); request.viewportSize = Math::Vector2(viewportSize.x, viewportSize.y); request.viewportPosition = Math::Vector2(viewportMousePosition.x, viewportMousePosition.y); return PickSceneViewportEntity(request).entityId; } void AlignSceneViewToOrientationAxis(SceneViewportOrientationAxis axis) override { if (!EnsureSceneViewCamera()) { return; } const Math::Vector3 axisDirection = GetSceneViewportOrientationAxisVector(axis); if (axisDirection.SqrMagnitude() <= Math::EPSILON) { return; } // To make the clicked cone face the screen, the camera must look back along that axis. m_sceneViewCamera.controller.AnimateToForward(axisDirection * -1.0f); ApplySceneViewCameraController(); } SceneViewportOverlayData GetSceneViewOverlayData() const override { SceneViewportOverlayData data = {}; if (m_sceneViewCamera.gameObject == nullptr || m_sceneViewCamera.camera == nullptr) { return data; } const Components::TransformComponent* transform = m_sceneViewCamera.gameObject->GetTransform(); if (transform == nullptr) { return data; } data.valid = true; data.cameraPosition = transform->GetPosition(); data.cameraForward = transform->GetForward(); data.cameraRight = transform->GetRight(); data.cameraUp = transform->GetUp(); data.verticalFovDegrees = m_sceneViewCamera.camera->GetFieldOfView(); data.nearClipPlane = m_sceneViewCamera.camera->GetNearClipPlane(); data.farClipPlane = m_sceneViewCamera.camera->GetFarClipPlane(); data.orbitDistance = m_sceneViewCamera.controller.GetDistance(); return data; } void RenderRequestedViewports( IEditorContext& context, const Rendering::RenderContext& renderContext) override { if (m_backend == nullptr || m_device == nullptr || !renderContext.IsValid()) { return; } EnsureSceneRenderer(); m_sceneViewLastRenderContext = renderContext; const auto* scene = context.GetSceneManager().GetScene(); for (ViewportEntry& entry : m_entries) { if (!entry.requestedThisFrame) { continue; } if (!EnsureViewportResources(entry)) { entry.statusText = "Failed to create viewport render targets"; continue; } RenderViewportEntry(entry, context, scene, renderContext); } } private: struct ViewportEntry { EditorViewportKind kind = EditorViewportKind::Scene; uint32_t requestedWidth = 0; uint32_t requestedHeight = 0; bool requestedThisFrame = false; ViewportRenderTargets renderTargets = {}; std::string statusText; }; struct SceneViewCameraState { std::unique_ptr gameObject; Components::CameraComponent* camera = nullptr; SceneViewportCameraController controller; }; struct SceneViewportRenderState { SceneViewportOverlayData overlay = {}; Rendering::BuiltinPostProcessRequest builtinPostProcess = {}; std::vector selectedObjectIds; }; ViewportEntry& GetEntry(EditorViewportKind kind) { const size_t index = kind == EditorViewportKind::Scene ? 0u : 1u; m_entries[index].kind = kind; return m_entries[index]; } void EnsureSceneRenderer() { if (!m_sceneRenderer) { m_sceneRenderer = std::make_unique(); } } bool EnsureSceneViewCamera() { if (m_sceneViewCamera.gameObject != nullptr && m_sceneViewCamera.camera != nullptr) { return true; } m_sceneViewCamera.gameObject = std::make_unique("EditorSceneCamera"); m_sceneViewCamera.camera = m_sceneViewCamera.gameObject->AddComponent(); if (m_sceneViewCamera.camera == nullptr) { m_sceneViewCamera.gameObject.reset(); return false; } m_sceneViewCamera.camera->SetPrimary(false); m_sceneViewCamera.camera->SetProjectionType(Components::CameraProjectionType::Perspective); m_sceneViewCamera.camera->SetFieldOfView(60.0f); m_sceneViewCamera.camera->SetNearClipPlane(0.03f); m_sceneViewCamera.camera->SetFarClipPlane(2000.0f); m_sceneViewCamera.controller.Reset(); ApplySceneViewCameraController(); return true; } void ApplySceneViewCameraController() { if (m_sceneViewCamera.gameObject == nullptr) { return; } m_sceneViewCamera.controller.ApplyTo(*m_sceneViewCamera.gameObject->GetTransform()); } void FocusSceneView(IEditorContext& context) { Components::GameObject* target = nullptr; const uint64_t selectedEntity = context.GetSelectionManager().GetSelectedEntity(); if (selectedEntity != 0) { target = context.GetSceneManager().GetEntity(selectedEntity); } if (target != nullptr) { m_sceneViewCamera.controller.Focus(target->GetTransform()->GetPosition()); return; } const auto& roots = context.GetSceneManager().GetRootEntities(); if (roots.empty()) { m_sceneViewCamera.controller.Focus(Math::Vector3::Zero()); return; } Math::Vector3 center = Math::Vector3::Zero(); uint32_t count = 0; for (const Components::GameObject* root : roots) { if (root == nullptr) { continue; } center += root->GetTransform()->GetPosition(); ++count; } if (count > 0) { center /= static_cast(count); } m_sceneViewCamera.controller.Focus(center); } bool EnsureViewportResources(ViewportEntry& entry) { const ViewportHostResourceReuseQuery reuseQuery = BuildViewportRenderTargetsReuseQuery( entry.kind, entry.renderTargets, entry.requestedWidth, entry.requestedHeight); if (CanReuseViewportResources(reuseQuery)) { return true; } if (entry.requestedWidth == 0 || entry.requestedHeight == 0) { return false; } return CreateViewportRenderTargets( entry.kind, entry.requestedWidth, entry.requestedHeight, m_device, m_backend, entry.renderTargets); } Rendering::RenderSurface BuildSurface(const ViewportEntry& entry) const { return BuildViewportColorSurface(entry.renderTargets); } void ApplyViewportRenderFailure( ViewportEntry& entry, const Rendering::RenderContext& renderContext, const ViewportRenderFallbackPolicy& policy) { ApplyViewportFailureStatus(entry.statusText, policy); if (policy.invalidateObjectIdFrame) { InvalidateViewportObjectIdFrame(entry.renderTargets); } if (!policy.shouldClear) { return; } ClearViewport( entry, renderContext, policy.clearColor.r, policy.clearColor.g, policy.clearColor.b, policy.clearColor.a); } void BuildSceneViewportRenderState( ViewportEntry& entry, IEditorContext& context, const Components::Scene& scene, SceneViewportRenderState& outState) { (void)scene; outState.overlay = GetSceneViewOverlayData(); if (!outState.overlay.valid) { return; } outState.selectedObjectIds = context.GetSelectionManager().GetSelectedEntities(); const SceneViewportBuiltinPostProcessBuildResult builtinPostProcess = BuildSceneViewportBuiltinPostProcess( outState.overlay, outState.selectedObjectIds, entry.renderTargets.objectIdShaderView != nullptr, kDebugSceneSelectionMask); outState.builtinPostProcess = builtinPostProcess.request; if (builtinPostProcess.warningStatusText != nullptr) { SetViewportStatusIfEmpty(entry.statusText, builtinPostProcess.warningStatusText); } } bool RenderSceneViewportEntry( ViewportEntry& entry, IEditorContext& context, const Components::Scene* scene, const Rendering::RenderContext& renderContext, Rendering::RenderSurface& surface) { if (!EnsureSceneViewCamera()) { ApplyViewportRenderFailure( entry, renderContext, BuildSceneViewportRenderFailurePolicy( SceneViewportRenderFailure::MissingSceneViewCamera)); return false; } ApplySceneViewCameraController(); entry.statusText.clear(); if (scene == nullptr) { ApplyViewportRenderFailure( entry, renderContext, BuildSceneViewportRenderFailurePolicy( SceneViewportRenderFailure::NoActiveScene)); return false; } SceneViewportRenderState sceneState = {}; BuildSceneViewportRenderState(entry, context, *scene, sceneState); std::vector requests = m_sceneRenderer->BuildRenderRequests(*scene, m_sceneViewCamera.camera, renderContext, surface); if (requests.empty()) { ApplyViewportRenderFailure( entry, renderContext, BuildSceneViewportRenderFailurePolicy( SceneViewportRenderFailure::SceneRendererFailed)); return false; } ApplySceneViewportRenderRequestSetup( entry.renderTargets, &sceneState.builtinPostProcess, nullptr, requests[0]); requests[0].hasClearColorOverride = true; requests[0].clearColorOverride = Math::Color(0.27f, 0.27f, 0.27f, 1.0f); if (!m_sceneRenderer->Render(requests)) { ApplyViewportRenderFailure( entry, renderContext, BuildSceneViewportRenderFailurePolicy( SceneViewportRenderFailure::SceneRendererFailed)); return false; } MarkSceneViewportRenderSuccess(entry.renderTargets, requests[0]); const Core::uint32 pendingAsyncLoads = Resources::ResourceManager::Get().GetAsyncPendingCount(); if (pendingAsyncLoads > 0) { entry.statusText = "Loading scene assets... (" + std::to_string(pendingAsyncLoads) + ")"; } return true; } bool RenderGameViewportEntry( ViewportEntry& entry, const Components::Scene* scene, const Rendering::RenderContext& renderContext, const Rendering::RenderSurface& surface) { if (scene == nullptr) { ApplyViewportRenderFailure( entry, renderContext, BuildGameViewportRenderFailurePolicy( GameViewportRenderFailure::NoActiveScene)); return false; } const auto cameras = scene->FindObjectsOfType(); if (cameras.empty()) { ApplyViewportRenderFailure( entry, renderContext, BuildGameViewportRenderFailurePolicy( GameViewportRenderFailure::NoCameraInScene)); return false; } if (!m_sceneRenderer->Render(*scene, nullptr, renderContext, surface)) { ApplyViewportRenderFailure( entry, renderContext, BuildGameViewportRenderFailurePolicy( GameViewportRenderFailure::SceneRendererFailed)); return false; } MarkGameViewportRenderSuccess(entry.renderTargets); entry.statusText.clear(); return true; } void RenderViewportEntry( ViewportEntry& entry, IEditorContext& context, const Components::Scene* scene, const Rendering::RenderContext& renderContext) { if (entry.renderTargets.colorView == nullptr || entry.renderTargets.depthView == nullptr) { ApplyViewportFailureStatus( entry.statusText, BuildViewportRenderTargetUnavailablePolicy()); InvalidateViewportObjectIdFrame(entry.renderTargets); return; } Rendering::RenderSurface surface = BuildSurface(entry); if (entry.kind == EditorViewportKind::Scene) { RenderSceneViewportEntry(entry, context, scene, renderContext, surface); return; } RenderGameViewportEntry(entry, scene, renderContext, surface); } void ClearViewport( ViewportEntry& entry, const Rendering::RenderContext& renderContext, float r, float g, float b, float a) { RHI::RHICommandList* commandList = renderContext.commandList; if (commandList == nullptr) { entry.statusText = "Viewport command list is unavailable"; return; } ViewportRenderTargets& targets = entry.renderTargets; const float clearColor[4] = { r, g, b, a }; RHI::RHIResourceView* colorView = targets.colorView; commandList->TransitionBarrier( colorView, targets.colorState, RHI::ResourceStates::RenderTarget); commandList->SetRenderTargets(1, &colorView, targets.depthView); commandList->ClearRenderTarget(colorView, clearColor); commandList->ClearDepthStencil(targets.depthView, 1.0f, 0); commandList->TransitionBarrier( colorView, RHI::ResourceStates::RenderTarget, RHI::ResourceStates::PixelShaderResource); targets.colorState = RHI::ResourceStates::PixelShaderResource; targets.hasValidObjectIdFrame = false; } bool TryPickSceneViewEntityWithObjectId( ViewportEntry& entry, const ImVec2& viewportSize, const ImVec2& viewportMousePosition, uint64_t& outEntityId) { if (m_device == nullptr) { outEntityId = 0; return false; } ViewportObjectIdPickContext pickContext = {}; pickContext.commandQueue = m_sceneViewLastRenderContext.commandQueue; pickContext.texture = entry.renderTargets.objectIdTexture; pickContext.textureState = entry.renderTargets.objectIdState; pickContext.textureWidth = entry.renderTargets.width; pickContext.textureHeight = entry.renderTargets.height; pickContext.hasValidFrame = entry.renderTargets.hasValidObjectIdFrame; pickContext.viewportSize = viewportSize; pickContext.viewportMousePosition = viewportMousePosition; return TryPickViewportObjectIdEntity( pickContext, [this](const ViewportObjectIdReadbackRequest& request, std::array& outRgba) { return m_device != nullptr && m_device->ReadTexturePixelRGBA8( request.commandQueue, request.texture, request.textureState, request.pixelX, request.pixelY, outRgba); }, outEntityId); } UI::ImGuiBackendBridge* m_backend = nullptr; RHI::RHIDevice* m_device = nullptr; std::unique_ptr m_sceneRenderer; Rendering::RenderContext m_sceneViewLastRenderContext = {}; std::array m_entries = {}; SceneViewCameraState m_sceneViewCamera; }; } // namespace Editor } // namespace XCEngine