| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
- using NJsonSchema.References;
- using JSONSCHEMA = NJsonSchema.JsonSchema;
- namespace SharpGLTF
- {
- using CodeGen;
- using SchemaReflection;
- class Program
- {
- #region MAIN
- static void Main(string[] args)
- {
- SchemaDownload.Syncronize(Constants.RemoteSchemaRepo, Constants.LocalRepoDirectory);
- _ProcessMainSchema();
- // XMP
- _ProcessKhronosXMPExtension();
- // material extensions
- _ProcessKhronosUnlitExtension();
- _ProcessKhronosSheenExtension();
- _ProcessKhronosClearCoatExtension();
- _ProcessKhronosTransmissionExtension();
- _ProcessKhronosSpecularGlossinessExtension();
- // lights
- _ProcessKhronosLightsPunctualExtension();
- // gpu mesh instancing
- _ProcessMeshGpuInstancingExtension();
- // textures
- _ProcessKhronosTextureTransformExtension();
- _ProcessMicrosoftTextureDDSExtension();
- _ProcessTextureWebpExtension();
- _ProcessTextureKtx2Extension();
- // these extansions are not fully supported and temporarily removed:
- // _ProcessDracoExtension();
- // _ProcessMicrosoftLODExtension();
- _ProcessAgiArticulationsExtension();
- }
- #endregion
- #region Main Schema code generation
- private static void _ProcessMainSchema()
- {
- // load and process schema
- var ctx1 = LoadSchemaContext(Constants.MainSchemaFile);
- // Ignore "glTF Property" because it is completely hand coded.
- ctx1.IgnoredByCodeEmitter("glTF Property");
- // We will mimeType "anyof" as a plain string.
- ctx1.Remove("image/jpeg-image/png");
- // replace Image.mimeType type from an Enum to String, so we can serialize it with more formats if required
- ctx1.FindClass("Image")
- .GetField("mimeType")
- .FieldType = ctx1.UseString();
- // replace Node.Matrix, Node.Rotation, Node.Scale and Node.Translation with System.Numerics.Vectors types
- var node = ctx1.FindClass("Node");
- node.GetField("matrix").SetDataType(typeof(System.Numerics.Matrix4x4), true).RemoveDefaultValue().SetItemsRange(0);
- node.GetField("rotation").SetDataType(typeof(System.Numerics.Quaternion), true).RemoveDefaultValue().SetItemsRange(0);
- node.GetField("scale").SetDataType(typeof(System.Numerics.Vector3), true).RemoveDefaultValue().SetItemsRange(0);
- node.GetField("translation").SetDataType(typeof(System.Numerics.Vector3), true).RemoveDefaultValue().SetItemsRange(0);
- // replace Material.emissiveFactor with System.Numerics.Vectors types
- ctx1.FindClass("Material")
- .GetField("emissiveFactor")
- .SetDataType(typeof(System.Numerics.Vector3), true)
- .SetDefaultValue("Vector3.Zero")
- .SetItemsRange(0);
- // replace Material.baseColorFactor with System.Numerics.Vectors types
- ctx1.FindClass("Material PBR Metallic Roughness")
- .GetField("baseColorFactor")
- .SetDataType(typeof(System.Numerics.Vector4), true)
- .SetDefaultValue("Vector4.One")
- .SetItemsRange(0);
- ctx1.FindEnum("LINEAR-NEAREST")
- .SetValue("DEFAULT",0);
- ctx1.FindEnum("LINEAR-LINEAR_MIPMAP_LINEAR-LINEAR_MIPMAP_NEAREST-NEAREST-NEAREST_MIPMAP_LINEAR-NEAREST_MIPMAP_NEAREST")
- .SetValue("DEFAULT", 0);
- // Accessor.type is declared as AnyOfEnum, but also as a STRING,
- // which can be used by extensions to store non standard values like MAT4x3
- ctx1.FindClass("Accessor")
- .GetField("type").SetDataType(typeof(string), true);
- // Since DimensionType can have additional values other than the ones defined by the schema
- // we need a "special" value to define it
- ctx1.FindEnum("MAT2-MAT3-MAT4-SCALAR-VEC2-VEC3-VEC4")
- .SetValue("CUSTOM", 0);
- ProcessSchema("gltf.g", ctx1);
- }
- #endregion
- #region Extensions code generation
- private static void _ProcessKhronosXMPExtension()
- {
- // Model extension
- var ctx = LoadSchemaContext(Constants.KhronosExtensions.XMP_Model);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- /*
- var jdict = ctx.UseClass("JsonDictionary");
- var jlist = ctx.UseClass("JsonList");
- ctx.FindClass("KHR_xmp glTF extension")
- .GetField("@context")
- .SetDataType(jdict);
- ctx.FindClass("KHR_xmp glTF extension")
- .GetField("packets")
- .SetDataType(jlist);*/
- /*
- ctx.FindClass("KHR_xmp glTF extension")
- .GetField("@context")
- .SetDataType(typeof(Dictionary<string,Object>), true);
- */
- ProcessSchema("ext.XMP.Model.g", ctx);
- // Node extension
- ctx = LoadSchemaContext(Constants.KhronosExtensions.XMP_Node);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- ProcessSchema("ext.XMP.Node.g", ctx);
- }
- private static void _ProcessKhronosSpecularGlossinessExtension()
- {
- var ctx = LoadSchemaContext(Constants.KhronosExtensions.PbrSpecularGlossiness);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- ctx.IgnoredByCodeEmitter("Texture Info");
- ctx.FindClass("KHR_materials_pbrSpecularGlossiness glTF extension")
- .GetField("diffuseFactor")
- .SetDataType(typeof(System.Numerics.Vector4), true)
- .SetDefaultValue("Vector4.One")
- .SetItemsRange(0);
- ctx.FindClass("KHR_materials_pbrSpecularGlossiness glTF extension")
- .GetField("specularFactor")
- .SetDataType(typeof(System.Numerics.Vector3), true)
- .SetDefaultValue("Vector3.One")
- .SetItemsRange(0);
- ProcessSchema("ext.pbrSpecularGlossiness.g", ctx);
- }
- private static void _ProcessKhronosUnlitExtension()
- {
- var ctx = LoadSchemaContext(Constants.KhronosExtensions.Unlit);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- ProcessSchema("ext.Unlit.g", ctx);
- }
- private static void _ProcessKhronosClearCoatExtension()
- {
- var ctx = LoadSchemaContext(Constants.KhronosExtensions.PbrClearCoat);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- ctx.IgnoredByCodeEmitter("Texture Info");
- ctx.IgnoredByCodeEmitter("Material Normal Texture Info");
- ProcessSchema("ext.ClearCoat.g", ctx);
- }
- private static void _ProcessKhronosTransmissionExtension()
- {
- var ctx = LoadSchemaContext(Constants.KhronosExtensions.PbrTransmission);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- ctx.IgnoredByCodeEmitter("Texture Info");
- ctx.IgnoredByCodeEmitter("Material Normal Texture Info");
- ProcessSchema("ext.Transmission.g", ctx);
- }
- private static void _ProcessKhronosSheenExtension()
- {
- var ctx = LoadSchemaContext(Constants.KhronosExtensions.PbrSheen);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- ctx.IgnoredByCodeEmitter("Texture Info");
- ctx.IgnoredByCodeEmitter("Material Normal Texture Info");
-
- ctx.FindClass("KHR_materials_sheen glTF extension")
- .GetField("sheenColorFactor")
- .SetDataType(typeof(System.Numerics.Vector3), true)
- .SetDefaultValue("Vector3.Zero")
- .SetItemsRange(0);
- ctx.FindClass("KHR_materials_sheen glTF extension")
- .GetField("sheenRoughnessFactor")
- .SetDataType(typeof(float), true)
- .SetItemsRange(0);
- ProcessSchema("ext.Sheen.g", ctx);
- }
- private static void _ProcessKhronosLightsPunctualExtension()
- {
- // Model
- var ctx = LoadSchemaContext(Constants.KhronosExtensions.LightsPunctual_Model);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- ctx.FindClass("light")
- .GetField("color")
- .SetDataType(typeof(System.Numerics.Vector3), true)
- .SetDefaultValue("Vector3.One")
- .SetItemsRange(0);
- ProcessSchema("ext.ModelLightsPunctual.g", ctx);
- // Node
- ctx = LoadSchemaContext(Constants.KhronosExtensions.LightsPunctual_Node);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- ProcessSchema("ext.NodeLightsPunctual.g", ctx);
- }
- private static void _ProcessKhronosTextureTransformExtension()
- {
- var ctx = LoadSchemaContext(Constants.KhronosExtensions.TextureTransform);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
- var tex = ctx.FindClass("KHR_texture_transform textureInfo extension");
- tex.GetField("offset")
- .SetDataType(typeof(System.Numerics.Vector2), true)
- .SetDefaultValue("Vector2.Zero")
- .SetItemsRange(0);
- tex.GetField("scale")
- .SetDataType(typeof(System.Numerics.Vector2), true)
- .SetDefaultValue("Vector2.One")
- .SetItemsRange(0);
- ProcessSchema("ext.TextureTransform.g", ctx);
- }
- private static void _ProcessMicrosoftTextureDDSExtension()
- {
- var ctx = LoadSchemaContext(Constants.VendorExtensions.TextureDDS);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ProcessSchema("ext.MSFT.TextureDDS.g", ctx);
- }
- private static void _ProcessTextureWebpExtension()
- {
- var ctx = LoadSchemaContext(Constants.VendorExtensions.TextureWebp);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ProcessSchema("ext.TextureWEBP.g", ctx);
- }
- private static void _ProcessTextureKtx2Extension()
- {
- var ctx = LoadSchemaContext(Constants.KhronosExtensions.Ktx2);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ProcessSchema("ext.TextureKTX2.g", ctx);
- }
- private static void _ProcessMeshGpuInstancingExtension()
- {
- var ctx = LoadSchemaContext(Constants.VendorExtensions.MeshGpuInstancing);
- ctx.IgnoredByCodeEmitter("glTF Property");
- ctx.IgnoredByCodeEmitter("glTF Child of Root Property");
-
- ProcessSchema("ext.MeshGpuInstancing.g", ctx);
- }
- private static void _ProcessAgiArticulationsExtension()
- {
- var ctx1 = LoadSchemaContext(Constants.VendorExtensions.RootAgiArticulations);
- ctx1.IgnoredByCodeEmitter("glTF Property");
- ctx1.IgnoredByCodeEmitter("glTF Child of Root Property");
- ProcessSchema("ext.RootAgiArticulations.g", ctx1);
- var ctx2 = LoadSchemaContext(Constants.VendorExtensions.NodeAgiArticulations);
- ctx2.IgnoredByCodeEmitter("glTF Property");
- ctx2.IgnoredByCodeEmitter("glTF Child of Root Property");
- ProcessSchema("ext.NodeAgiArticulations.g", ctx2);
- }
- #endregion
- #region code generation
- private static string _FindTargetDirectory(string dstDir)
- {
- var dir = Constants.LocalRepoDirectory;
- while(dir.Length > 3)
- {
- var xdir = System.IO.Path.Combine(dir, dstDir);
- if (System.IO.Directory.Exists(xdir)) return xdir;
- dir = System.IO.Path.GetDirectoryName(dir); // move up
- }
- return null;
- }
- private static void ProcessSchema(string dstFile, SchemaType.Context ctx)
- {
- var newEmitter = new CSharpEmitter();
- newEmitter.DeclareContext(ctx);
- newEmitter.SetCollectionContainer("List<TItem>");
- const string rootName = "ModelRoot";
- newEmitter.SetRuntimeName("glTF", rootName);
- newEmitter.SetRuntimeName("glTF Property", "ExtraProperties");
- newEmitter.SetRuntimeName("glTF Child of Root Property", "LogicalChildOfRoot");
- newEmitter.SetRuntimeName("Sampler", "TextureSampler");
- newEmitter.SetRuntimeName("UNSIGNED_BYTE-UNSIGNED_INT-UNSIGNED_SHORT", "IndexEncodingType");
- newEmitter.SetRuntimeName("BYTE-FLOAT-SHORT-UNSIGNED_BYTE-UNSIGNED_INT-UNSIGNED_SHORT", "EncodingType");
- newEmitter.SetRuntimeName("MAT2-MAT3-MAT4-SCALAR-VEC2-VEC3-VEC4", "DimensionType");
- newEmitter.SetRuntimeName("rotation-scale-translation-weights", "PropertyPath");
- newEmitter.SetRuntimeName("ARRAY_BUFFER-ELEMENT_ARRAY_BUFFER", "BufferMode");
- newEmitter.SetRuntimeName("orthographic-perspective", "CameraType");
- newEmitter.SetRuntimeName("BLEND-MASK-OPAQUE", "AlphaMode");
- newEmitter.SetRuntimeName("LINE_LOOP-LINE_STRIP-LINES-POINTS-TRIANGLE_FAN-TRIANGLE_STRIP-TRIANGLES", "PrimitiveType");
- newEmitter.SetRuntimeName("CUBICSPLINE-LINEAR-STEP", "AnimationInterpolationMode");
- newEmitter.SetRuntimeName("LINEAR-NEAREST", "TextureInterpolationFilter");
- newEmitter.SetRuntimeName("CLAMP_TO_EDGE-MIRRORED_REPEAT-REPEAT", "TextureWrapMode");
- newEmitter.SetRuntimeName("LINEAR-LINEAR_MIPMAP_LINEAR-LINEAR_MIPMAP_NEAREST-NEAREST-NEAREST_MIPMAP_LINEAR-NEAREST_MIPMAP_NEAREST", "TextureMipMapFilter");
- newEmitter.SetRuntimeName("KHR_materials_pbrSpecularGlossiness glTF extension", "MaterialPBRSpecularGlossiness");
- newEmitter.SetRuntimeName("KHR_materials_unlit glTF extension", "MaterialUnlit");
- newEmitter.SetRuntimeName("KHR_materials_clearcoat glTF extension", "MaterialClearCoat");
- newEmitter.SetRuntimeName("KHR_materials_transmission glTF extension", "MaterialTransmission");
- newEmitter.SetRuntimeName("KHR_materials_sheen glTF extension", "MaterialSheen");
- newEmitter.SetRuntimeName("KHR_xmp glTF extension", "XMPPacketsCollection");
- newEmitter.SetRuntimeName("KHR_xmp node extension", "XMPPacketReference");
- newEmitter.SetRuntimeName("light", "PunctualLight");
- newEmitter.SetRuntimeName("light/spot", "PunctualLightSpot");
- newEmitter.SetRuntimeName("KHR_lights_punctual glTF extension", "_ModelPunctualLights");
- newEmitter.SetRuntimeName("KHR_lights_punctual node extension", "_NodePunctualLight");
- newEmitter.SetRuntimeName("KHR_texture_transform textureInfo extension", "TextureTransform");
- newEmitter.SetRuntimeName("MSFT_texture_dds extension", "TextureDDS");
- newEmitter.SetRuntimeName("EXT_texture_webp glTF extension", "TextureWEBP");
- newEmitter.SetRuntimeName("KHR_texture_basisu glTF extension", "TextureKTX2");
- newEmitter.SetRuntimeName("EXT_mesh_gpu_instancing glTF extension", "MeshGpuInstancing");
- newEmitter.SetRuntimeName("AGI_articulations glTF extension", "RootAgiArticulations");
- newEmitter.SetRuntimeName("AGI_articulations glTF Node extension", "NodeAgiArticulations");
- newEmitter.SetRuntimeName("uniformScale-xRotate-xScale-xTranslate-yRotate-yScale-yTranslate-zRotate-zScale-zTranslate", "ArticulationTransformType");
- var classes = ctx.Classes.ToArray();
- var fields = classes.SelectMany(item => item.Fields).ToArray();
- var meshClass = ctx.FindClass("Mesh");
- if (meshClass != null)
- {
- newEmitter.SetCollectionContainer(meshClass.UseField("primitives"), "ChildrenCollection<TItem,Mesh>");
- }
- var animationClass = ctx.FindClass("Animation");
- if (animationClass != null)
- {
- newEmitter.SetCollectionContainer(animationClass.UseField("channels"), "ChildrenCollection<TItem,Animation>");
- newEmitter.SetCollectionContainer(animationClass.UseField("samplers"), "ChildrenCollection<TItem,Animation>");
- }
-
- foreach (var f in fields)
- {
- if (f.FieldType is ArrayType atype)
- {
- if (atype.ItemType is ClassType ctype)
- {
- if (ctype.BaseClass != null && ctype.BaseClass.PersistentName == "glTF Child of Root Property")
- {
- newEmitter.SetCollectionContainer(f, $"ChildrenCollection<TItem,{rootName}>");
- }
- }
- }
- }
- var textOut = newEmitter.EmitContext(ctx);
- var dstDir = _FindTargetDirectory(Constants.TargetProjectDirectory);
- var dstPath = System.IO.Path.Combine(dstDir, $"{dstFile}.cs");
- System.IO.File.WriteAllText(dstPath, textOut);
- }
- #endregion
- #region schema loader
- private static SchemaType.Context LoadSchemaContext(string srcSchema)
- {
- var schema = LoadSchema(srcSchema);
- var settings = new NJsonSchema.CodeGeneration.CSharp.CSharpGeneratorSettings
- {
- Namespace = "glTf.POCO",
- ClassStyle = NJsonSchema.CodeGeneration.CSharp.CSharpClassStyle.Poco
- };
- var ctypes = new NJsonSchema.CodeGeneration.CSharp.CSharpTypeResolver(settings);
- ctypes.Resolve(schema, false, null);
- return SchemaTypesReader.Generate(ctypes);
- }
- static JSONSCHEMA LoadSchema(string filePath)
- {
- // https://blogs.msdn.microsoft.com/benjaminperkins/2017/03/08/how-to-call-an-async-method-from-a-console-app-main-method/
- if (!System.IO.File.Exists(filePath)) throw new System.IO.FileNotFoundException(nameof(filePath), filePath);
- return JSONSCHEMA
- .FromFileAsync(filePath, s => _Resolver(s,filePath) )
- .ConfigureAwait(false)
- .GetAwaiter()
- .GetResult();
- }
- static NJsonSchema.JsonReferenceResolver _Resolver(JSONSCHEMA schema, string basePath)
- {
- var generator = new NJsonSchema.Generation.JsonSchemaGeneratorSettings();
- var solver = new NJsonSchema.JsonSchemaAppender(schema, generator.TypeNameGenerator);
- return new MyReferenceResolver(solver);
- }
- class MyReferenceResolver : NJsonSchema.JsonReferenceResolver
- {
- public MyReferenceResolver(NJsonSchema.JsonSchemaAppender resolver) : base(resolver) { }
- public override Task<IJsonReference> ResolveFileReferenceAsync(string filePath, System.Threading.CancellationToken cancellationToken)
- {
- if (System.IO.File.Exists(filePath)) return base.ResolveFileReferenceAsync(filePath);
- filePath = System.IO.Path.GetFileName(filePath);
- filePath = System.IO.Path.Combine(Constants.MainSchemaDir, filePath);
- if (System.IO.File.Exists(filePath)) return base.ResolveFileReferenceAsync(filePath);
- throw new System.IO.FileNotFoundException(filePath);
- }
- }
- #endregion
- }
- }
|