Skip to content

Commit

Permalink
Merge branch 'color-table'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ottermandias committed Sep 15, 2023
2 parents 652b2e9 + 916ff0c commit 25cdc00
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 218 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading;
using Dalamud.Game;
using Dalamud.Plugin.Services;
using FFXIVClientStructs.FFXIV.Client.Graphics.Kernel;
Expand All @@ -9,24 +8,24 @@

namespace Penumbra.Interop.MaterialPreview;

public sealed unsafe class LiveColorSetPreviewer : LiveMaterialPreviewerBase
public sealed unsafe class LiveColorTablePreviewer : LiveMaterialPreviewerBase
{
public const int TextureWidth = 4;
public const int TextureHeight = MtrlFile.ColorSet.RowArray.NumRows;
public const int TextureHeight = MtrlFile.ColorTable.NumRows;
public const int TextureLength = TextureWidth * TextureHeight * 4;

private readonly Framework _framework;

private readonly Texture** _colorSetTexture;
private readonly SafeTextureHandle _originalColorSetTexture;
private readonly Texture** _colorTableTexture;
private readonly SafeTextureHandle _originalColorTableTexture;

private Half[] _colorSet;
private Half[] _colorTable;
private bool _updatePending;

public Half[] ColorSet
=> _colorSet;
public Half[] ColorTable
=> _colorTable;

public LiveColorSetPreviewer(IObjectTable objects, Framework framework, MaterialInfo materialInfo)
public LiveColorTablePreviewer(IObjectTable objects, Framework framework, MaterialInfo materialInfo)
: base(objects, materialInfo)
{
_framework = framework;
Expand All @@ -35,17 +34,17 @@ public LiveColorSetPreviewer(IObjectTable objects, Framework framework, Material
if (mtrlHandle == null)
throw new InvalidOperationException("Material doesn't have a resource handle");

var colorSetTextures = ((Structs.CharacterBaseExt*)DrawObject)->ColorSetTextures;
var colorSetTextures = ((Structs.CharacterBaseExt*)DrawObject)->ColorTableTextures;
if (colorSetTextures == null)
throw new InvalidOperationException("Draw object doesn't have color set textures");
throw new InvalidOperationException("Draw object doesn't have color table textures");

_colorSetTexture = colorSetTextures + (MaterialInfo.ModelSlot * 4 + MaterialInfo.MaterialSlot);
_colorTableTexture = colorSetTextures + (MaterialInfo.ModelSlot * 4 + MaterialInfo.MaterialSlot);

_originalColorSetTexture = new SafeTextureHandle(*_colorSetTexture, true);
if (_originalColorSetTexture == null)
throw new InvalidOperationException("Material doesn't have a color set");
_originalColorTableTexture = new SafeTextureHandle(*_colorTableTexture, true);
if (_originalColorTableTexture == null)
throw new InvalidOperationException("Material doesn't have a color table");

_colorSet = new Half[TextureLength];
_colorTable = new Half[TextureLength];
_updatePending = true;

framework.Update += OnFrameworkUpdate;
Expand All @@ -58,9 +57,9 @@ protected override void Clear(bool disposing, bool reset)
base.Clear(disposing, reset);

if (reset)
_originalColorSetTexture.Exchange(ref *(nint*)_colorSetTexture);
_originalColorTableTexture.Exchange(ref *(nint*)_colorTableTexture);

_originalColorSetTexture.Dispose();
_originalColorTableTexture.Dispose();
}

public void ScheduleUpdate()
Expand All @@ -87,28 +86,28 @@ private void OnFrameworkUpdate(Framework _)
return;

bool success;
lock (_colorSet)
lock (_colorTable)
{
fixed (Half* colorSet = _colorSet)
fixed (Half* colorTable = _colorTable)
{
success = Structs.TextureUtility.InitializeContents(texture.Texture, colorSet);
success = Structs.TextureUtility.InitializeContents(texture.Texture, colorTable);
}
}

if (success)
texture.Exchange(ref *(nint*)_colorSetTexture);
texture.Exchange(ref *(nint*)_colorTableTexture);
}

protected override bool IsStillValid()
{
if (!base.IsStillValid())
return false;

var colorSetTextures = ((Structs.CharacterBaseExt*)DrawObject)->ColorSetTextures;
var colorSetTextures = ((Structs.CharacterBaseExt*)DrawObject)->ColorTableTextures;
if (colorSetTextures == null)
return false;

if (_colorSetTexture != colorSetTextures + (MaterialInfo.ModelSlot * 4 + MaterialInfo.MaterialSlot))
if (_colorTableTexture != colorSetTextures + (MaterialInfo.ModelSlot * 4 + MaterialInfo.MaterialSlot))
return false;

return true;
Expand Down
10 changes: 5 additions & 5 deletions Penumbra/Interop/Structs/CharacterBaseExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

namespace Penumbra.Interop.Structs;

[StructLayout( LayoutKind.Explicit )]
[StructLayout(LayoutKind.Explicit)]
public unsafe struct CharacterBaseExt
{
[FieldOffset( 0x0 )]
[FieldOffset(0x0)]
public CharacterBase CharacterBase;

[FieldOffset( 0x258 )]
public Texture** ColorSetTextures;
}
[FieldOffset(0x258)]
public Texture** ColorTableTextures;
}
Loading

0 comments on commit 25cdc00

Please sign in to comment.