TextureLoader.cs
8.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
using System;
using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniGLTF
{
public interface ITextureLoader : IDisposable
{
Texture2D Texture { get; }
/// <summary>
/// Call from any thread
/// </summary>
/// <param name="gltf"></param>
/// <param name="storage"></param>
void ProcessOnAnyThread(glTF gltf, IStorage storage);
/// <summary>
/// Call from unity main thread
/// </summary>
/// <param name="isLinear"></param>
/// <returns></returns>
IEnumerator ProcessOnMainThread(bool isLinear);
}
#if UNITY_EDITOR
public class AssetTextureLoader : ITextureLoader
{
public Texture2D Texture
{
private set;
get;
}
UnityPath m_assetPath;
public AssetTextureLoader(UnityPath assetPath, string _)
{
m_assetPath = assetPath;
}
public void Dispose()
{
}
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
}
public IEnumerator ProcessOnMainThread(bool isLinear)
{
//
// texture from assets
//
m_assetPath.ImportAsset();
var importer = m_assetPath.GetImporter<TextureImporter>();
if (importer == null)
{
Debug.LogWarningFormat("fail to get TextureImporter: {0}", m_assetPath);
}
importer.sRGBTexture = !isLinear;
importer.SaveAndReimport();
Texture = m_assetPath.LoadAsset<Texture2D>();
//Texture.name = m_textureName;
if (Texture == null)
{
Debug.LogWarningFormat("fail to Load Texture2D: {0}", m_assetPath);
}
yield break;
}
}
#endif
public class TextureLoader : ITextureLoader
{
int m_textureIndex;
public TextureLoader(int textureIndex)
{
m_textureIndex = textureIndex;
}
public Texture2D Texture
{
private set;
get;
}
public void Dispose()
{
}
static Byte[] ToArray(ArraySegment<byte> bytes)
{
if (bytes.Array == null)
{
return new byte[] { };
}
else if (bytes.Offset == 0 && bytes.Count == bytes.Array.Length)
{
return bytes.Array;
}
else
{
Byte[] result = new byte[bytes.Count];
Buffer.BlockCopy(bytes.Array, bytes.Offset, result, 0, result.Length);
return result;
}
}
Byte[] m_imageBytes;
string m_textureName;
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);
var segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName);
m_imageBytes = ToArray(segments);
}
public IEnumerator ProcessOnMainThread(bool isLinear)
{
//
// texture from image(png etc) bytes
//
Texture = new Texture2D(2, 2, TextureFormat.ARGB32, false, isLinear);
Texture.name = m_textureName;
if (m_imageBytes != null)
{
Texture.LoadImage(m_imageBytes);
}
yield break;
}
}
public class UnityWebRequestTextureLoader : ITextureLoader
{
public Texture2D Texture
{
private set;
get;
}
int m_textureIndex;
public UnityWebRequestTextureLoader(int textureIndex)
{
m_textureIndex = textureIndex;
}
UnityWebRequest m_uwr;
public void Dispose()
{
if (m_uwr != null)
{
m_uwr.Dispose();
m_uwr = null;
}
}
ArraySegment<Byte> m_segments;
string m_textureName;
public void ProcessOnAnyThread(glTF gltf, IStorage storage)
{
var imageIndex = gltf.GetImageIndexFromTextureIndex(m_textureIndex);
m_segments = gltf.GetImageBytes(storage, imageIndex, out m_textureName);
}
#if false
HttpHost m_http;
class HttpHost : IDisposable
{
TcpListener m_listener;
Socket m_connection;
public HttpHost(int port)
{
m_listener = new TcpListener(IPAddress.Loopback, port);
m_listener.Start();
m_listener.BeginAcceptSocket(OnAccepted, m_listener);
}
void OnAccepted(IAsyncResult ar)
{
var l = ar.AsyncState as TcpListener;
if (l == null) return;
m_connection = l.EndAcceptSocket(ar);
// 次の接続受付はしない
BeginRead(m_connection, new byte[8192]);
}
void BeginRead(Socket c, byte[] buffer)
{
AsyncCallback callback = ar =>
{
var s = ar.AsyncState as Socket;
if (s == null) return;
var size = s.EndReceive(ar);
if (size > 0)
{
OnRead(buffer, size);
}
BeginRead(s, buffer);
};
m_connection.BeginReceive(buffer, 0, buffer.Length, SocketFlags.None, callback, m_connection);
}
List<Byte> m_buffer = new List<byte>();
void OnRead(byte[] buffer, int len)
{
m_buffer.AddRange(buffer.Take(len));
}
public string Url
{
get
{
}
}
public void Dispose()
{
if (m_connection != null)
{
m_connection.Dispose();
m_connection = null;
}
if(m_listener != null)
{
m_listener.Stop();
m_listener = null;
}
}
}
#endif
class Deleter : IDisposable
{
string m_path;
public Deleter(string path)
{
m_path = path;
}
public void Dispose()
{
if (File.Exists(m_path))
{
File.Delete(m_path);
}
}
}
public IEnumerator ProcessOnMainThread(bool isLinear)
{
// tmp file
var tmp = Path.GetTempFileName();
using (var f = new FileStream(tmp, FileMode.Create))
{
f.Write(m_segments.Array, m_segments.Offset, m_segments.Count);
}
using (var d = new Deleter(tmp))
{
var url = "file:///" + tmp.Replace("\\", "/");
Debug.LogFormat("UnityWebRequest: {0}", url);
#if UNITY_2017_1_OR_NEWER
using (var m_uwr = UnityWebRequestTexture.GetTexture(url, true))
{
yield return m_uwr.SendWebRequest();
if (m_uwr.isNetworkError || m_uwr.isHttpError)
{
Debug.LogWarning(m_uwr.error);
}
else
{
// Get downloaded asset bundle
Texture = ((DownloadHandlerTexture)m_uwr.downloadHandler).texture;
Texture.name = m_textureName;
}
}
#elif UNITY_5
using (var m_uwr = new WWW(url))
{
yield return m_uwr;
// wait for request
while (!m_uwr.isDone)
{
yield return null;
}
if (!string.IsNullOrEmpty(m_uwr.error))
{
Debug.Log(m_uwr.error);
yield break;
}
// Get downloaded asset bundle
Texture = m_uwr.textureNonReadable;
Texture.name = m_textureName;
}
#else
#error Unsupported Unity version
#endif
}
}
}
}