예제 #1
0
파일: Material.cs 프로젝트: davidfoxhu/G3D
        private Material.Parameter GetFloatParam(XElement item, string element, Dictionary <string, XElement> paramDict)
        {
            Material.Parameter param = new Material.Parameter();

            XElement val    = item.Element(parent.ns + element);
            XElement xParam = item.Element(parent.ns + "param");

            if (xParam != null)
            {
                string pName = (string)xParam.Attribute("ref");
                if (pName == null)
                {
                    mgr.Error(parent.GetLocation(xParam), "missing ref");
                }
                else
                {
                    val = paramDict[pName];
                }
            }

            if (val == null)
            {
                return(null);
            }

            param.name  = item.Name.LocalName;
            param.fData = val.Value.Split(" \t\n".ToArray(), StringSplitOptions.RemoveEmptyEntries).Select((s) => float.Parse(s)).ToArray();
            return(param);
        }
예제 #2
0
파일: Material.cs 프로젝트: davidfoxhu/G3D
        private Material.Parameter GetParam(XElement xParam, Dictionary <string, XElement> paramDict)
        {
            Material.Parameter param = new Material.Parameter();
            param.name = (string)xParam.Attribute("ref");

            foreach (XElement val in xParam.Elements())
            {
                string n = val.Name.LocalName;

                if (n.StartsWith("float"))
                {
                    param.fData = val.Value.Split(" \t\n".ToArray(), StringSplitOptions.RemoveEmptyEntries).Select((s) => float.Parse(s)).ToArray();
                }
                else if (n.StartsWith("int"))
                {
                    param.iData = val.Value.Split(" \t\n".ToArray(), StringSplitOptions.RemoveEmptyEntries).Select((s) => int.Parse(s)).ToArray();
                }
                else if (n.StartsWith("sampler"))
                {
                    XElement source = val.Element(parent.ns + "source");
                    if (source == null || !paramDict.ContainsKey(source.Value))
                    {
                        mgr.Error(parent.GetLocation(xParam), "cannot find surface");
                        return(null);
                    }
                    param      = GetParam(paramDict[source.Value], paramDict);
                    param.name = (string)xParam.Attribute("ref");
                    return(param);
                }
                else if (n.StartsWith("surface"))
                {
                    XElement source = val.Element(parent.ns + "init_from");
                    if (source == null)
                    {
                        mgr.Error(parent.GetLocation(xParam), "cannot find image");
                        return(null);
                    }

                    string texture = source.Value;

                    mgr.Process("collada.texture", texture);
                    param.sData = mgr.name + "." + texture;
                    return(param);
                }
                else
                {
                    return(null);
                }
            }
            return(param);
        }
예제 #3
0
파일: Material.cs 프로젝트: davidfoxhu/G3D
        private Material.Parameter GetTextureParam(XElement item, Dictionary <string, XElement> paramDict)
        {
            XElement text = item.Element(parent.ns + "texture");

            if (text == null)
            {
                return(null);
            }

            string texture = (string)text.Attribute("texture");

            if (texture == null)
            {
                return(null);
            }

            XElement sampler = paramDict[texture];
            XElement surface = null;

            if (sampler != null)
            {
                sampler = GetParamData(sampler);
            }
            if (sampler != null)
            {
                sampler = sampler.Element(parent.ns + "source");
            }
            if (sampler != null)
            {
                surface = paramDict[sampler.Value];
            }
            if (surface != null)
            {
                surface = GetParamData(surface);
            }
            if (surface != null)
            {
                surface = surface.Element(parent.ns + "init_from");
            }
            if (surface != null)
            {
                texture = surface.Value;
            }

            mgr.Process("collada.texture", texture);

            Material.Parameter param = new Material.Parameter();
            param.name  = item.Name.LocalName;
            param.sData = mgr.name + "." + texture;
            return(param);
        }
예제 #4
0
파일: Material.cs 프로젝트: davidfoxhu/G3D
        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);
                        }
                    }
                }
            }
        }