Exemple #1
0
            //====================================================================
            // PCF specific

            void InitializePcfKernel(int kernelSize)
            {
                var texelSize = shadowMapTexelSize.GetValueSingle();

                int start;

                if (kernelSize % 2 == 0)
                {
                    start = -(kernelSize / 2) + 1;
                }
                else
                {
                    start = -(kernelSize - 1) / 2;
                }
                var end = start + kernelSize;

                var tapCount = kernelSize * kernelSize;
                var offsets  = new Vector2[tapCount];

                int i = 0;

                for (int y = start; y < end; y++)
                {
                    for (int x = start; x < end; x++)
                    {
                        offsets[i++] = new Vector2(x, y) * texelSize;
                    }
                }

                pcfOffsetsParameter.SetValue(offsets);
            }
        /// <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();
            }
        }
Exemple #3
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)
Exemple #4
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();
        }
Exemple #5
0
        public override bool TriggerEvent(EventType Event, string[] args)
        {
            if (MyEffect.get() == null)
            {
                return(false);
            }
            else
            {
                if (SpecularParameter == null)
                {
                    SpecularParameter = MyEffect.findEffectParameter("Specular");
                    OldSpecular       = SpecularParameter.GetValueSingle();
                }
                if (ColorParameter == null)
                {
                    ColorParameter = MyEffect.findEffectParameter("Color");
                    OldColor       = ColorParameter.GetValueVector4();
                }
            }

            switch (Event)
            {
            case EventType.Kill:
                Dead.set(true);
                if (args.Count() > 0)
                {
                    float f = Logic.ParseF(args[0]);
                    if (f != 0)
                    {
                        ChangeSpeed.set(f);
                    }
                }
                return(true);

            case EventType.Revive:
                Dead.set(false);
                if (args.Count() > 0)
                {
                    float f = Logic.ParseF(args[0]);
                    if (f != 0)
                    {
                        ChangeSpeed.set(f);
                    }
                }
                return(true);
            }

            return(base.TriggerEvent(Event, args));
        }
