MonoGameModelTemplate.cs 5.0 KB

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