2
0

MonoGameModelTemplate.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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.RuntimeModelMesh;
  11. using MODELMESHPART = SharpGLTF.Runtime.RuntimeModelMeshPart;
  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, LoaderContext context = null)
  19. {
  20. var model = Schema2.ModelRoot.Load(filePath, Validation.ValidationMode.TryFix);
  21. return CreateDeviceModel(device, model, context);
  22. }
  23. public static MonoGameDeviceContent<MonoGameModelTemplate> CreateDeviceModel(GraphicsDevice device, Schema2.ModelRoot srcModel, LoaderContext context = null)
  24. {
  25. if (context == null) context = new BasicEffectsLoaderContext(device);
  26. context.Reset();
  27. var templates = srcModel.LogicalScenes
  28. .Select(item => SceneTemplate.Create(item, true))
  29. .ToArray();
  30. var srcMeshes = templates
  31. .SelectMany(item => item.LogicalMeshIds)
  32. .Distinct()
  33. .Select(idx => srcModel.LogicalMeshes[idx]);
  34. foreach(var srcMesh in srcMeshes)
  35. {
  36. context._WriteMesh(srcMesh);
  37. }
  38. var dstMeshes = context.CreateRuntimeModels();
  39. var mdl = new MonoGameModelTemplate(templates,srcModel.DefaultScene.LogicalIndex, dstMeshes);
  40. return new MonoGameDeviceContent<MonoGameModelTemplate>(mdl, context.Disposables.ToArray());
  41. }
  42. internal MonoGameModelTemplate(SceneTemplate[] scenes, int defaultSceneIndex, IReadOnlyDictionary<int, MODELMESH> meshes)
  43. {
  44. _Meshes = meshes;
  45. _Effects = _Meshes.Values
  46. .SelectMany(item => item.Effects)
  47. .Distinct()
  48. .ToArray();
  49. _Scenes = scenes;
  50. _Bounds = scenes
  51. .Select(item => CalculateBounds(item))
  52. .ToArray();
  53. _DefaultSceneIndex = defaultSceneIndex;
  54. }
  55. #endregion
  56. #region data
  57. /// <summary>
  58. /// Meshes shared by all the scenes.
  59. /// </summary>
  60. internal readonly IReadOnlyDictionary<int, MODELMESH> _Meshes;
  61. /// <summary>
  62. /// Effects shared by all the meshes.
  63. /// </summary>
  64. private readonly Effect[] _Effects;
  65. private readonly SceneTemplate[] _Scenes;
  66. private readonly BoundingSphere[] _Bounds;
  67. private readonly int _DefaultSceneIndex;
  68. #endregion
  69. #region properties
  70. public int SceneCount => _Scenes.Length;
  71. public IReadOnlyList<Effect> Effects => _Effects;
  72. public BoundingSphere Bounds => GetBounds(_DefaultSceneIndex);
  73. #endregion
  74. #region API
  75. public int IndexOfScene(string sceneName) => Array.FindIndex(_Scenes, item => item.Name == sceneName);
  76. public BoundingSphere GetBounds(int sceneIndex) => _Bounds[sceneIndex];
  77. public MonoGameModelInstance CreateInstance() => CreateInstance(_DefaultSceneIndex);
  78. public MonoGameModelInstance CreateInstance(int sceneIndex)
  79. {
  80. return new MonoGameModelInstance(this, _Scenes[sceneIndex].CreateInstance());
  81. }
  82. private BoundingSphere CalculateBounds(SceneTemplate scene)
  83. {
  84. var instance = scene.CreateInstance();
  85. var bounds = default(BoundingSphere);
  86. foreach (var d in instance.DrawableInstances)
  87. {
  88. var b = _Meshes[d.Template.LogicalMeshIndex].BoundingSphere;
  89. if (d.Transform is Transforms.RigidTransform statXform) b = b.Transform(statXform.WorldMatrix.ToXna());
  90. if (d.Transform is Transforms.SkinnedTransform skinXform)
  91. {
  92. // this is a bit agressive and probably over-reaching, but with skins you never know the actual bounds
  93. // unless you calculate the bounds frame by frame.
  94. var bb = b;
  95. foreach (var xb in skinXform.SkinMatrices.Select(item => bb.Transform(item.ToXna())))
  96. {
  97. b = BoundingSphere.CreateMerged(b, xb);
  98. }
  99. }
  100. bounds = bounds.Radius == 0 ? b : BoundingSphere.CreateMerged(bounds, b);
  101. }
  102. return bounds;
  103. }
  104. #endregion
  105. }
  106. }