Ejemplo n.º 1
0
 internal Uniform(UniformBase uniformBase)
 {
     this.uniformBase = uniformBase;
 }
Ejemplo n.º 2
0
        public override T Import <T>(System.IO.Stream source, string source_name, ResourceManager man)
        {
            XmlDocument doc = new XmlDocument();

            doc.Load(source);
            if (doc.ChildNodes.Count == 2)
            {
                var vsn = doc.SelectSingleNode(".//VertexShader");
                var fsn = doc.SelectSingleNode(".//FragmentShader");
                var gsn = doc.SelectSingleNode(".//GeometryShader");

                T ret = Activator.CreateInstance <T>();

                if (vsn == null)
                {
                    return(default(T));
                }
                Program prog = new Program();
                var     vs   = LoadShader <VertexShader>(vsn, man, prog);

                if (gsn != null)
                {
                    LoadShader <GeometryShader>(gsn, man, prog);
                }

                if (fsn != null)
                {
                    LoadShader <FragmentShader>(fsn, man, prog);
                }
                (ret as Material).Shader = prog;
                if (!prog.Compile())
                {
                    StringBuilder err = new StringBuilder();
                    err.AppendLine("Compile and link failed for " + source_name);
                    foreach (var sh in prog.shaders)
                    {
                        err.AppendLine(sh.GetType().Name + " " + sh.SourceName + ":");
                        err.AppendLine(sh.GetCompileLog());
                    }
                    err.AppendLine("Linker:\n");
                    err.AppendLine(prog.GetLinkLog());
                    Debugging.Debug.WriteLine(err.ToString());
                }
                string log;
                if (!prog.Validate(out log))
                {
                    Debugging.Debug.WriteLine(log);
                }

                var uniforms = doc.SelectSingleNode(".//Uniforms");
                if (uniforms != null && uniforms.HasChildNodes)
                {
                    int tex_index = 0;
                    foreach (var n in uniforms.ChildNodes)
                    {
                        var node = n as XmlNode;
                        if (node.Name.ToLower() == "uniform")
                        {
                            string      t    = node.Attributes["type"].Value;
                            string      val  = node.Attributes["value"].Value;
                            string      name = node.Attributes["name"].Value;
                            UniformBase uni  = null;
                            switch (t)
                            {
                            case "float":
                                if ((uni = prog.GetUniformType <ScalarFloatUniform, float>(name)) != null && !string.IsNullOrEmpty(val))
                                {
                                    (uni as ScalarFloatUniform).val = float.Parse(val, System.Globalization.NumberFormatInfo.InvariantInfo);
                                }
                                break;

                            case "float2":
                                if ((uni = prog.GetUniformType <Vector2FloatUniform, Vector2>(name)) != null && !string.IsNullOrEmpty(val))
                                {
                                    (uni as Vector2FloatUniform).val = Vector2.Parse(val, System.Globalization.NumberFormatInfo.InvariantInfo);
                                }
                                break;

                            case "float3":
                                if ((uni = prog.GetUniformType <Vector3FloatUniform, Vector3>(name)) != null && !string.IsNullOrEmpty(val))
                                {
                                    (uni as Vector3FloatUniform).val = Vector3.Parse(val, System.Globalization.NumberFormatInfo.InvariantInfo);
                                }
                                break;

                            case "float4":
                                if ((uni = prog.GetUniformType <Vector4FloatUniform, Vector4>(name)) != null && !string.IsNullOrEmpty(val))
                                {
                                    (uni as Vector4FloatUniform).val = Vector4.Parse(val, System.Globalization.NumberFormatInfo.InvariantInfo);
                                }
                                break;

                            case "int":
                                if ((uni = prog.GetUniformType <ScalarIntUniform, int>(name)) != null && !string.IsNullOrEmpty(val))
                                {
                                    (uni as ScalarIntUniform).val = int.Parse(val);
                                }
                                break;

                            case "int2":
                                if ((uni = prog.GetUniformType <Vector2IntUniform, Vector2Int>(name)) != null && !string.IsNullOrEmpty(val))
                                {
                                    (uni as Vector2FloatUniform).val = Vector2Int.Parse(val);
                                }
                                break;

                            case "int3":
                                if ((uni = prog.GetUniformType <Vector3IntUniform, Vector3Int>(name)) != null && !string.IsNullOrEmpty(val))
                                {
                                    (uni as Vector3FloatUniform).val = Vector3Int.Parse(val);
                                }
                                break;

                            case "int4":
                                if ((uni = prog.GetUniformType <Vector4IntUniform, Vector4Int>(name)) != null && !string.IsNullOrEmpty(val))
                                {
                                    (uni as Vector4FloatUniform).val = Vector4Int.Parse(val);
                                }
                                break;

                            case "texture2d":
                                if ((uni = prog.GetUniformType <TextureUniform, Texture>(name)) != null && !string.IsNullOrEmpty(val) && man != null)
                                {
                                    man.Load(val, out (uni as TextureUniform).val);
                                    if (uni != null)
                                    {
                                        (uni as TextureUniform).TextureIndex = tex_index++;
                                    }
                                }
                                break;
                            }

                            if (uni != null)
                            {
                                (ret as Material).uniforms.Add(uni);
                                uni.SetValue();
                            }
                        }
                    }
                }
                (ret as Material).SetupMaterial();
                return(ret as T);
            }
            else
            {
                return(default(T));
            }
        }