Exemplo n.º 1
0
        private ConstParameterBinding <T> GetParameterBinding <T>(bool useDetailPass, string parameterName)
        {
            string        renderPass = useDetailPass ? "Detail" : "Base";
            EffectBinding effectBinding;

            if (!Material.TryGet(renderPass, out effectBinding))
            {
                throw new GraphicsException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Cannot access effect parameter '{0}' from the layer material because the material " +
                              "material does not have a render pass '{1}'.", parameterName, renderPass));
            }

            var parameterBindingUntyped = effectBinding.ParameterBindings[parameterName];

            if (parameterBindingUntyped == null)
            {
                throw new GraphicsException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Cannot access effect parameter '{0}' from the layer material because the effect " +
                              "does not have this parameter.", parameterName));
            }

            var parameterBinding = parameterBindingUntyped as ConstParameterBinding <T>;

            if (parameterBinding == null)
            {
                // Special handling of Texture2D which is often stored as Texture:
                // Replace ConstParameterBinding<Texture> with ConstParameterBinding<Texture2D>.
                if (typeof(T) == typeof(Texture2D))
                {
                    var parameterBinding2 = effectBinding.ParameterBindings[parameterName] as ConstParameterBinding <Texture>;
                    if (parameterBinding2 != null)
                    {
                        var value = (T)(object)parameterBinding2.Value;
                        parameterBinding = effectBinding.Set(parameterBinding2.Parameter, value);
                    }
                }
            }

            if (parameterBinding == null)
            {
                throw new GraphicsException(
                          string.Format(
                              CultureInfo.InvariantCulture,
                              "Cannot access effect parameter '{0}' (expected type: '{1}') from the layer material " +
                              "because the parameter binding is not a ConstParameterBinding or the effect parameter is " +
                              "of a different type.", parameterName, typeof(T).Name));
            }

            return(parameterBinding);
        }
Exemplo n.º 2
0
        private ConstParameterBinding <T> TryGetParameterBinding <T>(bool useDetailPass, string parameterName)
        {
            string        renderPass = useDetailPass ? "Detail" : "Base";
            EffectBinding effectBinding;

            if (!Material.TryGet(renderPass, out effectBinding))
            {
                return(null);
            }

            var parameterBindingUntyped = effectBinding.ParameterBindings[parameterName];

            if (parameterBindingUntyped == null)
            {
                return(null);
            }

            var parameterBinding = parameterBindingUntyped as ConstParameterBinding <T>;

            if (parameterBinding == null)
            {
                // Special handling of Texture2D which is often stored as Texture:
                // Replace ConstParameterBinding<Texture> with ConstParameterBinding<Texture2D>.
                if (typeof(T) == typeof(Texture2D))
                {
                    var parameterBinding2 = effectBinding.ParameterBindings[parameterName] as ConstParameterBinding <Texture>;
                    if (parameterBinding2 != null)
                    {
                        var value = (T)(object)parameterBinding2.Value;
                        parameterBinding = effectBinding.Set(parameterBinding2.Parameter, value);
                    }
                }
            }

            return(parameterBinding);
        }