@@ -93,6 +93,118 @@ namespace FlaxEditor.Windows.Assets
|
||||
}
|
||||
}
|
||||
|
||||
protected class UVsPropertiesProxyBase : PropertiesProxyBase
|
||||
{
|
||||
public enum UVChannel
|
||||
{
|
||||
None,
|
||||
TexCoord0,
|
||||
TexCoord1,
|
||||
TexCoord2,
|
||||
TexCoord3,
|
||||
LightmapUVs,
|
||||
};
|
||||
|
||||
private UVChannel _uvChannel = UVChannel.None;
|
||||
|
||||
[EditorOrder(0), EditorDisplay(null, "Preview UV Channel"), EnumDisplay(EnumDisplayAttribute.FormatMode.None)]
|
||||
[Tooltip("Set UV channel to preview.")]
|
||||
public UVChannel Channel
|
||||
{
|
||||
get => _uvChannel;
|
||||
set
|
||||
{
|
||||
if (_uvChannel == value)
|
||||
return;
|
||||
_uvChannel = value;
|
||||
Window._meshData?.RequestMeshData(Window._asset);
|
||||
}
|
||||
}
|
||||
|
||||
[EditorOrder(1), EditorDisplay(null, "LOD"), Limit(0, Model.MaxLODs), VisibleIf("ShowUVs")]
|
||||
[Tooltip("Level Of Detail index to preview UVs layout.")]
|
||||
public int LOD = 0;
|
||||
|
||||
[EditorOrder(2), EditorDisplay(null, "Mesh"), Limit(-1, 1000000), VisibleIf("ShowUVs")]
|
||||
[Tooltip("Mesh index to preview UVs layout. Use -1 for all meshes")]
|
||||
public int Mesh = -1;
|
||||
|
||||
private bool ShowUVs => _uvChannel != UVChannel.None;
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void OnClean()
|
||||
{
|
||||
Channel = UVChannel.None;
|
||||
|
||||
base.OnClean();
|
||||
}
|
||||
|
||||
protected class ProxyEditor : ProxyEditorBase
|
||||
{
|
||||
private UVsLayoutPreviewControl _uvsPreview;
|
||||
|
||||
public override void Initialize(LayoutElementsContainer layout)
|
||||
{
|
||||
var proxy = (UVsPropertiesProxyBase)Values[0];
|
||||
if (Utilities.Utils.OnAssetProperties(layout, proxy.Asset))
|
||||
return;
|
||||
|
||||
base.Initialize(layout);
|
||||
|
||||
_uvsPreview = layout.Custom<UVsLayoutPreviewControl>().CustomControl;
|
||||
_uvsPreview.Window = proxy.Window;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Refresh()
|
||||
{
|
||||
base.Refresh();
|
||||
|
||||
if (_uvsPreview != null)
|
||||
{
|
||||
var proxy = (UVsPropertiesProxyBase)Values[0];
|
||||
switch (proxy._uvChannel)
|
||||
{
|
||||
case UVChannel.TexCoord0: _uvsPreview.Channel = 0; break;
|
||||
case UVChannel.TexCoord1: _uvsPreview.Channel = 1; break;
|
||||
case UVChannel.TexCoord2: _uvsPreview.Channel = 2; break;
|
||||
case UVChannel.TexCoord3: _uvsPreview.Channel = 3; break;
|
||||
case UVChannel.LightmapUVs:
|
||||
{
|
||||
_uvsPreview.Channel = -1;
|
||||
if (proxy.Window.Asset && proxy.Window.Asset.IsLoaded)
|
||||
{
|
||||
// Pick UVs channel index from the first mesh
|
||||
proxy.Window.Asset.GetMeshes(out var meshes);
|
||||
foreach (var mesh in meshes)
|
||||
{
|
||||
if (mesh is Mesh m && m.HasLightmapUVs)
|
||||
{
|
||||
_uvsPreview.Channel = m.LightmapUVsIndex;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
default: _uvsPreview.Channel = -1; break;
|
||||
}
|
||||
_uvsPreview.LOD = proxy.LOD;
|
||||
_uvsPreview.Mesh = proxy.Mesh;
|
||||
_uvsPreview.HighlightIndex = proxy.Window._highlightIndex;
|
||||
_uvsPreview.IsolateIndex = proxy.Window._isolateIndex;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void Deinitialize()
|
||||
{
|
||||
_uvsPreview = null;
|
||||
|
||||
base.Deinitialize();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected sealed class UVsLayoutPreviewControl : RenderToTextureControl
|
||||
{
|
||||
private int _channel = -1;
|
||||
@@ -168,15 +280,21 @@ namespace FlaxEditor.Windows.Assets
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawMeshUVs(int meshIndex, ref MeshDataCache.MeshData meshData)
|
||||
private void DrawMeshUVs(int meshIndex, ref MeshDataCache.MeshData meshData, ref Rectangle bounds)
|
||||
{
|
||||
var uvScale = Size;
|
||||
if (meshData.IndexBuffer == null || meshData.VertexAccessor == null)
|
||||
{
|
||||
Render2D.DrawText(Style.Current.FontMedium, "Missing mesh data", bounds, Color.Red, TextAlignment.Center, TextAlignment.Center);
|
||||
return;
|
||||
}
|
||||
var linesColor = _highlightIndex != -1 && _highlightIndex == meshIndex ? Style.Current.BackgroundSelected : Color.White;
|
||||
var texCoordStream = meshData.VertexAccessor.TexCoord(_channel);
|
||||
if (!texCoordStream.IsValid)
|
||||
{
|
||||
Render2D.DrawText(Style.Current.FontMedium, "Missing texcoords channel", bounds, Color.Yellow, TextAlignment.Center, TextAlignment.Center);
|
||||
return;
|
||||
}
|
||||
var uvScale = bounds.Size;
|
||||
for (int i = 0; i < meshData.IndexBuffer.Length; i += 3)
|
||||
{
|
||||
// Cache triangle indices
|
||||
@@ -206,19 +324,19 @@ namespace FlaxEditor.Windows.Assets
|
||||
{
|
||||
base.DrawSelf();
|
||||
|
||||
var size = Size;
|
||||
if (_channel < 0 || size.MaxValue < 5.0f)
|
||||
var bounds = new Rectangle(Float2.Zero, Size);
|
||||
if (_channel < 0 || bounds.Size.MaxValue < 5.0f)
|
||||
return;
|
||||
if (Window._meshData == null)
|
||||
Window._meshData = new MeshDataCache();
|
||||
if (!Window._meshData.RequestMeshData(Window._asset))
|
||||
{
|
||||
Invalidate();
|
||||
Render2D.DrawText(Style.Current.FontMedium, "Loading...", new Rectangle(Float2.Zero, size), Color.White, TextAlignment.Center, TextAlignment.Center);
|
||||
Render2D.DrawText(Style.Current.FontMedium, "Loading...", bounds, Color.White, TextAlignment.Center, TextAlignment.Center);
|
||||
return;
|
||||
}
|
||||
|
||||
Render2D.PushClip(new Rectangle(Float2.Zero, size));
|
||||
Render2D.PushClip(bounds);
|
||||
|
||||
var meshDatas = Window._meshData.MeshDatas;
|
||||
var lodIndex = Mathf.Clamp(_lod, 0, meshDatas.Length - 1);
|
||||
@@ -230,12 +348,12 @@ namespace FlaxEditor.Windows.Assets
|
||||
{
|
||||
if (_isolateIndex != -1 && _isolateIndex != meshIndex)
|
||||
continue;
|
||||
DrawMeshUVs(meshIndex, ref lod[meshIndex]);
|
||||
DrawMeshUVs(meshIndex, ref lod[meshIndex], ref bounds);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
DrawMeshUVs(mesh, ref lod[mesh]);
|
||||
DrawMeshUVs(mesh, ref lod[mesh], ref bounds);
|
||||
}
|
||||
|
||||
Render2D.PopClip();
|
||||
|
||||
Reference in New Issue
Block a user