/// <summary>
        /// Create material
        /// </summary>
        /// <param name="effect">Effect</param>
        public Material(Effect effect)
        {
            if (effect == null)
            {
                throw new ArgumentNullException("effect");
            }

            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 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());
            }

            EffectParameter specularColorParameter =
                effect.Parameters["specularColor"];

            if (specularColorParameter != null)
            {
                specularColor = new Color(specularColorParameter.GetValueVector4());
            }

            EffectParameter specularPowerParameter =
                effect.Parameters["specularPower"];

            if (specularPowerParameter != null)
            {
                specularPower = specularPowerParameter.GetValueSingle();
            }
        }
Пример #2
0
        }         // Material(ambientColor, diffuseColor, setDiffuseTexture)

        #endregion

        #region Helpers for creating material from shader parameters

        /*TODO
         * /// <summary>
         * /// Search effect parameter
         * /// </summary>
         * /// <param name="parameters">Parameters</param>
         * /// <param name="paramName">Param name</param>
         * /// <returns>Object</returns>
         * private static object SearchEffectParameter(
         *      EffectDefault[] parameters, string paramName)
         * {
         *      foreach (EffectDefault param in parameters)
         *      {
         *              if (StringHelper.Compare(param.ParameterName, paramName))
         *              {
         *                      return param.Data;
         *              } // if (StringHelper.Compare)
         *      } // foreach (param in parameters)
         *      // Not found
         *      return null;
         * } // SearchEffectParameter(parameters, paramName)
         *
         * /// <summary>
         * /// Search effect float parameter
         * /// </summary>
         * /// <param name="parameters">Parameters</param>
         * /// <param name="paramName">Param name</param>
         * /// <param name="defaultValue">Default value</param>
         * /// <returns>Float</returns>
         * private static float SearchEffectFloatParameter(
         *      EffectDefault[] parameters, string paramName, float defaultValue)
         * {
         *      object ret = SearchEffectParameter(parameters, paramName);
         *      if (ret != null &&
         *              ret.GetType() == typeof(float))
         *              return (float)ret;
         *      // Not found? Then just return default value.
         *      return defaultValue;
         * } // SearchEffectFloatParameter(parameters, paramName, defaultValue)
         *
         * /// <summary>
         * /// Search effect color parameter
         * /// </summary>
         * /// <param name="parameters">Parameters</param>
         * /// <param name="paramName">Param name</param>
         * /// <param name="defaultColor">Default color</param>
         * /// <returns>Color</returns>
         * private static Color SearchEffectColorParameter(
         *      EffectDefault[] parameters, string paramName, Color defaultColor)
         * {
         *      object ret = SearchEffectParameter(parameters, paramName);
         *      if (ret != null &&
         *              ret.GetType() == typeof(float[]))
         *      {
         *              float[] data = (float[])ret;
         *              if (data.Length >= 4)
         *              {
         *                      byte red = (byte)(data[0] * 255.0f);
         *                      byte green = (byte)(data[1] * 255.0f);
         *                      byte blue = (byte)(data[2] * 255.0f);
         *                      byte alpha = (byte)(data[3] * 255.0f);
         *                      return Color.FromArgb(alpha, red, green, blue);
         *              } // if (data.Length)
         *      } // if (ret)
         *      // Not found? Then just return default value.
         *      return defaultColor;
         * } // SearchEffectColorParameter(parameters, paramName, defaultColor)
         *
         * /// <summary>
         * /// Search effect texture parameter
         * /// </summary>
         * /// <param name="parameters">Parameters</param>
         * /// <param name="paramName">Param name</param>
         * /// <param name="defaultTexture">Default texture</param>
         * /// <returns>Texture</returns>
         * private static Texture SearchEffectTextureParameter(
         *      EffectDefault[] parameters, string paramName, Texture defaultTexture)
         * {
         *      object ret = SearchEffectParameter(parameters, paramName);
         *      if (ret != null &&
         *              ret.GetType() == typeof(string))
         *      {
         *              // Use the models directory
         *              return new Texture(
         *                      Directories.TextureModelsSubDirectory + "\\" +
         *                      StringHelper.ExtractFilename((string)ret, true));
         *      } // if (ret)
         *      // Not found? Then just return default value.
         *      return defaultTexture;
         * } // SearchEffectTextureParameter(parameters, paramName, defaultTexture)
         */
        #endregion

        #region Constructor for creating material from EffectInstance from x file

        /*TODO
         * /// <summary>
         * /// Material
         * /// </summary>
         * public Material(EffectInstance modelEffectInstance,
         *      ExtendedMaterial dxMaterial)
         * {
         *      EffectDefault[] parameters = modelEffectInstance.GetDefaults();
         *
         *      // If shader could not be loaded or is missing, we can't set
         *      // any shader parameters, load material normally without shaders.
         *      if (GraphicForm.ParallaxShader.Valid == false)
         *      {
         *              // Load material like a normal extended material.
         *              LoadExtendedMaterial(dxMaterial);
         *
         *              // Leave rest to default, only load diffuseTexture from shader
         *              // if none is set in the extended material.
         *              if (diffuseTexture == null)
         *                      diffuseTexture = SearchEffectTextureParameter(
         *                              parameters, "diffuseTexture", null);
         *
         *              // Get outta here, all the advanced shader stuff is not required.
         *              return;
         *      } // if (GraphicForm.ParallaxShader.Valid)
         *
         *      d3dMaterial.Ambient = SearchEffectColorParameter(
         *              parameters, "ambientColor", DefaultAmbientColor);
         *      d3dMaterial.Diffuse = SearchEffectColorParameter(
         *              parameters, "diffuseColor", DefaultDiffuseColor);
         *      d3dMaterial.Specular = SearchEffectColorParameter(
         *              parameters, "specularColor", DefaultSpecularColor);
         *      d3dMaterial.SpecularSharpness = SearchEffectFloatParameter(
         *              parameters, "shininess", DefaultShininess);
         *
         *      // If diffuse is white, reduce it to nearly white!
         *      if (d3dMaterial.Diffuse == Color.White)
         *              d3dMaterial.Diffuse = Color.FromArgb(255, 230, 230, 230);
         *      // Same for specular color
         *      if (d3dMaterial.Specular == Color.White)
         *              d3dMaterial.Specular = Color.FromArgb(255, 230, 230, 230);
         *
         *      diffuseTexture = SearchEffectTextureParameter(
         *              parameters, "diffuseTexture", null);
         *      normalTexture = SearchEffectTextureParameter(
         *              parameters, "normalTexture", null);
         *      heightTexture = SearchEffectTextureParameter(
         *              parameters, "heightTexture", null);
         *
         *      parallaxAmount = SearchEffectFloatParameter(
         *              parameters, "parallaxAmount", DefaultParallaxAmount);
         * } // Material(modelEffectInstance, dxMaterial)
         */
        #endregion

        #region Create material from effect settings
        /// <summary>
        /// Create material
        /// </summary>
        /// <param name="effect">Effect</param>
        public Material(IXNAGame _game, Effect effect)
        {
            EffectParameter diffuseTextureParameter =
                effect.Parameters["diffuseTexture"];

            if (diffuseTextureParameter != null)
            {
                diffuseTexture = new TextureBookengine(_game,
                                                       diffuseTextureParameter.GetValueTexture2D());
            }

            EffectParameter normalTextureParameter =
                effect.Parameters["normalTexture"];

            if (normalTextureParameter != null)
            {
                normalTexture = new TextureBookengine(_game,
                                                      normalTextureParameter.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());
            }

            EffectParameter specularColorParameter =
                effect.Parameters["specularColor"];

            if (specularColorParameter != null)
            {
                specularColor = new Color(specularColorParameter.GetValueVector4());
            }

            EffectParameter specularPowerParameter =
                effect.Parameters["specularPower"];

            if (specularPowerParameter != null)
            {
                specularPower = specularPowerParameter.GetValueSingle();
            }
        }         // Material(effect)
