Skip to content

Commit c007b94

Browse files
author
jacky
committed
Solve Screen AND Multiply BlendMode Not Work Error.
Clear Code.
1 parent 59a868d commit c007b94

File tree

21 files changed

+540
-821
lines changed

21 files changed

+540
-821
lines changed

SpineViewerWPF/MainWindow.xaml.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
158158
App.GV.FrameWidth = Math.Round(GridPlayer.ColumnDefinitions[1].ActualWidth + 60 - 2, 2);
159159
App.GV.FrameHeight = Math.Round(this.ActualHeight - 60, 2);
160160
Player.Width = App.GV.FrameWidth;
161-
Player.Height = App.GV.FrameHeight ;
161+
Player.Height = App.GV.FrameHeight;
162162

163163
}
164164

@@ -353,7 +353,7 @@ private void Btn_SelectBG_Click(object sender, RoutedEventArgs e)
353353

354354
if (openFileDialog.ShowDialog() == true)
355355
{
356-
if(App.TextureBG != null)
356+
if (App.TextureBG != null)
357357
App.TextureBG.Dispose();
358358

359359
App.GV.SelectBG = openFileDialog.FileName;
@@ -367,12 +367,13 @@ private void Btn_SelectBG_Click(object sender, RoutedEventArgs e)
367367

368368
private void Tc_Control_SelectionChanged(object sender, SelectionChangedEventArgs e)
369369
{
370-
if(tc_Control.SelectedIndex != 4 && tc_Control.SelectedIndex != -1)
370+
if (tc_Control.SelectedIndex != 4 && tc_Control.SelectedIndex != -1)
371371
{
372372
GridAttributes.ColumnDefinitions[0].Width = new GridLength(App.GV.RedcodePanelWidth);
373373
gs_Control.Visibility = Visibility.Visible;
374374
btn_Exporer.Visibility = Visibility.Visible;
375-
}else if(tc_Control.SelectedIndex == 4)
375+
}
376+
else if (tc_Control.SelectedIndex == 4)
376377
{
377378
gs_Control.Visibility = Visibility.Hidden;
378379
btn_Exporer.Visibility = Visibility.Hidden;
@@ -405,5 +406,6 @@ private void Window_Closed(object sender, EventArgs e)
405406
Properties.Settings.Default.LastSelectDir = App.LastDir;
406407
Properties.Settings.Default.Save();
407408
}
409+
408410
}
409411
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.Xna.Framework.Graphics;
7+
8+
public static class BlendXna
9+
{
10+
11+
public const int GL_NEVER = 0x0200;
12+
public static readonly int GL_LESS = 0x0201;
13+
public static readonly int GL_EQUAL = 0x0202;
14+
public static readonly int GL_LEQUAL = 0x0203;
15+
public static readonly int GL_GREATER = 0x0204;
16+
public static readonly int GL_NOTEQUAL = 0x0205;
17+
public static readonly int GL_GEQUAL = 0x0206;
18+
public static readonly int GL_ALWAYS = 0x0207;
19+
20+
21+
public const int GL_ZERO = 0;
22+
public const int GL_ONE = 1;
23+
public const int GL_SRC_COLOR = 0x0300;
24+
public const int GL_ONE_MINUS_SRC_COLOR = 0x0301;
25+
public const int GL_SRC_ALPHA = 0x0302;
26+
public const int GL_ONE_MINUS_SRC_ALPHA = 0x0303;
27+
public const int GL_DST_ALPHA = 0x0304;
28+
public const int GL_ONE_MINUS_DST_ALPHA = 0x0305;
29+
30+
31+
32+
public const int GL_DST_COLOR = 0x0306;
33+
public const int GL_ONE_MINUS_DST_COLOR = 0x0307;
34+
public const int GL_SRC_ALPHA_SATURATE = 0x0308;
35+
36+
37+
public static Blend GetXNABlend(int glBlend)
38+
{
39+
switch (glBlend)
40+
{
41+
case GL_ZERO:
42+
return Blend.Zero;
43+
case GL_ONE:
44+
return Blend.One;
45+
case GL_SRC_COLOR:
46+
return Blend.SourceColor;
47+
case GL_ONE_MINUS_SRC_COLOR:
48+
return Blend.InverseSourceColor;
49+
case GL_SRC_ALPHA:
50+
return Blend.SourceAlpha;
51+
case GL_ONE_MINUS_SRC_ALPHA:
52+
return Blend.InverseSourceAlpha;
53+
case GL_DST_ALPHA:
54+
return Blend.DestinationAlpha;
55+
case GL_ONE_MINUS_DST_ALPHA:
56+
return Blend.InverseDestinationAlpha;
57+
case GL_DST_COLOR:
58+
return Blend.DestinationColor;
59+
case GL_ONE_MINUS_DST_COLOR:
60+
return Blend.InverseDestinationColor;
61+
case GL_SRC_ALPHA_SATURATE:
62+
return Blend.SourceAlphaSaturation;
63+
}
64+
65+
return Blend.One;
66+
}
67+
68+
69+
70+
}
71+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
2+
using System;
3+
using System.IO;
4+
using Microsoft.Xna.Framework;
5+
using Microsoft.Xna.Framework.Graphics;
6+
using SpineViewerWPF;
7+
8+
public static class Util {
9+
#if WINDOWS_STOREAPP
10+
private static async Task<Texture2D> LoadFile(GraphicsDevice device, String path) {
11+
var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
12+
var file = await folder.GetFileAsync(path).AsTask().ConfigureAwait(false);
13+
try {
14+
return Util.LoadTexture(device, await file.OpenStreamForReadAsync().ConfigureAwait(false));
15+
} catch (Exception ex) {
16+
throw new Exception("Error reading texture file: " + path, ex);
17+
}
18+
}
19+
20+
static public Texture2D LoadTexture (GraphicsDevice device, String path) {
21+
return LoadFile(device, path).Result;
22+
}
23+
#else
24+
static public Texture2D LoadTexture (GraphicsDevice device, String path) {
25+
26+
#if WINDOWS_PHONE
27+
Stream stream = Microsoft.Xna.Framework.TitleContainer.OpenStream(path);
28+
using (Stream input = stream)
29+
{
30+
#else
31+
using (Stream input = new FileStream(path, FileMode.Open, FileAccess.Read)) {
32+
#endif
33+
try {
34+
return Util.LoadTexture(device, input);
35+
} catch (Exception ex) {
36+
throw new Exception("Error reading texture file: " + path, ex);
37+
}
38+
}
39+
}
40+
#endif
41+
42+
static public Texture2D LoadTexture (GraphicsDevice device, Stream input) {
43+
NewTextureLoader ntl = new NewTextureLoader(device);
44+
return ntl.FromStreamFast(input, App.GV.PreMultiplyAlpha);
45+
}
46+
}
47+

SpineViewerWPF/SpineLibrary/spine-runtimes-2.1.08/XnaLoader/Util.cs

Lines changed: 0 additions & 83 deletions
This file was deleted.

SpineViewerWPF/SpineLibrary/spine-runtimes-2.1.25/XnaLoader/Util.cs

Lines changed: 0 additions & 83 deletions
This file was deleted.

SpineViewerWPF/SpineLibrary/spine-runtimes-3.1.07/XnaLoader/SkeletonRegionRenderer.cs

Lines changed: 57 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,63 @@ public void Draw (Skeleton skeleton) {
9090
Slot slot = drawOrderItems[i];
9191
RegionAttachment regionAttachment = slot.Attachment as RegionAttachment;
9292
if (regionAttachment != null) {
93-
BlendState blend = slot.Data.BlendMode == BlendMode.additive ? BlendState.Additive : defaultBlendState;
94-
if (device.BlendState != blend) {
95-
End();
96-
device.BlendState = blend;
97-
}
98-
99-
RegionItem item = batcher.NextItem();
93+
BlendState blendState = new BlendState();
94+
Blend blendSrc;
95+
Blend blendDst;
96+
if (premultipliedAlpha)
97+
{
98+
blendState.AlphaBlendFunction = BlendState.AlphaBlend.AlphaBlendFunction;
99+
blendState.BlendFactor = BlendState.AlphaBlend.BlendFactor;
100+
blendState.ColorBlendFunction = BlendState.AlphaBlend.ColorBlendFunction;
101+
blendState.ColorWriteChannels = BlendState.AlphaBlend.ColorWriteChannels;
102+
blendState.ColorWriteChannels1 = BlendState.AlphaBlend.ColorWriteChannels1;
103+
blendState.ColorWriteChannels2 = BlendState.AlphaBlend.ColorWriteChannels2;
104+
blendState.ColorWriteChannels3 = BlendState.AlphaBlend.ColorWriteChannels3;
105+
blendState.MultiSampleMask = BlendState.AlphaBlend.MultiSampleMask;
106+
}
107+
else
108+
{
109+
blendState.AlphaBlendFunction = BlendState.NonPremultiplied.AlphaBlendFunction;
110+
blendState.BlendFactor = BlendState.NonPremultiplied.BlendFactor;
111+
blendState.ColorBlendFunction = BlendState.NonPremultiplied.ColorBlendFunction;
112+
blendState.ColorWriteChannels = BlendState.NonPremultiplied.ColorWriteChannels;
113+
blendState.ColorWriteChannels1 = BlendState.NonPremultiplied.ColorWriteChannels1;
114+
blendState.ColorWriteChannels2 = BlendState.NonPremultiplied.ColorWriteChannels2;
115+
blendState.ColorWriteChannels3 = BlendState.NonPremultiplied.ColorWriteChannels3;
116+
blendState.MultiSampleMask = BlendState.NonPremultiplied.MultiSampleMask;
117+
}
118+
switch (slot.Data.BlendMode)
119+
{
120+
case BlendMode.additive:
121+
blendState = BlendState.Additive;
122+
break;
123+
case BlendMode.multiply:
124+
blendSrc = BlendXna.GetXNABlend(BlendXna.GL_DST_COLOR);
125+
blendDst = BlendXna.GetXNABlend(BlendXna.GL_ONE_MINUS_SRC_ALPHA);
126+
blendState.ColorSourceBlend = blendSrc;
127+
blendState.AlphaSourceBlend = blendSrc;
128+
blendState.ColorDestinationBlend = blendDst;
129+
blendState.AlphaDestinationBlend = blendDst;
130+
break;
131+
case BlendMode.screen:
132+
blendSrc = BlendXna.GetXNABlend(premultipliedAlpha ? BlendXna.GL_ONE : BlendXna.GL_SRC_ALPHA);
133+
blendDst = BlendXna.GetXNABlend(BlendXna.GL_ONE_MINUS_SRC_COLOR);
134+
blendState.ColorSourceBlend = blendSrc;
135+
blendState.AlphaSourceBlend = blendSrc;
136+
blendState.ColorDestinationBlend = blendDst;
137+
blendState.AlphaDestinationBlend = blendDst;
138+
break;
139+
default:
140+
blendState = defaultBlendState;
141+
break;
142+
}
143+
if (device.BlendState != blendState)
144+
{
145+
End();
146+
device.BlendState = blendState;
147+
}
148+
149+
RegionItem item = batcher.NextItem();
100150

101151
AtlasRegion region = (AtlasRegion)regionAttachment.RendererObject;
102152
item.texture = (Texture2D)region.page.rendererObject;

0 commit comments

Comments
 (0)