Skip to content

Commit

Permalink
Added mipmap bias and configuration file
Browse files Browse the repository at this point in the history
  • Loading branch information
ducakar committed Oct 31, 2014
1 parent 25bd797 commit afcb829
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
50 changes: 26 additions & 24 deletions DatabaseLoaderTexture_DDS.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

namespace DDSLoader
{
[DatabaseLoaderAttrib(new[] {"dds"})]
[DatabaseLoaderAttrib(new[] { "dds" })]
public class DatabaseLoaderTexture_DDS : DatabaseLoader<GameDatabase.TextureInfo>
{
private const uint DDSD_MIPMAPCOUNT_BIT = 0x00020000;
Expand Down Expand Up @@ -96,8 +96,6 @@ public static GameDatabase.TextureInfo LoadDDS(UrlDir.UrlFile urlFile)
int dwCaps4 = (int)reader.ReadUInt32();
int dwReserved2 = (int)reader.ReadUInt32();

long dxtBytesLength = reader.BaseStream.Length - 128;

TextureFormat textureFormat = TextureFormat.ARGB32;
bool isCompressed = false;
bool isNormalMap = (dds_pxlf_dwFlags & DDPF_NORMAL) != 0 || urlFile.name.EndsWith("NRM");
Expand Down Expand Up @@ -129,35 +127,39 @@ public static GameDatabase.TextureInfo LoadDDS(UrlDir.UrlFile urlFile)
return null;
}

byte[] dxtBytes = reader.ReadBytes((int)dxtBytesLength);
long dataBias = 128;

// Swap red and blue.
if (!isCompressed)
if (Settings.MipmapBias != 0 || Settings.NormalMipmapBias != 0)
{
int mipmapWidth = dwWidth;
int mipmapHeight = dwHeight;
int lineStart = 0;
int bias = isNormalMap ? Settings.NormalMipmapBias : Settings.MipmapBias;
int blockSize = textureFormat == TextureFormat.DXT1 ? 8 : 16;
int levels = Math.Min(bias, dwMipMapCount - 1);

for (int i = 0; i < dwMipMapCount; ++i)
for (int i = 0; i < levels; ++i)
{
int mipmapPitch = mipmapWidth * pixelSize;
dataBias += isCompressed
? ((dwWidth + 3) / 4) * ((dwHeight + 3) / 4) * blockSize
: dwWidth * dwHeight * pixelSize;

for (int y = 0; y < mipmapHeight; ++y, lineStart += mipmapPitch)
{
int pos = lineStart;
dwWidth = Math.Max(1, dwWidth / 2);
dwHeight = Math.Max(1, dwHeight / 2);
}
}

for (int x = 0; x < mipmapWidth; ++x, pos += pixelSize)
{
byte b = dxtBytes[pos + 0];
byte r = dxtBytes[pos + 2];
long dxtBytesLength = reader.BaseStream.Length - dataBias;
reader.BaseStream.Seek(dataBias, SeekOrigin.Begin);
byte[] dxtBytes = reader.ReadBytes((int)dxtBytesLength);

dxtBytes[pos + 0] = r;
dxtBytes[pos + 2] = b;
}
}
// Swap red and blue.
if (!isCompressed)
{
for (int i = 0; i < dxtBytes.Length; i += pixelSize)
{
byte b = dxtBytes[i + 0];
byte r = dxtBytes[i + 2];

mipmapWidth = Math.Max(1, mipmapWidth / 2);
mipmapHeight = Math.Max(1, mipmapHeight / 2);
dxtBytes[i + 0] = r;
dxtBytes[i + 2] = b;
}
}

Expand Down
25 changes: 25 additions & 0 deletions Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using UnityEngine;

namespace DDSLoader
{
[KSPAddon(KSPAddon.Startup.Instantly, false)]
public class Settings : MonoBehaviour
{
public static int MipmapBias = 0;
public static int NormalMipmapBias = 0;

public void Awake()
{
foreach (var config in GameDatabase.Instance.GetConfigs("DDSLoader"))
{
string sMipmapBias = config.config.GetValue("mipmapBias");
if (sMipmapBias != null)
int.TryParse(sMipmapBias, out MipmapBias);

string sNormalMipmapBias = config.config.GetValue("normalMipmapBias");
if (sNormalMipmapBias != null)
int.TryParse(sNormalMipmapBias, out NormalMipmapBias);
}
}
}
}

0 comments on commit afcb829

Please sign in to comment.