Add support for AssetPreview to use debug drawing if needed
This commit is contained in:
@@ -8,9 +8,8 @@ namespace FlaxEditor.Gizmo
|
||||
/// <summary>
|
||||
/// Interface for editor viewports that can contain and use <see cref="EditorPrimitives"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="FlaxEditor.Gizmo.IGizmoOwner" />
|
||||
[HideInEditor]
|
||||
public interface IEditorPrimitivesOwner : IGizmoOwner
|
||||
public interface IEditorPrimitivesOwner
|
||||
{
|
||||
/// <summary>
|
||||
/// 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);
|
||||
}
|
||||
|
||||
// Sort draw calls
|
||||
renderList.SortDrawCalls(ref renderContext, false, DrawCallsListType.GBuffer);
|
||||
|
||||
@@ -22,7 +22,7 @@ namespace FlaxEditor.Viewport
|
||||
/// Main editor gizmo viewport used by the <see cref="EditGameWindow"/>.
|
||||
/// </summary>
|
||||
/// <seealso cref="FlaxEditor.Viewport.EditorGizmoViewport" />
|
||||
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)
|
||||
|
||||
@@ -24,7 +24,7 @@ namespace FlaxEditor.Viewport
|
||||
/// <seealso cref="PrefabWindow" />
|
||||
/// <seealso cref="PrefabPreview" />
|
||||
/// <seealso cref="IGizmoOwner" />
|
||||
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
|
||||
/// </summary>
|
||||
public SelectionOutline SelectionOutline;
|
||||
|
||||
/// <summary>
|
||||
/// The editor primitives postFx.
|
||||
/// </summary>
|
||||
public EditorPrimitives EditorPrimitives;
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether draw <see cref="DebugDraw"/> shapes.
|
||||
/// </summary>
|
||||
public bool DrawDebugDraw = true;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PrefabWindowViewport"/> class.
|
||||
/// </summary>
|
||||
@@ -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>();
|
||||
SelectionOutline.SelectionGetter = () => TransformGizmo.SelectedParents;
|
||||
Task.CustomPostFx.Add(SelectionOutline);
|
||||
EditorPrimitives = FlaxEngine.Object.New<EditorPrimitives>();
|
||||
EditorPrimitives.Viewport = this;
|
||||
Task.CustomPostFx.Add(EditorPrimitives);
|
||||
_spritesRenderer = FlaxEngine.Object.New<PrefabSpritesRenderer>();
|
||||
_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)
|
||||
{
|
||||
@@ -876,26 +856,29 @@ namespace FlaxEditor.Viewport
|
||||
/// <inheritdoc />
|
||||
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();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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);
|
||||
Gizmos[i].Draw(ref renderContext);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override void OnDebugDraw(GPUContext context, ref RenderContext renderContext)
|
||||
{
|
||||
base.OnDebugDraw(context, ref renderContext);
|
||||
|
||||
unsafe
|
||||
{
|
||||
fixed (IntPtr* actors = _debugDrawData.ActorsPtrs)
|
||||
@@ -903,9 +886,6 @@ namespace FlaxEditor.Viewport
|
||||
DebugDraw.DrawActors(new IntPtr(actors), _debugDrawData.ActorsCount, false);
|
||||
}
|
||||
}
|
||||
DebugDraw.Draw(ref renderContext, target.View(), targetDepth.View(), true);
|
||||
DebugDraw.SetContext(IntPtr.Zero);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
/// </summary>
|
||||
/// <seealso cref="FlaxEditor.Viewport.EditorViewport" />
|
||||
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;
|
||||
|
||||
/// <summary>
|
||||
/// The preview light. Allows to modify rendering settings.
|
||||
@@ -61,6 +67,56 @@ namespace FlaxEditor.Viewport.Previews
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether draw <see cref="DebugDraw"/> shapes.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether draw <see cref="DebugDraw"/> shapes and other editor primitives such as gizmos.
|
||||
/// </summary>
|
||||
public bool ShowEditorPrimitives
|
||||
{
|
||||
get => _editorPrimitivesEnable;
|
||||
set
|
||||
{
|
||||
if (_editorPrimitivesEnable == value)
|
||||
return;
|
||||
_editorPrimitivesEnable = value;
|
||||
if (_editorPrimitives == null)
|
||||
{
|
||||
_editorPrimitives = Object.New<EditorPrimitives>();
|
||||
_editorPrimitives.Viewport = this;
|
||||
Task.CustomPostFx.Add(_editorPrimitives);
|
||||
Task.PostRender += OnPostRender;
|
||||
var view = Task.View;
|
||||
view.Flags |= ViewFlags.CustomPostProcess;
|
||||
Task.View = view;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the editor primitives renderer. Valid only if <see cref="ShowEditorPrimitives"/> is true.
|
||||
/// </summary>
|
||||
public EditorPrimitives EditorPrimitives => _editorPrimitives;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AssetPreview"/> class.
|
||||
/// </summary>
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Called when drawing debug shapes with <see cref="DebugDraw"/> for this viewport.
|
||||
/// </summary>
|
||||
/// <param name="context">The GPU context.</param>
|
||||
/// <param name="renderContext">The render context.</param>
|
||||
protected virtual void OnDebugDraw(GPUContext context, ref RenderContext renderContext)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool HasLoadedAssets => base.HasLoadedAssets && Sky.HasContentLoaded && EnvProbe.Probe.IsLoaded && PostFxVolume.HasContentLoaded;
|
||||
|
||||
/// <inheritdoc />
|
||||
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();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user