PageRenderTime 38ms CodeModel.GetById 31ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/ContentSystem/Rendering/ModelData.cs

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