MonoGameModelTemplate.cs 4.5 KB

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