Skip to content

Commit

Permalink
per-platform texture support
Browse files Browse the repository at this point in the history
  • Loading branch information
GMatrixGames committed Jan 20, 2022
1 parent d8627eb commit 59a952c
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 33 deletions.
3 changes: 2 additions & 1 deletion FModel/Creator/Utils.cs
Expand Up @@ -11,6 +11,7 @@
using CUE4Parse_Conversion.Textures;
using FModel.Framework;
using FModel.Services;
using FModel.Settings;
using FModel.ViewModels;
using SkiaSharp;
using SkiaSharp.HarfBuzz;
Expand Down Expand Up @@ -105,7 +106,7 @@ public static SKBitmap GetBitmap(UMaterialInstanceConstant material)
public static SKBitmap GetB64Bitmap(string b64) => SKBitmap.Decode(new MemoryStream(Convert.FromBase64String(b64)) {Position = 0});
public static SKBitmap GetBitmap(FSoftObjectPath softObjectPath) => GetBitmap(softObjectPath.AssetPathName.Text);
public static SKBitmap GetBitmap(string fullPath) => TryLoadObject(fullPath, out UTexture2D texture) ? GetBitmap(texture) : null;
public static SKBitmap GetBitmap(UTexture2D texture) => texture.IsVirtual ? null : texture.Decode();
public static SKBitmap GetBitmap(UTexture2D texture) => texture.IsVirtual ? null : texture.Decode(UserSettings.Default.OverridedPlatform);
public static SKBitmap GetBitmap(byte[] data) => SKBitmap.Decode(data);

public static SKBitmap ResizeWithRatio(this SKBitmap me, double width, double height)
Expand Down
8 changes: 8 additions & 0 deletions FModel/Settings/UserSettings.cs
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Windows;
using System.Windows.Input;
using CUE4Parse.UE4.Assets.Exports.Texture;
using CUE4Parse.UE4.Objects.Core.Serialization;
using CUE4Parse.UE4.Versions;
using CUE4Parse_Conversion.Meshes;
Expand Down Expand Up @@ -251,6 +252,13 @@ public int ImageMergerMargin
set => SetProperty(ref _manualGames, value);
}

private ETexturePlatform _overridedPlatform = ETexturePlatform.DesktopMobile;
public ETexturePlatform OverridedPlatform
{
get => _overridedPlatform;
set => SetProperty(ref _overridedPlatform, value);
}

