Skip to content

Commit

Permalink
Initial release with -bc1 compression only.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fusionette committed Jun 26, 2022
1 parent 4ece003 commit 1c268ca
Show file tree
Hide file tree
Showing 5 changed files with 198 additions and 2 deletions.
32 changes: 32 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@

#Ignore thumbnails created by Windows
Thumbs.db
#Ignore files built by Visual Studio
*.obj
*.exe
*.pdb
*.user
*.aps
*.pch
*.vspscc
*_i.c
*_p.c
*.ncb
*.suo
*.tlb
*.tlh
*.bak
*.cache
*.ilk
*.log
[Bb]in
[Dd]ebug*/
*.lib
*.sbr
obj/
[Rr]elease*/
_ReSharper*/
[Tt]est[Rr]esult*
.vs/
#Nuget packages folder
packages/
126 changes: 126 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
using System;
using System.Diagnostics;
using System.IO;

namespace texture_converter
{
class Program
{
static void Main(string[] args)
{
if (CheckRequirements())
{
string[] sourceFolders = Directory.GetDirectories("texture_sources", "*", SearchOption.AllDirectories);
foreach (string folder in sourceFolders)
{
ProcessFolder(folder);
}
Console.ForegroundColor = ConsoleColor.Green;
}

Console.WriteLine("Done. Press ENTER to exit.");
Console.ResetColor();
Console.ReadLine();
}

static bool CheckRequirements()
{
string[] nvcompress = { "nvcompress.exe", "cudart.dll", "jpeg62.dll", "libpng12.dll", "nvtt.dll", "zlib1.dll" };
foreach (string filename in nvcompress)
{
if (!File.Exists("nvcompress/" + filename))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("NVIDIA Compression tool not found.");
return false;
}
}
if (!Directory.Exists("texture_sources"))
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Folder texture_sources does not exist.");
return false;
}
return true;
}

static void ProcessFolder(string folder)
{
if (!folder.StartsWith("texture_sources", StringComparison.InvariantCultureIgnoreCase)) return;

Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(folder);
Console.ResetColor();

string[] sourceFiles = Directory.GetFiles(folder, "*", SearchOption.TopDirectoryOnly);
foreach (string fileName in sourceFiles)
{
ProcessFile(folder, fileName);
}
}

static void ProcessFile(string folder, string fullName)
{
if (File.Exists("texture_converter.dds")) File.Delete("texture_converter.dds");

if (fullName.EndsWith(".png", StringComparison.InvariantCultureIgnoreCase)
|| fullName.EndsWith(".jpg", StringComparison.InvariantCultureIgnoreCase)
|| fullName.EndsWith(".tga", StringComparison.InvariantCultureIgnoreCase))
{
// PNG, JPG and TGA are converted using the NVIDIA tool.
Process nvcompress = new Process();
nvcompress.StartInfo.FileName = "nvcompress/nvcompress.exe";
nvcompress.StartInfo.Arguments = "-bc1 " + fullName + " texture_converter.dds";
nvcompress.StartInfo.RedirectStandardOutput = true;
nvcompress.Start();
nvcompress.WaitForExit();
WriteTexture(folder, fullName);
return;
}

if (fullName.EndsWith(".dds", StringComparison.InvariantCultureIgnoreCase))
{
// DDS files are copied as-is and we just attach the header.
File.Copy(fullName, "texture_converter.dds");
WriteTexture(folder, fullName);
return;
}
}

static void WriteTexture(string folder, string fullName)
{
if (!File.Exists("texture_converter.dds")) return;
Console.Write(" " + Path.GetFileName(fullName));

// Assumes the full path starts with texture_sources and ends with a 3-character extension.
string textureName = "texture_library" + fullName.Substring(15).Replace("\\", "/").Substring(0, fullName.Length - 19);

// Read the converted DDS file and get the image dimensions.
byte[] ddsData = File.ReadAllBytes("texture_converter.dds");
int ddsWidth = BitConverter.ToInt32(ddsData, 16);
int ddsHeight = BitConverter.ToInt32(ddsData, 12);
int[] texFlags = { 0, 0, 0, 844649472 };

Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(" " + ddsWidth.ToString() + "x" + ddsHeight.ToString());
Console.ResetColor();

Directory.CreateDirectory("texture_library" + folder.Substring(15));
using (FileStream fs = new FileStream(textureName + ".texture", FileMode.Create))
{
char[] internalName = (textureName + ".dds").ToCharArray();
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(textureName.Length + 37);
bw.Write(ddsData.Length);
bw.Write(ddsWidth);
bw.Write(ddsHeight);
foreach (int i in texFlags) bw.Write(i);
bw.Write(internalName);
bw.Write((byte)0);
bw.Write(ddsData);
bw.Close();
}
File.Delete("texture_converter.dds");
}
}
}
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
# TextureConverter
Generate texture files using the NVIDIA tool.
# Texture Converter

This program will scan all folders under texture_sources and convert any .tga, .png, .jpg or .dds files it finds to .texture format, mirroring the directory structure into texture_library.

By default, it will call nvcompress.exe using the -bc1 compression setting, which is good for world textures and creates mipmaps. It will also set the texture width and height to whatever the original image size is.

The tool will not attempt to recompress .dds files, it will instead copy them as-is and just attach the .texture header.
8 changes: 8 additions & 0 deletions texture_converter.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

</Project>
25 changes: 25 additions & 0 deletions texture_converter.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.31829.152
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "texture_converter", "texture_converter.csproj", "{39EC798B-44A6-4EA7-9A81-8A84CB7F687C}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{39EC798B-44A6-4EA7-9A81-8A84CB7F687C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{39EC798B-44A6-4EA7-9A81-8A84CB7F687C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{39EC798B-44A6-4EA7-9A81-8A84CB7F687C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{39EC798B-44A6-4EA7-9A81-8A84CB7F687C}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {484307EC-6068-4417-83CE-CB89A411DE92}
EndGlobalSection
EndGlobal

0 comments on commit 1c268ca

Please sign in to comment.