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
|
using System;
using System.Collections.Generic;
using System.Linq;
using UniGLTF;
using UniGLTF.ShaderPropExporter;
using UnityEngine;
namespace VRM
{
public class VRMMaterialExporter : MaterialExporter
{
public static bool UseUnlit(string shaderName)
{
switch (shaderName)
{
case "Unlit/Color":
case "Unlit/Texture":
case "Unlit/Transparent":
case "Unlit/Transparent Cutout":
case "UniGLTF/UniUnlit":
case "VRM/UnlitTexture":
case "VRM/UnlitTransparent":
case "VRM/UnlitCutout":
return true;
}
return false;
}
protected override glTFMaterial CreateMaterial(Material m)
{
switch (m.shader.name)
{
case "VRM/UnlitTexture":
return Export_VRMUnlitTexture(m);
case "VRM/UnlitTransparent":
return Export_VRMUnlitTransparent(m);
case "VRM/UnlitCutout":
return Export_VRMUnlitCutout(m);
case "VRM/UnlitTransparentZWrite":
return Export_VRMUnlitTransparentZWrite(m);
case "VRM/MToon":
return Export_VRMMToon(m);
default:
return base.CreateMaterial(m);
}
}
static glTFMaterial Export_VRMUnlitTexture(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();
material.alphaMode = "OPAQUE";
return material;
}
static glTFMaterial Export_VRMUnlitTransparent(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();
material.alphaMode = "BLEND";
return material;
}
static glTFMaterial Export_VRMUnlitCutout(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();
material.alphaMode = "MASK";
return material;
}
static glTFMaterial Export_VRMUnlitTransparentZWrite(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();
material.alphaMode = "BLEND";
return material;
}
static glTFMaterial Export_VRMMToon(Material m)
{
var material = glTF_KHR_materials_unlit.CreateDefault();
switch (m.GetTag("RenderType", true))
{
case "Transparent":
material.alphaMode = "BLEND";
break;
case "TransparentCutout":
material.alphaMode = "MASK";
material.alphaCutoff = m.GetFloat("_Cutoff");
break;
default:
material.alphaMode = "OPAQUE";
break;
}
switch ((int)m.GetFloat("_CullMode"))
{
case 0:
material.doubleSided = true;
break;
case 1:
Debug.LogWarning("ignore cull front");
break;
case 2:
// cull back
break;
default:
throw new NotImplementedException();
}
return material;
}
#region CreateFromMaterial
public static readonly string[] VRMExtensionShaders = new string[]
{
"VRM/UnlitTransparentZWrite",
"VRM/MToon"
};
static readonly string[] TAGS = new string[]{
"RenderType",
// "Queue",
};
public static glTF_VRM_Material CreateFromMaterial(Material m, List<Texture> textures)
{
var material = new glTF_VRM_Material
{
name = m.name,
shader = m.shader.name,
renderQueue = m.renderQueue,
};
if (!VRMExtensionShaders.Contains(m.shader.name))
{
material.shader = glTF_VRM_Material.VRM_USE_GLTFSHADER;
return material;
}
var prop = PreShaderPropExporter.GetPropsForSupportedShader(m.shader.name);
if (prop == null)
{
Debug.LogWarningFormat("Fail to export shader: {0}", m.shader.name);
}
else
{
foreach (var keyword in m.shaderKeywords)
{
material.keywordMap.Add(keyword, m.IsKeywordEnabled(keyword));
}
// get properties
//material.SetProp(prop);
foreach (var kv in prop.Properties)
{
switch (kv.ShaderPropertyType)
{
case ShaderPropertyType.Color:
{
var value = m.GetColor(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);
}
break;
case ShaderPropertyType.Range:
case ShaderPropertyType.Float:
{
var value = m.GetFloat(kv.Key);
material.floatProperties.Add(kv.Key, value);
}
break;
case ShaderPropertyType.TexEnv:
{
var texture = m.GetTexture(kv.Key);
if (texture != null)
{
var value = textures.IndexOf(texture);
if (value == -1)
{
Debug.LogFormat("not found {0}", texture.name);
}
else
{
material.textureProperties.Add(kv.Key, value);
}
}
// offset & scaling
var offset = m.GetTextureOffset(kv.Key);
var scaling = m.GetTextureScale(kv.Key);
material.vectorProperties.Add(kv.Key,
new float[] { offset.x, offset.y, scaling.x, scaling.y });
}
break;
case ShaderPropertyType.Vector:
{
var value = m.GetVector(kv.Key).ToArray();
material.vectorProperties.Add(kv.Key, value);
}
break;
default:
throw new NotImplementedException();
}
}
}
foreach (var tag in TAGS)
{
var value = m.GetTag(tag, false);
if (!String.IsNullOrEmpty(value))
{
material.tagMap.Add(tag, value);
}
}
return material;
}
#endregion
}
}
|