diff --git a/Charm/EntityView.xaml.cs b/Charm/EntityView.xaml.cs index c59d80ae..f4758880 100644 --- a/Charm/EntityView.xaml.cs +++ b/Charm/EntityView.xaml.cs @@ -101,10 +101,7 @@ public static void Export(List entities, string name, ExportTypeFlag exp entity.SaveMaterialsFromParts(scene, dynamicParts); entity.SaveTexturePlates(savePath); } - if (ConfigSubsystem.Get().GetSBoxModelExportEnabled()) - { - SBoxHandler.SaveEntityVMDL($"{savePath}", entity); - } + SBoxHandler.SaveEntityVMDL($"{savePath}", entity); } // Scale and rotate diff --git a/Charm/MapView.xaml.cs b/Charm/MapView.xaml.cs index acd87413..2c84ef8b 100644 --- a/Charm/MapView.xaml.cs +++ b/Charm/MapView.xaml.cs @@ -29,8 +29,6 @@ public partial class MapView : UserControl private static MainWindow _mainWindow = null; private static ConfigSubsystem _config = CharmInstance.GetSubsystem(); - - private static bool sboxModels = _config.GetSBoxModelExportEnabled(); private static bool exportStatics = _config.GetIndvidualStaticsEnabled(); private void OnControlLoaded(object sender, RoutedEventArgs routedEventArgs) @@ -155,7 +153,7 @@ private static void ExtractDataTables(Tag map, string savePath, E { if (entry.DataResource.GetValue(data.MapDataTable.GetReader()) is SMapDataResource staticMapResource) // Static map { - staticMapResource.StaticMapParent.TagData.StaticMap.LoadIntoExporterScene(scene, savePath, _config.GetSBoxShaderExportEnabled()); + staticMapResource.StaticMapParent.TagData.StaticMap.LoadIntoExporterScene(scene, savePath, true); } }); }); diff --git a/Charm/SBoxConfigView.xaml.cs b/Charm/SBoxConfigView.xaml.cs index 3ff7f224..dd028a57 100644 --- a/Charm/SBoxConfigView.xaml.cs +++ b/Charm/SBoxConfigView.xaml.cs @@ -52,22 +52,6 @@ private void PopulateConfigPanel() content.SettingValue = content_path == "" ? "Not set" : content_path; content.ChangeButton.Click += SBoxContentPath_OnClick; SBoxConfigPanel.Children.Add(content); - - // Enable source 2 shader generation - ConfigSettingControl cbe = new ConfigSettingControl(); - cbe.SettingName = "Generate shaders (.shader)"; - bool bval2 = _config.GetSBoxShaderExportEnabled(); - cbe.SettingValue = bval2.ToString(); - cbe.ChangeButton.Click += SBoxShaderExportEnabled_OnClick; - SBoxConfigPanel.Children.Add(cbe); - - // Enable vmdl model generation - ConfigSettingControl cfe = new ConfigSettingControl(); - cfe.SettingName = "Generate models (.vmdl)"; - bool bval = _config.GetSBoxModelExportEnabled(); - cfe.SettingValue = bval.ToString(); - cfe.ChangeButton.Click += SBoxModelExportEnabled_OnClick; - SBoxConfigPanel.Children.Add(cfe); } private void SBoxToolsPath_OnClick(object sender, RoutedEventArgs e) @@ -145,24 +129,6 @@ public void OpenSBoxContentPathDialog() } } - private void SBoxShaderExportEnabled_OnClick(object sender, RoutedEventArgs e) - { - _config.SetSBoxShaderExportEnabled(!_config.GetSBoxShaderExportEnabled()); - PopulateConfigPanel(); - } - - private void SBoxMaterialExportEnabled_OnClick(object sender, RoutedEventArgs e) - { - _config.SetSBoxMaterialExportEnabled(!_config.GetSBoxMaterialExportEnabled()); - PopulateConfigPanel(); - } - - private void SBoxModelExportEnabled_OnClick(object sender, RoutedEventArgs e) - { - _config.SetSBoxModelExportEnabled(!_config.GetSBoxModelExportEnabled()); - PopulateConfigPanel(); - } - private void CreateSBProject(string path) { string project_template = "{\r\n \"Title\": \"Destiny Resources\",\r\n \"Type\": \"content\",\r\n \"Tags\": null,\r\n \"Schema\": 1,\r\n \"HasAssets\": true,\r\n \"AssetsPath\": \"\",\r\n \"Resources\": null,\r\n \"MenuResources\": null,\r\n \"HasCode\": false,\r\n \"CodePath\": null,\r\n \"PackageReferences\": [],\r\n \"EditorReferences\": null,\r\n \"Metadata\": {}\r\n}"; diff --git a/Tiger/ConfigSubsystem.cs b/Tiger/ConfigSubsystem.cs index abbfcd72..f05cde65 100644 --- a/Tiger/ConfigSubsystem.cs +++ b/Tiger/ConfigSubsystem.cs @@ -47,9 +47,6 @@ public class BlenderSettings // [ConfigSubsystem] public class SBoxSettings { - public bool SBoxShaderExportsEnabled { get; set; } = false; - public bool SBoxMaterialExportsEnabled { get; set; } = false; - public bool SBoxModelExportsEnabled { get; set; } = false; public string SBoxToolsPath { get; set; } = ""; public string SBoxContentPath { get; set; } = ""; } @@ -161,40 +158,6 @@ public bool TrySetSBoxContentPath(string path) return true; } - public void SetSBoxShaderExportEnabled(bool bS2ShaderExportEnabled) - { - _settings.SBox.SBoxShaderExportsEnabled = bS2ShaderExportEnabled; - Save(); - } - - public bool GetSBoxShaderExportEnabled() - { - return _settings.SBox.SBoxShaderExportsEnabled; - } - - public void SetSBoxMaterialExportEnabled(bool bS2VMATExportEnabled) - { - _settings.SBox.SBoxMaterialExportsEnabled = bS2VMATExportEnabled; - Save(); - } - - public bool GetSBoxMaterialExportEnabled() - { - return _settings.SBox.SBoxMaterialExportsEnabled; - } - - public void SetSBoxModelExportEnabled(bool bS2VMDLExportEnabled) - { - _settings.SBox.SBoxModelExportsEnabled = bS2VMDLExportEnabled; - Save(); - } - - public bool GetSBoxModelExportEnabled() - { - return _settings.SBox.SBoxModelExportsEnabled; - } - - #endregion #region exportSavePath diff --git a/Tiger/Exporters/MaterialExporter.cs b/Tiger/Exporters/MaterialExporter.cs index fcc49d41..8ca3fc7a 100644 --- a/Tiger/Exporters/MaterialExporter.cs +++ b/Tiger/Exporters/MaterialExporter.cs @@ -11,7 +11,6 @@ public override void Export(Exporter.ExportEventArgs args) { ConcurrentHashSet mapTextures = new(); ConcurrentHashSet mapMaterials = new(); - bool saveShaders = ConfigSubsystem.Get().GetSBoxShaderExportEnabled(); Parallel.ForEach(args.Scenes, scene => { @@ -38,12 +37,9 @@ public override void Export(Exporter.ExportEventArgs args) textures.Add(texture.Texture); } - if (saveShaders) - { - string shaderSaveDirectory = $"{args.OutputDirectory}/{scene.Name}"; - material.Material.SaveShaders(shaderSaveDirectory, material.Type, material.IsTerrain); - material.Material.SaveVertexShader(shaderSaveDirectory); - } + string shaderSaveDirectory = $"{args.OutputDirectory}/{scene.Name}"; + material.Material.SaveShaders(shaderSaveDirectory, material.Type, material.IsTerrain); + material.Material.SaveVertexShader(shaderSaveDirectory); } string textureSaveDirectory = $"{args.OutputDirectory}/{scene.Name}/Textures"; @@ -88,14 +84,11 @@ public override void Export(Exporter.ExportEventArgs args) } } - if (saveShaders) + string shaderSaveDirectory = $"{args.OutputDirectory}/Maps"; + Directory.CreateDirectory(shaderSaveDirectory); + foreach (ExportMaterial material in mapMaterials) { - string shaderSaveDirectory = $"{args.OutputDirectory}/Maps"; - Directory.CreateDirectory(shaderSaveDirectory); - foreach (ExportMaterial material in mapMaterials) - { - material.Material.SaveShaders(shaderSaveDirectory, material.Type, material.IsTerrain); - } + material.Material.SaveShaders(shaderSaveDirectory, material.Type, material.IsTerrain); } } } diff --git a/Tiger/Exporters/Source2Handler.cs b/Tiger/Exporters/Source2Handler.cs index 61d395b8..213a387c 100644 --- a/Tiger/Exporters/Source2Handler.cs +++ b/Tiger/Exporters/Source2Handler.cs @@ -21,18 +21,12 @@ namespace Tiger.Exporters; public class SBoxHandler { - private static ConfigSubsystem _config = CharmInstance.GetSubsystem(); - public static bool sboxShaders = _config.GetSBoxShaderExportEnabled(); - public static bool sboxModels = _config.GetSBoxModelExportEnabled(); - public static bool sboxMaterials = _config.GetSBoxMaterialExportEnabled(); - public static void SaveStaticVMDL(string savePath, string staticMeshName, List staticMesh) { try { if (!File.Exists($"{savePath}/{staticMeshName}.vmdl")) { - //Source 2 shit File.Copy("Exporters/template.vmdl", $"{savePath}/{staticMeshName}.vmdl", true); string text = File.ReadAllText($"{savePath}/{staticMeshName}.vmdl"); @@ -148,85 +142,91 @@ public static void SaveTerrainVMDL(string savePath, string hash, List terrainDyemaps = null) { - Directory.CreateDirectory($"{savePath}/Materials"); + if (isTerrain) + savePath = $"{savePath}/Materials/Terrain"; + else + savePath = $"{savePath}/Materials"; + + Directory.CreateDirectory(savePath); StringBuilder vmat = new StringBuilder(); - vmat.AppendLine("Layer0 \n{"); + vmat.AppendLine("Layer0\n{"); //If the shader doesnt exist, just use the default complex.shader - if (!File.Exists($"{savePath}/Shaders/PS_{materialHeader.PixelShader?.Hash}.shader")) + //if (!File.Exists($"{savePath}/Shaders/PS_{materialHeader.PixelShader?.Hash}.shader")) + //{ + // vmat.AppendLine($" shader \"complex.shader\""); + + // //Use just the first texture for the diffuse + // if (materialHeader.EnumeratePSTextures().Any()) + // { + // if (materialHeader.EnumeratePSTextures().ElementAt(0).Texture is not null) + // vmat.AppendLine($" TextureColor \"Textures/{materialHeader.EnumeratePSTextures().ElementAt(0).Texture.Hash}.png\""); + // } + //} + + //Material parameters + vmat.AppendLine($"\tshader \"ps_{materialHeader.PixelShader.Hash}.shader\""); + vmat.AppendLine($"\tF_ALPHA_TEST 1"); + vmat.AppendLine($"\tF_ADDITIVE_BLEND 1"); + + if(materialHeader.Unk0C != 0) + vmat.AppendLine($"\tF_RENDER_BACKFACES 1"); + + //Textures + foreach (var e in materialHeader.EnumeratePSTextures()) { - vmat.AppendLine($" shader \"complex.shader\""); + if (e.Texture == null) + continue; - //Use just the first texture for the diffuse - if (materialHeader.EnumeratePSTextures().Any()) - { - if (materialHeader.EnumeratePSTextures().ElementAt(0).Texture is not null) - vmat.AppendLine($" TextureColor \"Textures/{materialHeader.EnumeratePSTextures().ElementAt(0).Texture.Hash}.png\""); - } + vmat.AppendLine($"\tTextureT{e.TextureIndex} \"Textures/{e.Texture.Hash}.png\""); } - else - { - vmat.AppendLine($"\tshader \"ps_{materialHeader.PixelShader.Hash}.shader\""); - vmat.AppendLine($"\tF_ALPHA_TEST 1"); - vmat.AppendLine($"\tF_ADDITIVE_BLEND 1"); - if(materialHeader.Unk0C != 0) + if(terrainDyemaps is not null) + foreach(var tex in terrainDyemaps) { - vmat.AppendLine($"\tF_RENDER_BACKFACES 1"); + vmat.AppendLine($"\tTextureT14_{tex.Key} \"Textures/{tex.Value}.png\""); } - TfxBytecodeInterpreter bytecode = new(TfxBytecodeOp.ParseAll(materialHeader.PS_TFX_Bytecode)); - var bytecode_hlsl = bytecode.Evaluate(materialHeader.PS_TFX_Bytecode_Constants); - - vmat.AppendLine($"\tDynamicParams\r\n\t{{"); - foreach (var entry in bytecode_hlsl) - { - vmat.AppendLine($"\t\tcb0_{entry.Key} \"{entry.Value}\""); - } - - vmat.AppendLine($"\t\tcb2_0 \"float4(0,1,1,1)\""); - vmat.AppendLine($"\t\tcb2_1 \"float4(0,1,1,1)\""); - - for(int i = 0; i < 37; i++) - { - if(i < 5) - vmat.AppendLine($"\t\tcb8_{i} \"float4(0,0,0,0)\""); - else - vmat.AppendLine($"\t\tcb8_{i} \"float4(1,1,1,1)\""); - } - - vmat.AppendLine($"\t\tcb12_4 \"float4(1,0,0,0)\""); - vmat.AppendLine($"\t\tcb12_5 \"float4(0,1,0,0)\""); - vmat.AppendLine($"\t\tcb12_6 \"float4(0,0,1,0)\""); + //vmat.AppendLine(PopulateCBuffers(materialHeader.Decompile(materialHeader.VertexShader.GetBytecode(), $"vs{materialHeader.VertexShader.Hash}"), materialHeader, true).ToString()); + vmat.AppendLine(PopulateCBuffers(materialHeader).ToString()); - vmat.AppendLine($"\t\tcb13_0 \"Time\""); - vmat.AppendLine($"\t\tcb13_1 \"float4(1,1,1,1)\""); + //Dynamic expressions + TfxBytecodeInterpreter bytecode = new(TfxBytecodeOp.ParseAll(materialHeader.PS_TFX_Bytecode)); + var bytecode_hlsl = bytecode.Evaluate(materialHeader.PS_TFX_Bytecode_Constants); - vmat.AppendLine($"\t}}"); + vmat.AppendLine($"\tDynamicParams\r\n\t{{"); + foreach (var entry in bytecode_hlsl) + { + vmat.AppendLine($"\t\tcb0_{entry.Key} \"{entry.Value}\""); } - foreach (var e in materialHeader.EnumeratePSTextures()) - { - if (e.Texture == null) - continue; + vmat.AppendLine($"\t\tcb2_0 \"float4(0,1,1,1)\""); + vmat.AppendLine($"\t\tcb2_1 \"float4(0,1,1,1)\""); - vmat.AppendLine($"\tTextureT{e.TextureIndex} \"Textures/{e.Texture.Hash}.png\""); + for (int i = 0; i < 37; i++) + { + if (i < 5) + vmat.AppendLine($"\t\tcb8_{i} \"float4(0,0,0,0)\""); + else + vmat.AppendLine($"\t\tcb8_{i} \"float4(1,1,1,1)\""); } - //vmat.AppendLine(PopulateCBuffers(materialHeader.Decompile(materialHeader.VertexShader.GetBytecode(), $"vs{materialHeader.VertexShader.Hash}"), materialHeader, true).ToString()); - vmat.AppendLine(PopulateCBuffers(materialHeader).ToString()); - vmat.AppendLine("}"); + vmat.AppendLine($"\t\tcb12_4 \"float4(1,0,0,0)\""); + vmat.AppendLine($"\t\tcb12_5 \"float4(0,1,0,0)\""); + vmat.AppendLine($"\t\tcb12_6 \"float4(0,0,1,0)\""); - string terrainDir = isTerrain ? "/Terrain/" : ""; - if (isTerrain) - Directory.CreateDirectory($"{savePath}/materials/{terrainDir}"); + vmat.AppendLine($"\t\tcb13_0 \"Time\""); + vmat.AppendLine($"\t\tcb13_1 \"float4(1,1,1,1)\""); + vmat.AppendLine($"\t}}"); + + vmat.AppendLine("}"); try { - File.WriteAllText($"{savePath}/materials/{terrainDir}{hash}.vmat", vmat.ToString()); + File.WriteAllText($"{savePath}/{hash}.vmat", vmat.ToString()); } catch (IOException) { diff --git a/Tiger/Schema/Shaders/Material.cs b/Tiger/Schema/Shaders/Material.cs index 5462c93c..c31756e7 100644 --- a/Tiger/Schema/Shaders/Material.cs +++ b/Tiger/Schema/Shaders/Material.cs @@ -144,7 +144,8 @@ public void SaveShaders(string saveDirectory, MaterialType type, bool isTerrain if (vfx != String.Empty) { File.WriteAllText($"{saveDirectory}/Shaders/PS_{PixelShader.Hash}.shader", vfx); - SBoxHandler.SaveVMAT(saveDirectory, FileHash, this, isTerrain); + if(!isTerrain) + SBoxHandler.SaveVMAT(saveDirectory, FileHash, this); } } catch (IOException) // threading error diff --git a/Tiger/Schema/Static/Terrain.cs b/Tiger/Schema/Static/Terrain.cs index e8131c21..fa02f15d 100644 --- a/Tiger/Schema/Static/Terrain.cs +++ b/Tiger/Schema/Static/Terrain.cs @@ -26,15 +26,15 @@ public void LoadIntoExporter(ExporterScene scene, string saveDirectory, bool exp for (int i = 0; i < _tag.MeshGroups.Count; i++) { var partEntry = _tag.MeshGroups[i]; - var lastValidEntry = _tag.MeshGroups.LastOrDefault(e => e.Dyemap != null); + Texture lastValidEntry = partEntry.Dyemap; //Use the last valid dyemap for any invalid if (partEntry.Dyemap == null) { - if (lastValidEntry.Dyemap != null) + if (lastValidEntry != null) { - scene.Textures.Add(lastValidEntry.Dyemap); - dyeMaps.Add(lastValidEntry.Dyemap); + scene.Textures.Add(lastValidEntry); + dyeMaps.Add(lastValidEntry); } } else @@ -77,41 +77,17 @@ public void LoadIntoExporter(ExporterScene scene, string saveDirectory, bool exp TransformPositions(part.Key); TransformTexcoords(part.Key); TransformVertexColors(part.Key); - } - scene.AddStatic(Hash, parts.Keys.ToList()); - // We need to add these textures after the static is initialised - foreach (var part in parts) - { - Texture dyemap = _tag.MeshGroups[part.Key.GroupIndex].Dyemap; - if (dyemap != null) + Dictionary extraTexs = new(); + for (int i = 0; i < dyeMaps.Count; i++) //Add all the dyemaps to the vmat { - if (CharmInstance.GetSubsystem().GetSBoxShaderExportEnabled()) - { - if (File.Exists($"{saveDirectory}/materials/Terrain/{part.Value.FileHash}.vmat")) - { - string[] vmat = File.ReadAllLines($"{saveDirectory}/materials/Terrain/{part.Value.FileHash}.vmat"); - int lastBraceIndex = Array.FindLastIndex(vmat, line => line.Trim().Equals("}")); //Searches for the last brace (}) - bool textureFound = Array.Exists(vmat, line => line.Trim().StartsWith("TextureT14")); - if (!textureFound && lastBraceIndex != -1) - { - var newVmat = vmat.Take(lastBraceIndex).ToList(); - - for (int i = 0; i < dyeMaps.Count; i++) //Add all the dyemaps to the vmat - { - newVmat.Add($" TextureT{terrainTextureIndex}_{i} \"Textures/{dyeMaps[i].Hash}.png\""); - } - - newVmat.AddRange(vmat.Skip(lastBraceIndex)); - File.WriteAllLines($"{saveDirectory}/materials/Terrain/{Hash}_{part.Value.FileHash}.vmat", newVmat); - } - } - File.Delete($"{saveDirectory}/materials/Terrain/{part.Value.FileHash}.vmat"); //Delete the old vmat, dont need it anymore - } + extraTexs.Add(i, dyeMaps[i].Hash); } + SBoxHandler.SaveVMAT(saveDirectory, $"{Hash}_{part.Value.FileHash}", part.Value, true, extraTexs); } - if (CharmInstance.GetSubsystem().GetSBoxModelExportEnabled()) - SBoxHandler.SaveTerrainVMDL(saveDirectory, Hash, parts.Keys.ToList(), TagData); + + scene.AddStatic(Hash, parts.Keys.ToList()); + SBoxHandler.SaveTerrainVMDL(saveDirectory, Hash, parts.Keys.ToList(), TagData); } public StaticPart MakePart(SStaticPart entry)