Exemple #6
0
 public float GetValueSingle()
 {
     return(_targetParameter.GetValueSingle());
 }
    private static void GetParameterValues(EffectParameter parameter, Dictionary<EffectParameter, object> values)
    {
      if (parameter.ParameterClass == EffectParameterClass.Struct)
      {
        if (parameter.Elements.Count > 0)
        {
          // ----- Effect parameter is an array of structs.
          foreach (EffectParameter element in parameter.Elements)
            GetParameterValues(element, values);
        }
        else
        {
          // ----- Effect parameter is a struct.
          foreach (EffectParameter member in parameter.StructureMembers)
            GetParameterValues(member, values);
        }

        return;
      }

      object value = null;
      if (parameter.Elements.Count == 0)
      {
        // ----- Parameter is not an array.
        if (parameter.ParameterClass == EffectParameterClass.Scalar)
        {
          // Scalar values.
          if (parameter.ParameterType == EffectParameterType.Bool)
            value = parameter.GetValueBoolean();
          else if (parameter.ParameterType == EffectParameterType.Int32)
            value = parameter.GetValueInt32();
          else if (parameter.ParameterType == EffectParameterType.Single)
            value = parameter.GetValueSingle();
        }
        else if (parameter.ParameterClass == EffectParameterClass.Vector
                 && parameter.ParameterType == EffectParameterType.Single)
        {
          // Vector values.
          if (parameter.ColumnCount == 2 || parameter.RowCount == 2)
            value = parameter.GetValueVector2();
          else if (parameter.ColumnCount == 3 || parameter.RowCount == 3)
            value = parameter.GetValueVector3();
          else if (parameter.ColumnCount == 4 || parameter.RowCount == 4)
            value = parameter.GetValueVector4();
        }
        else if (parameter.ParameterClass == EffectParameterClass.Matrix
                 && parameter.ParameterType == EffectParameterType.Single)
        {
          // Matrix value.

          value = parameter.GetValueMatrix();
#else
          // MonoGame throws exception if following condition is not met.
          if (parameter.RowCount == 4 || parameter.ColumnCount == 4)       
            value = parameter.GetValueMatrix();

        }
        else if (parameter.ParameterClass == EffectParameterClass.Object)
        {
          // Object values.
          if (parameter.ParameterType == EffectParameterType.String)
          {
            value = parameter.GetValueString();
          }
          else
          {
            // Effect parameter is texture. (Value is always null.)
          }
        }
      }
      else
      {
        // ----- Parameter is array.
        int length = parameter.Elements.Count;
        Debug.Assert(length > 0, "Effect parameter should be an array.");

        if (parameter.ParameterClass == EffectParameterClass.Scalar)
        {

          // Scalar value bindings.
          if (parameter.ParameterType == EffectParameterType.Bool)
            value = parameter.GetValueBooleanArray(length);
          else if (parameter.ParameterType == EffectParameterType.Int32)
            value = parameter.GetValueInt32Array(length);
          else if (parameter.ParameterType == EffectParameterType.Single)
            value = parameter.GetValueSingleArray(length);

        }
        else if (parameter.ParameterClass == EffectParameterClass.Vector && parameter.ParameterType == EffectParameterType.Single)
        {

          if (parameter.ColumnCount == 2 || parameter.RowCount == 2)
            value = parameter.GetValueVector2Array(length);
          else if (parameter.ColumnCount == 3 || parameter.RowCount == 3)
            value = parameter.GetValueVector3Array(length);
          else if (parameter.ColumnCount == 4 || parameter.RowCount == 4)
            value = parameter.GetValueVector4Array(length);

        }
        else if (parameter.ParameterClass == EffectParameterClass.Matrix && parameter.ParameterType == EffectParameterType.Single)
        {

          value = parameter.GetValueMatrixArray(length);

        }
        else if (parameter.ParameterClass == EffectParameterClass.Object)
        {
          // Note: Arrays of strings or textures are not supported in XNA.
        }
      }

      if (value != null)
        values.Add(parameter, value);
    }
        private void CheckParameterClass(System.Xml.XmlWriter writer, EffectParameter param)
        {
            switch (param.ParameterClass)
            {
            case EffectParameterClass.Vector:
                if (param.RowCount == 1)
                {
                    if (param.Elements.Count > 0)
                    {
                        writer.WriteStartElement("VectorValues");
                        for (int i = 0; i < param.Elements.Count; ++i)
                        {
                            writer.WriteElementString("Value", XmlConvert.ToString(param.Elements[i].GetValueVector3().X));
                        }
                        writer.WriteEndElement();
                    }
                    else
                    {
                        writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector3().X));
                    }
                }
                if (param.RowCount == 2)
                {
                    writer.WriteStartElement("Vector2Values");
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector2().X));
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector2().Y));
                    writer.WriteEndElement();
                }
                if (param.RowCount == 3)
                {
                    writer.WriteStartElement("Vector3Values");
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector3().X));
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector3().Y));
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector3().Z));
                    writer.WriteEndElement();
                }
                if (param.RowCount == 4)
                {
                    writer.WriteStartElement("Vector4Values");
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector4().X));
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector4().Y));
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector4().Z));
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueVector4().W));
                    writer.WriteEndElement();
                }
                break;

            case EffectParameterClass.Scalar:
                if (param.Elements.Count > 0)
                {
                    writer.WriteStartElement("ArrayValues");
                    for (int i = 0; i < param.Elements.Count; ++i)
                    {
                        writer.WriteElementString("Value", XmlConvert.ToString(param.Elements[i].GetValueSingle()));
                    }
                    writer.WriteEndElement();
                }
                else
                {
                    writer.WriteElementString("Value", XmlConvert.ToString(param.GetValueSingle()));
                }
                break;

            case EffectParameterClass.Matrix:
                writer.WriteElementString("Value", "Matrix");
                break;

            default:
                writer.WriteElementString("Value", param.ParameterClass.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:
                return((mTagString != null) ? mTagString : String.Empty);

                break;

            default:
                return(null);

                break;
            }

            return(null);
        }
        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);
        }
Exemple #11
0
 public FloatParameter(EffectParameter Param)
     : base(Param)
 {
     this.Value = Param.GetValueSingle();
 }
        }         // 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)
Exemple #13
0
 public void ChangeRadius(float amount)
 {
     rangeParameter.SetValue(amount + rangeParameter.GetValueSingle());
 }