Skip to content

Commit

Permalink
Save map cubemaps as vtex
Browse files Browse the repository at this point in the history
  • Loading branch information
DeltaDesigns committed Dec 20, 2023
1 parent e42e781 commit 9363235
Show file tree
Hide file tree
Showing 8 changed files with 216 additions and 31 deletions.
1 change: 1 addition & 0 deletions Charm/ActivityMapEntityView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,7 @@ private static void ExtractDataTables(List<FileHash> dataTables, string hash, st
break;
case CubemapResource cubemap:
dynamicScene.AddCubemap(cubemap);
dynamicScene.Textures.Add(cubemap.CubemapTexture);
break;
case SMapLightResource mapLight:
dynamicScene.AddMapLight(mapLight);
Expand Down
2 changes: 1 addition & 1 deletion Charm/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ private void CheckGameVersion()
private async void CheckVersion()
{
var currentVersion = new ApplicationVersion("2.0.0");
var versionChecker = new ApplicationVersionChecker("https://github.com/MontagueM/Charm/raw/main/", currentVersion);
var versionChecker = new ApplicationVersionChecker("https://github.com/DeltaDesigns/Charm/raw/main/", currentVersion);
versionChecker.LatestVersionName = "version";
try
{
Expand Down
30 changes: 0 additions & 30 deletions Charm/MapView.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,36 +147,6 @@ public static void ExportFullMap(Tag<SMapContainer> map, ExportTypeFlag exportTy
ExtractDataTables(map, savePath, scene, ExportTypeFlag.Full);
}

public static void ExportTerrainMap(Tag<SMapContainer> map)
{
ExporterScene scene = Exporter.Get().CreateScene($"{map.Hash}_Terrain", ExportType.Terrain);
bool export = false;
string meshName = map.Hash.ToString();
string savePath = _config.GetExportSavePath() + $"/{meshName}";
if (_config.GetSingleFolderMapsEnabled())
{
savePath = _config.GetExportSavePath() + "/Maps";
}

Directory.CreateDirectory(savePath);

Parallel.ForEach(map.TagData.MapDataTables, data =>
{
data.MapDataTable.TagData.DataEntries.ForEach(entry =>
{
if (entry.DataResource.GetValue(data.MapDataTable.GetReader()) is SMapTerrainResource terrainArrangement) // Terrain
{
terrainArrangement.Terrain.LoadIntoExporter(scene, savePath, _config.GetSBoxShaderExportEnabled());
if (exportStatics)
{
ExporterScene staticScene = Exporter.Get().CreateScene($"{terrainArrangement.Terrain.Hash}_Terrain", ExportType.StaticInMap);
terrainArrangement.Terrain.LoadIntoExporter(staticScene, savePath, true);
}
}
});
});
}

private static void ExtractDataTables(Tag<SMapContainer> map, string savePath, ExporterScene scene, ExportTypeFlag exportTypeFlag)
{
Parallel.ForEach(map.TagData.MapDataTables, data =>
Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions Tiger/Exporters/MaterialExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ public override void Export(Exporter.ExportEventArgs args)
if (texture is null)
continue;
texture.SavetoFile($"{textureSaveDirectory}/{texture.Hash}");
if(texture.IsCubemap())
{
SBoxHandler.SaveCubemapVTEX(texture, textureSaveDirectory);
}
}

if (saveShaders)
Expand Down
126 changes: 126 additions & 0 deletions Tiger/Exporters/Source2Handler.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
using System;
using System.IO;
using System.Reflection;
using System.Text;
using System.Text.Json;
using Arithmic;
using Tiger.Schema;
using Tiger.Schema.Entity;
using Tiger.Schema.Shaders;
using Tiger.Schema.Static;

using Pfim;
using Pfim.dds;
using ValveResourceFormat;
using ValveResourceFormat.ResourceTypes;

using ValveTexture = ValveResourceFormat.ResourceTypes.Texture;
using Texture = Tiger.Schema.Texture;

namespace Tiger.Exporters;

public class SBoxHandler
Expand Down Expand Up @@ -282,4 +293,119 @@ public static StringBuilder PopulateCBuffers(IMaterial materialHeader, bool isVe

return cbuffers;
}

public static void SaveCubemapVTEX(Texture tex, string savePath)
{
if(tex.IsCubemap())
{
var file = TextureFile.CreateDefault(tex, ImageDimension.CUBEARRAY);
var json = JsonSerializer.Serialize(file, JsonSerializerOptions.Default);
File.WriteAllText($"{savePath}/{tex.Hash}.vtex", json);

//DDS2Vtex(tex, savePath);
}
}

//Doesnt really work but im gonna leave it here anyways i guess
//public static void DDS2Vtex(Texture tex, string savePath)
//{
// using var img = Pfimage.FromStream(tex.GetTexture(), new PfimConfig(decompress: false));
// var dds = img as Dds;

// if (dds == null)
// {
// Console.Error.WriteLine($"Error, file is not a DDS, but a {img.GetType()} (format {img.Format}, datalen {img.DataLen})");
// }

// var flags = VTexFlags.NO_LOD;
// var numMipLevels = (byte)1;

// if (dds.Header.MipMapCount != 0)
// {
// Console.Error.WriteLine("Warning, DDS has mipmaps, which may not work correctly.");
// flags &= ~VTexFlags.NO_LOD;
// numMipLevels = (byte)dds.Header.MipMapCount;
// }

// var format = dds switch
// {
// Dxt1Dds => VTexFormat.DXT1,
// Dxt5Dds => VTexFormat.DXT5,
// Bc6hDds => VTexFormat.BC6H,
// Bc7Dds => VTexFormat.BC7,
// _ => VTexFormat.UNKNOWN,
// };

// if (format == VTexFormat.UNKNOWN)
// {
// Console.Error.WriteLine($"Error, do not handle DDS with format {dds.GetType()}.");
// }

// // TODO: check blocksize
// using FileStream stream = new FileStream($"{savePath}/{tex.Hash}.vtex_c", FileMode.Create);
// using var writer = new BinaryWriter(stream);
// ValveTexture vtex = null!;
// var nonDataSize = 0;
// var offsetOfDataSize = 0;

// using (var resource = new Resource())
// {
// var assembly = Assembly.GetExecutingAssembly();
// using var template = assembly.GetManifestResourceStream("vtex.template");
// resource.Read(template);
// vtex = (ValveTexture)resource.DataBlock;

// // Write a copy of the vtex_c up to the DATA block region
// nonDataSize = (int)resource.DataBlock.Offset;

// resource.Reader.BaseStream.Seek(8, SeekOrigin.Begin);
// var blockOffset = resource.Reader.ReadUInt32();
// var blockCount = resource.Reader.ReadUInt32();
// resource.Reader.BaseStream.Seek(blockOffset - 8, SeekOrigin.Current); // 8 is 2 uint32s we just read
// for (var i = 0; i < blockCount; i++)
// {
// var blockType = Encoding.UTF8.GetString(resource.Reader.ReadBytes(4));
// resource.Reader.BaseStream.Position += 8; // Offset, size
// if (blockType == "DATA")
// {
// offsetOfDataSize = (int)resource.Reader.BaseStream.Position - 4;
// break;
// }
// }

// resource.Reader.BaseStream.Position = 0;
// writer.Write(resource.Reader.ReadBytes(nonDataSize).ToArray());
// }

// // Write the VTEX data
// writer.Write(vtex.Version);
// writer.Write((ushort)flags);
// writer.Write(vtex.Reflectivity[0]);
// writer.Write(vtex.Reflectivity[1]);
// writer.Write(vtex.Reflectivity[2]);
// writer.Write(vtex.Reflectivity[3]);
// writer.Write((ushort)dds.Width);
// writer.Write((ushort)dds.Height);
// writer.Write((ushort)(dds.Header.Depth != 0 ? dds.Header.Depth : 1));
// writer.Write((byte)format);
// writer.Write((byte)numMipLevels);
// writer.Write((uint)0);

// // Extra data
// writer.Write((uint)0);
// writer.Write((uint)0);

// var resourceSize = (uint)stream.Length;
// var resourceDataSize = (uint)(resourceSize - nonDataSize);

// // Dxt data goes here
// writer.Write(dds.Data);

// // resource: fixup the full and DATA block size
// writer.Seek(0, SeekOrigin.Begin);
// writer.Write(resourceSize);

// writer.Seek(offsetOfDataSize, SeekOrigin.Begin);
// writer.Write(resourceDataSize);
//}
}
80 changes: 80 additions & 0 deletions Tiger/Exporters/VTEXTextureFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
using Tiger.Schema;
using Tiger.Schema.Entity;

namespace Tiger.Exporters
{
public enum GammaType
{
Linear,
SRGB,
}

public enum ImageFormatType
{
DXT5,
DXT1,
RGBA8888,
BC7,
BC6H,
}

public enum ImageDimension
{
[Description("1D")]
_1D,
[Description("2D")]
_2D,
[Description("3D")]
_3D,
[Description("1DArray")]
_1DARRAY,
[Description("2DArray")]
_2DARRAY,
[Description("3DArray")]
_3DARRAY,
[Description("CUBE")]
CUBE,
[Description("CUBEARRAY")]
CUBEARRAY
}

public class TextureFile
{
public List<string> Images { get; set; }

public string InputColorSpace { get; set; }

public string OutputFormat { get; set; }

public string OutputColorSpace { get; set; }

public string OutputTypeString { get; set; }

public static TextureFile CreateDefault(Texture texture, ImageDimension dimension)
{
return new TextureFile
{
Images = new List<string> { $"textures/{texture.Hash}.png" },
OutputFormat = ImageFormatType.RGBA8888.ToString(),
OutputColorSpace = (texture.IsSrgb() ? GammaType.SRGB : GammaType.Linear).ToString(),
InputColorSpace = (texture.IsSrgb() ? GammaType.SRGB : GammaType.Linear).ToString(),
OutputTypeString = GetDisplayName(dimension)
};
}

private static string GetDisplayName(ImageDimension value)
{
var field = value.GetType().GetField(value.ToString());
var attribute = (DescriptionAttribute)Attribute.GetCustomAttribute(field, typeof(DescriptionAttribute));

return attribute == null ? value.ToString() : attribute.Description;
}
}
}
4 changes: 4 additions & 0 deletions Tiger/Schema/Shaders/Material.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,10 @@ public void SaveMaterial(string saveDirectory)
continue;

texture.Texture.SavetoFile($"{saveDirectory}/Textures/{texture.Texture.Hash}");
if (texture.Texture.IsCubemap())
{
SBoxHandler.SaveCubemapVTEX(texture.Texture, $"{saveDirectory}/Textures");
}
}
}

Expand Down

0 comments on commit 9363235

Please sign in to comment.