Skip to content

Commit

Permalink
Implement emission texture dumping
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreter committed Sep 20, 2024
1 parent 0dc7a1f commit e1e9358
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
17 changes: 14 additions & 3 deletions Library/Textures/MicroSplatDump.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,21 @@ public static void DumpMicroSplat()
for (int i = 0; i < t2occl.depth; i++) DumpTexure(
string.Format("{0}/array.{1}.metallic.png", path, i),
t2occl, i, true, ExtractBlueFromTexture);
// for (int i = 0; i < t2occl.depth; i++) DumpTexure(
// string.Format("{0}/array.{1}.emissive.png", path, i),
// t2occl, i, true, ExtractEmissionFromTexture);
}

if (mesh.TexDiffuse is Texture2DArray t2diffx)
{
if (atlas.normalTexture is Texture2DArray t2normx)
{
int len = Mathf.Min(t2diffx.depth, t2normx.depth);
for (int i = 0; i < len; i++) DumpTexure2(
string.Format("{0}/array.{1}.emissive.png", path, i),
t2diffx, t2normx, i, true, ExtractEmissionFromTexture);
}

}


}

// ####################################################################
Expand Down
44 changes: 44 additions & 0 deletions Library/Textures/OcbTextureDumper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,35 @@ public static void DumpTexure(string path, Texture2DArray arr, int idx,
bool linear = true, Func<Color[], Color[]> converter = null)
=> DumpTexure(path, (Texture)arr, idx, linear, converter);

// Dump generic `Texture` to disk at given `path`
private static void DumpTexure2(string path, Texture src1, Texture src2, int idx = 0,
bool linear = true, Func<Color[], Color[], Color[]> converter = null)
{
if (src1 == null || src2 == null) return;
Texture2D tex1 = TextureFromGPU(src1, idx, linear);
Texture2D tex2 = TextureFromGPU(src2, idx, linear);
if (converter != null)
{
var pixels1 = tex1.GetPixels(0);
var pixels2 = tex2.GetPixels(0);
var pixels = converter(pixels1, pixels2);
tex1.SetPixels(pixels);
tex1.Apply(true, false);
}
byte[] bytes = tex1.EncodeToPNG();
File.WriteAllBytes(path, bytes);
}

// Dump `Texture2D` to disk at given `path`
public static void DumpTexure2(string path, Texture2D src1, Texture2D src2,
bool linear = true, Func<Color[], Color[], Color[]> converter = null)
=> DumpTexure2(path, src1, src2, 0, linear, converter);

// Dump `Texture2DArray[idx]` to disk at given `path`
public static void DumpTexure2(string path, Texture2DArray arr1, Texture2DArray arr2,
int idx, bool linear = true, Func<Color[], Color[], Color[]> converter = null)
=> DumpTexure2(path, (Texture)arr1, (Texture)arr2, idx, linear, converter);

// ####################################################################
// Converter helpers for various texture formats
// ####################################################################
Expand Down Expand Up @@ -141,6 +170,21 @@ public static Color[] ExtractHeightFromAlbedoTexture(Color[] pixels)
return pixels;
}

// Emission is stored in
public static Color[] ExtractEmissionFromTexture(Color[] pixels1, Color[] pixels2)
{
if (pixels1.Length != pixels2.Length) return null;
Color[] pixels = new Color[pixels1.Length];
for (int i = pixels1.Length - 1; i >= 0; i--)
{
pixels[i].r = pixels1[i].a;
pixels[i].g = pixels1[i].b;
pixels[i].b = pixels2[i].r;
pixels[i].a = pixels2[i].b;
}
return pixels;
}

// MicroSplat stores the height in the albedo alpha channel
public static Color[] RemoveHeightFromAlbedoTexture(Color[] pixels)
{
Expand Down
Binary file modified MicroSplat.dll
Binary file not shown.
2 changes: 1 addition & 1 deletion ModInfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
<Description value="Replace vanilla MicroSplat shader for better visual fidelity." />
<Website value="https://github.com/OCB7D2D/OcbMicroSplat" />
<Author value="ocbMaurice" />
<Version value="0.9.2" />
<Version value="0.9.2.1" />
</xml>

0 comments on commit e1e9358

Please sign in to comment.