增加纹理加载选项

This commit is contained in:
ww-rm
2025-06-12 23:47:46 +08:00
parent 28fd11cf3e
commit 9be77ba8bd
10 changed files with 104 additions and 62 deletions

View File

@@ -26,10 +26,11 @@ namespace Spine.Implementations.SpineWrappers.V21
private readonly ImmutableArray<IAnimation> _animations;
private readonly FrozenDictionary<string, IAnimation> _animationsByName;
public SpineObjectData21(string skelPath, string atlasPath) : base(skelPath, atlasPath)
public SpineObjectData21(string skelPath, string atlasPath, Spine.SpineWrappers.TextureLoader textureLoader)
: base(skelPath, atlasPath, textureLoader)
{
// 加载 atlas
try { _atlas = new Atlas(atlasPath, _textureLoader); }
try { _atlas = new Atlas(atlasPath, textureLoader); }
catch (Exception ex) { throw new InvalidDataException($"Failed to load atlas '{atlasPath}'", ex); }
try

View File

@@ -26,10 +26,11 @@ namespace Spine.Implementations.SpineWrappers.V36
private readonly ImmutableArray<IAnimation> _animations;
private readonly FrozenDictionary<string, IAnimation> _animationsByName;
public SpineObjectData36(string skelPath, string atlasPath) : base(skelPath, atlasPath)
public SpineObjectData36(string skelPath, string atlasPath, Spine.SpineWrappers.TextureLoader textureLoader)
: base(skelPath, atlasPath, textureLoader)
{
// 加载 atlas
try { _atlas = new Atlas(atlasPath, _textureLoader); }
try { _atlas = new Atlas(atlasPath, textureLoader); }
catch (Exception ex) { throw new InvalidDataException($"Failed to load atlas '{atlasPath}'", ex); }
try

View File

@@ -26,10 +26,11 @@ namespace Spine.Implementations.SpineWrappers.V37
private readonly ImmutableArray<IAnimation> _animations;
private readonly FrozenDictionary<string, IAnimation> _animationsByName;
public SpineObjectData37(string skelPath, string atlasPath) : base(skelPath, atlasPath)
public SpineObjectData37(string skelPath, string atlasPath, Spine.SpineWrappers.TextureLoader textureLoader)
: base(skelPath, atlasPath, textureLoader)
{
// 加载 atlas
try { _atlas = new Atlas(atlasPath, _textureLoader); }
try { _atlas = new Atlas(atlasPath, textureLoader); }
catch (Exception ex) { throw new InvalidDataException($"Failed to load atlas '{atlasPath}'", ex); }
try

View File

@@ -27,10 +27,11 @@ namespace Spine.Implementations.SpineWrappers.V38
private readonly ImmutableArray<IAnimation> _animations;
private readonly FrozenDictionary<string, IAnimation> _animationsByName;
public SpineObjectData38(string skelPath, string atlasPath) : base(skelPath, atlasPath)
public SpineObjectData38(string skelPath, string atlasPath, Spine.SpineWrappers.TextureLoader textureLoader)
: base(skelPath, atlasPath, textureLoader)
{
// 加载 atlas
try { _atlas = new Atlas(atlasPath, _textureLoader); }
try { _atlas = new Atlas(atlasPath, textureLoader); }
catch (Exception ex) { throw new InvalidDataException($"Failed to load atlas '{atlasPath}'", ex); }
try

View File

@@ -26,10 +26,11 @@ namespace Spine.Implementations.SpineWrappers.V40
private readonly ImmutableArray<IAnimation> _animations;
private readonly FrozenDictionary<string, IAnimation> _animationsByName;
public SpineObjectData40(string skelPath, string atlasPath) : base(skelPath, atlasPath)
public SpineObjectData40(string skelPath, string atlasPath, Spine.SpineWrappers.TextureLoader textureLoader)
: base(skelPath, atlasPath, textureLoader)
{
// 加载 atlas
try { _atlas = new Atlas(atlasPath, _textureLoader); }
try { _atlas = new Atlas(atlasPath, textureLoader); }
catch (Exception ex) { throw new InvalidDataException($"Failed to load atlas '{atlasPath}'", ex); }
// 加载 skel

View File

@@ -26,10 +26,11 @@ namespace Spine.Implementations.SpineWrappers.V41
private readonly ImmutableArray<IAnimation> _animations;
private readonly FrozenDictionary<string, IAnimation> _animationsByName;
public SpineObjectData41(string skelPath, string atlasPath) : base(skelPath, atlasPath)
public SpineObjectData41(string skelPath, string atlasPath, Spine.SpineWrappers.TextureLoader textureLoader)
: base(skelPath, atlasPath, textureLoader)
{
// 加载 atlas
try { _atlas = new Atlas(atlasPath, _textureLoader); }
try { _atlas = new Atlas(atlasPath, textureLoader); }
catch (Exception ex) { throw new InvalidDataException($"Failed to load atlas '{atlasPath}'", ex); }
// 加载 skel

View File

@@ -26,10 +26,11 @@ namespace Spine.Implementations.SpineWrappers.V42
private readonly ImmutableArray<IAnimation> _animations;
private readonly FrozenDictionary<string, IAnimation> _animationsByName;
public SpineObjectData42(string skelPath, string atlasPath) : base(skelPath, atlasPath)
public SpineObjectData42(string skelPath, string atlasPath, Spine.SpineWrappers.TextureLoader textureLoader)
: base(skelPath, atlasPath, textureLoader)
{
// 加载 atlas
try { _atlas = new Atlas(atlasPath, _textureLoader); }
try { _atlas = new Atlas(atlasPath, textureLoader); }
catch (Exception ex) { throw new InvalidDataException($"Failed to load atlas '{atlasPath}'", ex); }
// 加载 skel

View File

@@ -45,10 +45,12 @@ namespace Spine
/// <param name="skelPath">skel 文件路径</param>
/// <param name="atlasPath">atlas 文件路径, 为空时会根据 <paramref name="skelPath"/> 进行自动检测</param>
/// <param name="version">要使用的运行时版本, 为空时会自动检测</param>
public SpineObject(string skelPath, string? atlasPath = null, SpineVersion? version = null)
public SpineObject(string skelPath, string? atlasPath = null, SpineVersion? version = null, TextureLoader? textureLoader = null)
{
if (string.IsNullOrWhiteSpace(skelPath)) throw new ArgumentException(skelPath, nameof(skelPath));
if (!File.Exists(skelPath)) throw new FileNotFoundException($"{nameof(skelPath)} not found", skelPath);
textureLoader ??= TextureLoader.DefaultLoader;
SkelPath = Path.GetFullPath(skelPath);
AssetsDir = Directory.GetParent(skelPath).FullName;
Name = Path.GetFileNameWithoutExtension(skelPath);
@@ -91,7 +93,7 @@ namespace Spine
{
try
{
_data = SpineObjectData.New(v, skelPath, atlasPath);
_data = SpineObjectData.New(v, skelPath, atlasPath, textureLoader);
Version = v;
break;
}
@@ -109,7 +111,7 @@ namespace Spine
{
// 根据版本实例化对象
Version = version;
_data = SpineObjectData.New(Version, skelPath, atlasPath);
_data = SpineObjectData.New(Version, skelPath, atlasPath, textureLoader);
}
// 创建状态实例

View File

@@ -20,18 +20,13 @@ namespace Spine.SpineWrappers
/// <summary>
/// 构建版本对象
/// </summary>
public static SpineObjectData New(SpineVersion version, string skelPath, string atlasPath) => CreateInstance(version.Tag, skelPath, atlasPath);
/// <summary>
/// 纹理加载器, 可以设置一些预置参数
/// </summary>
public static TextureLoader TextureLoader => _textureLoader;
protected static readonly TextureLoader _textureLoader = new();
public static SpineObjectData New(SpineVersion version, string skelPath, string atlasPath, TextureLoader textureLoader)
=> CreateInstance(version.Tag, skelPath, atlasPath, textureLoader);
/// <summary>
/// 构造函数, 继承的子类应当实现一个相同签名的构造函数
/// </summary>
public SpineObjectData(string skelPath, string atlasPath) { }
public SpineObjectData(string skelPath, string atlasPath, TextureLoader textureLoader) { }
public abstract string SkeletonVersion { get; }

View File

@@ -19,23 +19,62 @@ namespace Spine.SpineWrappers
SpineRuntime42.TextureLoader
{
/// <summary>
/// 强制启用 Smooth
/// 默认的全局纹理加载器
/// </summary>
public bool ForceSmooth { get; set; } = false;
public static TextureLoader DefaultLoader { get; } = new();
/// <summary>
/// 强制启用 Repeated
/// 在读取纹理时强制进行通道预乘操作
/// </summary>
public bool ForceRepeated { get; set; } = false;
public bool ForcePremul { get; set; } = false;
/// <summary>
/// 强制使用 Nearest
/// </summary>
public bool ForceNearest { get; set; } = false;
/// <summary>
/// 强制启用 Mipmap
/// </summary>
public bool ForceMipmap { get; set; } = false;
public void Load(SpineRuntime21.AtlasPage page, string path)
private SFML.Graphics.Texture ReadTexture(string path)
{
var texture = new SFML.Graphics.Texture(path);
if (ForcePremul)
{
using var image = new SFML.Graphics.Image(path);
var width = image.Size.X;
var height = image.Size.Y;
var pixels = image.Pixels;
var size = width * height * 4;
for (int i = 0; i < size; i += 4)
{
byte a = pixels[i + 3];
if (a == 0)
{
pixels[i + 0] = 0;
pixels[i + 1] = 0;
pixels[i + 2] = 0;
}
else if (a != 255)
{
float f = a / 255f;
pixels[i + 0] = (byte)(pixels[i + 0] * f);
pixels[i + 1] = (byte)(pixels[i + 1] * f);
pixels[i + 2] = (byte)(pixels[i + 2] * f);
}
}
var tex = new SFML.Graphics.Texture(width, height);
tex.Update(pixels);
return tex;
}
return new(path);
}
public virtual void Load(SpineRuntime21.AtlasPage page, string path)
{
var texture = ReadTexture(path);
if (page.magFilter == SpineRuntime21.TextureFilter.Linear)
{
texture.Smooth = true;
@@ -61,16 +100,16 @@ namespace Spine.SpineWrappers
break;
}
if (ForceSmooth) texture.Smooth = true;
if (ForceRepeated) texture.Repeated = true;
if (ForceNearest) texture.Smooth = false;
if (ForceMipmap) texture.GenerateMipmap();
page.rendererObject = texture;
}
public void Load(SpineRuntime36.AtlasPage page, string path)
public virtual void Load(SpineRuntime36.AtlasPage page, string path)
{
var texture = new SFML.Graphics.Texture(path);
var texture = ReadTexture(path);
if (page.magFilter == SpineRuntime36.TextureFilter.Linear)
{
texture.Smooth = true;
@@ -96,16 +135,16 @@ namespace Spine.SpineWrappers
break;
}
if (ForceSmooth) texture.Smooth = true;
if (ForceRepeated) texture.Repeated = true;
if (ForceNearest) texture.Smooth = false;
if (ForceMipmap) texture.GenerateMipmap();
page.rendererObject = texture;
}
public void Load(SpineRuntime37.AtlasPage page, string path)
public virtual void Load(SpineRuntime37.AtlasPage page, string path)
{
var texture = new SFML.Graphics.Texture(path);
var texture = ReadTexture(path);
if (page.magFilter == SpineRuntime37.TextureFilter.Linear)
{
texture.Smooth = true;
@@ -131,16 +170,16 @@ namespace Spine.SpineWrappers
break;
}
if (ForceSmooth) texture.Smooth = true;
if (ForceRepeated) texture.Repeated = true;
if (ForceNearest) texture.Smooth = false;
if (ForceMipmap) texture.GenerateMipmap();
page.rendererObject = texture;
}
public void Load(SpineRuntime38.AtlasPage page, string path)
public virtual void Load(SpineRuntime38.AtlasPage page, string path)
{
var texture = new SFML.Graphics.Texture(path);
var texture = ReadTexture(path);
if (page.magFilter == SpineRuntime38.TextureFilter.Linear)
{
texture.Smooth = true;
@@ -166,16 +205,20 @@ namespace Spine.SpineWrappers
break;
}
if (ForceSmooth) texture.Smooth = true;
if (ForceRepeated) texture.Repeated = true;
if (ForceNearest) texture.Smooth = false;
if (ForceMipmap) texture.GenerateMipmap();
page.rendererObject = texture;
// 似乎是不需要设置的, 因为存在某些 png 和 atlas 大小不同的情况, 一般是有一些缩放, 如果设置了反而渲染异常
// page.width = (int)texture.Size.X;
// page.height = (int)texture.Size.Y;
}
public void Load(SpineRuntime40.AtlasPage page, string path)
public virtual void Load(SpineRuntime40.AtlasPage page, string path)
{
var texture = new SFML.Graphics.Texture(path);
var texture = ReadTexture(path);
if (page.magFilter == SpineRuntime40.TextureFilter.Linear)
{
texture.Smooth = true;
@@ -201,16 +244,16 @@ namespace Spine.SpineWrappers
break;
}
if (ForceSmooth) texture.Smooth = true;
if (ForceRepeated) texture.Repeated = true;
if (ForceNearest) texture.Smooth = false;
if (ForceMipmap) texture.GenerateMipmap();
page.rendererObject = texture;
}
public void Load(SpineRuntime41.AtlasPage page, string path)
public virtual void Load(SpineRuntime41.AtlasPage page, string path)
{
var texture = new SFML.Graphics.Texture(path);
var texture = ReadTexture(path);
if (page.magFilter == SpineRuntime41.TextureFilter.Linear)
{
texture.Smooth = true;
@@ -236,16 +279,16 @@ namespace Spine.SpineWrappers
break;
}
if (ForceSmooth) texture.Smooth = true;
if (ForceRepeated) texture.Repeated = true;
if (ForceNearest) texture.Smooth = false;
if (ForceMipmap) texture.GenerateMipmap();
page.rendererObject = texture;
}
public void Load(SpineRuntime42.AtlasPage page, string path)
public virtual void Load(SpineRuntime42.AtlasPage page, string path)
{
var texture = new SFML.Graphics.Texture(path);
var texture = ReadTexture(path);
if (page.magFilter == SpineRuntime42.TextureFilter.Linear)
{
texture.Smooth = true;
@@ -271,18 +314,13 @@ namespace Spine.SpineWrappers
break;
}
if (ForceSmooth) texture.Smooth = true;
if (ForceRepeated) texture.Repeated = true;
if (ForceNearest) texture.Smooth = false;
if (ForceMipmap) texture.GenerateMipmap();
page.rendererObject = texture;
// 似乎是不需要设置的, 因为存在某些 png 和 atlas 大小不同的情况, 一般是有一些缩放, 如果设置了反而渲染异常
// page.width = (int)texture.Size.X;
// page.height = (int)texture.Size.Y;
}
public void Unload(object texture)
public virtual void Unload(object texture)
{
((SFML.Graphics.Texture)texture).Dispose();
}