-
Notifications
You must be signed in to change notification settings - Fork 475
/
NormalMapEncodingConverter.cs
73 lines (62 loc) · 2.15 KB
/
NormalMapEncodingConverter.cs
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
using System.Threading.Tasks;
using UnityEngine;
namespace UnityGLTF
{
public static class NormalMapEncodingConverter
{
public static async Task<Texture2D> ConvertToDxt5nmAndCheckTextureFormatAsync(Texture2D source)
{
Texture2D dest = source;
var pixels = source.GetPixels();
if (source.format != TextureFormat.RGBA32)
{
dest = new Texture2D(source.width, source.height, TextureFormat.RGBA32, source.mipmapCount > 0, true);
dest.wrapMode = source.wrapMode;
dest.wrapModeU = source.wrapModeU;
dest.wrapModeV = source.wrapModeV;
dest.wrapModeW = source.wrapModeW;
dest.filterMode = source.filterMode;
dest.anisoLevel = source.anisoLevel;
dest.mipMapBias = source.mipMapBias;
#if UNITY_EDITOR
Texture.DestroyImmediate(source);
#else
Texture.Destroy(source);
#endif
}
await Task.Run(() =>
{
for (var i = 0; i < pixels.Length; i++)
{
var c = pixels[i];
pixels[i] = new Color(1, c.g, 1, c.r);
}
});
dest.SetPixels(pixels);
return dest;
}
public static void ConvertToDxt5nm(Texture2D source, Texture2D dest)
{
var pixels = source.GetPixels();
for (var i = 0; i < pixels.Length; i++)
{
var c = pixels[i];
pixels[i] = new Color(1, c.g, 1, c.r);
}
dest.SetPixels(pixels);
}
public static async Task ConvertToDxt5nmAsync(Texture2D texture)
{
var pixels = texture.GetPixels();
await Task.Run(() =>
{
for (var i = 0; i < pixels.Length; i++)
{
var c = pixels[i];
pixels[i] = new Color(1, c.g, 1, c.r);
}
});
texture.SetPixels(pixels);
}
}
}