Skip to content

Commit

Permalink
better material color handling
Browse files Browse the repository at this point in the history
  • Loading branch information
4sval committed Nov 26, 2021
1 parent 60dae60 commit 347b28c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 31 deletions.
45 changes: 20 additions & 25 deletions FModel/ViewModels/ModelViewerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,6 @@ public bool AppendMode

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)
{
Expand Down Expand Up @@ -215,10 +213,10 @@ public async Task<bool> TryChangeSelectedMaterial(UMaterialInstance materialInst
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;
});

Expand All @@ -235,7 +233,7 @@ private void LoadMaterialInstance(UMaterialInstance materialInstance, ModelAndCa
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(() =>
{
Expand Down Expand Up @@ -278,8 +276,6 @@ private void LoadSkeletalMesh(USkeletalMesh mesh, ModelAndCam cam)
PushLod(lod.Sections.Value, lod.Verts, lod.Indices.Value, cam);
break;
}

// bones here
}

private void PushLod(CMeshSection[] sections, CMeshVertex[] verts, FRawStaticIndexBuffer indices, ModelAndCam cam)
Expand Down Expand Up @@ -308,7 +304,7 @@ private void PushLod(CMeshSection[] sections, CMeshVertex[] verts, FRawStaticInd
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
Expand All @@ -320,15 +316,14 @@ private void PushLod(CMeshSection[] sections, CMeshVertex[] verts, FRawStaticInd
}
}

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

Expand Down Expand Up @@ -500,9 +495,6 @@ private string FixName(string input)
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())
Expand Down Expand Up @@ -537,13 +529,15 @@ public bool ShowMaterialColor
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);
}
}
}
Expand All @@ -557,7 +551,7 @@ public bool ShowDiffuseOnly
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;
Expand Down Expand Up @@ -586,13 +580,19 @@ public ObservableElement3DCollection Group3d
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;
TriangleCount = 0;
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;
Expand All @@ -604,9 +604,4 @@ public void Dispose()
}
}
}

public class CustomPBRMaterial : PBRMaterial
{
public Color4 MaterialColor { get; set; }
}
}
2 changes: 1 addition & 1 deletion FModel/Views/ModelViewer.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
Expand Down
1 change: 1 addition & 0 deletions FModel/Views/ModelViewer.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ private void OnMouse3DDown(object sender, MouseDown3DEventArgs e)
{
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)
Expand Down
8 changes: 4 additions & 4 deletions FModel/Views/Resources/Converters/TagToColorConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -12,12 +12,12 @@ public class TagToColorConverter : IValueConverter

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)
Expand Down
2 changes: 1 addition & 1 deletion FModel/Views/Resources/Resources.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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}}" />
Expand Down

0 comments on commit 347b28c

Please sign in to comment.