Refactor animated model previews to unify features used by various windows
This commit is contained in:
@@ -3,6 +3,7 @@
|
|||||||
using FlaxEditor.GUI.ContextMenu;
|
using FlaxEditor.GUI.ContextMenu;
|
||||||
using FlaxEngine;
|
using FlaxEngine;
|
||||||
using FlaxEditor.GUI.Input;
|
using FlaxEditor.GUI.Input;
|
||||||
|
using FlaxEngine.GUI;
|
||||||
using Object = FlaxEngine.Object;
|
using Object = FlaxEngine.Object;
|
||||||
|
|
||||||
namespace FlaxEditor.Viewport.Previews
|
namespace FlaxEditor.Viewport.Previews
|
||||||
@@ -13,9 +14,12 @@ namespace FlaxEditor.Viewport.Previews
|
|||||||
/// <seealso cref="AssetPreview" />
|
/// <seealso cref="AssetPreview" />
|
||||||
public class AnimatedModelPreview : AssetPreview
|
public class AnimatedModelPreview : AssetPreview
|
||||||
{
|
{
|
||||||
private ContextMenuButton _showBoundsButton;
|
private ContextMenuButton _showNodesButton, _showBoundsButton, _showFloorButton;
|
||||||
|
private bool _showNodes, _showBounds, _showFloor;
|
||||||
private AnimatedModel _previewModel;
|
private AnimatedModel _previewModel;
|
||||||
private bool _showNodes, _showBounds;
|
private StaticModel _floorModel;
|
||||||
|
private ContextMenuButton _showCurrentLODButton;
|
||||||
|
private bool _showCurrentLOD;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the skinned model asset to preview.
|
/// Gets or sets the skinned model asset to preview.
|
||||||
@@ -47,6 +51,8 @@ namespace FlaxEditor.Viewport.Previews
|
|||||||
_showNodes = value;
|
_showNodes = value;
|
||||||
if (value)
|
if (value)
|
||||||
ShowDebugDraw = true;
|
ShowDebugDraw = true;
|
||||||
|
if (_showNodesButton != null)
|
||||||
|
_showNodesButton.Checked = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -66,6 +72,33 @@ namespace FlaxEditor.Viewport.Previews
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether show floor model.
|
||||||
|
/// </summary>
|
||||||
|
public bool ShowFloor
|
||||||
|
{
|
||||||
|
get => _showFloor;
|
||||||
|
set
|
||||||
|
{
|
||||||
|
_showFloor = value;
|
||||||
|
if (value && !_floorModel)
|
||||||
|
{
|
||||||
|
_floorModel = new StaticModel
|
||||||
|
{
|
||||||
|
Position = new Vector3(0, -25, 0),
|
||||||
|
Scale = new Vector3(5, 0.5f, 5),
|
||||||
|
Model = FlaxEngine.Content.LoadAsync<Model>(StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Primitives/Cube.flax")),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if (value)
|
||||||
|
Task.AddCustomActor(_floorModel);
|
||||||
|
else
|
||||||
|
Task.RemoveCustomActor(_floorModel);
|
||||||
|
if (_showFloorButton != null)
|
||||||
|
_showFloorButton.Checked = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a value indicating whether scale the model to the normalized bounds.
|
/// Gets or sets a value indicating whether scale the model to the normalized bounds.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -93,8 +126,6 @@ namespace FlaxEditor.Viewport.Previews
|
|||||||
//_previewModel.BoundsScale = 1000.0f;
|
//_previewModel.BoundsScale = 1000.0f;
|
||||||
UpdateMode = AnimatedModel.AnimationUpdateMode.Manual
|
UpdateMode = AnimatedModel.AnimationUpdateMode.Manual
|
||||||
};
|
};
|
||||||
|
|
||||||
// Link actors for rendering
|
|
||||||
Task.AddCustomActor(_previewModel);
|
Task.AddCustomActor(_previewModel);
|
||||||
|
|
||||||
if (useWidgets)
|
if (useWidgets)
|
||||||
@@ -102,6 +133,21 @@ namespace FlaxEditor.Viewport.Previews
|
|||||||
// Show Bounds
|
// Show Bounds
|
||||||
_showBoundsButton = ViewWidgetShowMenu.AddButton("Bounds", () => ShowBounds = !ShowBounds);
|
_showBoundsButton = ViewWidgetShowMenu.AddButton("Bounds", () => ShowBounds = !ShowBounds);
|
||||||
|
|
||||||
|
// Show Skeleton
|
||||||
|
_showNodesButton = ViewWidgetShowMenu.AddButton("Skeleton", () => ShowNodes = !ShowNodes);
|
||||||
|
|
||||||
|
// Show Floor
|
||||||
|
_showFloorButton = ViewWidgetShowMenu.AddButton("Floor", button => ShowFloor = !ShowFloor);
|
||||||
|
_showFloorButton.IndexInParent = 1;
|
||||||
|
|
||||||
|
// Show Current LOD
|
||||||
|
_showCurrentLODButton = ViewWidgetShowMenu.AddButton("Current LOD", button =>
|
||||||
|
{
|
||||||
|
_showCurrentLOD = !_showCurrentLOD;
|
||||||
|
_showCurrentLODButton.Icon = _showCurrentLOD ? Style.Current.CheckBoxTick : SpriteHandle.Invalid;
|
||||||
|
});
|
||||||
|
_showCurrentLODButton.IndexInParent = 2;
|
||||||
|
|
||||||
// Preview LOD
|
// Preview LOD
|
||||||
{
|
{
|
||||||
var previewLOD = ViewWidgetButtonMenu.AddButton("Preview LOD");
|
var previewLOD = ViewWidgetButtonMenu.AddButton("Preview LOD");
|
||||||
@@ -113,6 +159,12 @@ namespace FlaxEditor.Viewport.Previews
|
|||||||
ViewWidgetButtonMenu.VisibleChanged += control => previewLODValue.Value = _previewModel.ForcedLOD;
|
ViewWidgetButtonMenu.VisibleChanged += control => previewLODValue.Value = _previewModel.ForcedLOD;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enable shadows
|
||||||
|
PreviewLight.ShadowsMode = ShadowsCastingMode.All;
|
||||||
|
PreviewLight.CascadeCount = 3;
|
||||||
|
PreviewLight.ShadowsDistance = 2000.0f;
|
||||||
|
Task.ViewFlags |= ViewFlags.Shadows;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnBegin(RenderTask task, GPUContext context)
|
private void OnBegin(RenderTask task, GPUContext context)
|
||||||
@@ -137,6 +189,44 @@ namespace FlaxEditor.Viewport.Previews
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int ComputeLODIndex(SkinnedModel model)
|
||||||
|
{
|
||||||
|
if (PreviewActor.ForcedLOD != -1)
|
||||||
|
return PreviewActor.ForcedLOD;
|
||||||
|
|
||||||
|
// Based on RenderTools::ComputeModelLOD
|
||||||
|
CreateProjectionMatrix(out var projectionMatrix);
|
||||||
|
float screenMultiple = 0.5f * Mathf.Max(projectionMatrix.M11, projectionMatrix.M22);
|
||||||
|
var sphere = PreviewActor.Sphere;
|
||||||
|
var viewOrigin = ViewPosition;
|
||||||
|
float distSqr = Vector3.DistanceSquared(ref sphere.Center, ref viewOrigin);
|
||||||
|
var screenRadiusSquared = Mathf.Square(screenMultiple * sphere.Radius) / Mathf.Max(1.0f, distSqr);
|
||||||
|
|
||||||
|
// Check if model is being culled
|
||||||
|
if (Mathf.Square(model.MinScreenSize * 0.5f) > screenRadiusSquared)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
// Skip if no need to calculate LOD
|
||||||
|
if (model.LoadedLODs == 0)
|
||||||
|
return -1;
|
||||||
|
var lods = model.LODs;
|
||||||
|
if (lods.Length == 0)
|
||||||
|
return -1;
|
||||||
|
if (lods.Length == 1)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
// Iterate backwards and return the first matching LOD
|
||||||
|
for (int lodIndex = lods.Length - 1; lodIndex >= 0; lodIndex--)
|
||||||
|
{
|
||||||
|
if (Mathf.Square(lods[lodIndex].ScreenSize * 0.5f) >= screenRadiusSquared)
|
||||||
|
{
|
||||||
|
return lodIndex + PreviewActor.LODBias;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
protected override void OnDebugDraw(GPUContext context, ref RenderContext renderContext)
|
protected override void OnDebugDraw(GPUContext context, ref RenderContext renderContext)
|
||||||
{
|
{
|
||||||
@@ -186,6 +276,37 @@ namespace FlaxEditor.Viewport.Previews
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void Draw()
|
||||||
|
{
|
||||||
|
base.Draw();
|
||||||
|
|
||||||
|
var skinnedModel = _previewModel.SkinnedModel;
|
||||||
|
if (_showCurrentLOD && skinnedModel)
|
||||||
|
{
|
||||||
|
var lodIndex = ComputeLODIndex(skinnedModel);
|
||||||
|
string text = string.Format("Current LOD: {0}", lodIndex);
|
||||||
|
if (lodIndex != -1)
|
||||||
|
{
|
||||||
|
var lods = skinnedModel.LODs;
|
||||||
|
lodIndex = Mathf.Clamp(lodIndex + PreviewActor.LODBias, 0, lods.Length - 1);
|
||||||
|
var lod = lods[lodIndex];
|
||||||
|
int triangleCount = 0, vertexCount = 0;
|
||||||
|
for (int meshIndex = 0; meshIndex < lod.Meshes.Length; meshIndex++)
|
||||||
|
{
|
||||||
|
var mesh = lod.Meshes[meshIndex];
|
||||||
|
triangleCount += mesh.TriangleCount;
|
||||||
|
vertexCount += mesh.VertexCount;
|
||||||
|
}
|
||||||
|
text += string.Format("\nTriangles: {0:N0}\nVertices: {1:N0}", triangleCount, vertexCount);
|
||||||
|
}
|
||||||
|
var font = Style.Current.FontMedium;
|
||||||
|
var pos = new Vector2(10, 50);
|
||||||
|
Render2D.DrawText(font, text, new Rectangle(pos + Vector2.One, Size), Color.Black);
|
||||||
|
Render2D.DrawText(font, text, new Rectangle(pos, Size), Color.White);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Update(float deltaTime)
|
public override void Update(float deltaTime)
|
||||||
{
|
{
|
||||||
@@ -220,8 +341,13 @@ namespace FlaxEditor.Viewport.Previews
|
|||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void OnDestroy()
|
public override void OnDestroy()
|
||||||
{
|
{
|
||||||
|
Object.Destroy(ref _floorModel);
|
||||||
Object.Destroy(ref _previewModel);
|
Object.Destroy(ref _previewModel);
|
||||||
NodesMask = null;
|
NodesMask = null;
|
||||||
|
_showNodesButton = null;
|
||||||
|
_showBoundsButton = null;
|
||||||
|
_showFloorButton = null;
|
||||||
|
_showCurrentLODButton = null;
|
||||||
|
|
||||||
base.OnDestroy();
|
base.OnDestroy();
|
||||||
}
|
}
|
||||||
|
|||||||
105
Source/Editor/Viewport/Previews/AnimationPreview.cs
Normal file
105
Source/Editor/Viewport/Previews/AnimationPreview.cs
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
// Copyright (c) 2012-2021 Wojciech Figat. All rights reserved.
|
||||||
|
|
||||||
|
using FlaxEditor.GUI.Input;
|
||||||
|
using FlaxEngine;
|
||||||
|
using FlaxEditor.Viewport.Widgets;
|
||||||
|
using FlaxEngine.GUI;
|
||||||
|
|
||||||
|
namespace FlaxEditor.Viewport.Previews
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Animation asset preview editor viewport.
|
||||||
|
/// </summary>
|
||||||
|
/// <seealso cref="AnimatedModelPreview" />
|
||||||
|
public class AnimationPreview : AnimatedModelPreview
|
||||||
|
{
|
||||||
|
private ViewportWidgetButton _playPauseButton;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="AnimationPreview"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="useWidgets">if set to <c>true</c> use widgets.</param>
|
||||||
|
public AnimationPreview(bool useWidgets)
|
||||||
|
: base(useWidgets)
|
||||||
|
{
|
||||||
|
PlayAnimation = true;
|
||||||
|
|
||||||
|
// Playback Speed
|
||||||
|
{
|
||||||
|
var playbackSpeed = ViewWidgetButtonMenu.AddButton("Playback Speed");
|
||||||
|
var playbackSpeedValue = new FloatValueBox(-1, 90, 2, 70.0f, 0.0f, 10000.0f, 0.001f)
|
||||||
|
{
|
||||||
|
Parent = playbackSpeed
|
||||||
|
};
|
||||||
|
playbackSpeedValue.ValueChanged += () => PreviewActor.UpdateSpeed = playbackSpeedValue.Value;
|
||||||
|
ViewWidgetButtonMenu.VisibleChanged += control => playbackSpeedValue.Value = PreviewActor.UpdateSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Play/Pause widget
|
||||||
|
{
|
||||||
|
var playPauseWidget = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight);
|
||||||
|
_playPauseButton = new ViewportWidgetButton(null, Editor.Instance.Icons.Pause64)
|
||||||
|
{
|
||||||
|
TooltipText = "Animation playback play (F5) or pause (F6)",
|
||||||
|
Parent = playPauseWidget,
|
||||||
|
};
|
||||||
|
_playPauseButton.Clicked += button =>
|
||||||
|
{
|
||||||
|
PlayAnimation = !PlayAnimation;
|
||||||
|
button.Icon = PlayAnimation ? Editor.Instance.Icons.Pause64 : Editor.Instance.Icons.Play64;
|
||||||
|
};
|
||||||
|
playPauseWidget.Parent = this;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Enable shadows
|
||||||
|
PreviewLight.ShadowsMode = ShadowsCastingMode.All;
|
||||||
|
PreviewLight.CascadeCount = 2;
|
||||||
|
PreviewLight.ShadowsDistance = 1000.0f;
|
||||||
|
Task.ViewFlags |= ViewFlags.Shadows;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void Draw()
|
||||||
|
{
|
||||||
|
base.Draw();
|
||||||
|
|
||||||
|
var style = Style.Current;
|
||||||
|
var skinnedModel = SkinnedModel;
|
||||||
|
if (skinnedModel == null)
|
||||||
|
{
|
||||||
|
Render2D.DrawText(style.FontLarge, "Missing Base Model", new Rectangle(Vector2.Zero, Size), Color.Red, TextAlignment.Center, TextAlignment.Center, TextWrapping.WrapWords);
|
||||||
|
}
|
||||||
|
else if (!skinnedModel.IsLoaded)
|
||||||
|
{
|
||||||
|
Render2D.DrawText(style.FontLarge, "Loading...", new Rectangle(Vector2.Zero, Size), style.ForegroundDisabled, TextAlignment.Center, TextAlignment.Center);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override bool OnKeyDown(KeyboardKeys key)
|
||||||
|
{
|
||||||
|
var inputOptions = Editor.Instance.Options.Options.Input;
|
||||||
|
if (inputOptions.Play.Process(this, key))
|
||||||
|
{
|
||||||
|
PlayAnimation = true;
|
||||||
|
_playPauseButton.Icon = PlayAnimation ? Editor.Instance.Icons.Pause64 : Editor.Instance.Icons.Play64;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
if (inputOptions.Pause.Process(this, key))
|
||||||
|
{
|
||||||
|
PlayAnimation = false;
|
||||||
|
_playPauseButton.Icon = PlayAnimation ? Editor.Instance.Icons.Pause64 : Editor.Instance.Icons.Play64;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return base.OnKeyDown(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
public override void OnDestroy()
|
||||||
|
{
|
||||||
|
_playPauseButton = null;
|
||||||
|
|
||||||
|
base.OnDestroy();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -7,13 +7,10 @@ using FlaxEditor.Content;
|
|||||||
using FlaxEditor.CustomEditors;
|
using FlaxEditor.CustomEditors;
|
||||||
using FlaxEditor.CustomEditors.Editors;
|
using FlaxEditor.CustomEditors.Editors;
|
||||||
using FlaxEditor.GUI;
|
using FlaxEditor.GUI;
|
||||||
using FlaxEditor.GUI.ContextMenu;
|
|
||||||
using FlaxEditor.GUI.Input;
|
|
||||||
using FlaxEditor.Scripting;
|
using FlaxEditor.Scripting;
|
||||||
using FlaxEditor.Surface;
|
using FlaxEditor.Surface;
|
||||||
using FlaxEditor.Viewport.Cameras;
|
using FlaxEditor.Viewport.Cameras;
|
||||||
using FlaxEditor.Viewport.Previews;
|
using FlaxEditor.Viewport.Previews;
|
||||||
using FlaxEditor.Viewport.Widgets;
|
|
||||||
using FlaxEngine;
|
using FlaxEngine;
|
||||||
using FlaxEngine.GUI;
|
using FlaxEngine.GUI;
|
||||||
using Object = FlaxEngine.Object;
|
using Object = FlaxEngine.Object;
|
||||||
@@ -49,70 +46,15 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
new ScriptType(typeof(Matrix)),
|
new ScriptType(typeof(Matrix)),
|
||||||
};
|
};
|
||||||
|
|
||||||
private sealed class AnimationGraphPreview : AnimatedModelPreview
|
private sealed class AnimationGraphPreview : AnimationPreview
|
||||||
{
|
{
|
||||||
private readonly AnimationGraphWindow _window;
|
private readonly AnimationGraphWindow _window;
|
||||||
private ContextMenuButton _showFloorButton;
|
|
||||||
private ViewportWidgetButton _playPauseButton;
|
|
||||||
private StaticModel _floorModel;
|
|
||||||
|
|
||||||
public AnimationGraphPreview(AnimationGraphWindow window)
|
public AnimationGraphPreview(AnimationGraphWindow window)
|
||||||
: base(true)
|
: base(true)
|
||||||
{
|
{
|
||||||
_window = window;
|
_window = window;
|
||||||
|
ShowFloor = true;
|
||||||
// Show floor widget
|
|
||||||
_showFloorButton = ViewWidgetShowMenu.AddButton("Floor", OnShowFloorModelClicked);
|
|
||||||
_showFloorButton.Icon = Style.Current.CheckBoxTick;
|
|
||||||
_showFloorButton.IndexInParent = 1;
|
|
||||||
|
|
||||||
// Playback Speed
|
|
||||||
{
|
|
||||||
var playbackSpeed = ViewWidgetButtonMenu.AddButton("Playback Speed");
|
|
||||||
var playbackSpeedValue = new FloatValueBox(-1, 90, 2, 70.0f, 0.0f, 10000.0f, 0.001f)
|
|
||||||
{
|
|
||||||
Parent = playbackSpeed
|
|
||||||
};
|
|
||||||
playbackSpeedValue.ValueChanged += () => PreviewActor.UpdateSpeed = playbackSpeedValue.Value;
|
|
||||||
ViewWidgetButtonMenu.VisibleChanged += control => playbackSpeedValue.Value = PreviewActor.UpdateSpeed;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Play/Pause widget
|
|
||||||
{
|
|
||||||
var playPauseWidget = new ViewportWidgetsContainer(ViewportWidgetLocation.UpperRight);
|
|
||||||
_playPauseButton = new ViewportWidgetButton(null, Editor.Instance.Icons.Pause64)
|
|
||||||
{
|
|
||||||
TooltipText = "Animation playback play (F5) or pause (F6)",
|
|
||||||
Parent = playPauseWidget,
|
|
||||||
};
|
|
||||||
_playPauseButton.Clicked += button =>
|
|
||||||
{
|
|
||||||
PlayAnimation = !PlayAnimation;
|
|
||||||
button.Icon = PlayAnimation ? Editor.Instance.Icons.Pause64 : Editor.Instance.Icons.Play64;
|
|
||||||
};
|
|
||||||
playPauseWidget.Parent = this;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Floor model
|
|
||||||
_floorModel = new StaticModel
|
|
||||||
{
|
|
||||||
Position = new Vector3(0, -25, 0),
|
|
||||||
Scale = new Vector3(5, 0.5f, 5),
|
|
||||||
Model = FlaxEngine.Content.LoadAsync<Model>(StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Primitives/Cube.flax"))
|
|
||||||
};
|
|
||||||
Task.AddCustomActor(_floorModel);
|
|
||||||
|
|
||||||
// Enable shadows
|
|
||||||
PreviewLight.ShadowsMode = ShadowsCastingMode.All;
|
|
||||||
PreviewLight.CascadeCount = 2;
|
|
||||||
PreviewLight.ShadowsDistance = 1000.0f;
|
|
||||||
Task.ViewFlags |= ViewFlags.Shadows;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void OnShowFloorModelClicked(ContextMenuButton obj)
|
|
||||||
{
|
|
||||||
_floorModel.IsActive = !_floorModel.IsActive;
|
|
||||||
_showFloorButton.Icon = _floorModel.IsActive ? Style.Current.CheckBoxTick : SpriteHandle.Invalid;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -125,39 +67,6 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
{
|
{
|
||||||
Render2D.DrawText(style.FontLarge, "Loading...", new Rectangle(Vector2.Zero, Size), style.ForegroundDisabled, TextAlignment.Center, TextAlignment.Center);
|
Render2D.DrawText(style.FontLarge, "Loading...", new Rectangle(Vector2.Zero, Size), style.ForegroundDisabled, TextAlignment.Center, TextAlignment.Center);
|
||||||
}
|
}
|
||||||
if (_window._properties.BaseModel == null)
|
|
||||||
{
|
|
||||||
Render2D.DrawText(style.FontLarge, "Missing Base Model", new Rectangle(Vector2.Zero, Size), Color.Red, TextAlignment.Center, TextAlignment.Center, TextWrapping.WrapWords);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override bool OnKeyDown(KeyboardKeys key)
|
|
||||||
{
|
|
||||||
var inputOptions = Editor.Instance.Options.Options.Input;
|
|
||||||
if (inputOptions.Play.Process(this, key))
|
|
||||||
{
|
|
||||||
PlayAnimation = true;
|
|
||||||
_playPauseButton.Icon = PlayAnimation ? Editor.Instance.Icons.Pause64 : Editor.Instance.Icons.Play64;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (inputOptions.Pause.Process(this, key))
|
|
||||||
{
|
|
||||||
PlayAnimation = false;
|
|
||||||
_playPauseButton.Icon = PlayAnimation ? Editor.Instance.Icons.Pause64 : Editor.Instance.Icons.Play64;
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
return base.OnKeyDown(key);
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override void OnDestroy()
|
|
||||||
{
|
|
||||||
Object.Destroy(ref _floorModel);
|
|
||||||
_playPauseButton = null;
|
|
||||||
_showFloorButton = null;
|
|
||||||
|
|
||||||
base.OnDestroy();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -268,6 +177,7 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
private FlaxObjectRefPickerControl _debugPicker;
|
private FlaxObjectRefPickerControl _debugPicker;
|
||||||
private NavigationBar _navigationBar;
|
private NavigationBar _navigationBar;
|
||||||
private PropertiesProxy _properties;
|
private PropertiesProxy _properties;
|
||||||
|
private ToolStripButton _showNodesButton;
|
||||||
private Tab _previewTab;
|
private Tab _previewTab;
|
||||||
private readonly List<AnimGraphDebugFlowInfo> _debugFlows = new List<AnimGraphDebugFlowInfo>();
|
private readonly List<AnimGraphDebugFlowInfo> _debugFlows = new List<AnimGraphDebugFlowInfo>();
|
||||||
|
|
||||||
@@ -310,7 +220,7 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
_surface.ContextChanged += OnSurfaceContextChanged;
|
_surface.ContextChanged += OnSurfaceContextChanged;
|
||||||
|
|
||||||
// Toolstrip
|
// Toolstrip
|
||||||
_toolstrip.AddButton(editor.Icons.Bone64, () => _preview.ShowNodes = !_preview.ShowNodes).SetAutoCheck(true).LinkTooltip("Show animated model nodes debug view");
|
_showNodesButton = (ToolStripButton)_toolstrip.AddButton(editor.Icons.Bone64, () => _preview.ShowNodes = !_preview.ShowNodes).LinkTooltip("Show animated model nodes debug view");
|
||||||
_toolstrip.AddSeparator();
|
_toolstrip.AddSeparator();
|
||||||
_toolstrip.AddButton(editor.Icons.Docs64, () => Platform.OpenUrl(Utilities.Constants.DocsUrl + "manual/animation/anim-graph/index.html")).LinkTooltip("See documentation to learn more");
|
_toolstrip.AddButton(editor.Icons.Docs64, () => Platform.OpenUrl(Utilities.Constants.DocsUrl + "manual/animation/anim-graph/index.html")).LinkTooltip("See documentation to learn more");
|
||||||
|
|
||||||
@@ -512,6 +422,8 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
}
|
}
|
||||||
_debugFlows.Clear();
|
_debugFlows.Clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_showNodesButton.Checked = _preview.ShowNodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -522,6 +434,8 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
_properties = null;
|
_properties = null;
|
||||||
_navigationBar = null;
|
_navigationBar = null;
|
||||||
_debugPicker = null;
|
_debugPicker = null;
|
||||||
|
_showNodesButton = null;
|
||||||
|
_previewTab = null;
|
||||||
|
|
||||||
base.OnDestroy();
|
base.OnDestroy();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ using FlaxEditor.CustomEditors;
|
|||||||
using FlaxEditor.CustomEditors.Elements;
|
using FlaxEditor.CustomEditors.Elements;
|
||||||
using FlaxEditor.CustomEditors.GUI;
|
using FlaxEditor.CustomEditors.GUI;
|
||||||
using FlaxEditor.GUI;
|
using FlaxEditor.GUI;
|
||||||
using FlaxEditor.GUI.ContextMenu;
|
|
||||||
using FlaxEditor.Scripting;
|
using FlaxEditor.Scripting;
|
||||||
using FlaxEditor.Viewport.Cameras;
|
using FlaxEditor.Viewport.Cameras;
|
||||||
using FlaxEditor.Viewport.Previews;
|
using FlaxEditor.Viewport.Previews;
|
||||||
@@ -31,92 +30,17 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
private sealed class Preview : AnimatedModelPreview
|
private sealed class Preview : AnimatedModelPreview
|
||||||
{
|
{
|
||||||
private readonly SkinnedModelWindow _window;
|
private readonly SkinnedModelWindow _window;
|
||||||
private ContextMenuButton _showFloorButton;
|
|
||||||
private ContextMenuButton _showCurrentLODButton;
|
|
||||||
private StaticModel _floorModel;
|
|
||||||
private bool _showCurrentLOD;
|
|
||||||
|
|
||||||
public Preview(SkinnedModelWindow window)
|
public Preview(SkinnedModelWindow window)
|
||||||
: base(true)
|
: base(true)
|
||||||
{
|
{
|
||||||
_window = window;
|
_window = window;
|
||||||
PlayAnimation = true;
|
|
||||||
|
|
||||||
// Show floor widget
|
|
||||||
_showFloorButton = ViewWidgetShowMenu.AddButton("Floor", button =>
|
|
||||||
{
|
|
||||||
_floorModel.IsActive = !_floorModel.IsActive;
|
|
||||||
_showFloorButton.Icon = _floorModel.IsActive ? Style.Current.CheckBoxTick : SpriteHandle.Invalid;
|
|
||||||
});
|
|
||||||
_showFloorButton.IndexInParent = 1;
|
|
||||||
|
|
||||||
// Show current LOD widget
|
|
||||||
_showCurrentLODButton = ViewWidgetShowMenu.AddButton("Current LOD", button =>
|
|
||||||
{
|
|
||||||
_showCurrentLOD = !_showCurrentLOD;
|
|
||||||
_showCurrentLODButton.Icon = _showCurrentLOD ? Style.Current.CheckBoxTick : SpriteHandle.Invalid;
|
|
||||||
});
|
|
||||||
_showCurrentLODButton.IndexInParent = 2;
|
|
||||||
|
|
||||||
// Floor model
|
|
||||||
_floorModel = new StaticModel
|
|
||||||
{
|
|
||||||
Position = new Vector3(0, -25, 0),
|
|
||||||
Scale = new Vector3(5, 0.5f, 5),
|
|
||||||
Model = FlaxEngine.Content.LoadAsync<Model>(StringUtils.CombinePaths(Globals.EngineContentFolder, "Editor/Primitives/Cube.flax")),
|
|
||||||
IsActive = false
|
|
||||||
};
|
|
||||||
Task.AddCustomActor(_floorModel);
|
|
||||||
|
|
||||||
// Enable shadows
|
|
||||||
PreviewLight.ShadowsMode = ShadowsCastingMode.All;
|
|
||||||
PreviewLight.CascadeCount = 3;
|
|
||||||
PreviewLight.ShadowsDistance = 2000.0f;
|
|
||||||
Task.ViewFlags |= ViewFlags.Shadows;
|
|
||||||
}
|
|
||||||
|
|
||||||
private int ComputeLODIndex(SkinnedModel model)
|
|
||||||
{
|
|
||||||
if (PreviewActor.ForcedLOD != -1)
|
|
||||||
return PreviewActor.ForcedLOD;
|
|
||||||
|
|
||||||
// Based on RenderTools::ComputeModelLOD
|
|
||||||
CreateProjectionMatrix(out var projectionMatrix);
|
|
||||||
float screenMultiple = 0.5f * Mathf.Max(projectionMatrix.M11, projectionMatrix.M22);
|
|
||||||
var sphere = PreviewActor.Sphere;
|
|
||||||
var viewOrigin = ViewPosition;
|
|
||||||
float distSqr = Vector3.DistanceSquared(ref sphere.Center, ref viewOrigin);
|
|
||||||
var screenRadiusSquared = Mathf.Square(screenMultiple * sphere.Radius) / Mathf.Max(1.0f, distSqr);
|
|
||||||
|
|
||||||
// Check if model is being culled
|
|
||||||
if (Mathf.Square(model.MinScreenSize * 0.5f) > screenRadiusSquared)
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
// Skip if no need to calculate LOD
|
|
||||||
if (model.LoadedLODs == 0)
|
|
||||||
return -1;
|
|
||||||
var lods = model.LODs;
|
|
||||||
if (lods.Length == 0)
|
|
||||||
return -1;
|
|
||||||
if (lods.Length == 1)
|
|
||||||
return 0;
|
|
||||||
|
|
||||||
// Iterate backwards and return the first matching LOD
|
|
||||||
for (int lodIndex = lods.Length - 1; lodIndex >= 0; lodIndex--)
|
|
||||||
{
|
|
||||||
if (Mathf.Square(lods[lodIndex].ScreenSize * 0.5f) >= screenRadiusSquared)
|
|
||||||
{
|
|
||||||
return lodIndex + PreviewActor.LODBias;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void DrawSelf()
|
public override void Draw()
|
||||||
{
|
{
|
||||||
base.DrawSelf();
|
base.Draw();
|
||||||
|
|
||||||
var style = Style.Current;
|
var style = Style.Current;
|
||||||
var asset = _window.Asset;
|
var asset = _window.Asset;
|
||||||
@@ -124,40 +48,6 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
{
|
{
|
||||||
Render2D.DrawText(style.FontLarge, "Loading...", new Rectangle(Vector2.Zero, Size), style.ForegroundDisabled, TextAlignment.Center, TextAlignment.Center);
|
Render2D.DrawText(style.FontLarge, "Loading...", new Rectangle(Vector2.Zero, Size), style.ForegroundDisabled, TextAlignment.Center, TextAlignment.Center);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_showCurrentLOD)
|
|
||||||
{
|
|
||||||
var lodIndex = ComputeLODIndex(asset);
|
|
||||||
string text = string.Format("Current LOD: {0}", lodIndex);
|
|
||||||
if (lodIndex != -1)
|
|
||||||
{
|
|
||||||
var lods = asset.LODs;
|
|
||||||
lodIndex = Mathf.Clamp(lodIndex + PreviewActor.LODBias, 0, lods.Length - 1);
|
|
||||||
var lod = lods[lodIndex];
|
|
||||||
int triangleCount = 0, vertexCount = 0;
|
|
||||||
for (int meshIndex = 0; meshIndex < lod.Meshes.Length; meshIndex++)
|
|
||||||
{
|
|
||||||
var mesh = lod.Meshes[meshIndex];
|
|
||||||
triangleCount += mesh.TriangleCount;
|
|
||||||
vertexCount += mesh.VertexCount;
|
|
||||||
}
|
|
||||||
text += string.Format("\nTriangles: {0:N0}\nVertices: {1:N0}", triangleCount, vertexCount);
|
|
||||||
}
|
|
||||||
var font = Style.Current.FontMedium;
|
|
||||||
var pos = new Vector2(10, 50);
|
|
||||||
Render2D.DrawText(font, text, new Rectangle(pos + Vector2.One, Size), Color.Black);
|
|
||||||
Render2D.DrawText(font, text, new Rectangle(pos, Size), Color.White);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public override void OnDestroy()
|
|
||||||
{
|
|
||||||
Object.Destroy(ref _floorModel);
|
|
||||||
_showFloorButton = null;
|
|
||||||
_showCurrentLODButton = null;
|
|
||||||
|
|
||||||
base.OnDestroy();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -934,8 +824,9 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
public SkinnedMesh.Vertex[] VertexBuffer;
|
public SkinnedMesh.Vertex[] VertexBuffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
private readonly AnimatedModelPreview _preview;
|
private Preview _preview;
|
||||||
private AnimatedModel _highlightActor;
|
private AnimatedModel _highlightActor;
|
||||||
|
private ToolStripButton _showNodesButton;
|
||||||
|
|
||||||
private MeshData[][] _meshDatas;
|
private MeshData[][] _meshDatas;
|
||||||
private bool _meshDatasInProgress;
|
private bool _meshDatasInProgress;
|
||||||
@@ -947,7 +838,7 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
{
|
{
|
||||||
// Toolstrip
|
// Toolstrip
|
||||||
_toolstrip.AddSeparator();
|
_toolstrip.AddSeparator();
|
||||||
_toolstrip.AddButton(editor.Icons.Bone64, () => _preview.ShowNodes = !_preview.ShowNodes).SetAutoCheck(true).LinkTooltip("Show animated model nodes debug view");
|
_showNodesButton = (ToolStripButton)_toolstrip.AddButton(editor.Icons.Bone64, () => _preview.ShowNodes = !_preview.ShowNodes).LinkTooltip("Show animated model nodes debug view");
|
||||||
_toolstrip.AddSeparator();
|
_toolstrip.AddSeparator();
|
||||||
_toolstrip.AddButton(editor.Icons.Docs64, () => Platform.OpenUrl(Utilities.Constants.DocsUrl + "manual/animation/skinned-model/index.html")).LinkTooltip("See documentation to learn more");
|
_toolstrip.AddButton(editor.Icons.Docs64, () => Platform.OpenUrl(Utilities.Constants.DocsUrl + "manual/animation/skinned-model/index.html")).LinkTooltip("See documentation to learn more");
|
||||||
|
|
||||||
@@ -1095,6 +986,8 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_showNodesButton.Checked = _preview.ShowNodes;
|
||||||
|
|
||||||
base.Update(deltaTime);
|
base.Update(deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1167,6 +1060,8 @@ namespace FlaxEditor.Windows.Assets
|
|||||||
base.OnDestroy();
|
base.OnDestroy();
|
||||||
|
|
||||||
Object.Destroy(ref _highlightActor);
|
Object.Destroy(ref _highlightActor);
|
||||||
|
_preview = null;
|
||||||
|
_showNodesButton = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user