Пример #3
0
        public static object GetValue(this EffectParameter parameter)
        {
            if (parameter.ParameterClass == EffectParameterClass.Object)
            {
                if (parameter.ParameterType == EffectParameterType.Texture2D)
                {
                    return(parameter.GetValueTexture2D());
                }
                if (parameter.ParameterType == EffectParameterType.Texture3D)
                {
                    return(parameter.GetValueTexture3D());
                }
                if (parameter.ParameterType == EffectParameterType.TextureCube)
                {
                    return(parameter.GetValueTextureCube());
                }
            }

            if (parameter.ParameterType == EffectParameterType.Int32)
            {
                return(parameter.GetValueInt32());
            }
            if (parameter.ParameterClass == EffectParameterClass.Matrix)
            {
                return(parameter.GetValueMatrix());
            }

            if (parameter.ParameterClass == EffectParameterClass.Vector)
            {
                if (parameter.ColumnCount == 1)
                {
                    return(parameter.GetValueSingle());
                }
                if (parameter.ColumnCount == 2)
                {
                    return(parameter.GetValueVector2());
                }
                if (parameter.ColumnCount == 3)
                {
                    return(parameter.GetValueVector3());
                }
                if (parameter.ColumnCount == 4)
                {
                    return(parameter.GetValueVector4());
                }
            }

            throw new NotSupportedException();
        }
