Add support for AssetPreview to use debug drawing if needed

This commit is contained in:
Wojtek Figat
2021-06-01 10:27:30 +02:00
parent 2c2c7744eb
commit 623299e59f
5 changed files with 135 additions and 53 deletions

View File

@@ -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);
}
}
}
}