private IDictionary<FGame, string> _presets = new Dictionary<FGame, string>
{
{FGame.Unknown, Constants._NO_PRESET_TRIGGER},
Expand Down
13 changes: 6 additions & 7 deletions FModel/ViewModels/CUE4ParseViewModel.cs
Expand Up @@ -28,7 +28,6 @@
using CUE4Parse.UE4.Wwise;
using CUE4Parse_Conversion;
using CUE4Parse_Conversion.Sounds;
using CUE4Parse_Conversion.Textures;
using EpicManifestParser.Objects;
using FModel.Creator;
using FModel.Extensions;
Expand Down Expand Up @@ -72,7 +71,7 @@ public bool ModelIsOverwritingMaterial
public SearchViewModel SearchVm { get; }
public TabControlViewModel TabControl { get; }
public int LocalizedResourcesCount { get; set; }
public bool HotfixedResourcesDone { get; set; } = false;
public bool HotfixedResourcesDone { get; set; }
public int VirtualPathCount { get; set; }

public CUE4ParseViewModel(string gameDirectory)
Expand All @@ -84,7 +83,7 @@ public CUE4ParseViewModel(string gameDirectory)
Game = FGame.FortniteGame;
Provider = new StreamedFileProvider("FortniteLive", true,
new VersionContainer(
UserSettings.Default.OverridedGame[Game],
UserSettings.Default.OverridedGame[Game], UserSettings.Default.OverridedPlatform,
customVersions: UserSettings.Default.OverridedCustomVersions[Game],
optionOverrides: UserSettings.Default.OverridedOptions[Game]));
break;
Expand All @@ -94,15 +93,15 @@ public CUE4ParseViewModel(string gameDirectory)
Game = FGame.ShooterGame;
Provider = new StreamedFileProvider("ValorantLive", true,
new VersionContainer(
UserSettings.Default.OverridedGame[Game],
UserSettings.Default.OverridedGame[Game], UserSettings.Default.OverridedPlatform,
customVersions: UserSettings.Default.OverridedCustomVersions[Game],
optionOverrides: UserSettings.Default.OverridedOptions[Game]));
break;
}
default:
{
Game = gameDirectory.SubstringBeforeLast("\\Content").SubstringAfterLast("\\").ToEnum(FGame.Unknown);
var versions = new VersionContainer(UserSettings.Default.OverridedGame[Game],
var versions = new VersionContainer(UserSettings.Default.OverridedGame[Game], UserSettings.Default.OverridedPlatform,
customVersions: UserSettings.Default.OverridedCustomVersions[Game],
optionOverrides: UserSettings.Default.OverridedOptions[Game]);

Expand All @@ -120,7 +119,7 @@ public CUE4ParseViewModel(string gameDirectory)
}
case FGame.Unknown when UserSettings.Default.ManualGames.TryGetValue(gameDirectory, out var settings):
{
versions = new VersionContainer(settings.OverridedGame,
versions = new VersionContainer(settings.OverridedGame, UserSettings.Default.OverridedPlatform,
customVersions: settings.OverridedCustomVersions,
optionOverrides: settings.OverridedOptions);
goto default;
Expand Down Expand Up @@ -764,7 +763,7 @@ private void SaveAndPlaySound(string fullPath, string ext, byte[] data)

private void SaveExport(UObject export)
{
var toSave = new Exporter(export, UserSettings.Default.TextureExportFormat, UserSettings.Default.LodExportFormat, UserSettings.Default.MeshExportFormat);
var toSave = new Exporter(export, UserSettings.Default.TextureExportFormat, UserSettings.Default.LodExportFormat, UserSettings.Default.MeshExportFormat, UserSettings.Default.OverridedPlatform);
var toSaveDirectory = new DirectoryInfo(UserSettings.Default.ModelDirectory);
if (toSave.TryWriteToDir(toSaveDirectory, out var savedFileName))
{
Expand Down
28 changes: 22 additions & 6 deletions FModel/ViewModels/ModelViewerViewModel.cs
Expand Up @@ -186,7 +186,7 @@ public async Task SaveLoadedModels()
{
foreach (var model in _loadedModels)
{
var toSave = new CUE4Parse_Conversion.Exporter(model.Export, UserSettings.Default.TextureExportFormat, UserSettings.Default.LodExportFormat, UserSettings.Default.MeshExportFormat);
var toSave = new CUE4Parse_Conversion.Exporter(model.Export, UserSettings.Default.TextureExportFormat, UserSettings.Default.LodExportFormat, UserSettings.Default.MeshExportFormat, UserSettings.Default.OverridedPlatform);
if (toSave.TryWriteToDir(new DirectoryInfo(folderBrowser.SelectedPath), out var savedFileName))
{
Log.Information("Successfully saved {FileName}", savedFileName);
Expand Down Expand Up @@ -419,19 +419,35 @@ private void PushLod(CMeshSection[] sections, CMeshVertex[] verts, FRawStaticInd
}
else if (parameters.Diffuse is UTexture2D diffuse)
{
m.AlbedoMap = new TextureModel(diffuse.Decode()?.Encode(SKEncodedImageFormat.Png, 100).AsStream());
m.AlbedoMap = new TextureModel(diffuse.Decode(UserSettings.Default.OverridedPlatform)?.Encode(SKEncodedImageFormat.Png, 100).AsStream());
}

if (parameters.Normal is UTexture2D normal)
{
m.NormalMap = new TextureModel(normal.Decode()?.Encode(SKEncodedImageFormat.Png, 100).AsStream());
m.NormalMap = new TextureModel(normal.Decode(UserSettings.Default.OverridedPlatform)?.Encode(SKEncodedImageFormat.Png, 100).AsStream());
}

if (parameters.Specular is UTexture2D specular)
{
var mip = specular.GetFirstMip();
TextureDecoder.DecodeTexture(mip, specular.Format, specular.isNormalMap,
out var data, out var colorType);
byte[] data;
SKColorType colorType;
switch (UserSettings.Default.OverridedPlatform)
{
case ETexturePlatform.Playstation:
PlaystationDecoder.DecodeTexturePlaystation(mip, specular.Format, specular.isNormalMap,
out data, out colorType);
break;
case ETexturePlatform.NintendoSwitch:
NintendoSwitchDecoder.DecodeTextureNSW(mip, specular.Format, specular.isNormalMap,
out data, out colorType);
break;
default:
TextureDecoder.DecodeTexture(mip, specular.Format, specular.isNormalMap,
out data, out colorType);
break;
}


switch (_game)
{
Expand Down Expand Up @@ -540,7 +556,7 @@ private void PushLod(CMeshSection[] sections, CMeshVertex[] verts, FRawStaticInd
if (parameters.HasTopEmissiveTexture && parameters.Emissive is UTexture2D emissive && parameters.EmissiveColor is { A: > 0 } emissiveColor)
{
m.EmissiveColor = new Color4(emissiveColor.R, emissiveColor.G, emissiveColor.B, emissiveColor.A);
m.EmissiveMap = new TextureModel(emissive.Decode()?.Encode(SKEncodedImageFormat.Png, 100).AsStream());
m.EmissiveMap = new TextureModel(emissive.Decode(UserSettings.Default.OverridedPlatform)?.Encode(SKEncodedImageFormat.Png, 100).AsStream());
}
}
else
Expand Down
19 changes: 17 additions & 2 deletions FModel/ViewModels/SettingsViewModel.cs
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Threading.Tasks;
using CUE4Parse.UE4.Assets.Exports.Texture;
using CUE4Parse.UE4.Objects.Core.Misc;
using CUE4Parse.UE4.Objects.Core.Serialization;
using CUE4Parse.UE4.Versions;
Expand Down Expand Up @@ -39,6 +40,13 @@ public string SelectedPreset
}
}

private ETexturePlatform _selectedUePlatform;
public ETexturePlatform SelectedUePlatform
{
get => _selectedUePlatform;
set => SetProperty(ref _selectedUePlatform, value);
}

private EGame _selectedUeGame;
public EGame SelectedUeGame
{
Expand Down Expand Up @@ -127,6 +135,7 @@ public ETextureFormat SelectedTextureExportFormat
public ReadOnlyObservableCollection<EMeshFormat> MeshExportFormats { get; private set; }
public ReadOnlyObservableCollection<ELodFormat> LodExportFormats { get; private set; }
public ReadOnlyObservableCollection<ETextureFormat> TextureExportFormats { get; private set; }
public ReadOnlyObservableCollection<ETexturePlatform> Platforms { get; private set; }

public bool EnableElements => SelectedPreset == Constants._NO_PRESET_TRIGGER;

Expand All @@ -141,6 +150,7 @@ public ETextureFormat SelectedTextureExportFormat
private string _gameSnapshot;
private EUpdateMode _updateModeSnapshot;
private string _presetSnapshot;
private ETexturePlatform _uePlatformSnapshot;
private EGame _ueGameSnapshot;
private List<FCustomVersion> _customVersionsSnapshot;
private Dictionary<string, bool> _optionsSnapshot;
Expand All @@ -167,6 +177,7 @@ public void Initialize()
_gameSnapshot = UserSettings.Default.GameDirectory;
_updateModeSnapshot = UserSettings.Default.UpdateMode;
_presetSnapshot = UserSettings.Default.Presets[_game];
_uePlatformSnapshot = UserSettings.Default.OverridedPlatform;
if (_game == FGame.Unknown && UserSettings.Default.ManualGames.TryGetValue(_gameSnapshot, out var settings))
{
_ueGameSnapshot = settings.OverridedGame;
Expand All @@ -188,6 +199,7 @@ public void Initialize()

SelectedUpdateMode = _updateModeSnapshot;
SelectedPreset = _presetSnapshot;
SelectedUePlatform = _uePlatformSnapshot;
SelectedUeGame = _ueGameSnapshot;
SelectedCustomVersions = _customVersionsSnapshot;
SelectedOptions = _optionsSnapshot;
Expand All @@ -211,6 +223,7 @@ public void Initialize()
MeshExportFormats = new ReadOnlyObservableCollection<EMeshFormat>(new ObservableCollection<EMeshFormat>(EnumerateMeshExportFormat()));
LodExportFormats = new ReadOnlyObservableCollection<ELodFormat>(new ObservableCollection<ELodFormat>(EnumerateLodExportFormat()));
TextureExportFormats = new ReadOnlyObservableCollection<ETextureFormat>(new ObservableCollection<ETextureFormat>(EnumerateTextureExportFormat()));
Platforms = new ReadOnlyObservableCollection<ETexturePlatform>(new ObservableCollection<ETexturePlatform>(EnumerateUePlatforms()));
}

public async Task InitPresets(string gameName)
Expand Down Expand Up @@ -257,8 +270,8 @@ public SettingsOut Save()
{
var ret = SettingsOut.Nothing;

if (_ueGameSnapshot != SelectedUeGame || // combobox
_customVersionsSnapshot != SelectedCustomVersions || _optionsSnapshot != SelectedOptions ||
if (_ueGameSnapshot != SelectedUeGame || _customVersionsSnapshot != SelectedCustomVersions ||
_uePlatformSnapshot != SelectedUePlatform || _optionsSnapshot != SelectedOptions || // combobox
_outputSnapshot != UserSettings.Default.OutputDirectory || // textbox
_rawDataSnapshot != UserSettings.Default.RawDataDirectory || // textbox
_propertiesSnapshot != UserSettings.Default.PropertiesDirectory || // textbox
Expand All @@ -276,6 +289,7 @@ public SettingsOut Save()

UserSettings.Default.UpdateMode = SelectedUpdateMode;
UserSettings.Default.Presets[_game] = SelectedPreset;
UserSettings.Default.OverridedPlatform = SelectedUePlatform;
if (_game == FGame.Unknown && UserSettings.Default.ManualGames.ContainsKey(UserSettings.Default.GameDirectory))
{
UserSettings.Default.ManualGames[UserSettings.Default.GameDirectory].OverridedGame = SelectedUeGame;
Expand Down Expand Up @@ -317,5 +331,6 @@ private IEnumerable<string> EnumeratePresets()
private IEnumerable<EMeshFormat> EnumerateMeshExportFormat() => Enum.GetValues<EMeshFormat>();
private IEnumerable<ELodFormat> EnumerateLodExportFormat() => Enum.GetValues<ELodFormat>();
private IEnumerable<ETextureFormat> EnumerateTextureExportFormat() => Enum.GetValues<ETextureFormat>();
private IEnumerable<ETexturePlatform> EnumerateUePlatforms() => Enum.GetValues<ETexturePlatform>();
}
}
2 changes: 1 addition & 1 deletion FModel/ViewModels/TabControlViewModel.cs
Expand Up @@ -222,7 +222,7 @@ public void ClearImages()
});
}

public void AddImage(UTexture2D texture) => AddImage(texture.Name, texture.bRenderNearestNeighbor, texture.Decode());
public void AddImage(UTexture2D texture) => AddImage(texture.Name, texture.bRenderNearestNeighbor, texture.Decode(UserSettings.Default.OverridedPlatform));
public void AddImage(string name, bool rnn, SKBitmap[] img)
{
foreach (var i in img) AddImage(name, rnn, i);
Expand Down

0 comments on commit 59a952c

Please sign in to comment.