Example #1
0
        private void ReadVariable(Effect effect, XElement child, bool isStatic)
        {
            Effect.Variable var = new Effect.Variable();
            var.isStatic = isStatic;
            var.name     = (string)child.Attribute("name");
            var.aux      = (string)child.Attribute("aux");
            var.hint     = (string)child.Attribute("hint");
            if (child.Attribute("type") != null)
            {
                var.type = (Effect.Type)Enum.Parse(typeof(Effect.Type), (string)child.Attribute("type"));
            }

            effect.variables.Add(var);
        }
Example #2
0
        protected override void Process(XElement item)
        {
            string name = mgr.name + "." + (string)item.Attribute("id");
            Dictionary <string, XElement> paramDict = new Dictionary <string, XElement>();

            XElement xInst = item.Element(parent.ns + "instance_effect");

            if (xInst == null)
            {
                return;
            }

            XElement xEffect = parent.GetFromLib(xInst, "url", "effect");

            if (xEffect == null)
            {
                return;
            }

            XElement profileC  = xEffect.Element(parent.ns + "profile_COMMON");
            XElement profileCG = xEffect.Element(parent.ns + "profile_CG");

            if (profileCG == null)
            {
                profileCG = xEffect.Element(parent.ns + "profile_GLSL");
            }
            if (profileC != null)
            {
                XElement   technique  = null;
                XElement[] techniques = profileC.Elements(parent.ns + "technique").ToArray();
                if (techniques.Length > 1)
                {
                    technique = techniques.FirstOrDefault((t) => {
                        XAttribute sid = t.Attribute("sid");
                        if (sid == null)
                        {
                            return(false);
                        }
                        return(string.Compare(sid.Value, "common", true) == 0);
                    });
                }
                if (techniques.Length > 0 && technique == null)
                {
                    technique = techniques[0];
                }
                if (technique == null)
                {
                    mgr.Error(parent.GetLocation(profileC), "missing technique");
                    return;
                }

                XElement xDetails = technique.Element(parent.ns + "constant");
                if (xDetails == null)
                {
                    xDetails = technique.Element(parent.ns + "lambert");
                }
                if (xDetails == null)
                {
                    xDetails = technique.Element(parent.ns + "phong");
                }
                if (xDetails == null)
                {
                    xDetails = technique.Element(parent.ns + "blinn");
                }
                if (xDetails == null)
                {
                    mgr.Error(parent.GetLocation(technique), "missing constant/lambert/phong/blinn");
                    return;
                }

                Material mat = mgr.GetData <Material>(name, GetterType.CreateOnly);
                if (mat == null)
                {
                    return;
                }
                mat.name = name;
                if (mat.effect == null)
                {
                    mat.effect = mgr.GetData <Effect>("effects." + GetName(item), GetterType.FetchOrDefault);
                }

                GetNewParams(paramDict, xEffect);
                GetNewParams(paramDict, profileC);
                GetNewParams(paramDict, technique);
                GetSetParams(paramDict, xInst);

                string[] ColOrTex     = new string[] { "emission", "ambient", "diffuse", "specular", "reflective", "transparent" };
                string[] ParamOrFloat = new string[] { "shininess", "reflectivity", "transparency", "index_of_refraction" };
                foreach (XElement xParam in xDetails.Elements())
                {
                    Material.Parameter param = null;
                    if (ColOrTex.Contains(xParam.Name.LocalName))
                    {
                        param = GetTextureParam(xParam, paramDict);
                        if (param == null)
                        {
                            param = GetFloatParam(xParam, "color", paramDict);
                        }
                    }
                    else if (ParamOrFloat.Contains(xParam.Name.LocalName))
                    {
                        param = GetFloatParam(xParam, "float", paramDict);
                    }
                    if (param == null)
                    {
                        continue;
                    }

                    Effect.Variable effectParam = mat.effect.variables.FirstOrDefault((p) => p.hint == param.name);
                    if (effectParam != null)
                    {
                        switch (effectParam.type)
                        {
                        case Effect.Type.int1:
                        case Effect.Type.int2:
                        case Effect.Type.int3:
                        case Effect.Type.int4:
                            if (param.iData == null)
                            {
                                continue;
                            }
                            break;

                        case Effect.Type.texture:
                            if (param.sData == null)
                            {
                                continue;
                            }
                            break;

                        default:
                            if (param.fData == null)
                            {
                                continue;
                            }
                            break;
                        }
                        param.name  = effectParam.name;
                        param.param = effectParam;
                        mat.parameters.Add(param);
                    }
                }
            }
            else if (profileCG != null)
            {
                XElement   techniqueHint = xInst.Element(parent.ns + "technique_hint");
                XElement   technique     = null;
                XElement[] techniques    = profileCG.Elements(parent.ns + "technique").ToArray();

                if (techniques.Length > 1 && techniqueHint != null && techniqueHint.Attribute("ref") != null)
                {
                    technique = techniques.FirstOrDefault((t) => {
                        XAttribute sid = t.Attribute("sid");
                        if (sid == null)
                        {
                            return(false);
                        }
                        return(string.Compare(sid.Value, (string)techniqueHint.Attribute("ref"), true) == 0);
                    });
                }
                if (techniques.Length > 0 && technique == null)
                {
                    technique = techniques[0];
                }
                if (technique == null)
                {
                    mgr.Error(parent.GetLocation(profileCG), "missing technique");
                    return;
                }

                Material mat = mgr.GetData <Material>(name, GetterType.CreateOnly);
                if (mat == null)
                {
                    return;
                }

                mat.name = name;
                if (mat.effect == null)
                {
                    string   effect  = null;
                    XElement include = profileCG.Element(parent.ns + "include");
                    if (include != null)
                    {
                        effect = (string)include.Attribute("sid");
                    }
                    if (effect == null)
                    {
                        effect = GetName(item);
                    }

                    mat.effect = mgr.GetData <Effect>("effects." + effect, GetterType.FetchOrDefault);
                }

                GetNewParams(paramDict, xEffect);
                GetNewParams(paramDict, profileCG);
                GetNewParams(paramDict, technique);
                GetSetParams(paramDict, xInst);
                GetSetParams(paramDict, technique);

                foreach (Effect.Variable effectParam in mat.effect.variables)
                {
                    if (paramDict.ContainsKey(effectParam.name))
                    {
                        Material.Parameter param = GetParam(paramDict[effectParam.name], paramDict);
                        if (param != null)
                        {
                            param.param = effectParam;
                            mat.parameters.Add(param);
                        }
                    }
                }
            }
        }