forked from KhronosGroup/glTF-Asset-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompatibility.cs
285 lines (247 loc) · 11.1 KB
/
Compatibility.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
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
using AssetGenerator.Runtime;
using AssetGenerator.Runtime.Extensions;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using Schema = glTFLoader.Schema;
namespace AssetGenerator.ModelGroups
{
internal class Compatibility : ModelGroup
{
public override ModelGroupId Id => ModelGroupId.Compatibility;
public Compatibility(List<string> imageList)
{
NoSampleImages = true;
// There are no common properties in this model group.
Model CreateModel(Action<List<Property>, Asset, List<string>, List<string>, Runtime.MeshPrimitive> setProperties,
Action<Schema.Gltf> postRuntimeChanges = null, Dictionary<Type, Type> schemaTypeMapping = null, bool? setLoadableTag = true)
{
var properties = new List<Property>();
var gltf = CreateGLTF(() => new Scene());
Runtime.MeshPrimitive meshPrimitive = MeshPrimitive.CreateSinglePlane(includeTextureCoords: false);
var extensionsUsed = new List<string>();
var extensionsRequired = new List<string>();
// Apply the properties that are specific to this gltf.
setProperties(properties, gltf.Asset, extensionsUsed, extensionsRequired, meshPrimitive);
// Create the gltf object.
if (extensionsUsed.Any())
{
gltf.ExtensionsUsed = extensionsUsed;
}
if (extensionsRequired.Any())
{
gltf.ExtensionsRequired = extensionsRequired;
}
gltf.Scenes.First().Nodes = new List<Node>
{
new Node
{
Mesh = new Runtime.Mesh
{
MeshPrimitives = new List<Runtime.MeshPrimitive>
{
meshPrimitive
}
},
},
};
var model = new Model
{
Properties = properties,
GLTF = gltf
};
model.Loadable = setLoadableTag;
model.PostRuntimeChanges = postRuntimeChanges;
if (schemaTypeMapping != null)
{
model.CreateSchemaInstance = type =>
{
if (schemaTypeMapping.TryGetValue(type, out Type mappedType))
{
type = mappedType;
}
return Activator.CreateInstance(type);
};
}
return model;
}
Property SetMinVersion(Asset asset)
{
return new Property(PropertyName.MinVersion, asset.MinVersion = "2.1");
}
Property SetVersionCurrent(Asset asset)
{
return new Property(PropertyName.Version, asset.Version = "2.0");
}
Property SetVersionFuture(Asset asset)
{
return new Property(PropertyName.Version, asset.Version = "2.1");
}
void SetPostRuntimeAtRoot(Schema.Gltf gltf)
{
// Add an simulated feature at the root level.
var experimentalGltf = (ExperimentalGltf)gltf;
experimentalGltf.Lights = new[]
{
new ExperimentalLight
{
Color = new[] { 0.3f, 0.4f, 0.5f }
}
};
}
void SetPostRuntimeInProperty(Schema.Gltf gltf)
{
// Add an simulated feature into an existing property.
var experimentalNode = (ExperimentalNode)gltf.Nodes[0];
experimentalNode.Light = 0;
}
void SetPostRuntimeWithFallback(Schema.Gltf gltf)
{
// Add an simulated feature with a fallback option.
gltf.Materials = new Schema.Material[]
{
new ExperimentalMaterial
{
AlphaMode = Schema.Material.AlphaModeEnum.BLEND,
AlphaMode2 = ExperimentalAlphaMode2.QUANTUM,
}
};
}
var experimentalSchemaTypeMapping = new Dictionary<Type, Type>
{
{ typeof(Schema.Gltf), typeof(ExperimentalGltf) },
{ typeof(Schema.Node), typeof(ExperimentalNode) },
{ typeof(Schema.Material), typeof(ExperimentalMaterial) },
};
var shouldLoad = ":white_check_mark:";
Models = new List<Model>
{
CreateModel((properties, asset, extensionsUsed, extensionsRequired, meshPrimitive) =>
{
properties.Add(SetVersionCurrent(asset));
properties.Add(new Property(PropertyName.ModelShouldLoad, shouldLoad));
}),
CreateModel((properties, asset, extensionsUsed, extensionsRequired, meshPrimitive) =>
{
properties.Add(SetVersionFuture(asset));
properties.Add(new Property(PropertyName.Description, "Light object added at root"));
properties.Add(new Property(PropertyName.ModelShouldLoad, shouldLoad));
}, SetPostRuntimeAtRoot, experimentalSchemaTypeMapping),
CreateModel((properties, asset, extensionsUsed, extensionsRequired, meshPrimitive) =>
{
properties.Add(SetVersionFuture(asset));
properties.Add(new Property(PropertyName.Description, "Light property added to node object"));
properties.Add(new Property(PropertyName.ModelShouldLoad, shouldLoad));
}, SetPostRuntimeInProperty, experimentalSchemaTypeMapping),
CreateModel((properties, asset, extensionsUsed, extensionsRequired, meshPrimitive) =>
{
properties.Add(SetVersionFuture(asset));
properties.Add(new Property(PropertyName.Description, "Alpha mode updated with a new enum value, and a fallback value"));
properties.Add(new Property(PropertyName.ModelShouldLoad, shouldLoad));
}, SetPostRuntimeWithFallback, experimentalSchemaTypeMapping),
CreateModel((properties, asset, extensionsUsed, extensionsRequired, meshPrimitive) =>
{
properties.Add(SetMinVersion(asset));
properties.Add(SetVersionFuture(asset));
properties.Add(new Property(PropertyName.Description, "Requires a specific version or higher"));
properties.Add(new Property(PropertyName.ModelShouldLoad, "Only in version 2.1 or higher"));
}, null, null, setLoadableTag: false),
CreateModel((properties, asset, extensionsUsed, extensionsRequired, meshPrimitive) =>
{
properties.Add(SetVersionCurrent(asset));
var emptyTexture = new Texture();
var extension = new FAKE_materials_quantumRendering
{
PlanckFactor = new Vector4(0.2f, 0.2f, 0.2f, 0.8f),
CopenhagenTexture = new TextureInfo
{
Texture = emptyTexture
},
EntanglementFactor = new Vector3(0.4f, 0.4f, 0.4f),
ProbabilisticFactor = 0.3f,
SuperpositionCollapseTexture = new TextureInfo
{
Texture = emptyTexture
},
};
meshPrimitive.Material = new Runtime.Material
{
Extensions = new List<Extension>
{
extension
}
};
extensionsUsed.Add(extension.Name);
extensionsRequired.Add(extension.Name);
properties.Add(new Property(PropertyName.Description, "Extension required"));
properties.Add(new Property(PropertyName.ModelShouldLoad, ":x:"));
}, null, null, setLoadableTag: false),
CreateModel((properties, asset, extensionsUsed, extensionsRequired, meshPrimitive) =>
{
properties.Add(SetVersionCurrent(asset));
var extension = new KHR_materials_pbrSpecularGlossiness
{
SpecularFactor = new Vector3(0.04f, 0.04f, 0.04f),
GlossinessFactor = 0.0f,
};
meshPrimitive.Material = new Runtime.Material
{
// Metallic-Roughness
PbrMetallicRoughness = new PbrMetallicRoughness
{
MetallicFactor = 0.0f
},
// Specular-Glossiness
Extensions = new[] { extension },
};
extensionsUsed.Add(extension.Name);
properties.Add(new Property(PropertyName.Description, "Specular Glossiness extension used but not required"));
properties.Add(new Property(PropertyName.ModelShouldLoad, shouldLoad));
}),
};
GenerateUsedPropertiesList();
}
private class ExperimentalNode : Schema.Node
{
[JsonProperty("light")]
public int? Light { get; set; }
public bool ShouldSerializeLight()
{
return Light.HasValue;
}
}
private class ExperimentalLight
{
[JsonProperty("color")]
public float[] Color { get; set; }
}
// Used to add a property to the root level, or into an existing property.
private class ExperimentalGltf : Schema.Gltf
{
// Creates a new root level property
[JsonProperty("lights")]
public ExperimentalLight[] Lights { get; set; }
public bool ShouldSerializeLights()
{
return Lights != null;
}
}
private enum ExperimentalAlphaMode2
{
OPAQUE = 0,
MASK = 1,
BLEND = 2,
QUANTUM = 3,
}
// Used to add a new enum into an existing property with a fallback option.
private class ExperimentalMaterial : Schema.Material
{
[JsonConverter(typeof(StringEnumConverter))]
[JsonProperty("alphaMode2")]
public ExperimentalAlphaMode2 AlphaMode2 { get; set; }
}
}
}