@@ -13,7 +13,7 @@ namespace Explorer.Helpers
13
13
{
14
14
public static class Texture2DHelpers
15
15
{
16
- #if CPP
16
+ #if CPP // If Mono
17
17
#else
18
18
private static bool isNewEncodeMethod = false ;
19
19
private static MethodInfo EncodeToPNGMethod => m_encodeToPNGMethod ?? GetEncodeToPNGMethod ( ) ;
@@ -56,43 +56,38 @@ public static bool IsReadable(this Texture2D tex)
56
56
}
57
57
}
58
58
59
- public static Texture2D Copy ( Texture2D other , Rect rect , bool isDTXnmNormal = false )
59
+ public static Texture2D Copy ( Texture2D orig , Rect rect , bool isDTXnmNormal = false )
60
60
{
61
61
Color [ ] pixels ;
62
62
63
- if ( ! other . IsReadable ( ) )
63
+ if ( ! orig . IsReadable ( ) )
64
64
{
65
- other = ForceReadTexture ( other , isDTXnmNormal ) ;
65
+ orig = ForceReadTexture ( orig ) ;
66
66
}
67
67
68
- pixels = other . GetPixels ( ( int ) rect . x , ( int ) rect . y , ( int ) rect . width , ( int ) rect . height ) ;
68
+ pixels = orig . GetPixels ( ( int ) rect . x , ( int ) rect . y , ( int ) rect . width , ( int ) rect . height ) ;
69
69
70
70
var _newTex = new Texture2D ( ( int ) rect . width , ( int ) rect . height ) ;
71
71
_newTex . SetPixels ( pixels ) ;
72
72
73
73
return _newTex ;
74
74
}
75
75
76
- public static Texture2D ForceReadTexture ( Texture2D tex , bool isDTXnmNormal = false )
76
+ public static Texture2D ForceReadTexture ( Texture2D tex )
77
77
{
78
78
try
79
79
{
80
80
var origFilter = tex . filterMode ;
81
81
tex . filterMode = FilterMode . Point ;
82
82
83
- RenderTexture rt = RenderTexture . GetTemporary ( tex . width , tex . height , 0 , RenderTextureFormat . ARGB32 ) ;
83
+ var rt = RenderTexture . GetTemporary ( tex . width , tex . height , 0 , RenderTextureFormat . ARGB32 ) ;
84
84
rt . filterMode = FilterMode . Point ;
85
85
RenderTexture . active = rt ;
86
86
Graphics . Blit ( tex , rt ) ;
87
87
88
- Texture2D _newTex = new Texture2D ( tex . width , tex . height , TextureFormat . RGBA32 , false ) ;
89
- _newTex . ReadPixels ( new Rect ( 0 , 0 , tex . width , tex . height ) , 0 , 0 ) ;
90
-
91
- if ( isDTXnmNormal )
92
- {
93
- _newTex = DTXnmToRGBA ( _newTex ) ;
94
- }
88
+ var _newTex = new Texture2D ( tex . width , tex . height , TextureFormat . ARGB32 , false ) ;
95
89
90
+ _newTex . ReadPixels ( new Rect ( 0 , 0 , tex . width , tex . height ) , 0 , 0 ) ;
96
91
_newTex . Apply ( false , false ) ;
97
92
98
93
RenderTexture . active = null ;
@@ -117,8 +112,11 @@ public static void SaveTextureAsPNG(Texture2D tex, string dir, string name, bool
117
112
byte [ ] data ;
118
113
var savepath = dir + @"\" + name + ".png" ;
119
114
120
- // Fix for non-Readable or Compressed textures.
121
- tex = ForceReadTexture ( tex , isDTXnmNormal ) ;
115
+ // Make sure we can EncodeToPNG it.
116
+ if ( tex . format != TextureFormat . ARGB32 || ! tex . IsReadable ( ) )
117
+ {
118
+ tex = ForceReadTexture ( tex ) ;
119
+ }
122
120
123
121
if ( isDTXnmNormal )
124
122
{
@@ -129,15 +127,13 @@ public static void SaveTextureAsPNG(Texture2D tex, string dir, string name, bool
129
127
#if CPP
130
128
data = tex . EncodeToPNG ( ) ;
131
129
#else
132
- var method = EncodeToPNGMethod ;
133
-
134
130
if ( isNewEncodeMethod )
135
131
{
136
- data = ( byte [ ] ) method . Invoke ( null , new object [ ] { tex } ) ;
132
+ data = ( byte [ ] ) EncodeToPNGMethod . Invoke ( null , new object [ ] { tex } ) ;
137
133
}
138
134
else
139
135
{
140
- data = ( byte [ ] ) method . Invoke ( tex , new object [ 0 ] ) ;
136
+ data = ( byte [ ] ) EncodeToPNGMethod . Invoke ( tex , new object [ 0 ] ) ;
141
137
}
142
138
#endif
143
139
@@ -148,8 +144,10 @@ public static void SaveTextureAsPNG(Texture2D tex, string dir, string name, bool
148
144
else
149
145
{
150
146
#if CPP
151
- // The IL2CPP method will return invalid byte data.
152
- // However, we can just iterate into safe C# byte[] array.
147
+ // The Il2Cpp EncodeToPNG() method does return System.Byte[],
148
+ // but for some reason it is not recognized or valid.
149
+ // Simple fix is iterating into a new array manually.
150
+
153
151
byte [ ] safeData = new byte [ data . Length ] ;
154
152
for ( int i = 0 ; i < data . Length ; i ++ )
155
153
{
@@ -185,7 +183,7 @@ public static Texture2D DTXnmToRGBA(Texture2D tex)
185
183
) ;
186
184
}
187
185
188
- var newtex = new Texture2D ( tex . width , tex . height , TextureFormat . RGBA32 , false ) ;
186
+ var newtex = new Texture2D ( tex . width , tex . height , TextureFormat . ARGB32 , false ) ;
189
187
newtex . SetPixels ( colors ) ;
190
188
191
189
return newtex ;
0 commit comments