Skip to content

Commit

Permalink
Some cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaDesigns committed Dec 19, 2023
1 parent 974e82d commit 2e93163
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 503 deletions.
8 changes: 4 additions & 4 deletions Tiger/Exporters/MaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public override void Export(Exporter.ExportEventArgs args)

if (saveShaders)
{
string shaderSaveDirectory = $"{args.OutputDirectory}/{scene.Name}/Shaders";
material.Material.SavePixelShader(shaderSaveDirectory, material.IsTerrain);
string shaderSaveDirectory = $"{args.OutputDirectory}/{scene.Name}";
material.Material.SaveShaders(shaderSaveDirectory, material.IsTerrain);
material.Material.SaveVertexShader(shaderSaveDirectory);
}
}
Expand Down Expand Up @@ -86,11 +86,11 @@ public override void Export(Exporter.ExportEventArgs args)

if (saveShaders)
{
string shaderSaveDirectory = $"{args.OutputDirectory}/Maps/Shaders";
string shaderSaveDirectory = $"{args.OutputDirectory}/Maps";
Directory.CreateDirectory(shaderSaveDirectory);
foreach (ExportMaterial material in mapMaterials)
{
material.Material.SavePixelShader(shaderSaveDirectory, material.IsTerrain);
material.Material.SaveShaders(shaderSaveDirectory, material.IsTerrain);
material.Material.SaveVertexShader(shaderSaveDirectory);
}
}
Expand Down
45 changes: 20 additions & 25 deletions Tiger/Exporters/Source2Handler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,19 +139,21 @@ public static void SaveTerrainVMDL(string savePath, string hash, List<StaticPart

public static void SaveVMAT(string savePath, string hash, IMaterial materialHeader, bool isTerrain = false)
{
Directory.CreateDirectory($"{savePath}/Materials");
StringBuilder vmat = new StringBuilder();

vmat.AppendLine("Layer0 \n{");

//If the shader doesnt exist, just use the default complex.shader
if (!File.Exists($"{savePath}/SBox/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 \"materials/Textures/{materialHeader.EnumeratePSTextures().ElementAt(0).Texture.Hash}.png\"");
vmat.AppendLine($" TextureColor \"Textures/{materialHeader.EnumeratePSTextures().ElementAt(0).Texture.Hash}.png\"");
}
}
else
Expand All @@ -174,6 +176,7 @@ public static void SaveVMAT(string savePath, string hash, IMaterial materialHead
vmat.AppendLine($"\t\tcb0_{entry.Key} \"{entry.Value}\"");
}
vmat.AppendLine($"\t\tcb13_0 \"Time\"");
vmat.AppendLine($"\t\tcb13_1 \"float4(1,1,1,1)\"");
vmat.AppendLine($"\t}}");
}

Expand All @@ -182,7 +185,7 @@ public static void SaveVMAT(string savePath, string hash, IMaterial materialHead
if (e.Texture == null)
continue;

vmat.AppendLine($"\tTextureT{e.TextureIndex} \"materials/Textures/{e.Texture.Hash}.png\"");
vmat.AppendLine($"\tTextureT{e.TextureIndex} \"Textures/{e.Texture.Hash}.png\"");
}

//vmat.AppendLine(PopulateCBuffers(materialHeader.Decompile(materialHeader.VertexShader.GetBytecode(), $"vs{materialHeader.VertexShader.Hash}"), materialHeader, true).ToString());
Expand All @@ -191,17 +194,14 @@ public static void SaveVMAT(string savePath, string hash, IMaterial materialHead

string terrainDir = isTerrain ? "/Terrain/" : "";
if (isTerrain)
Directory.CreateDirectory($"{savePath}/SBox/materials/{terrainDir}");
Directory.CreateDirectory($"{savePath}/materials/{terrainDir}");

if (!File.Exists($"{savePath}/SBox/materials/{terrainDir}{hash}.vmat"))
try
{
File.WriteAllText($"{savePath}/materials/{terrainDir}{hash}.vmat", vmat.ToString());
}
catch (IOException)
{
try
{
File.WriteAllText($"{savePath}/SBox/materials/{terrainDir}{hash}.vmat", vmat.ToString());
}
catch (IOException)
{
}
}
}

