// Project: XnaShooter, File: Material.cs // Namespace: XnaShooter.Graphics, Class: Material // Path: C:\code\XnaShooter\Graphics, Author: Abi // Code lines: 360, Size of file: 11,42 KB // Creation date: 07.09.2006 05:56 // Last modified: 26.10.2006 22:44 // Generated with Commenter by abi.exDream.com #region Using directives #if DEBUG //using NUnit.Framework; #endif using System; using System.Collections.Generic; using System.Text; using XnaShooter.Helpers; using XnaShooter.Shaders; using Microsoft.Xna.Framework.Graphics; using XnaShooter.Game; #endregion namespace XnaShooter.Graphics { /// /// Material class for Xna materials used for Models. Consists of /// normal Xna material settings (ambient, diffuse, specular), /// the diffuse texture and optionally of normal map, height map and shader /// parameters. /// public class Material : IGraphicContent { #region Constants /// /// Default color values are: /// 0.15f for ambient and 1.0f for diffuse and 1.0f specular. /// public static readonly Color DefaultAmbientColor = //new Color(80, 80, 80), //new Color(64, 64, 64), new Color(40, 40, 40), DefaultDiffuseColor = new Color(230, 230, 230), DefaultSpecularColor = new Color(255, 255, 255); /// /// Default specular power (24) /// const float DefaultSpecularPower = 24.0f; /// /// Parallax amount for parallax and offset shaders. /// public const float DefaultParallaxAmount = 0.024f;//0.04f;//0.07f; #endregion #region Variables /// /// Colors /// public Color diffuseColor = DefaultDiffuseColor, ambientColor = DefaultAmbientColor, specularColor = DefaultSpecularColor; /// /// Specular power /// public float specularPower = DefaultSpecularPower; /// /// Diffuse texture for the material. Can be null for unused. /// public Texture diffuseTexture = null; /// /// Normal texture in case we use normal mapping. Can be null for unused. /// public Texture normalTexture = null; /// /// Height texture in case we use parallax mapping. Can be null for unused. /// public Texture heightTexture = null; /// /// Detail texture, used for landscape rendering. Can be null for unused. /// public Texture detailTexture = null; /// /// Parallax amount for parallax and offset shaders. /// public float parallaxAmount = DefaultParallaxAmount; /// /// Filenames for the textures used here for reloading! /// public string diffuseTextureFilename = "", normalTextureFilename = "", heightTextureFilename = "", detailTextureFilename = ""; #endregion #region Properties /// /// Checks if the diffuse texture has alpha /// public bool HasAlpha { get { if (diffuseTexture != null) return diffuseTexture.HasAlphaPixels; else return false; } // get } // HasAlpha #endregion #region Constructors #region Default Constructors /// /// Create material, just using default values. /// public Material() { BaseGame.RegisterGraphicContentObject(this); } // Material() /// /// Create material, just using default color values. /// public Material(string setDiffuseTexture) : this() { diffuseTexture = new Texture(setDiffuseTexture); } // Material(setDiffuseTexture) /// /// Create material /// public Material(Color setAmbientColor, Color setDiffuseColor, string setDiffuseTexture) : this() { ambientColor = setAmbientColor; diffuseColor = setDiffuseColor; diffuseTexture = new Texture(setDiffuseTexture); // Leave rest to default } // Material(ambientColor, diffuseColor, setDiffuseTexture) /// /// Create material /// public Material(Color setAmbientColor, Color setDiffuseColor, Texture setDiffuseTexture) : this() { ambientColor = setAmbientColor; diffuseColor = setDiffuseColor; diffuseTexture = setDiffuseTexture; // Leave rest to default } // Material(ambientColor, diffuseColor, setDiffuseTexture) /// /// Create material /// public Material(string setDiffuseTexture, string setNormalTexture) : this() { diffuseTexture = new Texture(setDiffuseTexture); normalTexture = new Texture(setNormalTexture); // Leave rest to default } // Material(ambientColor, diffuseColor, setDiffuseTexture) /// /// Create material /// public Material(string setDiffuseTexture, string setNormalTexture, string setHeightTexture) : this() { diffuseTexture = new Texture(setDiffuseTexture); normalTexture = new Texture(setNormalTexture); heightTexture = new Texture(setHeightTexture); // Leave rest to default } // Material(ambientColor, diffuseColor, setDiffuseTexture) /// /// Create material /// public Material(Color setAmbientColor, Color setDiffuseColor, Color setSpecularColor, string setDiffuseTexture, string setNormalTexture, string setHeightTexture, string setDetailTexture) : this() { ambientColor = setAmbientColor; diffuseColor = setDiffuseColor; specularColor = setSpecularColor; diffuseTexture = new Texture(setDiffuseTexture); if (String.IsNullOrEmpty(setNormalTexture) == false) normalTexture = new Texture(setNormalTexture); if (String.IsNullOrEmpty(setHeightTexture) == false) heightTexture = new Texture(setHeightTexture); if (String.IsNullOrEmpty(setDetailTexture) == false) detailTexture = new Texture(setDetailTexture); // Leave rest to default } // Material(ambientColor, diffuseColor, setDiffuseTexture) #endregion #region Create material from effect settings /// /// Create material /// /// Effect public Material(Effect effect) : this() { EffectParameter diffuseTextureParameter = effect.Parameters["diffuseTexture"]; if (diffuseTextureParameter != null) diffuseTexture = new Texture( diffuseTextureParameter.GetValueTexture2D()); EffectParameter normalTextureParameter = effect.Parameters["normalTexture"]; if (normalTextureParameter != null) normalTexture = new Texture( normalTextureParameter.GetValueTexture2D()); EffectParameter heightTextureParameter = effect.Parameters["heightTexture"]; if (heightTextureParameter != null) heightTexture = new Texture( heightTextureParameter.GetValueTexture2D()); EffectParameter diffuseColorParameter = effect.Parameters["diffuseColor"]; if (diffuseColorParameter != null) diffuseColor = new Color(diffuseColorParameter.GetValueVector4()); EffectParameter ambientColorParameter = effect.Parameters["ambientColor"]; if (ambientColorParameter != null) ambientColor = new Color(ambientColorParameter.GetValueVector4()); // Make sure ambientColor is not darker than DefaultAmbientColor if (ambientColor.R < DefaultAmbientColor.R) ambientColor = DefaultAmbientColor; EffectParameter specularColorParameter = effect.Parameters["specularColor"]; if (specularColorParameter != null) specularColor = new Color(specularColorParameter.GetValueVector4()); EffectParameter specularPowerParameter = effect.Parameters["specularPower"]; if (specularPowerParameter != null) specularPower = specularPowerParameter.GetValueSingle(); EffectParameter parallaxAmountParameter = effect.Parameters["parallaxAmount"]; if (parallaxAmountParameter != null) parallaxAmount = parallaxAmountParameter.GetValueSingle(); parallaxAmount = 0.0f; } // Material(effect) #endregion #endregion #region Dispose /// /// Dispose /// public void Dispose() { if (diffuseTexture != null) { diffuseTextureFilename = diffuseTexture.Filename; diffuseTexture.Dispose(); } // if diffuseTexture = null; if (normalTexture != null) { normalTextureFilename = normalTexture.Filename; normalTexture.Dispose(); } // if normalTexture = null; if (heightTexture != null) { heightTextureFilename = heightTexture.Filename; heightTexture.Dispose(); } // if heightTexture = null; if (detailTexture != null) { detailTextureFilename = detailTexture.Filename; detailTexture.Dispose(); } // if detailTexture = null; } // Dispose() #endregion #region Load /// /// Load /// public void Load() { if (diffuseTexture == null && String.IsNullOrEmpty(diffuseTextureFilename) == false) { diffuseTexture = new Texture(diffuseTextureFilename); } // if if (normalTexture == null && String.IsNullOrEmpty(normalTextureFilename) == false) { normalTexture = new Texture(normalTextureFilename); } // if if (heightTexture == null && String.IsNullOrEmpty(heightTextureFilename) == false) { heightTexture = new Texture(heightTextureFilename); } // if if (detailTexture == null && String.IsNullOrEmpty(detailTextureFilename) == false) { detailTexture = new Texture(detailTextureFilename); } // if } // Load() #endregion } // class Material } // namespace XnaShooter.Graphics