PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/ContentSystem/Rendering/ModelData.cs

#
C# | 137 lines | 66 code | 10 blank | 61 comment | 4 complexity | 345a59775cedc44b834726dcd3c615a9 MD5 | raw file
Possible License(s): Apache-2.0
  1. using System.Collections.Generic;
  2. using Delta.Utilities;
  3. using Delta.Utilities.Helpers;
  4. namespace Delta.ContentSystem.Rendering
  5. {
  6. /// <summary>
  7. /// Model data class, which combines a list of meshes and a list of mesh
  8. /// animations. Because models can have multiple meshes and animations they
  9. /// should be used for more complex 3d data. If you only need a static mesh
  10. /// just use the Mesh class, which is better optimized and can be used for
  11. /// level geometry with lots of extra optimizations and mesh merging than
  12. /// possibly than rendering everything as models.
  13. /// <para />
  14. /// Please note that Models just contain meshes and animations and have no
  15. /// data own their own (just meta data).
  16. /// </summary>
  17. public class ModelData : Content
  18. {
  19. #region Get (Static)
  20. /// <summary>
  21. /// Get and load content based on the content name. This method makes sure
  22. /// we do not load the same content twice (the constructor is protected).
  23. /// </summary>
  24. /// <param name="contentName">Content name we want to load, this is
  25. /// passed onto the Content System, which will do the actual loading with
  26. /// help of the Load method in this class.</param>
  27. /// <returns>The loaded Content object, always unique for the same
  28. /// name, this helps comparing data.</returns>
  29. public static ModelData Get(string contentName)
  30. {
  31. return Get<ModelData>(contentName, ContentType.Model);
  32. }
  33. #endregion
  34. #region Meshes (Public)
  35. /// <summary>
  36. /// List of meshes to be used for this model. Often is just one mesh.
  37. /// Please note that we also will load and link up the animations inside
  38. /// of each mesh (see load method below), this way it is easier to manage.
  39. /// </summary>
  40. public List<MeshData> Meshes = new List<MeshData>();
  41. #endregion
  42. #region InitialScale (Public)
  43. /// <summary>
  44. /// Provides the initial scale value as provided by the content meta data.
  45. /// Usually 1.0f, which means rendering is done normally. If this value is
  46. /// different the content model will be scaled accordingly (should rarely
  47. /// be needed, scaling has obviously performance impacts, it is currently
  48. /// also ignored for most content types including meshes).
  49. /// </summary>
  50. public float InitialScale
  51. {
  52. get
  53. {
  54. return data.InitialScale;
  55. }
  56. }
  57. #endregion
  58. #region Constructors
  59. /// <summary>
  60. /// Create model data object with no data in it. Just add stuff to the
  61. /// Meshes and Animations fields.
  62. /// </summary>
  63. public ModelData()
  64. : base(EmptyName, ContentType.Model)
  65. {
  66. }
  67. /// <summary>
  68. /// Create mesh animation data by loading it from content, see Load below.
  69. /// Use the static Get method to call this.
  70. /// </summary>
  71. /// <param name="contentName">Name of the content.</param>
  72. protected ModelData(string contentName)
  73. : base(contentName, ContentType.Model)
  74. {
  75. }
  76. #endregion
  77. #region ToString (Public)
  78. /// <summary>
  79. /// To string, will display the model name with the meshes in it (plus
  80. /// optionally their animations if they have those).
  81. /// </summary>
  82. /// <returns>
  83. /// A <see cref="System.String"/> that represents this ModelData
  84. /// instance with the list of mesh names.
  85. /// </returns>
  86. public override string ToString()
  87. {
  88. return GetType().GetClassName() + " " + Name + ": " +
  89. "Meshes=" + Meshes.Write();
  90. }
  91. #endregion
  92. #region Methods (Private)
  93. #region Load
  94. /// <summary>
  95. /// Native load method, will just load meshes and animations contained in
  96. /// this content type (models have no data on their own).
  97. /// </summary>
  98. /// <param name="alreadyLoadedNativeData">Ignored here, can't be cloned.
  99. /// </param>
  100. protected override void Load(Content alreadyLoadedNativeData)
  101. {
  102. // Initialize a new list for all the mesh children
  103. Meshes.Clear();
  104. // Note: Only direct mesh children nodes are allowed, nothing else!
  105. foreach (ContentMetaData childrenData in data.Children)
  106. {
  107. if (childrenData.Type != ContentType.Mesh)
  108. {
  109. Log.Warning("Invalid children node for " + this + " found, only " +
  110. "mesh nodes are allowed: " + childrenData);
  111. // Skip this, maybe other data works
  112. continue;
  113. }
  114. // Note: We just add names, we don't want to load the images right away,
  115. // only when they need to be displayed they will be loaded (in the
  116. // Material class).
  117. Meshes.Add(MeshData.Get(childrenData.Name));
  118. }
  119. if (Meshes.Count == 0)
  120. {
  121. Log.Warning("No meshes found for " + this);
  122. }
  123. }
  124. #endregion
  125. #endregion
  126. }
  127. }