Expand All @@ -216,31 +216,26 @@ public static void SaveDecalVMAT(string savePath, string hash, IMaterial materia
if (materialHeader.EnumeratePSTextures().Any())
{
if (materialHeader.EnumeratePSTextures().ElementAt(0).Texture is not null)
vmat.AppendLine($" TextureColor \"materials/Textures/{materialHeader.EnumeratePSTextures().ElementAt(0).Texture.Hash}.png\"");
vmat.AppendLine($" TextureColor \"Textures/{materialHeader.EnumeratePSTextures().ElementAt(0).Texture.Hash}.png\"");
}

foreach (var e in materialHeader.EnumeratePSTextures())
{
if (e.Texture == null)
{
continue;
}

vmat.AppendLine($" TextureT{e.TextureIndex} \"materials/Textures/{e.Texture.Hash}.png\"");
vmat.AppendLine($" TextureT{e.TextureIndex} \"Textures/{e.Texture.Hash}.png\"");
}

vmat.AppendLine("}");

if (!File.Exists($"{savePath}/SBox/materials/{hash}_decal.vmat"))
try
{
Directory.CreateDirectory($"{savePath}/materials/");
File.WriteAllText($"{savePath}/materials/{hash}_decal.vmat", vmat.ToString());
}
catch (IOException)
{
try
{
Directory.CreateDirectory($"{savePath}/SBox/materials/");
File.WriteAllText($"{savePath}/SBox/materials/{hash}_decal.vmat", vmat.ToString());
}
catch (IOException)
{
}
}
}

Expand Down
27 changes: 9 additions & 18 deletions Tiger/Schema/Shaders/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Tiger.Schema.Shaders
{

public struct TextureView
{
public string Dimension;
Expand Down Expand Up @@ -125,34 +124,26 @@ public string Decompile(byte[] shaderBytecode, string name, string savePath = "h
return hlsl;
}

public void SavePixelShader(string saveDirectory, bool isTerrain = false)
public void SaveShaders(string saveDirectory, bool isTerrain = false)
{
Directory.CreateDirectory($"{saveDirectory}/Shaders");
if (PixelShader != null && PixelShader.Hash.IsValid())
{
string pixel = Decompile(PixelShader.GetBytecode(), $"ps{PixelShader.Hash}");
string vertex = Decompile(VertexShader.GetBytecode(), $"vs{VertexShader.Hash}");
string vfx = SBoxHandler.sboxShaders ? new S2ShaderConverter().HlslToVfx(this, pixel, vertex, isTerrain) : "";

if (SBoxHandler.sboxShaders)
{
Directory.CreateDirectory($"{saveDirectory}/SBox");
Directory.CreateDirectory($"{saveDirectory}/SBox/materials");
}
string vfx = new S2ShaderConverter().HlslToVfx(this, pixel, vertex, isTerrain);

try
{
if (vfx != String.Empty && !File.Exists($"{saveDirectory}/SBox/PS_{PixelShader.Hash}.shader"))
if (vfx != String.Empty)
{
File.WriteAllText($"{saveDirectory}/SBox/PS_{PixelShader.Hash}.shader", vfx);
File.WriteAllText($"{saveDirectory}/Shaders/PS_{PixelShader.Hash}.shader", vfx);
SBoxHandler.SaveVMAT(saveDirectory, FileHash, this, isTerrain);
}
}
catch (IOException) // threading error
{
}

//Need to save vmat after shader has be exported, to check if it exists
if (SBoxHandler.sboxShaders)
SBoxHandler.SaveVMAT(saveDirectory, FileHash, this, isTerrain);
}
}

Expand All @@ -179,20 +170,20 @@ public void SaveVertexShader(string saveDirectory)
//Only useful for saving single material from DevView or MaterialView, better control for output compared to scene system
public void SaveMaterial(string saveDirectory)
{
var hlslPath = $"{saveDirectory}/Shaders/Raw";
var hlslPath = $"{saveDirectory}/Raw_Shaders";
var texturePath = $"{saveDirectory}/Textures";
Directory.CreateDirectory(hlslPath);
Directory.CreateDirectory(texturePath);

if (PixelShader != null)
{
Decompile(PixelShader.GetBytecode(), $"ps{PixelShader.Hash}", hlslPath);
SavePixelShader($"{saveDirectory}/Shaders/");
SaveShaders($"{saveDirectory}");
}
if (VertexShader != null)
{
Decompile(VertexShader.GetBytecode(), $"vs{VertexShader.Hash}", hlslPath);
SaveVertexShader($"{saveDirectory}/Shaders/");
SaveVertexShader($"{saveDirectory}");
}

foreach (STextureTag texture in EnumerateVSTextures())
Expand Down
Loading

0 comments on commit 2e93163

Please sign in to comment.