Esempio n. 1
0
 public static Material CreateMaterialFromElement(this RElement ele, Dictionary<string, Texture> textures)
 {
     string n = ele.Name;
     bool shad = getShadow(ele);
     Sampler s = null;
     if (ele.Attributes.ContainsKey(P.Sampler))
     {
         s = ele.Attributes[P.Sampler].CreateSamplerFromAttribute(ele.Attributes[P.Samples].ToInt());
     }
     if (n == P.Mate.Matt)
     {
         Matte m = new Matte(ele.Attributes[P.AmbRefCoeff].ToFloat(), ele.Attributes[P.DiffRefCoeff].ToFloat(), ele.Attributes[P.Col].ToVector3());
         m.Shadows = shad;
         if (s != null) m.SetSampler(s);
         return m;
     }
     else if (n == P.Mate.Pho)
     {
         Phong p = new Phong();
         p.SetSpecularColor(ele.Attributes[P.SpCol].ToVector3());
         p.SetExp(ele.Attributes[P.Exp].ToFloat());
         p.SetCD(GetVector(ele.Attributes[P.Col]));
         p.SetAmbientRC(ele.Attributes[P.AmbRefCoeff].ToFloat());
         p.SetDiffuseRC(ele.Attributes[P.DiffRefCoeff].ToFloat());
         p.SetSpecularRC(ele.Attributes[P.SpecCoeff].ToFloat());
         p.Shadows = shad;
         if (s != null) p.SetSampler(s);
         return p;
     }
     else if (n == P.Mate.Refl)
     {
         Reflective r = new Reflective();
         r.SetSpecularColor(ele.Attributes[P.SpCol].ToVector3());
         r.SetSpecularRC(ele.Attributes[P.SpecCoeff].ToFloat());
         r.SetRColor(GetVector(ele.Attributes[P.RefCol]));
         r.SetReflectiveRC(ele.Attributes[P.RefCoeff].ToFloat());
         r.SetExp(ele.Attributes[P.Exp].ToFloat());
         r.SetCD(GetVector(ele.Attributes[P.Col]));
         r.SetAmbientRC(ele.Attributes[P.AmbRefCoeff].ToFloat());
         r.SetDiffuseRC(ele.Attributes[P.DiffRefCoeff].ToFloat());
         r.Shadows = shad;
         if (s != null) r.SetSampler(s);
         return r;
     }
     else if (n == P.Mate.Trans)
     {
         Transparent t = new Transparent();
         t.SetSpecularColor(ele.Attributes[P.SpCol].ToVector3());
         t.SetSpecularRC(ele.Attributes[P.SpecCoeff].ToFloat());
         t.SetReflectiveRC(ele.Attributes[P.RefCoeff].ToFloat());
         t.SetExp(ele.Attributes[P.Exp].ToFloat());
         t.SetCD(GetVector(ele.Attributes[P.Col]));
         t.SetAmbientRC(ele.Attributes[P.AmbRefCoeff].ToFloat());
         t.SetDiffuseRC(ele.Attributes[P.DiffRefCoeff].ToFloat());
         t.SetIndexOfRefraction(ele.Attributes[P.IOR].ToFloat());
         t.SetTransmissionCoefficient(ele.Attributes[P.TransCoeff].ToFloat());
         t.Shadows = shad;
         if (s != null) t.SetSampler(s);
         return t;
     }
     else if (n == P.Mate.GlosRef)
     {
         GlossyReflective p = new GlossyReflective();
         p.SetSpecularColor(ele.Attributes[P.SpCol].ToVector3());
         p.SetSpecularRC(ele.Attributes[P.SpecCoeff].ToFloat());
         p.SetExp(ele.Attributes[P.Exp].ToFloat());
         p.SetCD(ele.Attributes[P.Col].ToVector3());
         p.SetAmbientRC(ele.Attributes[P.AmbRefCoeff].ToFloat());
         p.SetDiffuseRC(ele.Attributes[P.DiffRefCoeff].ToFloat());
         p.SetCR(ele.Attributes[P.GlosCol].ToVector3());
         p.SetKR(ele.Attributes[P.GlosCoeff].ToFloat());
         p.Shadows = shad;
         if (s != null) p.SetSampler(s);
         return p;
     }
     else if (n == P.Mate.Emis)
     {
         Emissive em = new Emissive();
         em.Color = ele.Attributes[P.Col].ToVector3();
         em.Radiance = ele.Attributes[P.Radi].ToFloat();
         em.Shadows = shad;
         if (s != null) em.SetSampler(s);
         return em;
     }
     else if (n == (P.Text + P.Mate.Matt))
     {
         SV_Matte p = new SV_Matte(ele.Attributes[P.AmbRefCoeff].ToFloat(), ele.Attributes[P.DiffRefCoeff].ToFloat(), textures[ele.Attributes[P.Text].Value]) { Shadows = shad };
         if (s != null) p.SetSampler(s);
         return p;
     }
     else if (n == (P.Text + P.Mate.Pho))
     {
         SV_Phong p = new SV_Phong();
         p.SetSpecularColor(textures[ele.Attributes[P.SpTxt].Value]);
         p.SetExp(ele.Attributes[P.Exp].ToFloat());
         p.SetCD(textures[ele.Attributes[P.Text].Value]);
         p.SetAmbientRC(ele.Attributes[P.AmbRefCoeff].ToFloat());
         p.SetDiffuseRC(ele.Attributes[P.DiffRefCoeff].ToFloat());
         p.Shadows = shad;
         if (s != null) p.SetSampler(s);
         return p;
     }
     throw new Exception(); //Create an exception for not found elements;
 }
