Skip to content

Commit

Permalink
add support for surface translucency in texture loader / cache (#82)
Browse files Browse the repository at this point in the history
* add support for surface translucency in texture loader / cache

* null checks
  • Loading branch information
gmriggs authored Jan 23, 2024
1 parent 36a99cd commit c4aab0d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
26 changes: 17 additions & 9 deletions ACViewer/Render/TextureCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class TextureCache
public static GraphicsDevice GraphicsDevice => GameView.Instance.GraphicsDevice;
public static SpriteBatch SpriteBatch => GameView.Instance.SpriteBatch;

public static Dictionary<TexturePalette, Texture2D> Textures { get; set; }
public static Dictionary<TextureChanges, Texture2D> Textures { get; set; }

public static List<Texture2D> Uncached { get; set; }

Expand Down Expand Up @@ -54,7 +54,7 @@ public static void Init(bool dispose = true)
GfxObjCache.Init();
SetupCache.Init();

Textures = new Dictionary<TexturePalette, Texture2D>();
Textures = new Dictionary<TextureChanges, Texture2D>();
Uncached = new List<Texture2D>();
}

Expand Down Expand Up @@ -138,7 +138,9 @@ private static Texture2D LoadTexture(uint textureID, bool useDummy = false, Surf
return _tex;

case SurfacePixelFormat.PFID_A8R8G8B8:
ConvertToABGR(data);
ConvertToBGRA(data);
if (surface != null && surface.Translucency > 0)
PremultiplyAlpha(data, 1.0f - surface.Translucency);
break;
}
}
Expand Down Expand Up @@ -291,16 +293,22 @@ private static byte[] AddAlpha(byte[] rgb)
return rgba;
}

private static void ConvertToABGR(byte[] argb)
private static void ConvertToBGRA(byte[] rgba)
{
for (var i = 0; i < argb.Length; i += 4)
for (var i = 0; i < rgba.Length; i += 4)
{
var tmp = argb[i];
argb[i] = argb[i + 2];
argb[i + 2] = tmp;
var tmp = rgba[i];
rgba[i] = rgba[i + 2];
rgba[i + 2] = tmp;
}
}

private static void PremultiplyAlpha(byte[] bgra, float alpha)
{
for (var i = 3; i < bgra.Length; i += 4)
bgra[i] = (byte)(bgra[i] * alpha);
}

private static byte[] IndexToColor(ACE.DatLoader.FileTypes.Texture texture, bool isClipMap = false, PaletteChanges paletteChanges = null)
{
var colors = GetColors(texture);
Expand Down Expand Up @@ -492,7 +500,7 @@ private static Texture2D GetTexture(uint textureID, Surface surface = null, Pale
{
//Console.WriteLine($"-> GetTexture({textureID:X8})");

var texturePalette = new TexturePalette(textureID, paletteChanges);
var texturePalette = new TextureChanges(textureID, surface?.Translucency ?? 0.0f, paletteChanges);

if (useCache && Textures.TryGetValue(texturePalette, out var cached))
return cached;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@

namespace ACViewer.Render
{
public class TexturePalette: IEquatable<TexturePalette>
public class TextureChanges: IEquatable<TextureChanges>
{
public uint TextureId { get; set; } // 0x6 texture file id
public float Translucency { get; set; }
public PaletteChanges PaletteChanges { get; set; }

public TexturePalette(uint textureId, PaletteChanges paletteChanges = null)
public TextureChanges(uint textureId, float translucency = 0.0f, PaletteChanges paletteChanges = null)
{
TextureId = textureId;
Translucency = translucency;
PaletteChanges = paletteChanges;
}

public bool Equals(TexturePalette tp)
public bool Equals(TextureChanges tp)
{
if (TextureId != tp.TextureId)
return false;

if (Translucency != tp.Translucency)
return false;

if (PaletteChanges == null && tp.PaletteChanges == null)
return true;

Expand All @@ -37,6 +42,8 @@ public override int GetHashCode()
int hash = 0;

hash = (hash * 397) ^ TextureId.GetHashCode();

hash = (hash * 397) ^ Translucency.GetHashCode();

if (PaletteChanges != null)
hash = (hash * 397) ^ PaletteChanges.GetHashCode();
Expand Down

0 comments on commit c4aab0d

Please sign in to comment.