Exemplo n.º 1
0
 public override void Write(ContentWriter writer, ExternalReferenceContent <T> value)
 {
     if (!value.HasBeenBuilt)
     {
         value.Build();
     }
     writer.Write(value.FileName);
 }
Exemplo n.º 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);
        }