Esempio n. 2
0
 static Material getMaterial(XElement xele)
 {
     foreach (XElement ele in xele.Elements())
     {
         string typ = ele.Name.LocalName;
         if (typ == "Matte")
         {
             return new Matte(float.Parse(getAttribute("ReflectionCoff", ele)), float.Parse(getAttribute("DiffuseCoff", ele)), getVector(getAttribute("Color", ele))) { Shadows = bool.Parse(getAttribute("Shadows", ele)) };
         }
         else if (typ == "Phong")
         {
             Phong p = new Phong();
             p.SetSpecularColor(getVector(getAttribute("SpecularColor", ele)));
             p.SetExp(float.Parse(getAttribute("Exponent", ele)));
             p.SetCD(getVector(getAttribute("Color", ele)));
             p.SetAmbientRC(float.Parse(getAttribute("AmbientRelfectionCoff", ele)));
             p.SetDiffuseRC(float.Parse(getAttribute("DiffuseRelfectionCoff", ele)));
             p.Shadows = bool.Parse(getAttribute("Shadows", ele));
             return p;
         }
         else if (typ == "Reflective")
         {
             Reflective r = new Reflective();
             r.SetSpecularColor(getVector(getAttribute("SpecularColor", ele)));
             r.SetSpecularRC(float.Parse(getAttribute("SpecularCoff", ele)));
             r.SetRColor(getVector(getAttribute("ReflectiveColor", ele)));
             r.SetReflectiveRC(float.Parse(getAttribute("ReflectiveCoff", ele)));
             r.SetExp(float.Parse(getAttribute("Exponent", ele)));
             r.SetCD(getVector(getAttribute("Color", ele)));
             r.SetAmbientRC(float.Parse(getAttribute("AmbientRelfectionCoff", ele)));
             r.SetDiffuseRC(float.Parse(getAttribute("DiffuseRelfectionCoff", ele)));
             r.Shadows = bool.Parse(getAttribute("Shadows", ele));
             return r;
         }
         else if (typ == "Transparent")
         {
             Transparent t = new Transparent();
             t.SetSpecularColor(getVector(getAttribute("SpecularColor", ele)));
             t.SetSpecularRC(float.Parse(getAttribute("SpecularCoff", ele)));
             t.SetReflectiveRC(float.Parse(getAttribute("ReflectiveCoff", ele)));
             t.SetExp(float.Parse(getAttribute("Exponent", ele)));
             t.SetCD(getVector(getAttribute("Color", ele)));
             t.SetAmbientRC(float.Parse(getAttribute("AmbientRelfectionCoff", ele)));
             t.SetDiffuseRC(float.Parse(getAttribute("DiffuseRelfectionCoff", ele)));
             t.SetIndexOfRefraction(float.Parse(getAttribute("IOR", ele)));
             t.SetTransmissionCoefficient(float.Parse(getAttribute("TransCoff", ele)));
             t.Shadows = bool.Parse(getAttribute("Shadows", ele));
             return t;
         }
         else if (typ == "TextureMatte")
         {
             return new SV_Matte(float.Parse(getAttribute("ReflectionCoff", ele)), float.Parse(getAttribute("DiffuseCoff", ele)), textures[getAttribute("Texture", ele)]);
         }
         else if (typ == "TexturePhong")
         {
             SV_Phong p = new SV_Phong();
             p.SetSpecularColor(textures[getAttribute("SpecularTexture", ele)]);
             p.SetExp(float.Parse(getAttribute("Exponent", ele)));
             p.SetCD(textures[getAttribute("Texture", ele)]);
             p.SetAmbientRC(float.Parse(getAttribute("AmbientRelfectionCoff", ele)));
             p.SetDiffuseRC(float.Parse(getAttribute("DiffuseRelfectionCoff", ele)));
             p.Shadows = bool.Parse(getAttribute("Shadows", ele));
             return p;
         }
         break;
     }
     throw new ElementNotFound("Material");
 }