TextureIO.cs
5.45 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
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
#if UNITY_EDITOR
using UnityEditor;
#endif
namespace UniGLTF
{
public static class TextureIO
{
public static RenderTextureReadWrite GetColorSpace(glTFTextureTypes textureType)
{
switch (textureType)
{
case glTFTextureTypes.Metallic:
case glTFTextureTypes.Normal:
case glTFTextureTypes.Occlusion:
return RenderTextureReadWrite.Linear;
case glTFTextureTypes.BaseColor:
case glTFTextureTypes.Emissive:
return RenderTextureReadWrite.sRGB;
default:
return RenderTextureReadWrite.sRGB;
}
}
public static glTFTextureTypes GetglTFTextureType(string shaderName, string propName)
{
switch (propName)
{
case "_Color":
return glTFTextureTypes.BaseColor;
case "_MetallicGlossMap":
return glTFTextureTypes.Metallic;
case "_BumpMap":
return glTFTextureTypes.Normal;
case "_OcclusionMap":
return glTFTextureTypes.Occlusion;
case "_EmissionMap":
return glTFTextureTypes.Emissive;
default:
return glTFTextureTypes.Unknown;
}
}
public static glTFTextureTypes GetglTFTextureType(glTF glTf, int textureIndex)
{
foreach (var material in glTf.materials)
{
var textureInfo = material.GetTextures().FirstOrDefault(x => (x!=null) && x.index == textureIndex);
if (textureInfo != null)
{
return textureInfo.TextreType;
}
}
return glTFTextureTypes.Unknown;
}
#if UNITY_EDITOR
public static void MarkTextureAssetAsNormalMap(string assetPath)
{
if (string.IsNullOrEmpty(assetPath))
{
return;
}
var textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;
if (null == textureImporter)
{
return;
}
//Debug.LogFormat("[MarkTextureAssetAsNormalMap] {0}", assetPath);
textureImporter.textureType = TextureImporterType.NormalMap;
textureImporter.SaveAndReimport();
}
#endif
public struct TextureExportItem
{
public Texture Texture;
public glTFTextureTypes TextureType;
public TextureExportItem(Texture texture, glTFTextureTypes textureType)
{
Texture = texture;
TextureType = textureType;
}
}
public static IEnumerable<TextureExportItem> GetTextures(Material m)
{
var props = ShaderPropExporter.PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
if (props == null)
{
yield return new TextureExportItem(m.mainTexture, glTFTextureTypes.BaseColor);
}
foreach (var prop in props.Properties)
{
if (prop.ShaderPropertyType == ShaderPropExporter.ShaderPropertyType.TexEnv)
{
yield return new TextureExportItem(m.GetTexture(prop.Key), GetglTFTextureType(m.shader.name, prop.Key));
}
}
}
struct BytesWithMime
{
public Byte[] Bytes;
public string Mime;
}
static BytesWithMime GetBytesWithMime(Texture texture, glTFTextureTypes textureType)
{
#if UNITY_EDITOR
var path = UnityPath.FromAsset(texture);
if (path.IsUnderAssetsFolder)
{
if (path.Extension == ".png")
{
return new BytesWithMime
{
Bytes = System.IO.File.ReadAllBytes(path.FullPath),
Mime = "image/png",
};
}
}
#endif
return new BytesWithMime
{
Bytes = TextureItem.CopyTexture(texture, TextureIO.GetColorSpace(textureType), null).EncodeToPNG(),
Mime = "image/png",
};
}
public static int ExportTexture(glTF gltf, int bufferIndex, Texture texture, glTFTextureTypes textureType)
{
var bytesWithMime = GetBytesWithMime(texture, textureType); ;
// add view
var view = gltf.buffers[bufferIndex].Append(bytesWithMime.Bytes, glBufferTarget.NONE);
var viewIndex = gltf.AddBufferView(view);
// add image
var imageIndex = gltf.images.Count;
gltf.images.Add(new glTFImage
{
name = texture.name,
bufferView = viewIndex,
mimeType = bytesWithMime.Mime,
});
// add sampler
var samplerIndex = gltf.samplers.Count;
var sampler = TextureSamplerUtil.Export(texture);
gltf.samplers.Add(sampler);
// add texture
gltf.textures.Add(new glTFTexture
{
sampler = samplerIndex,
source = imageIndex,
});
return imageIndex;
}
}
}