better material color handling

This commit is contained in:
iAmAsval 2021-11-26 19:53:11 +01:00
parent 60dae604b1
commit 347b28cb52
5 changed files with 27 additions and 31 deletions

View File

@ -73,8 +73,6 @@ namespace FModel.ViewModels
private readonly FGame _game;
private readonly int[] _facesIndex = { 1, 0, 2 };
private readonly float[] _table = { 255 * 0.9f, 25 * 3.0f, 255 * 0.6f, 255 * 0.0f };
private readonly int[] _table2 = { 0, 1, 2, 4, 7, 3, 5, 6 };
public ModelViewerViewModel(FGame game)
{
@ -215,10 +213,10 @@ namespace FModel.ViewModels
if (SelectedModel is not { } model || model.SelectedGeometry is null)
return false;
CustomPBRMaterial m = null;
PBRMaterial m = null;
await _threadWorkerView.Begin(_ =>
{
var (material, _, _) = LoadMaterial(materialInstance, B(0));
var (material, _, _) = LoadMaterial(materialInstance);
m = material;
});
@ -235,7 +233,7 @@ namespace FModel.ViewModels
cam.TriangleCount = 1984; // no need to count
SetupCameraAndAxis(new FBox(new FVector(-11), new FVector(11)), cam);
var (m, isRendering, isTransparent) = LoadMaterial(materialInstance, B(0));
var (m, isRendering, isTransparent) = LoadMaterial(materialInstance);
Application.Current.Dispatcher.Invoke(() =>
{
@ -278,8 +276,6 @@ namespace FModel.ViewModels
PushLod(lod.Sections.Value, lod.Verts, lod.Indices.Value, cam);
break;
}
// bones here
}
private void PushLod(CMeshSection[] sections, CMeshVertex[] verts, FRawStaticIndexBuffer indices, ModelAndCam cam)
@ -308,7 +304,7 @@ namespace FModel.ViewModels
if (section.Material == null || !section.Material.TryLoad<UMaterialInterface>(out var unrealMaterial))
continue;
var (m, isRendering, isTransparent) = LoadMaterial(unrealMaterial, B(i));
var (m, isRendering, isTransparent) = LoadMaterial(unrealMaterial);
Application.Current.Dispatcher.Invoke(() =>
{
cam.Group3d.Add(new MeshGeometryModel3D
@ -320,15 +316,14 @@ namespace FModel.ViewModels
}
}
private (CustomPBRMaterial material, bool isRendering, bool isTransparent) LoadMaterial(UMaterialInterface unrealMaterial, int index)
private (PBRMaterial material, bool isRendering, bool isTransparent) LoadMaterial(UMaterialInterface unrealMaterial)
{
var m = new CustomPBRMaterial {RenderShadowMap = true, EnableAutoTangent = true, RenderEnvironmentMap = true}; // default
var m = new PBRMaterial {RenderShadowMap = true, EnableAutoTangent = true, RenderEnvironmentMap = true}; // default
Application.Current.Dispatcher.Invoke(() => // tweak this later
{
m = new CustomPBRMaterial // recreate on ui thread
m = new PBRMaterial // recreate on ui thread
{
RenderShadowMap = true, EnableAutoTangent = true, RenderEnvironmentMap = true,
MaterialColor = new Color4(_table[C(index)] / 255, _table[C(index >> 1)] / 255, _table[C(index >> 2)] / 255, 1)
RenderShadowMap = true, EnableAutoTangent = true, RenderEnvironmentMap = true
};
});
@ -500,9 +495,6 @@ namespace FModel.ViewModels
return input.Replace('-', '_');
}
private int B(int x) => (x & 0xFFF8) | _table2[x & 7] ^ 7;
private int C(int x) => (x & 1) | ((x >> 2) & 2);
public void Clear()
{
foreach (var g in _loadedModels.ToList())
@ -537,13 +529,15 @@ namespace FModel.ViewModels
set
{
SetProperty(ref _showMaterialColor, value);
foreach (var g in Group3d)
for (int i = 0; i < Group3d.Count; i++)
{
if (g is not MeshGeometryModel3D { Material: CustomPBRMaterial material })
if (Group3d[i] is not MeshGeometryModel3D { Material: PBRMaterial material })
continue;
var index = B(i);
material.RenderAlbedoMap = !_showMaterialColor;
material.AlbedoColor = _showMaterialColor ? material.MaterialColor : Color4.White;
material.AlbedoColor = !_showMaterialColor ? Color4.White :
new Color4(_table[C(index)] / 255, _table[C(index >> 1)] / 255, _table[C(index >> 2)] / 255, 1);
}
}
}
@ -557,7 +551,7 @@ namespace FModel.ViewModels
SetProperty(ref _showDiffuseOnly, value);
foreach (var g in Group3d)
{
if (g is not MeshGeometryModel3D { Material: CustomPBRMaterial material })
if (g is not MeshGeometryModel3D { Material: PBRMaterial material })
continue;
material.RenderAmbientOcclusionMap = !material.RenderAmbientOcclusionMap;
@ -586,6 +580,9 @@ namespace FModel.ViewModels
set => SetProperty(ref _group3d, value);
}
private readonly float[] _table = { 255 * 0.9f, 25 * 3.0f, 255 * 0.6f, 255 * 0.0f };
private readonly int[] _table2 = { 0, 1, 2, 4, 7, 3, 5, 6 };
public ModelAndCam(UObject export)
{
Export = export;
@ -593,6 +590,9 @@ namespace FModel.ViewModels
Group3d = new ObservableElement3DCollection();
}
private int B(int x) => (x & 0xFFF8) | _table2[x & 7] ^ 7;
private int C(int x) => (x & 1) | ((x >> 2) & 2);
public void Dispose()
{
TriangleCount = 0;
@ -604,9 +604,4 @@ namespace FModel.ViewModels
}
}
}
public class CustomPBRMaterial : PBRMaterial
{
public Color4 MaterialColor { get; set; }
}
}

