2
0

MainSchemaProcessor.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SharpGLTF.CodeGen;
  6. using SharpGLTF.SchemaReflection;
  7. namespace SharpGLTF
  8. {
  9. public class MainSchemaProcessor : SchemaProcessor
  10. {
  11. public override IEnumerable<(string TargetFileName, SchemaType.Context Schema)> ReadSchema()
  12. {
  13. // load and process schema
  14. var ctx = SchemaProcessing.LoadSchemaContext(Constants.MainSchemaFile);
  15. // Ignore "glTF Property" because it is completely hand coded.
  16. ctx.IgnoredByCodeEmitter("glTF Property");
  17. // We will mimeType "anyof" as a plain string.
  18. ctx.Remove("image/jpeg-image/png");
  19. // replace Image.mimeType type from an Enum to String, so we can serialize it with more formats if required
  20. ctx.FindClass("Image")
  21. .GetField("mimeType")
  22. .FieldType = ctx.UseString();
  23. // replace Node.Matrix, Node.Rotation, Node.Scale and Node.Translation with System.Numerics.Vectors types
  24. var node = ctx.FindClass("Node");
  25. // the default values of the transform properties is both a "known value" and null, so
  26. // we preffer to set here the "known value" since it's also used to check whether
  27. // the value should be serialized.
  28. // But then, we need to set the values to null in the Node Constructor,
  29. // because Matrix and SRT are mutually exclusive.
  30. node.GetField("matrix").SetDataType(typeof(System.Numerics.Matrix4x4), true).SetDefaultValue("System.Numerics.Matrix4x4.Identity").SetItemsRange(0);
  31. node.GetField("scale").SetDataType(typeof(System.Numerics.Vector3), true).SetDefaultValue("Vector3.One").SetItemsRange(0);
  32. node.GetField("rotation").SetDataType(typeof(System.Numerics.Quaternion), true).SetDefaultValue("Quaternion.Identity").SetItemsRange(0);
  33. node.GetField("translation").SetDataType(typeof(System.Numerics.Vector3), true).SetDefaultValue("Vector3.Zero").SetItemsRange(0);
  34. // replace Material.emissiveFactor with System.Numerics.Vectors types
  35. ctx.FindClass("Material")
  36. .GetField("emissiveFactor")
  37. .SetDataType(typeof(System.Numerics.Vector3), true)
  38. .SetDefaultValue("Vector3.Zero")
  39. .SetItemsRange(0);
  40. // replace Material.baseColorFactor with System.Numerics.Vectors types
  41. ctx.FindClass("Material PBR Metallic Roughness")
  42. .GetField("baseColorFactor")
  43. .SetDataType(typeof(System.Numerics.Vector4), true)
  44. .SetDefaultValue("Vector4.One")
  45. .SetItemsRange(0);
  46. ctx.FindEnum("LINEAR-NEAREST")
  47. .SetValue("DEFAULT", 0);
  48. ctx.FindEnum("LINEAR-LINEAR_MIPMAP_LINEAR-LINEAR_MIPMAP_NEAREST-NEAREST-NEAREST_MIPMAP_LINEAR-NEAREST_MIPMAP_NEAREST")
  49. .SetValue("DEFAULT", 0);
  50. // Accessor.type is declared as AnyOfEnum, but also as a STRING,
  51. // which can be used by extensions to store non standard values like MAT4x3
  52. ctx.FindClass("Accessor")
  53. .GetField("type").SetDataType(typeof(string), true);
  54. // Since DimensionType can have additional values other than the ones defined by the schema
  55. // we need a "special" value to define it
  56. ctx.FindEnum("MAT2-MAT3-MAT4-SCALAR-VEC2-VEC3-VEC4")
  57. .SetValue("CUSTOM", 0);
  58. yield return ("gltf.g", ctx);
  59. }
  60. public override void PrepareTypes(CSharpEmitter newEmitter, SchemaType.Context ctx)
  61. {
  62. newEmitter.SetCollectionContainer("List<TItem>");
  63. const string rootName = "ModelRoot";
  64. newEmitter.SetRuntimeName("glTF", rootName);
  65. newEmitter.SetRuntimeName("glTF Property", "ExtraProperties");
  66. newEmitter.SetRuntimeName("glTF Child of Root Property", "LogicalChildOfRoot");
  67. newEmitter.SetRuntimeName("Sampler", "TextureSampler");
  68. newEmitter.SetRuntimeName("UNSIGNED_BYTE-UNSIGNED_INT-UNSIGNED_SHORT", "IndexEncodingType");
  69. newEmitter.SetRuntimeName("BYTE-FLOAT-SHORT-UNSIGNED_BYTE-UNSIGNED_INT-UNSIGNED_SHORT", "EncodingType");
  70. newEmitter.SetRuntimeName("MAT2-MAT3-MAT4-SCALAR-VEC2-VEC3-VEC4", "DimensionType");
  71. newEmitter.SetRuntimeName("rotation-scale-translation-weights", "PropertyPath");
  72. newEmitter.SetRuntimeName("ARRAY_BUFFER-ELEMENT_ARRAY_BUFFER", "BufferMode");
  73. newEmitter.SetRuntimeName("orthographic-perspective", "CameraType");
  74. newEmitter.SetRuntimeName("BLEND-MASK-OPAQUE", "AlphaMode");
  75. newEmitter.SetRuntimeName("LINE_LOOP-LINE_STRIP-LINES-POINTS-TRIANGLE_FAN-TRIANGLE_STRIP-TRIANGLES", "PrimitiveType");
  76. newEmitter.SetRuntimeName("CUBICSPLINE-LINEAR-STEP", "AnimationInterpolationMode");
  77. newEmitter.SetRuntimeName("LINEAR-NEAREST", "TextureInterpolationFilter");
  78. newEmitter.SetRuntimeName("CLAMP_TO_EDGE-MIRRORED_REPEAT-REPEAT", "TextureWrapMode");
  79. newEmitter.SetRuntimeName("LINEAR-LINEAR_MIPMAP_LINEAR-LINEAR_MIPMAP_NEAREST-NEAREST-NEAREST_MIPMAP_LINEAR-NEAREST_MIPMAP_NEAREST", "TextureMipMapFilter");
  80. // add "pointer" enum value to PropertyPath, which is required by KHR_animation_pointer
  81. var propertyPathEnum = ctx.FindEnum("rotation-scale-translation-weights");
  82. propertyPathEnum?.SetValue("pointer", 0);
  83. var meshClass = ctx.FindClass("Mesh");
  84. if (meshClass != null)
  85. {
  86. newEmitter.SetCollectionContainer(meshClass.UseField("primitives"), "ChildrenList<TItem,Mesh>");
  87. }
  88. var animationClass = ctx.FindClass("Animation");
  89. if (animationClass != null)
  90. {
  91. newEmitter.SetCollectionContainer(animationClass.UseField("channels"), "ChildrenList<TItem,Animation>");
  92. newEmitter.SetCollectionContainer(animationClass.UseField("samplers"), "ChildrenList<TItem,Animation>");
  93. }
  94. var classes = ctx.Classes.ToArray();
  95. var fields = classes
  96. .SelectMany(item => item.Fields)
  97. .ToArray();
  98. foreach (var f in fields)
  99. {
  100. if (f.FieldType is ArrayType atype)
  101. {
  102. if (atype.ItemType is ClassType ctype)
  103. {
  104. if (ctype.BaseClass != null && ctype.BaseClass.PersistentName == "glTF Child of Root Property")
  105. {
  106. newEmitter.SetCollectionContainer(f, $"ChildrenList<TItem,{rootName}>");
  107. }
  108. }
  109. }
  110. }
  111. }
  112. }
  113. }