Пример #4
0
 public Texture2D GetValueTexture2D()
 {
     return(_targetParameter.GetValueTexture2D());
 }
        private void EffectParametersSerialization(System.Xml.XmlWriter writer, EffectParameter param)
        {
            switch (param.ParameterType)
            {
            case EffectParameterType.Bool:
                writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueBoolean()));
                break;

            case EffectParameterType.Int32:
                writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueInt32()));
                break;

            case EffectParameterType.Texture2D:
                writer.WriteElementString("Value", ResourceManager.Instance.Textures.FirstOrDefault(x => x.Value == param.GetValueTexture2D()).Key);
                break;

            case EffectParameterType.TextureCube:
                writer.WriteElementString("Value", ResourceManager.Instance.TexturesCube.FirstOrDefault(x => x.Value == param.GetValueTextureCube()).Key);
                break;

            case EffectParameterType.String:
                writer.WriteElementString("Value", param.GetValueString());
                break;

            case EffectParameterType.Single:
                CheckParameterClass(writer, param);
                break;

            default:
                writer.WriteElementString("Value", param.ParameterType.ToString());
                break;
            }
        }
        public override object GetValue(object component)
        {
            switch (mParameter.ParameterType)
            {
            case EffectParameterType.Bool:
                return(mParameter.GetValueBoolean());

                break;

            case EffectParameterType.Int32:
                return(mParameter.GetValueInt32());

                break;

            case EffectParameterType.Single:
                switch (mParameter.ParameterClass)
                {
                case EffectParameterClass.MatrixColumns:
                    return(mParameter.GetValueMatrix());

                    break;

                case EffectParameterClass.MatrixRows:
                    return(mParameter.GetValueMatrix());

                    break;

                case EffectParameterClass.Scalar:
                    return(mParameter.GetValueSingle());

                    break;

                case EffectParameterClass.Vector:
                    switch (mParameter.ColumnCount)
                    {
                    case 1:
                        return(mParameter.GetValueSingle());

                        break;

                    case 2:
                        return(mParameter.GetValueVector2());

                        break;

                    case 3:
                        return(mParameter.GetValueVector3());

                        break;

                    case 4:
                        return(mParameter.GetValueVector4());

                        break;

                    default:
                        return(null);

                        break;
                    }
                    break;

                default:
                    return(mParameter.GetValueSingle());

                    break;
                }
                break;

            case EffectParameterType.String:
                return(mParameter.GetValueString());

                break;

            case EffectParameterType.Texture:
            case EffectParameterType.Texture2D:
                Texture2D texture = mParameter.GetValueTexture2D();
                return((texture != null && texture.Name != string.Empty) ? texture.Name.Replace('/', Path.DirectorySeparatorChar) :
                       ((mTagString != null && mTagString != string.Empty) ? mTagString.Replace('/', Path.DirectorySeparatorChar) : String.Empty));

                break;

            default:
                return(null);

                break;
            }

            return(null);
        }
        }         // Material(ambientColor, diffuseColor, setDiffuseTexture)

        #endregion

        #region Create material from effect settings
        /// <summary>
        /// Create material
        /// </summary>
        /// <param name="effect">Effect</param>
        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)