MonoGameModelTemplate.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Microsoft.Xna.Framework;
  5. using Microsoft.Xna.Framework.Graphics;
  6. #if USINGMONOGAMEMODEL
  7. using MODELMESH = Microsoft.Xna.Framework.Graphics.ModelMesh;
  8. using MODELMESHPART = Microsoft.Xna.Framework.Graphics.ModelMeshPart;
  9. #else
  10. using MODELMESH = SharpGLTF.Runtime.ModelMeshReplacement;
  11. using MODELMESHPART = SharpGLTF.Runtime.ModelMeshPartReplacement;
  12. #endif
  13. namespace SharpGLTF.Runtime
  14. {
  15. public class MonoGameModelTemplate
  16. {
  17. #region lifecycle
  18. public static MonoGameDeviceContent<MonoGameModelTemplate> LoadDeviceModel(GraphicsDevice device, string filePath)
  19. {
  20. var model = Schema2.ModelRoot.Load(filePath, Validation.ValidationMode.TryFix);
  21. return CreateDeviceModel(device, model);
  22. }
  23. public static MonoGameDeviceContent<MonoGameModelTemplate> CreateDeviceModel(GraphicsDevice device, Schema2.ModelRoot srcModel)
  24. {
  25. srcModel.FixTextureSampler();
  26. var templates = srcModel.LogicalScenes
  27. .Select(item => SceneTemplate.Create(item, true))
  28. .ToArray();
  29. var context = new LoaderContext(device);
  30. var meshes = templates
  31. .SelectMany(item => item.LogicalMeshIds)
  32. .ToDictionary(k => k, k => context.CreateMesh(srcModel.LogicalMeshes[k]));
  33. var mdl = new MonoGameModelTemplate(templates,srcModel.DefaultScene.LogicalIndex, meshes);
  34. return new MonoGameDeviceContent<MonoGameModelTemplate>(mdl, context.Disposables.ToArray());
  35. }
  36. internal MonoGameModelTemplate(SceneTemplate[] scenes, int defaultSceneIndex, IReadOnlyDictionary<int, MODELMESH> meshes)
  37. {
  38. _Meshes = meshes;
  39. _Effects = _Meshes.Values
  40. .SelectMany(item => item.Effects)
  41. .Distinct()
  42. .ToArray();
  43. _Scenes = scenes;
  44. _Bounds = scenes
  45. .Select(item => CalculateBounds(item))
  46. .ToArray();
  47. _DefaultSceneIndex = defaultSceneIndex;
  48. }
  49. #endregion
  50. #region data
  51. /// <summary>
  52. /// Meshes shared by all the scenes.
  53. /// </summary>
  54. internal readonly IReadOnlyDictionary<int, MODELMESH> _Meshes;
  55. /// <summary>
  56. /// Effects shared by all the meshes.
  57. /// </summary>
  58. private readonly Effect[] _Effects;
  59. private readonly SceneTemplate[] _Scenes;
  60. private readonly BoundingSphere[] _Bounds;
  61. private readonly int _DefaultSceneIndex;
  62. #endregion
  63. #region properties
  64. public int SceneCount => _Scenes.Length;
  65. public IReadOnlyList<Effect> Effects => _Effects;
  66. public BoundingSphere Bounds => GetBounds(_DefaultSceneIndex);
  67. public IEnumerable<string> AnimationTracks => GetAnimationTracks(_DefaultSceneIndex);
  68. #endregion
  69. #region API
  70. public int IndexOfScene(string sceneName) => Array.FindIndex(_Scenes, item => item.Name == sceneName);
  71. public BoundingSphere GetBounds(int sceneIndex) => _Bounds[sceneIndex];
  72. public IEnumerable<string> GetAnimationTracks(int sceneIndex) => _Scenes[sceneIndex].AnimationTracks;
  73. public MonoGameModelInstance CreateInstance() => CreateInstance(_DefaultSceneIndex);
  74. public MonoGameModelInstance CreateInstance(int sceneIndex)
  75. {
  76. return new MonoGameModelInstance(this, _Scenes[sceneIndex].CreateInstance());
  77. }
  78. private BoundingSphere CalculateBounds(SceneTemplate scene)
  79. {
  80. var instance = scene.CreateInstance();
  81. instance.SetPoseTransforms();
  82. var bounds = default(BoundingSphere);
  83. foreach (var d in instance.DrawableInstances)
  84. {
  85. var b = _Meshes[d.Template.LogicalMeshIndex].BoundingSphere;
  86. if (d.Transform is Transforms.RigidTransform statXform) b = b.Transform(statXform.WorldMatrix.ToXna());
  87. if (d.Transform is Transforms.SkinnedTransform skinXform)
  88. {
  89. // this is a bit agressive and probably over-reaching, but with skins you never know the actual bounds
  90. // unless you calculate the bounds frame by frame.
  91. var bb = b;
  92. foreach (var xb in skinXform.SkinMatrices.Select(item => bb.Transform(item.ToXna())))
  93. {
  94. b = BoundingSphere.CreateMerged(b, xb);
  95. }
  96. }
  97. bounds = bounds.Radius == 0 ? b : BoundingSphere.CreateMerged(bounds, b);
  98. }
  99. return bounds;
  100. }
  101. #endregion
  102. }
  103. }