public void Apply(IRenderer renderer)
        {
            if (_effectParameter != null)
            {
                switch (_engineValue)
                {
                case EngineValue.WorldMatrix:
                case EngineValue.WorldMatrixInverse:
                case EngineValue.WorldInverseTranspose:
                case EngineValue.WorldViewMatrix:
                case EngineValue.WorldViewProjection:
                case EngineValue.ViewMatrix:
                case EngineValue.ViewMatrixInverse:
                case EngineValue.ViewProjectionMatrix:
                case EngineValue.ProjectionMatrix:
                case EngineValue.ProjectionMatrixInverse:
                    Matrix mat;
                    Engine.ValueMap.GetValue(_engineValue, out mat);
                    _effectParameter.SetValue(mat);
                    break;

                case EngineValue.Viewport:
                    Vector4 vec4;
                    Engine.ValueMap.GetValue(_engineValue, out vec4);
                    _effectParameter.SetValue(vec4);
                    break;

                case EngineValue.CameraDirection:
                case EngineValue.CameraRight:
                case EngineValue.CameraPosition:
                case EngineValue.CameraUp:
                    Vector3 vec3;
                    Engine.ValueMap.GetValue(_engineValue, out vec3);
                    _effectParameter.SetValue(vec3);
                    break;

                case EngineValue.Resolution:
                case EngineValue.FrustumNearFar:
                    Vector2 vec2;
                    Engine.ValueMap.GetValue(_engineValue, out vec2);
                    _effectParameter.SetValue(vec2);
                    break;

                case EngineValue.Time:
                case EngineValue.TimePerFrame:
                case EngineValue.FrameRate:
                case EngineValue.RandomFloat:
                    float fValue;
                    Engine.ValueMap.GetValue(_engineValue, out fValue);
                    _effectParameter.SetValue(fValue);
                    break;

                case EngineValue.RandomInt:
                    int iValue;
                    Engine.ValueMap.GetValue(_engineValue, out iValue);
                    break;
                }
            }
        }
Exemple #2
0
        public void SetParameter(String paramName, bool isSemantic, Color value)
        {
            if (IsValid)
            {
                IEffectParameter param = null;

                if (isSemantic)
                {
                    param = _effect.Parameters.GetParameterBySemantic(paramName);
                }
                else
                {
                    param = _effect.Parameters[paramName];
                }

                if (param == null)
                {
                    return;
                }

                Vector4 v = value.ToVector4();

                param.SetValue(v);
                _cachedParameters[paramName] = new MaterialParameter(param, paramName, isSemantic, v);
            }
            else
            {
                throw new ArgumentNullException("Material does not have a valid effect loaded, cannot set parameter. Call LoadEffect() first.");
            }
        }
Exemple #3
0
        /// <summary>
        /// Execute the logic for the given material and renderable (usually a Mesh).
        /// </summary>
        /// <param name="material">Material that is executing the logic.</param>
        /// <param name="renderer">Renderer used for drawing</param>
        /// <param name="renderable">Renderable that owns the Material</param>
        public override void Execute(Material material, IRenderer renderer, IRenderable renderable)
        {
            if (renderable == null || renderer == null || material == null)
            {
                return;
            }

            if (IsEnabled)
            {
                if (_needsRefresh)
                {
                    CacheParameters(material.Effect);
                }

                //We assume this comes to us already sorted.
                ILightCollection lights = renderable.WorldLights;

                //Return if the collection doesn't need to update us or there are no lights to update with
                if (!lights.RefreshShader || lights.LightCount == 0)
                {
                    //Set to zero
                    if (_cachedLightCount != null)
                    {
                        _cachedLightCount.SetValue(0);
                    }
                    return;
                }

                //Tell the collection not to update us until it changes
                lights.RefreshShader = false;

                //Set the global ambient value
                if (_cachedGlobalAmbient != null)
                {
                    _cachedGlobalAmbient.SetValue(lights.GlobalAmbient.ToVector3());
                }

                //Update the actual light list
                if (_cachedLightList != null)
                {
                    //If we're dealing with a single parameter and not an array...
                    if (_cachedLightList.Elements.Count == 0)
                    {
                        SetLight(lights[0], _cachedLightList);
                        return;
                    }

                    //Ensure the max light counts match, if the element count is less than
                    //the collection's max lights, use that, otherwise use the collection's
                    int numLights = 0;
                    if (lights.LightCount <= _cachedLightList.Elements.Count)
                    {
                        numLights = lights.LightCount;
                    }
                    else if (lights.LightCount > _cachedLightList.Elements.Count)
                    {
                        numLights = _cachedLightList.Elements.Count;
                    }

                    //Set the number of lights
                    if (_cachedLightCount != null)
                    {
                        _cachedLightCount.SetValue(numLights);
                    }

                    //Go through each light, and update it to the shader
                    for (int i = 0; i < numLights; i++)
                    {
                        Light            light  = lights[i];
                        IEffectParameter lParam = _cachedLightList.Elements[i];
                        SetLight(light, lParam);
                    }
                }
            }
        }