增强支持的纹理图像格式

This commit is contained in:
ww-rm
2025-09-24 23:00:21 +08:00
parent dbc15952cc
commit e965223284

View File

@@ -1,4 +1,6 @@
using System; using SFML.Graphics;
using SkiaSharp;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@@ -40,38 +42,27 @@ namespace Spine.SpineWrappers
/// </summary> /// </summary>
public bool ForceMipmap { get; set; } public bool ForceMipmap { get; set; }
private SFML.Graphics.Texture ReadTexture(string path) private Texture ReadTexture(string path)
{ {
if (ForcePremul) using var codec = SKCodec.Create(path, out var result);
{ if (codec is null || result != SKCodecResult.Success)
using var image = new SFML.Graphics.Image(path); throw new InvalidOperationException($"Failed to create codec '{path}', {result}");
var width = image.Size.X;
var height = image.Size.Y; var width = codec.Info.Width;
var pixels = image.Pixels; var height = codec.Info.Height;
var size = width * height * 4;
for (int i = 0; i < size; i += 4) // 判断是否需要强制预乘
{ var alphaType = ForcePremul ? SKAlphaType.Premul : SKAlphaType.Unpremul;
byte a = pixels[i + 3]; var info = new SKImageInfo(width, height, SKColorType.Rgba8888, alphaType);
if (a == 0)
{ result = codec.GetPixels(info, out var pixels);
pixels[i + 0] = 0; if (result != SKCodecResult.Success)
pixels[i + 1] = 0; throw new InvalidOperationException($"Failed to decode image '{path}', {result}");
pixels[i + 2] = 0;
} Texture tex = new((uint)width, (uint)height);
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); tex.Update(pixels);
return tex; return tex;
} }
return new(path);
}
public virtual void Load(SpineRuntime21.AtlasPage page, string path) public virtual void Load(SpineRuntime21.AtlasPage page, string path)
{ {
@@ -394,7 +385,7 @@ namespace Spine.SpineWrappers
public virtual void Unload(object texture) public virtual void Unload(object texture)
{ {
((SFML.Graphics.Texture)texture).Dispose(); ((Texture)texture).Dispose();
} }
} }
} }