View File

@ -77,7 +77,7 @@
</Grid>
</Grid>
<Separator DockPanel.Dock="Top" Tag="MATERIALS" Style="{StaticResource CustomSeparator}" />
<ListBox DockPanel.Dock="Top" Style="{StaticResource MaterialsListBox}">
<ListBox x:Name="MaterialsListName" DockPanel.Dock="Top" Style="{StaticResource MaterialsListBox}">
<ListBox.ContextMenu>
<ContextMenu DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Copy Name" Click="OnCopyClick">

View File

@ -78,6 +78,7 @@ namespace FModel.Views
{
if (!Keyboard.Modifiers.HasFlag(ModifierKeys.Shift) || e.HitTestResult.ModelHit is not MeshGeometryModel3D m) return;
_applicationView.ModelViewer.SelectedModel.SelectedGeometry = m;
MaterialsListName.ScrollIntoView(m);
}
private void OnFocusClick(object sender, RoutedEventArgs e)

View File

@ -2,7 +2,7 @@
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;
using FModel.ViewModels;
using HelixToolkit.Wpf.SharpDX;
namespace FModel.Views.Resources.Converters
{
@ -12,12 +12,12 @@ namespace FModel.Views.Resources.Converters
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is not CustomPBRMaterial material)
if (value is not PBRMaterial material)
return new SolidColorBrush(Colors.Red);
return new SolidColorBrush(Color.FromScRgb(
material.MaterialColor.Alpha, material.MaterialColor.Red,
material.MaterialColor.Green, material.MaterialColor.Blue));
material.AlbedoColor.Alpha, material.AlbedoColor.Red,
material.AlbedoColor.Green, material.AlbedoColor.Blue));
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)

View File

@ -672,7 +672,7 @@
</Canvas>
</Viewbox>
</ToggleButton>
<Rectangle Grid.Column="5" Width="19" Height="22" Fill="{Binding Material, Mode=OneTime, Converter={x:Static converters:TagToColorConverter.Instance}}"
<Rectangle Grid.Column="5" Width="19" Height="22" Fill="{Binding Material, Converter={x:Static converters:TagToColorConverter.Instance}}"
Visibility="{Binding DataContext.ModelViewer.SelectedModel.ShowMaterialColor,
RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type local:Views.ModelViewer}},
Converter={StaticResource BoolToVisibilityConverter}}" />