ModelMeshReplacement.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Xna.Framework.Graphics;
  7. namespace SharpGLTF.Runtime
  8. {
  9. /// <summary>
  10. /// Replaces <see cref="ModelMeshPart"/>.
  11. /// </summary>
  12. sealed class ModelMeshPartReplacement
  13. {
  14. internal ModelMeshReplacement _Parent;
  15. private Effect _Effect;
  16. public Effect Effect
  17. {
  18. get => _Effect;
  19. set
  20. {
  21. if (_Effect == value) return;
  22. _Effect = value;
  23. _Parent.InvalidateEffectsCollection(); // if we change this property, we need to invalidate the parent's effect collection.
  24. }
  25. }
  26. public IndexBuffer IndexBuffer { get; set; }
  27. public int NumVertices { get; set; }
  28. public int PrimitiveCount { get; set; }
  29. public int StartIndex { get; set; }
  30. public object Tag { get; set; }
  31. public VertexBuffer VertexBuffer { get; set; }
  32. public int VertexOffset { get; set; }
  33. }
  34. /// <summary>
  35. /// Replaces <see cref="ModelMesh"/>
  36. /// </summary>
  37. sealed class ModelMeshReplacement
  38. {
  39. private GraphicsDevice graphicsDevice;
  40. public ModelMeshReplacement(GraphicsDevice graphicsDevice, List<ModelMeshPartReplacement> parts)
  41. {
  42. // TODO: Complete member initialization
  43. this.graphicsDevice = graphicsDevice;
  44. MeshParts = parts.ToArray();
  45. foreach (var mp in MeshParts) mp._Parent = this;
  46. }
  47. private IReadOnlyList<Effect> _Effects;
  48. public IReadOnlyCollection<Effect> Effects
  49. {
  50. get
  51. {
  52. if (_Effects != null) return _Effects;
  53. // effects collection has changed since last call, so we reconstruct the collection.
  54. _Effects = MeshParts
  55. .Select(item => item.Effect)
  56. .Distinct()
  57. .ToArray();
  58. return _Effects;
  59. }
  60. }
  61. public Microsoft.Xna.Framework.BoundingSphere BoundingSphere { get; set; }
  62. public IList<ModelMeshPartReplacement> MeshParts { get; set; }
  63. public string Name { get; set; }
  64. public ModelBone ParentBone { get; set; }
  65. public object Tag { get; set; }
  66. internal void InvalidateEffectsCollection() { _Effects = null; }
  67. public void Draw()
  68. {
  69. for (int i = 0; i < MeshParts.Count; i++)
  70. {
  71. var part = MeshParts[i];
  72. var effect = part.Effect;
  73. if (part.PrimitiveCount > 0)
  74. {
  75. this.graphicsDevice.SetVertexBuffer(part.VertexBuffer);
  76. this.graphicsDevice.Indices = part.IndexBuffer;
  77. for (int j = 0; j < effect.CurrentTechnique.Passes.Count; j++)
  78. {
  79. effect.CurrentTechnique.Passes[j].Apply();
  80. graphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.VertexOffset, part.StartIndex, part.PrimitiveCount);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }