forked from KhronosGroup/glTF-Asset-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
189 lines (166 loc) · 9.03 KB
/
Program.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
using AssetGenerator.Conversion;
using AssetGenerator.ModelGroups;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
namespace AssetGenerator
{
internal class Program
{
private static void Main(string[] args)
{
Stopwatch.StartNew();
Assembly executingAssembly = Assembly.GetExecutingAssembly();
string executingAssemblyFolder = Path.GetDirectoryName(executingAssembly.Location);
char pathSeparator = Path.DirectorySeparatorChar;
string outputFolder = Path.GetFullPath(Path.Combine(executingAssemblyFolder, string.Format(@"..{0}..{0}..{0}..{0}Output", pathSeparator)));
string positiveTestsFolder = Path.Combine(outputFolder, "Positive");
string negativeTestsFolder = Path.Combine(outputFolder, "Negative");
var jsonSerializer = new JsonSerializer
{
Formatting = Formatting.Indented,
ContractResolver = new CamelCasePropertyNamesContractResolver()
};
// Make an inventory of what images there are.
var imageList = FileHelper.FindImageFiles(Path.Combine(executingAssemblyFolder, "Resources"));
// Create an ordered list initalizing each model group.
var positiveTests = new List<ModelGroup>
{
new Animation_Node(imageList),
new Animation_NodeMisc(imageList),
new Animation_SamplerType(imageList),
new Animation_Skin(imageList),
new Animation_SkinType(imageList),
new Buffer_Interleaved(imageList),
new Buffer_Misc(imageList),
new Compatibility(imageList),
new Instancing(imageList),
new Material(imageList),
new Material_AlphaBlend(imageList),
new Material_AlphaMask(imageList),
new Material_DoubleSided(imageList),
new Material_MetallicRoughness(imageList),
new Material_Mixed(imageList),
new Material_SpecularGlossiness(imageList),
new Mesh_PrimitiveAttribute(imageList),
new Mesh_PrimitiveMode(imageList),
new Mesh_PrimitiveVertexColor(imageList),
new Mesh_Primitives(imageList),
new Mesh_PrimitivesUV(imageList),
new Node_Attribute(imageList),
new Node_NegativeScale(imageList),
new Texture_Sampler(imageList),
};
var negativeTests = new List<ModelGroup>
{
new Mesh_PrimitiveRestart(imageList),
new Mesh_NoPosition(imageList),
};
// Retains the manifest from each test type for use in updating the main readme table.
var mainManifests = new List<List<Manifest>>
{
ProcessModelGroups(positiveTests, positiveTestsFolder),
ProcessModelGroups(negativeTests, negativeTestsFolder),
};
ReadmeBuilder.UpdateMainReadme(executingAssembly, mainManifests, Directory.GetParent(outputFolder).ToString(), new string[] { "Positive", "Negative" });
// Writes a readme explaining the different test types.
using (var newReadme = new FileStream(Path.Combine(outputFolder, "README.md"), FileMode.Create))
{
executingAssembly.GetManifestResourceStream("AssetGenerator.ReadmeTemplates.Page_Output.md").CopyTo(newReadme);
}
Console.WriteLine("Model Creation Complete!");
Console.WriteLine($"Completed in : {TimeSpan.FromTicks(Stopwatch.GetTimestamp()).ToString()}");
/// <summary>
/// Saves each model group to a master manifest as it is created, and writes that manifest to file.
/// </summary>
/// <returns>Manifest containing all of the model groups and created.</returns>
List<Manifest> ProcessModelGroups(List<ModelGroup> modelGroupList, string savePath)
{
// Generates the models and saves the model group manifest in with the master manifest.
var manifests = new List<Manifest>();
foreach (var modelGroup in modelGroupList)
{
manifests.Add(GenerateModels(modelGroup, savePath));
}
// Writes the master manifest to file.
using (var writeManifest = new StreamWriter(Path.Combine(savePath, "Manifest.json")))
{
jsonSerializer.Serialize(writeManifest, manifests.ToArray());
}
return manifests;
}
/// <summary>
/// Create and write all models contained in a model group. Writes a mini-manifest for just the model group to file.
/// Generates a readme describing the models created. Also copies over required textures and figures.
/// </summary>
/// <returns>Manifest containing the model group's models.</returns>
Manifest GenerateModels(ModelGroup modelGroup, string savePath)
{
var readme = new ReadmeBuilder();
var manifest = new Manifest(modelGroup.Id);
string modelGroupFolder = Path.Combine(savePath, modelGroup.Id.ToString());
Directory.CreateDirectory(modelGroupFolder);
// Copy all of the images used by the model group into that model group's output directory.
FileHelper.CopyImageFiles(executingAssemblyFolder, modelGroupFolder, modelGroup.UsedFigures);
FileHelper.CopyImageFiles(executingAssemblyFolder, modelGroupFolder, modelGroup.UsedTextures, useThumbnails: true);
readme.SetupHeader(modelGroup);
var numCombos = modelGroup.Models.Count;
for (var comboIndex = 0; comboIndex < numCombos; comboIndex++)
{
var model = modelGroup.Models[comboIndex];
var filename = $"{modelGroup.Id}_{comboIndex:00}.gltf";
void Convert(Func<BinaryDataType, BinaryData> getBinaryData)
{
// Pass the desired properties to the runtime layer, which then coverts that data into
// a gltf loader object, ready to create the model.
var converter = new Converter(getBinaryData, model.CreateSchemaInstance);
glTFLoader.Schema.Gltf gltf = converter.Convert(model.GLTF);
// Makes last second changes to the model that bypass the runtime layer.
model.PostRuntimeChanges?.Invoke(gltf);
// Create the .gltf file and writes the model's data to it.
string assetFile = Path.Combine(modelGroupFolder, filename);
glTFLoader.Interface.SaveModel(gltf, assetFile);
}
void WriteBinaryDataFiles(params BinaryData[] bins)
{
foreach (var bin in bins)
{
// Write model data to the .bin file.
File.WriteAllBytes(Path.Combine(modelGroupFolder, bin.Name), bin.Bytes);
}
}
if (!model.SeparateBuffers)
{
using (var binaryData = new BinaryData($"{modelGroup.Id}_{comboIndex:00}.bin"))
{
Convert(type => binaryData);
WriteBinaryDataFiles(binaryData);
}
}
else
{
using (var binaryData = new BinaryData($"{modelGroup.Id}_{comboIndex:00}.bin"))
using (var binaryDataAnimation = new BinaryData($"{modelGroup.Id}_Animation_{comboIndex:00}.bin"))
{
Convert(type => type == BinaryDataType.Animation ? binaryDataAnimation : binaryData);
WriteBinaryDataFiles(binaryData, binaryDataAnimation);
}
}
readme.SetupTable(modelGroup, comboIndex, model, Path.GetFileName(savePath));
manifest.Models.Add(new Manifest.Model(filename, modelGroup.Id, modelGroup.NoSampleImages, model.Camera, model.Animated, model.Loadable));
}
// Write the readme and manifest specific to this model group.
readme.WriteOut(executingAssembly, modelGroup, modelGroupFolder);
using (var writeModelGroupManifest = new StreamWriter(Path.Combine(modelGroupFolder, "Manifest.json")))
{
jsonSerializer.Serialize(writeModelGroupManifest, manifest);
}
return manifest;
}
}
}
}