Material.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. //-----------------------------------------------------------------------------
  2. // Material.cs
  3. //
  4. // Microsoft XNA Community Game Platform
  5. // Copyright (C) Microsoft Corporation. All rights reserved.
  6. //-----------------------------------------------------------------------------
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Text;
  10. using RacingGame.Helpers;
  11. using RacingGame.Shaders;
  12. using Microsoft.Xna.Framework;
  13. using Microsoft.Xna.Framework.Graphics;
  14. namespace RacingGame.Graphics
  15. {
  16. /// <summary>
  17. /// Material class for DirectX materials used for Models. Consists of
  18. /// normal DirectX material settings (ambient, diffuse, specular),
  19. /// the diffuse texture and optionally of normal map, height map and shader
  20. /// parameters.
  21. /// </summary>
  22. public class Material : IDisposable
  23. {
  24. /// <summary>
  25. /// Default color values are:
  26. /// 0.15f for ambient and 1.0f for diffuse and 1.0f specular.
  27. /// </summary>
  28. public static readonly Color
  29. DefaultAmbientColor = new Color(40, 40, 40),
  30. DefaultDiffuseColor = new Color(210, 210, 210),
  31. DefaultSpecularColor = new Color(255, 255, 255);
  32. /// <summary>
  33. /// Default specular power (24)
  34. /// </summary>
  35. const float DefaultSpecularPower = 24.0f;
  36. /// <summary>
  37. /// Parallax amount for parallax and offset shaders.
  38. /// </summary>
  39. public const float DefaultParallaxAmount = 0.04f;
  40. /// <summary>
  41. /// Colors
  42. /// </summary>
  43. public Color diffuseColor = DefaultDiffuseColor,
  44. ambientColor = DefaultAmbientColor,
  45. specularColor = DefaultSpecularColor;
  46. /// <summary>
  47. /// Specular power
  48. /// </summary>
  49. public float specularPower = DefaultSpecularPower;
  50. /// <summary>
  51. /// Diffuse texture for the material. Can be null for unused.
  52. /// </summary>
  53. public Texture diffuseTexture = null;
  54. /// <summary>
  55. /// Normal texture in case we use normal mapping. Can be null for unused.
  56. /// </summary>
  57. public Texture normalTexture = null;
  58. /// <summary>
  59. /// Height texture in case we use parallax mapping. Can be null for unused.
  60. /// </summary>
  61. public Texture heightTexture = null;
  62. /// <summary>
  63. /// Detail texture, used for landscape rendering. Can be null for unused.
  64. /// </summary>
  65. public Texture detailTexture = null;
  66. /// <summary>
  67. /// Parallax amount for parallax and offset shaders.
  68. /// </summary>
  69. public float parallaxAmount = DefaultParallaxAmount;
  70. /// <summary>
  71. /// Checks if the diffuse texture has alpha
  72. /// </summary>
  73. public bool HasAlpha
  74. {
  75. get
  76. {
  77. if (diffuseTexture != null)
  78. return diffuseTexture.HasAlphaPixels;
  79. else
  80. return false;
  81. }
  82. }
  83. /// <summary>
  84. /// Create material, just using default values.
  85. /// </summary>
  86. public Material()
  87. {
  88. }
  89. /// <summary>
  90. /// Create material, just using default color values.
  91. /// </summary>
  92. public Material(string setDiffuseTexture)
  93. {
  94. diffuseTexture = new Texture(setDiffuseTexture);
  95. }
  96. /// <summary>
  97. /// Create material
  98. /// </summary>
  99. public Material(Color setAmbientColor, Color setDiffuseColor,
  100. string setDiffuseTexture)
  101. {
  102. ambientColor = setAmbientColor;
  103. diffuseColor = setDiffuseColor;
  104. diffuseTexture = new Texture(setDiffuseTexture);
  105. // Leave rest to default
  106. }
  107. /// <summary>
  108. /// Create material
  109. /// </summary>
  110. public Material(Color setAmbientColor, Color setDiffuseColor,
  111. Texture setDiffuseTexture)
  112. {
  113. ambientColor = setAmbientColor;
  114. diffuseColor = setDiffuseColor;
  115. diffuseTexture = setDiffuseTexture;
  116. // Leave rest to default
  117. }
  118. /// <summary>
  119. /// Create material
  120. /// </summary>
  121. public Material(string setDiffuseTexture, string setNormalTexture)
  122. {
  123. diffuseTexture = new Texture(setDiffuseTexture);
  124. normalTexture = new Texture(setNormalTexture);
  125. // Leave rest to default
  126. }
  127. /// <summary>
  128. /// Create material
  129. /// </summary>
  130. public Material(string setDiffuseTexture, string setNormalTexture,
  131. string setHeightTexture)
  132. {
  133. diffuseTexture = new Texture(setDiffuseTexture);
  134. normalTexture = new Texture(setNormalTexture);
  135. heightTexture = new Texture(setHeightTexture);
  136. // Leave rest to default
  137. }
  138. /// <summary>
  139. /// Create material
  140. /// </summary>
  141. public Material(Color setAmbientColor, Color setDiffuseColor,
  142. Color setSpecularColor, string setDiffuseTexture,
  143. string setNormalTexture, string setHeightTexture,
  144. string setDetailTexture)
  145. {
  146. ambientColor = setAmbientColor;
  147. diffuseColor = setDiffuseColor;
  148. specularColor = setSpecularColor;
  149. diffuseTexture = new Texture(setDiffuseTexture);
  150. if (String.IsNullOrEmpty(setNormalTexture) == false)
  151. normalTexture = new Texture(setNormalTexture);
  152. if (String.IsNullOrEmpty(setHeightTexture) == false)
  153. heightTexture = new Texture(setHeightTexture);
  154. if (String.IsNullOrEmpty(setDetailTexture) == false)
  155. detailTexture = new Texture(setDetailTexture);
  156. // Leave rest to default
  157. }
  158. /// <summary>
  159. /// Create material
  160. /// </summary>
  161. /// <param name="effect">Effect</param>
  162. public Material(Effect effect)
  163. {
  164. if (effect == null)
  165. throw new ArgumentNullException("effect");
  166. EffectParameter diffuseTextureParameter =
  167. effect.Parameters["diffuseTexture"];
  168. if (diffuseTextureParameter != null)
  169. diffuseTexture = new Texture(
  170. diffuseTextureParameter.GetValueTexture2D());
  171. EffectParameter normalTextureParameter =
  172. effect.Parameters["normalTexture"];
  173. if (normalTextureParameter != null)
  174. normalTexture = new Texture(
  175. normalTextureParameter.GetValueTexture2D());
  176. EffectParameter diffuseColorParameter =
  177. effect.Parameters["diffuseColor"];
  178. if (diffuseColorParameter != null)
  179. diffuseColor = new Color(diffuseColorParameter.GetValueVector4());
  180. EffectParameter ambientColorParameter =
  181. effect.Parameters["ambientColor"];
  182. if (ambientColorParameter != null)
  183. ambientColor = new Color(ambientColorParameter.GetValueVector4());
  184. EffectParameter specularColorParameter =
  185. effect.Parameters["specularColor"];
  186. if (specularColorParameter != null)
  187. specularColor = new Color(specularColorParameter.GetValueVector4());
  188. EffectParameter specularPowerParameter =
  189. effect.Parameters["specularPower"];
  190. if (specularPowerParameter != null)
  191. specularPower = specularPowerParameter.GetValueSingle();
  192. }
  193. /// <summary>
  194. /// Dispose
  195. /// </summary>
  196. public void Dispose()
  197. {
  198. Dispose(true);
  199. GC.SuppressFinalize(this);
  200. }
  201. /// <summary>
  202. /// Dispose
  203. /// </summary>
  204. /// <param name="disposing">Disposing</param>
  205. protected virtual void Dispose(bool disposing)
  206. {
  207. if (disposing)
  208. {
  209. if (diffuseTexture != null)
  210. diffuseTexture.Dispose();
  211. if (normalTexture != null)
  212. normalTexture.Dispose();
  213. if (heightTexture != null)
  214. heightTexture.Dispose();
  215. if (detailTexture != null)
  216. detailTexture.Dispose();
  217. }
  218. }
  219. }
  220. }