From 623299e59f2670d9d413c5f88ef6d04bde3c33b7 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Tue, 1 Jun 2021 10:27:30 +0200 Subject: [PATCH] Add support for AssetPreview to use debug drawing if needed --- Source/Editor/Gizmo/EditorPrimitives.cs | 12 ++- .../Viewport/MainEditorGizmoViewport.cs | 10 +- .../Editor/Viewport/PrefabWindowViewport.cs | 64 ++++------- .../Editor/Viewport/Previews/AssetPreview.cs | 100 +++++++++++++++++- Source/Engine/Graphics/RenderTask.cpp | 2 +- 5 files changed, 135 insertions(+), 53 deletions(-) diff --git a/Source/Editor/Gizmo/EditorPrimitives.cs b/Source/Editor/Gizmo/EditorPrimitives.cs index cdb9f4747..3e37a2361 100644 --- a/Source/Editor/Gizmo/EditorPrimitives.cs +++ b/Source/Editor/Gizmo/EditorPrimitives.cs @@ -8,9 +8,8 @@ namespace FlaxEditor.Gizmo /// /// Interface for editor viewports that can contain and use . /// - /// [HideInEditor] - public interface IEditorPrimitivesOwner : IGizmoOwner + public interface IEditorPrimitivesOwner { /// /// Draws the custom editor primitives. @@ -68,11 +67,14 @@ namespace FlaxEditor.Gizmo var renderList = RenderList.GetFromPool(); var prevList = renderContext.List; renderContext.List = renderList; - for (int i = 0; i < Viewport.Gizmos.Count; i++) + try { - Viewport.Gizmos[i].Draw(ref renderContext); + Viewport.DrawEditorPrimitives(context, ref renderContext, target, targetDepth); + } + catch (Exception ex) + { + Editor.LogWarning(ex); } - Viewport.DrawEditorPrimitives(context, ref renderContext, target, targetDepth); // Sort draw calls renderList.SortDrawCalls(ref renderContext, false, DrawCallsListType.GBuffer); diff --git a/Source/Editor/Viewport/MainEditorGizmoViewport.cs b/Source/Editor/Viewport/MainEditorGizmoViewport.cs index 6202a8d6e..68667b16f 100644 --- a/Source/Editor/Viewport/MainEditorGizmoViewport.cs +++ b/Source/Editor/Viewport/MainEditorGizmoViewport.cs @@ -22,7 +22,7 @@ namespace FlaxEditor.Viewport /// Main editor gizmo viewport used by the . /// /// - public partial class MainEditorGizmoViewport : EditorGizmoViewport, IEditorPrimitivesOwner + public partial class MainEditorGizmoViewport : EditorGizmoViewport, IEditorPrimitivesOwner, IGizmoOwner { private readonly Editor _editor; @@ -463,6 +463,12 @@ namespace FlaxEditor.Viewport DebugDraw.Draw(ref renderContext, target.View(), targetDepth.View(), true); } + + // Draw gizmos + for (int i = 0; i < Gizmos.Count; i++) + { + Gizmos[i].Draw(ref renderContext); + } } private void OnPostRender(GPUContext context, RenderContext renderContext) @@ -660,7 +666,7 @@ namespace FlaxEditor.Viewport var orientation = ViewOrientation; FocusSelection(ref orientation); } - + /// /// Focuses the viewport on the current selection of the gizmo. /// diff --git a/Source/Editor/Viewport/PrefabWindowViewport.cs b/Source/Editor/Viewport/PrefabWindowViewport.cs index 946cf3620..df3057203 100644 --- a/Source/Editor/Viewport/PrefabWindowViewport.cs +++ b/Source/Editor/Viewport/PrefabWindowViewport.cs @@ -24,7 +24,7 @@ namespace FlaxEditor.Viewport /// /// /// - public class PrefabWindowViewport : PrefabPreview, IEditorPrimitivesOwner + public class PrefabWindowViewport : PrefabPreview, IGizmoOwner { [HideInEditor] private sealed class PrefabSpritesRenderer : MainEditorGizmoViewport.EditorSpritesRenderer @@ -51,7 +51,6 @@ namespace FlaxEditor.Viewport private ViewportWidgetButton _scaleSnapping; private readonly ViewportDebugDrawData _debugDrawData = new ViewportDebugDrawData(32); - private IntPtr _debugDrawContext; private PrefabSpritesRenderer _spritesRenderer; private readonly DragAssets _dragAssets; private readonly DragActorType _dragActorType = new DragActorType(ValidateDragActorType); @@ -67,16 +66,6 @@ namespace FlaxEditor.Viewport /// public SelectionOutline SelectionOutline; - /// - /// The editor primitives postFx. - /// - public EditorPrimitives EditorPrimitives; - - /// - /// Gets or sets a value indicating whether draw shapes. - /// - public bool DrawDebugDraw = true; - /// /// Initializes a new instance of the class. /// @@ -88,8 +77,9 @@ namespace FlaxEditor.Viewport _window.SelectionChanged += OnSelectionChanged; Undo = window.Undo; ViewportCamera = new FPSCamera(); - _debugDrawContext = DebugDraw.AllocateContext(); _dragAssets = new DragAssets(ValidateDragItem); + ShowDebugDraw = true; + ShowEditorPrimitives = true; // Prepare rendering task Task.ActorsSource = ActorsSources.CustomActors; @@ -102,9 +92,6 @@ namespace FlaxEditor.Viewport SelectionOutline = FlaxEngine.Object.New(); SelectionOutline.SelectionGetter = () => TransformGizmo.SelectedParents; Task.CustomPostFx.Add(SelectionOutline); - EditorPrimitives = FlaxEngine.Object.New(); - EditorPrimitives.Viewport = this; - Task.CustomPostFx.Add(EditorPrimitives); _spritesRenderer = FlaxEngine.Object.New(); _spritesRenderer.Task = Task; _spritesRenderer.Viewport = this; @@ -282,13 +269,6 @@ namespace FlaxEditor.Viewport { var task = renderContext.Task; - // Render editor primitives, gizmo and debug shapes in debug view modes - // Note: can use Output buffer as both input and output because EditorPrimitives is using a intermediate buffers - if (EditorPrimitives && EditorPrimitives.CanRender) - { - EditorPrimitives.Render(context, ref renderContext, task.Output, task.Output); - } - // Render editor sprites if (_spritesRenderer && _spritesRenderer.CanRender) { @@ -791,7 +771,7 @@ namespace FlaxEditor.Viewport var orientation = ViewOrientation; FocusSelection(ref orientation); } - + /// /// Focuses the viewport on the current selection of the gizmo. /// @@ -876,35 +856,35 @@ namespace FlaxEditor.Viewport /// public override void OnDestroy() { - if (_debugDrawContext != IntPtr.Zero) - { - DebugDraw.FreeContext(_debugDrawContext); - _debugDrawContext = IntPtr.Zero; - } FlaxEngine.Object.Destroy(ref SelectionOutline); - FlaxEngine.Object.Destroy(ref EditorPrimitives); FlaxEngine.Object.Destroy(ref _spritesRenderer); base.OnDestroy(); } /// - public void DrawEditorPrimitives(GPUContext context, ref RenderContext renderContext, GPUTexture target, GPUTexture targetDepth) + public override void DrawEditorPrimitives(GPUContext context, ref RenderContext renderContext, GPUTexture target, GPUTexture targetDepth) { - // Draw selected objects debug shapes and visuals - if (DrawDebugDraw && (renderContext.View.Flags & ViewFlags.DebugDraw) == ViewFlags.DebugDraw) + base.DrawEditorPrimitives(context, ref renderContext, target, targetDepth); + + // Draw gizmos + for (int i = 0; i < Gizmos.Count; i++) { - DebugDraw.SetContext(_debugDrawContext); - DebugDraw.UpdateContext(_debugDrawContext, 1.0f / Engine.FramesPerSecond); - unsafe + Gizmos[i].Draw(ref renderContext); + } + } + + /// + protected override void OnDebugDraw(GPUContext context, ref RenderContext renderContext) + { + base.OnDebugDraw(context, ref renderContext); + + unsafe + { + fixed (IntPtr* actors = _debugDrawData.ActorsPtrs) { - fixed (IntPtr* actors = _debugDrawData.ActorsPtrs) - { - DebugDraw.DrawActors(new IntPtr(actors), _debugDrawData.ActorsCount, false); - } + DebugDraw.DrawActors(new IntPtr(actors), _debugDrawData.ActorsCount, false); } - DebugDraw.Draw(ref renderContext, target.View(), targetDepth.View(), true); - DebugDraw.SetContext(IntPtr.Zero); } } } diff --git a/Source/Editor/Viewport/Previews/AssetPreview.cs b/Source/Editor/Viewport/Previews/AssetPreview.cs index 791e98143..f6126480c 100644 --- a/Source/Editor/Viewport/Previews/AssetPreview.cs +++ b/Source/Editor/Viewport/Previews/AssetPreview.cs @@ -1,5 +1,7 @@ // Copyright (c) 2012-2021 Wojciech Figat. All rights reserved. +using System; +using FlaxEditor.Gizmo; using FlaxEditor.GUI.ContextMenu; using FlaxEditor.Viewport.Cameras; using FlaxEngine; @@ -11,9 +13,13 @@ namespace FlaxEditor.Viewport.Previews /// Generic asset preview editor viewport base class. /// /// - public abstract class AssetPreview : EditorViewport + public abstract class AssetPreview : EditorViewport, IEditorPrimitivesOwner { private ContextMenuButton _showDefaultSceneButton; + private IntPtr _debugDrawContext; + private bool _debugDrawEnable; + private bool _editorPrimitivesEnable; + private EditorPrimitives _editorPrimitives; /// /// The preview light. Allows to modify rendering settings. @@ -61,6 +67,56 @@ namespace FlaxEditor.Viewport.Previews } } + /// + /// Gets or sets a value indicating whether draw shapes. + /// + public bool ShowDebugDraw + { + get => _debugDrawEnable; + set + { + if (_debugDrawEnable == value) + return; + _debugDrawEnable = value; + if (_debugDrawContext == IntPtr.Zero) + { + _debugDrawContext = DebugDraw.AllocateContext(); + var view = Task.View; + view.Flags |= ViewFlags.DebugDraw; + Task.View = view; + } + } + } + + /// + /// Gets or sets a value indicating whether draw shapes and other editor primitives such as gizmos. + /// + public bool ShowEditorPrimitives + { + get => _editorPrimitivesEnable; + set + { + if (_editorPrimitivesEnable == value) + return; + _editorPrimitivesEnable = value; + if (_editorPrimitives == null) + { + _editorPrimitives = Object.New(); + _editorPrimitives.Viewport = this; + Task.CustomPostFx.Add(_editorPrimitives); + Task.PostRender += OnPostRender; + var view = Task.View; + view.Flags |= ViewFlags.CustomPostProcess; + Task.View = view; + } + } + } + + /// + /// Gets the editor primitives renderer. Valid only if is true. + /// + public EditorPrimitives EditorPrimitives => _editorPrimitives; + /// /// Initializes a new instance of the class. /// @@ -114,7 +170,7 @@ namespace FlaxEditor.Viewport.Previews SunPower = 9.0f }; // - SkyLight = new SkyLight() + SkyLight = new SkyLight { Mode = SkyLight.Modes.CustomTexture, Brightness = 2.1f, @@ -135,20 +191,58 @@ namespace FlaxEditor.Viewport.Previews Task.AddCustomActor(PostFxVolume); } + private void OnPostRender(GPUContext context, RenderContext renderContext) + { + if (renderContext.View.Mode != ViewMode.Default && _editorPrimitives && _editorPrimitives.CanRender) + { + // Render editor primitives, gizmo and debug shapes in debug view modes + // Note: can use Output buffer as both input and output because EditorPrimitives is using a intermediate buffers + _editorPrimitives.Render(context, ref renderContext, renderContext.Task.Output, renderContext.Task.Output); + } + } + + /// + /// Called when drawing debug shapes with for this viewport. + /// + /// The GPU context. + /// The render context. + protected virtual void OnDebugDraw(GPUContext context, ref RenderContext renderContext) + { + } + /// public override bool HasLoadedAssets => base.HasLoadedAssets && Sky.HasContentLoaded && EnvProbe.Probe.IsLoaded && PostFxVolume.HasContentLoaded; /// public override void OnDestroy() { - // Ensure to cleanup created actor objects Object.Destroy(ref PreviewLight); Object.Destroy(ref EnvProbe); Object.Destroy(ref Sky); Object.Destroy(ref SkyLight); Object.Destroy(ref PostFxVolume); + Object.Destroy(ref _editorPrimitives); + if (_debugDrawContext != IntPtr.Zero) + { + DebugDraw.FreeContext(_debugDrawContext); + _debugDrawContext = IntPtr.Zero; + } base.OnDestroy(); } + + /// + public virtual void DrawEditorPrimitives(GPUContext context, ref RenderContext renderContext, GPUTexture target, GPUTexture targetDepth) + { + // Draw selected objects debug shapes and visuals + if (ShowDebugDraw && (renderContext.View.Flags & ViewFlags.DebugDraw) == ViewFlags.DebugDraw) + { + DebugDraw.SetContext(_debugDrawContext); + DebugDraw.UpdateContext(_debugDrawContext, 1.0f / Mathf.Max(Engine.FramesPerSecond, 1)); + OnDebugDraw(context, ref renderContext); + DebugDraw.Draw(ref renderContext, target.View(), targetDepth.View(), true); + DebugDraw.SetContext(IntPtr.Zero); + } + } } } diff --git a/Source/Engine/Graphics/RenderTask.cpp b/Source/Engine/Graphics/RenderTask.cpp index b4da776d7..f3e73c220 100644 --- a/Source/Engine/Graphics/RenderTask.cpp +++ b/Source/Engine/Graphics/RenderTask.cpp @@ -267,7 +267,7 @@ void SceneRenderTask::OnCollectDrawCalls(RenderContext& renderContext) { for (int32 i = 0; i < CustomActors.Count(); i++) { - if (CustomActors[i]->GetIsActive()) + if (CustomActors[i] && CustomActors[i]->GetIsActive()) CustomActors[i]->DrawHierarchy(renderContext); } }