From 3f9c4dfe9987f73dddf26badeb560cc904429b6b Mon Sep 17 00:00:00 2001 From: ww-rm Date: Wed, 7 May 2025 17:43:42 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8A=BD=E5=87=BATextureLoader=EF=BC=8C?= =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=83=A8=E5=88=86=E5=8F=AF=E8=B0=83=E5=8F=82?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SpineObject/SpineObject21.cs | 20 -- .../SpineObject/SpineObject36.cs | 20 -- .../SpineObject/SpineObject37.cs | 20 -- .../SpineObject/SpineObject38.cs | 23 -- .../SpineObject/SpineObject40.cs | 20 -- .../SpineObject/SpineObject41.cs | 20 -- .../SpineObject/SpineObject42.cs | 20 -- SpineViewer/Spine/SpineObject.cs | 6 + SpineViewer/Spine/TextureLoader.cs | 287 ++++++++++++++++++ 9 files changed, 293 insertions(+), 143 deletions(-) create mode 100644 SpineViewer/Spine/TextureLoader.cs diff --git a/SpineViewer/Spine/Implementations/SpineObject/SpineObject21.cs b/SpineViewer/Spine/Implementations/SpineObject/SpineObject21.cs index ea9d93f..2635e29 100644 --- a/SpineViewer/Spine/Implementations/SpineObject/SpineObject21.cs +++ b/SpineViewer/Spine/Implementations/SpineObject/SpineObject21.cs @@ -29,26 +29,6 @@ namespace SpineViewer.Spine.Implementations.SpineObject // }; //} - private class TextureLoader : SpineRuntime21.TextureLoader - { - public void Load(AtlasPage page, string path) - { - var texture = new SFML.Graphics.Texture(path); - if (page.magFilter == TextureFilter.Linear) - texture.Smooth = true; - if (page.uWrap == TextureWrap.Repeat && page.vWrap == TextureWrap.Repeat) - texture.Repeated = true; - - page.rendererObject = texture; - } - - public void Unload(object texture) - { - ((SFML.Graphics.Texture)texture).Dispose(); - } - } - - private readonly static TextureLoader textureLoader = new(); private static readonly Animation EmptyAnimation = new(EMPTY_ANIMATION, [], 0); private readonly Atlas atlas; diff --git a/SpineViewer/Spine/Implementations/SpineObject/SpineObject36.cs b/SpineViewer/Spine/Implementations/SpineObject/SpineObject36.cs index 693452a..418b6c5 100644 --- a/SpineViewer/Spine/Implementations/SpineObject/SpineObject36.cs +++ b/SpineViewer/Spine/Implementations/SpineObject/SpineObject36.cs @@ -29,26 +29,6 @@ namespace SpineViewer.Spine.Implementations.SpineObject }; } - private class TextureLoader : SpineRuntime36.TextureLoader - { - public void Load(AtlasPage page, string path) - { - var texture = new SFML.Graphics.Texture(path); - if (page.magFilter == TextureFilter.Linear) - texture.Smooth = true; - if (page.uWrap == TextureWrap.Repeat && page.vWrap == TextureWrap.Repeat) - texture.Repeated = true; - - page.rendererObject = texture; - } - - public void Unload(object texture) - { - ((SFML.Graphics.Texture)texture).Dispose(); - } - } - - private static readonly TextureLoader textureLoader = new(); private static readonly Animation EmptyAnimation = new(EMPTY_ANIMATION, [], 0); private readonly Atlas atlas; diff --git a/SpineViewer/Spine/Implementations/SpineObject/SpineObject37.cs b/SpineViewer/Spine/Implementations/SpineObject/SpineObject37.cs index c23b685..7f7686e 100644 --- a/SpineViewer/Spine/Implementations/SpineObject/SpineObject37.cs +++ b/SpineViewer/Spine/Implementations/SpineObject/SpineObject37.cs @@ -26,26 +26,6 @@ namespace SpineViewer.Spine.Implementations.SpineObject }; } - private class TextureLoader : SpineRuntime37.TextureLoader - { - public void Load(AtlasPage page, string path) - { - var texture = new SFML.Graphics.Texture(path); - if (page.magFilter == TextureFilter.Linear) - texture.Smooth = true; - if (page.uWrap == TextureWrap.Repeat && page.vWrap == TextureWrap.Repeat) - texture.Repeated = true; - - page.rendererObject = texture; - } - - public void Unload(object texture) - { - ((SFML.Graphics.Texture)texture).Dispose(); - } - } - - private static readonly TextureLoader textureLoader = new(); private static readonly Animation EmptyAnimation = new(EMPTY_ANIMATION, [], 0); private readonly Atlas atlas; diff --git a/SpineViewer/Spine/Implementations/SpineObject/SpineObject38.cs b/SpineViewer/Spine/Implementations/SpineObject/SpineObject38.cs index 5ab79e6..0b357a2 100644 --- a/SpineViewer/Spine/Implementations/SpineObject/SpineObject38.cs +++ b/SpineViewer/Spine/Implementations/SpineObject/SpineObject38.cs @@ -30,29 +30,6 @@ namespace SpineViewer.Spine.Implementations.SpineObject }; } - private class TextureLoader : SpineRuntime38.TextureLoader - { - public void Load(AtlasPage page, string path) - { - var texture = new SFML.Graphics.Texture(path); - if (page.magFilter == TextureFilter.Linear) - texture.Smooth = true; - if (page.uWrap == TextureWrap.Repeat && page.vWrap == TextureWrap.Repeat) - texture.Repeated = true; - - page.rendererObject = texture; - // 似乎是不需要设置的, 因为存在某些 png 和 atlas 大小不同的情况, 一般是有一些缩放, 如果设置了反而渲染异常 - // page.width = (int)texture.Size.X; - // page.height = (int)texture.Size.Y; - } - - public void Unload(object texture) - { - ((SFML.Graphics.Texture)texture).Dispose(); - } - } - - private static readonly TextureLoader textureLoader = new(); private static readonly Animation EmptyAnimation = new(EMPTY_ANIMATION, [], 0); private readonly Atlas atlas; diff --git a/SpineViewer/Spine/Implementations/SpineObject/SpineObject40.cs b/SpineViewer/Spine/Implementations/SpineObject/SpineObject40.cs index 58be8e1..ecc672b 100644 --- a/SpineViewer/Spine/Implementations/SpineObject/SpineObject40.cs +++ b/SpineViewer/Spine/Implementations/SpineObject/SpineObject40.cs @@ -28,26 +28,6 @@ namespace SpineViewer.Spine.Implementations.SpineObject }; } - private class TextureLoader : SpineRuntime40.TextureLoader - { - public void Load(AtlasPage page, string path) - { - var texture = new SFML.Graphics.Texture(path); - if (page.magFilter == TextureFilter.Linear) - texture.Smooth = true; - if (page.uWrap == TextureWrap.Repeat && page.vWrap == TextureWrap.Repeat) - texture.Repeated = true; - - page.rendererObject = texture; - } - - public void Unload(object texture) - { - ((SFML.Graphics.Texture)texture).Dispose(); - } - } - - private static readonly TextureLoader textureLoader = new(); private static readonly Animation EmptyAnimation = new(EMPTY_ANIMATION, [], 0); private readonly Atlas atlas; diff --git a/SpineViewer/Spine/Implementations/SpineObject/SpineObject41.cs b/SpineViewer/Spine/Implementations/SpineObject/SpineObject41.cs index 23c15ee..5a7e4e6 100644 --- a/SpineViewer/Spine/Implementations/SpineObject/SpineObject41.cs +++ b/SpineViewer/Spine/Implementations/SpineObject/SpineObject41.cs @@ -28,26 +28,6 @@ namespace SpineViewer.Spine.Implementations.SpineObject }; } - private class TextureLoader : SpineRuntime41.TextureLoader - { - public void Load(AtlasPage page, string path) - { - var texture = new SFML.Graphics.Texture(path); - if (page.magFilter == TextureFilter.Linear) - texture.Smooth = true; - if (page.uWrap == TextureWrap.Repeat && page.vWrap == TextureWrap.Repeat) - texture.Repeated = true; - - page.rendererObject = texture; - } - - public void Unload(object texture) - { - ((SFML.Graphics.Texture)texture).Dispose(); - } - } - - private static TextureLoader textureLoader = new(); private static readonly Animation EmptyAnimation = new(EMPTY_ANIMATION, [], 0); private readonly Atlas atlas; diff --git a/SpineViewer/Spine/Implementations/SpineObject/SpineObject42.cs b/SpineViewer/Spine/Implementations/SpineObject/SpineObject42.cs index ea4510e..70d7cea 100644 --- a/SpineViewer/Spine/Implementations/SpineObject/SpineObject42.cs +++ b/SpineViewer/Spine/Implementations/SpineObject/SpineObject42.cs @@ -28,26 +28,6 @@ namespace SpineViewer.Spine.Implementations.SpineObject }; } - private class TextureLoader : SpineRuntime42.TextureLoader - { - public void Load(AtlasPage page, string path) - { - var texture = new SFML.Graphics.Texture(path); - if (page.magFilter == TextureFilter.Linear) - texture.Smooth = true; - if (page.uWrap == TextureWrap.Repeat && page.vWrap == TextureWrap.Repeat) - texture.Repeated = true; - - page.rendererObject = texture; - } - - public void Unload(object texture) - { - ((SFML.Graphics.Texture)texture).Dispose(); - } - } - - private static readonly TextureLoader textureLoader = new(); private static readonly Animation EmptyAnimation = new(EMPTY_ANIMATION, [], 0); private readonly Atlas atlas; diff --git a/SpineViewer/Spine/SpineObject.cs b/SpineViewer/Spine/SpineObject.cs index a10ba79..4f97a62 100644 --- a/SpineViewer/Spine/SpineObject.cs +++ b/SpineViewer/Spine/SpineObject.cs @@ -32,6 +32,12 @@ namespace SpineViewer.Spine /// protected static readonly Size PreviewResolution = new(256, 256); + /// + /// 纹理加载器 + /// + public static TextureLoader TextureLoader => textureLoader; + protected readonly static TextureLoader textureLoader = new(); + /// /// 创建特定版本的 Spine /// diff --git a/SpineViewer/Spine/TextureLoader.cs b/SpineViewer/Spine/TextureLoader.cs new file mode 100644 index 0000000..2dedb18 --- /dev/null +++ b/SpineViewer/Spine/TextureLoader.cs @@ -0,0 +1,287 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace SpineViewer.Spine +{ + /// + /// 实现不同版本的 TextureLoader + /// + public class TextureLoader : + SpineRuntime21.TextureLoader, + SpineRuntime36.TextureLoader, + SpineRuntime37.TextureLoader, + SpineRuntime38.TextureLoader, + SpineRuntime40.TextureLoader, + SpineRuntime41.TextureLoader, + SpineRuntime42.TextureLoader + { + /// + /// 强制启用 Smooth + /// + public bool ForceSmooth { get; set; } = false; + + /// + /// 强制启用 Repeated + /// + public bool ForceRepeated { get; set; } = false; + + /// + /// 强制启用 Mipmap + /// + public bool ForceMipmap { get; set; } = false; + + public void Load(SpineRuntime21.AtlasPage page, string path) + { + var texture = new SFML.Graphics.Texture(path); + if (page.magFilter == SpineRuntime21.TextureFilter.Linear) + { + texture.Smooth = true; + } + if (page.uWrap == SpineRuntime21.TextureWrap.Repeat && page.vWrap == SpineRuntime21.TextureWrap.Repeat) + { + texture.Repeated = true; + } + switch (page.minFilter) + { + case SpineRuntime21.TextureFilter.Linear: + texture.Smooth = true; + break; + case SpineRuntime21.TextureFilter.MipMap: + case SpineRuntime21.TextureFilter.MipMapNearestNearest: + texture.GenerateMipmap(); + break; + case SpineRuntime21.TextureFilter.MipMapLinearNearest: + case SpineRuntime21.TextureFilter.MipMapNearestLinear: + case SpineRuntime21.TextureFilter.MipMapLinearLinear: + texture.Smooth = true; + texture.GenerateMipmap(); + break; + } + + if (ForceSmooth) texture.Smooth = true; + if (ForceRepeated) texture.Repeated = true; + if (ForceMipmap) texture.GenerateMipmap(); + + page.rendererObject = texture; + } + + public void Load(SpineRuntime36.AtlasPage page, string path) + { + var texture = new SFML.Graphics.Texture(path); + if (page.magFilter == SpineRuntime36.TextureFilter.Linear) + { + texture.Smooth = true; + } + if (page.uWrap == SpineRuntime36.TextureWrap.Repeat && page.vWrap == SpineRuntime36.TextureWrap.Repeat) + { + texture.Repeated = true; + } + switch (page.minFilter) + { + case SpineRuntime36.TextureFilter.Linear: + texture.Smooth = true; + break; + case SpineRuntime36.TextureFilter.MipMap: + case SpineRuntime36.TextureFilter.MipMapNearestNearest: + texture.GenerateMipmap(); + break; + case SpineRuntime36.TextureFilter.MipMapLinearNearest: + case SpineRuntime36.TextureFilter.MipMapNearestLinear: + case SpineRuntime36.TextureFilter.MipMapLinearLinear: + texture.Smooth = true; + texture.GenerateMipmap(); + break; + } + + if (ForceSmooth) texture.Smooth = true; + if (ForceRepeated) texture.Repeated = true; + if (ForceMipmap) texture.GenerateMipmap(); + + page.rendererObject = texture; + } + + public void Load(SpineRuntime37.AtlasPage page, string path) + { + var texture = new SFML.Graphics.Texture(path); + if (page.magFilter == SpineRuntime37.TextureFilter.Linear) + { + texture.Smooth = true; + } + if (page.uWrap == SpineRuntime37.TextureWrap.Repeat && page.vWrap == SpineRuntime37.TextureWrap.Repeat) + { + texture.Repeated = true; + } + switch (page.minFilter) + { + case SpineRuntime37.TextureFilter.Linear: + texture.Smooth = true; + break; + case SpineRuntime37.TextureFilter.MipMap: + case SpineRuntime37.TextureFilter.MipMapNearestNearest: + texture.GenerateMipmap(); + break; + case SpineRuntime37.TextureFilter.MipMapLinearNearest: + case SpineRuntime37.TextureFilter.MipMapNearestLinear: + case SpineRuntime37.TextureFilter.MipMapLinearLinear: + texture.Smooth = true; + texture.GenerateMipmap(); + break; + } + + if (ForceSmooth) texture.Smooth = true; + if (ForceRepeated) texture.Repeated = true; + if (ForceMipmap) texture.GenerateMipmap(); + + page.rendererObject = texture; + } + + public void Load(SpineRuntime38.AtlasPage page, string path) + { + var texture = new SFML.Graphics.Texture(path); + if (page.magFilter == SpineRuntime38.TextureFilter.Linear) + { + texture.Smooth = true; + } + if (page.uWrap == SpineRuntime38.TextureWrap.Repeat && page.vWrap == SpineRuntime38.TextureWrap.Repeat) + { + texture.Repeated = true; + } + switch (page.minFilter) + { + case SpineRuntime38.TextureFilter.Linear: + texture.Smooth = true; + break; + case SpineRuntime38.TextureFilter.MipMap: + case SpineRuntime38.TextureFilter.MipMapNearestNearest: + texture.GenerateMipmap(); + break; + case SpineRuntime38.TextureFilter.MipMapLinearNearest: + case SpineRuntime38.TextureFilter.MipMapNearestLinear: + case SpineRuntime38.TextureFilter.MipMapLinearLinear: + texture.Smooth = true; + texture.GenerateMipmap(); + break; + } + + if (ForceSmooth) texture.Smooth = true; + if (ForceRepeated) texture.Repeated = true; + if (ForceMipmap) texture.GenerateMipmap(); + + page.rendererObject = texture; + } + + public void Load(SpineRuntime40.AtlasPage page, string path) + { + var texture = new SFML.Graphics.Texture(path); + if (page.magFilter == SpineRuntime40.TextureFilter.Linear) + { + texture.Smooth = true; + } + if (page.uWrap == SpineRuntime40.TextureWrap.Repeat && page.vWrap == SpineRuntime40.TextureWrap.Repeat) + { + texture.Repeated = true; + } + switch (page.minFilter) + { + case SpineRuntime40.TextureFilter.Linear: + texture.Smooth = true; + break; + case SpineRuntime40.TextureFilter.MipMap: + case SpineRuntime40.TextureFilter.MipMapNearestNearest: + texture.GenerateMipmap(); + break; + case SpineRuntime40.TextureFilter.MipMapLinearNearest: + case SpineRuntime40.TextureFilter.MipMapNearestLinear: + case SpineRuntime40.TextureFilter.MipMapLinearLinear: + texture.Smooth = true; + texture.GenerateMipmap(); + break; + } + + if (ForceSmooth) texture.Smooth = true; + if (ForceRepeated) texture.Repeated = true; + if (ForceMipmap) texture.GenerateMipmap(); + + page.rendererObject = texture; + } + + public void Load(SpineRuntime41.AtlasPage page, string path) + { + var texture = new SFML.Graphics.Texture(path); + if (page.magFilter == SpineRuntime41.TextureFilter.Linear) + { + texture.Smooth = true; + } + if (page.uWrap == SpineRuntime41.TextureWrap.Repeat && page.vWrap == SpineRuntime41.TextureWrap.Repeat) + { + texture.Repeated = true; + } + switch (page.minFilter) + { + case SpineRuntime41.TextureFilter.Linear: + texture.Smooth = true; + break; + case SpineRuntime41.TextureFilter.MipMap: + case SpineRuntime41.TextureFilter.MipMapNearestNearest: + texture.GenerateMipmap(); + break; + case SpineRuntime41.TextureFilter.MipMapLinearNearest: + case SpineRuntime41.TextureFilter.MipMapNearestLinear: + case SpineRuntime41.TextureFilter.MipMapLinearLinear: + texture.Smooth = true; + texture.GenerateMipmap(); + break; + } + + if (ForceSmooth) texture.Smooth = true; + if (ForceRepeated) texture.Repeated = true; + if (ForceMipmap) texture.GenerateMipmap(); + + page.rendererObject = texture; + } + + public void Load(SpineRuntime42.AtlasPage page, string path) + { + var texture = new SFML.Graphics.Texture(path); + if (page.magFilter == SpineRuntime42.TextureFilter.Linear) + { + texture.Smooth = true; + } + if (page.uWrap == SpineRuntime42.TextureWrap.Repeat && page.vWrap == SpineRuntime42.TextureWrap.Repeat) + { + texture.Repeated = true; + } + switch (page.minFilter) + { + case SpineRuntime42.TextureFilter.Linear: + texture.Smooth = true; + break; + case SpineRuntime42.TextureFilter.MipMap: + case SpineRuntime42.TextureFilter.MipMapNearestNearest: + texture.GenerateMipmap(); + break; + case SpineRuntime42.TextureFilter.MipMapLinearNearest: + case SpineRuntime42.TextureFilter.MipMapNearestLinear: + case SpineRuntime42.TextureFilter.MipMapLinearLinear: + texture.Smooth = true; + texture.GenerateMipmap(); + break; + } + + if (ForceSmooth) texture.Smooth = true; + if (ForceRepeated) texture.Repeated = true; + 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) => ((SFML.Graphics.Texture)texture).Dispose(); + } +}