Skip to content

Commit

Permalink
Clean up terrain exports, always export vmat/vmdl/shader
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaDesigns committed Dec 24, 2023
1 parent 3f2124c commit ecf4903
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 190 deletions.
5 changes: 1 addition & 4 deletions Charm/EntityView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ public static void Export(List<Entity> 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
Expand Down
4 changes: 1 addition & 3 deletions Charm/MapView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ public partial class MapView : UserControl
private static MainWindow _mainWindow = null;

private static ConfigSubsystem _config = CharmInstance.GetSubsystem<ConfigSubsystem>();

private static bool sboxModels = _config.GetSBoxModelExportEnabled();
private static bool exportStatics = _config.GetIndvidualStaticsEnabled();

private void OnControlLoaded(object sender, RoutedEventArgs routedEventArgs)
Expand Down Expand Up @@ -155,7 +153,7 @@ private static void ExtractDataTables(Tag<SMapContainer> 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);
}
});
});
Expand Down
34 changes: 0 additions & 34 deletions Charm/SBoxConfigView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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}";
Expand Down
37 changes: 0 additions & 37 deletions Tiger/ConfigSubsystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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; } = "";
}
Expand Down Expand Up @@ -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
Expand Down
21 changes: 7 additions & 14 deletions Tiger/Exporters/MaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public override void Export(Exporter.ExportEventArgs args)
{
ConcurrentHashSet<Texture> mapTextures = new();
ConcurrentHashSet<ExportMaterial> mapMaterials = new();
bool saveShaders = ConfigSubsystem.Get().GetSBoxShaderExportEnabled();

Parallel.ForEach(args.Scenes, scene =>
{
Expand All @@ -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";
Expand Down Expand Up @@ -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);
}
}
}
124 changes: 62 additions & 62 deletions Tiger/Exporters/Source2Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,12 @@ namespace Tiger.Exporters;

public class SBoxHandler
{
private static ConfigSubsystem _config = CharmInstance.GetSubsystem<ConfigSubsystem>();
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<StaticPart> 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");

Expand Down Expand Up @@ -148,85 +142,91 @@ public static void SaveTerrainVMDL(string savePath, string hash, List<StaticPart
}
}

public static void SaveVMAT(string savePath, string hash, IMaterial materialHeader, bool isTerrain = false)
public static void SaveVMAT(string savePath, string hash, IMaterial materialHeader, bool isTerrain = false, Dictionary<int, FileHash> 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)
{
Expand Down
3 changes: 2 additions & 1 deletion Tiger/Schema/Shaders/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit ecf4903

Please sign in to comment.