Пример #1
0
        public MaterialContent CreateMaterial(ContentProcessorContext context, Dictionary <string, object> parameters)
        {
            MaterialContent content = new MaterialContent()
            {
                Name = _name
            };

            int i = 0;

            foreach (PassTemplate template in _passes)
            {
                MaterialContent.Pass pass = new MaterialContent.Pass(string.IsNullOrEmpty(template.Name) ? string.Format("{0}_Pass_{1}", _name, i) : template.Name);
                pass.VertexShaderSources.AddRange(template.VertexShaders.Select(s => new ExternalReferenceContent <ShaderSourceContent>(s)));
                pass.FragmentShaderSources.AddRange(template.FragmentShaders.Select(s => new ExternalReferenceContent <ShaderSourceContent>(s)));

                foreach (UniformDefinition def in template.Variables)
                {
                    UniformValueContent value = null;
                    object o;
                    if (!parameters.TryGetValue(def.Name, out o))
                    {
                        foreach (string name in def.Aliases)
                        {
                            if (parameters.TryGetValue(name, out o))
                            {
                                break;
                            }
                        }
                    }
                    if (o != null)
                    {
                        value = GetAsUniformValueContent(def.Type, def.Name, o, context);
                    }

                    if (value == null) // no match, or not able to convert to uniform type
                    {
                        value = def.Value;
                    }

                    pass.Parameters[def.Name] = value;
                }

                foreach (UniformDefinition def in template.Constants)
                {
                    pass.Parameters[def.Name] = def.Value;
                }

                pass.ComparisonParameters = template.ComparisonParameters;

                i++;
                content.Passes.Add(pass);
            }

            return(content);
        }
Пример #2
0
        public override MaterialContent Process(object input, ContentProcessorContext context)
        {
            Dictionary <string, object> materialInput = (Dictionary <string, object>)input;
            MaterialContent             material      = new MaterialContent();

            if (materialInput.ContainsKey("name"))
            {
                material.Name = (string)materialInput["name"];
            }
            else
            {
                material.Name = string.Format("Material_{0}", _materialID++);
            }

            if (!materialInput.ContainsKey("passes"))
            {
                throw new ContentException("Material json definition is missing the required key passes.");
            }

            Dictionary <string, object> globalParameters = new Dictionary <string, object>();

            if (materialInput.ContainsKey("parameteres"))
            {
                globalParameters = (Dictionary <string, object>)materialInput["parameters"];
            }

            Dictionary <string, object> globalState = new Dictionary <string, object>();

            if (materialInput.ContainsKey("state"))
            {
                globalState = (Dictionary <string, object>)materialInput["state"];
            }

            List <ShaderSourceContent> shaderSources = new List <ShaderSourceContent>();

            int passCount = 0;

            foreach (Dictionary <string, object> item in ((List <object>)materialInput["passes"]).Cast <Dictionary <string, object> >())
            {
                MaterialContent.Pass pass = new MaterialContent.Pass();
                if (item.ContainsKey("name"))
                {
                    pass.Name = (string)item["name"];
                }

                if (!item.ContainsKey("vs"))
                {
                    throw new ContentException(string.Format("Pass {0} is missing the required key vs.", passCount));
                }
                if (!item.ContainsKey("fs"))
                {
                    throw new ContentException(string.Format("Pass {0} is missing the required key fs.", passCount));
                }

                if (item["vs"] is string)
                {
                    object content;
                    pass.VertexShaderSources.Add(
                        context.ContentManager.BuildContent <ShaderSourceContent>((string)item["vs"], out content,
                                                                                  importerData: new Dictionary <string, object>()
                    {
                        { "ShaderType", "VertexShader" }
                    }));
                    shaderSources.Add((ShaderSourceContent)content);
                }
                else if (item["vs"] is List <string> )
                {
                    pass.VertexShaderSources.AddRange(((List <string>)item["vs"]).Select(
                                                          f =>
                    {
                        object content;
                        ExternalReferenceContent <ShaderSourceContent> result = context.ContentManager.BuildContent <
                            ShaderSourceContent>(f, out content, importerData: new Dictionary <string, object>()
                        {
                            { "ShaderType", "VertexShader" }
                        });
                        shaderSources.Add((ShaderSourceContent)content);
                        return(result);
                    }));
                }
                else
                {
                    throw new ContentException(string.Format("In Pass {0} vs must be either a string or a list of strings.", passCount));
                }

                if (item["fs"] is string)
                {
                    object content;
                    pass.VertexShaderSources.Add(
                        context.ContentManager.BuildContent <ShaderSourceContent>((string)item["fs"], out content,
                                                                                  importerData: new Dictionary <string, object>()
                    {
                        { "ShaderType", "FragmentShader" }
                    }));
                    shaderSources.Add((ShaderSourceContent)content);
                }
                else if (item["fs"] is List <string> )
                {
                    pass.VertexShaderSources.AddRange(((List <string>)item["fs"]).Select(
                                                          f =>
                    {
                        object content;
                        ExternalReferenceContent <ShaderSourceContent> result = context.ContentManager.BuildContent <ShaderSourceContent>(f, out content, importerData: new Dictionary <string, object>()
                        {
                            { "ShaderType", "FragmentShader" }
                        });
                        shaderSources.Add((ShaderSourceContent)content);
                        return(result);
                    }
                                                          ));
                }
                else
                {
                    throw new ContentException(string.Format("In Pass {0} fs must be either a string or a list of strings.", passCount));
                }

                Dictionary <string, object> parameters = new Dictionary <string, object>();
                if (item.ContainsKey("parameters"))
                {
                    parameters = (Dictionary <string, object>)item["parameters"];
                }

                (new[] { globalParameters, parameters }).SelectMany(x => x).ToList().ForEach(x => pass.Parameters[x.Key] = ParseMaterialParameter(x.Value, context.ContentManager));

                Dictionary <string, object> stateData = new Dictionary <string, object>(globalState);
                if (item.ContainsKey("state"))
                {
                    ((Dictionary <string, object>)item["state"]).ToList().ForEach(x => stateData[x.Key] = x.Value);
                }


                pass.State = CreateState(stateData);

                material.Passes.Add(pass);

                passCount++;
            }

            return(material);
        }