Example #1
0
        private void BuildVertexData(Model.SubModel sub, ModelSettings settings)
        {
            sub.vertexStride = 0;
            foreach (Model.SourceInput input in sub.inputList)
            {
                if (input.data != null)
                {
                    sub.vertexStride += input.data.dataRefs.Count;
                }
            }

            for (int i = 0; i < sub.VertexMap.Count / sub.indexStride; i++)
            {
                foreach (Model.SourceInput input in sub.inputList)
                {
                    if (input.data != null)
                    {
                        for (int j = 0; j < input.data.dataRefs.Count; j++)
                        {
                            sub.VertexList.Add(input.data.data[sub.VertexMap[i * sub.indexStride + input.offset] * input.data.dataRefs.Count + j]);
                        }
                    }
                }
            }
        }
Example #2
0
        private void RemapOffsets(Model.SubModel sub)
        {
            List <int> newMap = new List <int>();

            for (int i = 0; i < sub.VertexMap.Count; i += sub.indexStride)
            {
                foreach (Model.SourceInput input in sub.inputList)
                {
                    if (input.data == null)
                    {
                        newMap.Add(input.offset);
                    }
                    else
                    {
                        newMap.Add(sub.VertexMap[i + input.offset]);
                    }
                }
            }
            sub.VertexMap = newMap;

            sub.indexStride = 0;
            foreach (Model.SourceInput input in sub.inputList)
            {
                input.offset = sub.indexStride++;
            }
        }
Example #3
0
        private void RemapInputs(Model.SubModel sub, ModelSettings settings)
        {
            if (settings.channels.Count <= 0)
            {
                return;
            }

            List <Model.SourceInput> newInputs = new List <Model.SourceInput>();

            foreach (ModelSettings.Channel channel in settings.channels)
            {
                Model.SourceInput input = sub.inputList.FirstOrDefault((i) => i.semantic == channel.semantic && i.set == channel.set);
                if (input == null && !channel.useDefault)
                {
                    continue;
                }

                Model.SourceInput newInput = new Model.SourceInput();
                newInput.name     = channel.name;
                newInput.data     = (input != null)?input.data:null;
                newInput.semantic = channel.semantic;
                newInput.set      = channel.set;
                newInput.offset   = (input != null)?input.offset:channel.def;
                if (newInput.data != null)
                {
                    newInput.data.semantic = newInput.name;
                }
                newInputs.Add(newInput);
            }
            sub.inputList = newInputs;
        }
Example #4
0
        private void Process(Model.SubModel sub, ModelSettings settings)
        {
            RemapInputs(sub, settings);
            RemapOffsets(sub);

            foreach (Model.SourceInput input in sub.inputList)
            {
                if (input.data != null)
                {
                    ApplyMap(sub, input.data, input.offset);
                }
            }

            if (settings.optimise)
            {
                Remap(sub);
            }

            if ((sub.type & settings.outputTypes) != sub.type)
            {
                Converter conv = converters.FirstOrDefault((c) => c.source == sub.type && (c.dest & settings.outputTypes) == c.dest);
                if (conv != null)
                {
                    conv.converter(sub);
                }
                else
                {
                    mgr.Error(string.Format("Cannot convert {0} to a suitible format", sub.type));
                }
            }

            BuildVertexData(sub, settings);
        }
Example #5
0
        private void ReadIndexes(Model.SubModel sub, XElement xSub, int num)
        {
            int count = (int)xSub.Attribute("count");

            if (num > 0)
            {
                sub.countList.AddRange(Enumerable.Repeat(num, count));
            }
            else if (num == -1)
            {
                XElement xVCount = xSub.Element(parent.ns + "vcount");
                if (xVCount == null)
                {
                    return;
                }

                string[] sCounts = xVCount.Value.Split(" \t\n".ToArray(), StringSplitOptions.RemoveEmptyEntries);
                sub.countList.AddRange(sCounts.Select((s) => int.Parse(s)));
            }
            foreach (XElement xP in xSub.Elements(parent.ns + "p"))
            {
                string[] array = xP.Value.Split(" \t\n".ToArray(), StringSplitOptions.RemoveEmptyEntries);
                sub.VertexMap.AddRange(array.Select((s) => int.Parse(s)));

                if (num == 0)
                {
                    sub.countList.Add(array.Length / sub.indexStride);
                }
            }
            for (int i = 0; i < sub.VertexMap.Count / sub.indexStride; i++)
            {
                sub.IndexList.Add(i);
            }
        }
Example #6
0
 private void ApplyMap(Model.SubModel sub, Model.SourceAccessor sourceData, int offset)
 {
     if (sourceData.map == null)
     {
         return;
     }
     for (int i = 0; i < sub.VertexMap.Count; i += sub.indexStride)
     {
         sub.VertexMap[i + offset] = sourceData.map[sub.VertexMap[i + offset]];
     }
 }
Example #7
0
        private int ReadInputs(Model model, Model.SubModel sub, XElement xSub)
        {
            int numOffsets = 0;
            List <Model.SourceInput> list = new List <Model.SourceInput>();

            foreach (XElement xInput in xSub.Elements(parent.ns + "input"))
            {
                int offset = (int)xInput.Attribute("offset");
                if (offset + 1 > numOffsets)
                {
                    numOffsets = offset + 1;
                }

                Model.SourceInput source = new Model.SourceInput();
                source.name     = ((string)xInput.Attribute("source")).Substring(1);
                source.data     = model.accessorList.FirstOrDefault((s) => s.name == source.name);
                source.semantic = (string)xInput.Attribute("semantic");
                if (xInput.Attribute("set") != null)
                {
                    source.set = (string)xInput.Attribute("set");
                }
                source.offset = offset;
                list.Add(source);
            }
            foreach (Model.SourceInput source in list)
            {
                if (source.data != null)
                {
                    source.name          = source.semantic + source.set;
                    source.data.semantic = source.name;
                    sub.inputList.Add(source);
                }
                foreach (Model.SourceAccessorRef item in model.mapAccessorToInput.Where((k) => k.name == source.name))
                {
                    Model.SourceInput source2 = new Model.SourceInput();
                    source2.name     = item.semantic;
                    source2.data     = item.accessor;
                    source2.semantic = item.semantic;
                    source2.set      = "";
                    source2.offset   = source.offset;
                    while (char.IsDigit(source2.semantic.Last()))
                    {
                        source2.set      = source2.semantic.Last().ToString() + source2.set;
                        source2.semantic = source2.semantic.Substring(0, source2.semantic.Length - 1);
                    }
                    if (source2.data != null)
                    {
                        source2.data.semantic = source2.name;
                    }
                    sub.inputList.Add(source2);
                }
            }
            return(numOffsets);
        }
Example #8
0
        void PolyToQuadTris(Model.SubModel sub)
        {
            List <int> IndexList = sub.IndexList;

            sub.IndexList = new List <int>();
            int offset = 0;

            for (int i = 0; i < sub.countList.Count; i++)
            {
                int count = sub.countList[i];
                if (count == 4 || count == 3)
                {
                    for (int j = 0; j < count; j++)
                    {
                        sub.IndexList.Add(IndexList[offset + j]);
                    }
                    offset += count;
                    continue;
                }
                sub.countList[i] = 0;

                int  a = offset, b = offset + count - 1;
                bool left = true;
                while (b - a > 1)
                {
                    if (left)
                    {
                        sub.IndexList.Add(IndexList[b]);
                        sub.IndexList.Add(IndexList[a]);
                        a++;
                        sub.IndexList.Add(IndexList[a]);
                        left = false;
                    }
                    else
                    {
                        sub.IndexList.Add(IndexList[b]);
                        sub.IndexList.Add(IndexList[a]);
                        b--;
                        sub.IndexList.Add(IndexList[b]);
                        left = true;
                    }
                    sub.countList[i] += 3;
                }
                offset += count;
            }
            sub.type = Model.IndexType.QuadsAndTris;
        }
Example #9
0
        private Model.SubModel MakeSubModel(XElement xSub, Model.IndexType type, List <string> materials)
        {
            Model.SubModel sub = new Model.SubModel();

            string mat = (string)xSub.Attribute("material");

            sub.material = materials.IndexOf(mat);
            if (sub.material == -1)
            {
                sub.material = materials.Count;
                materials.Add(mat);
            }

            sub.type        = type;
            sub.indexStride = 0;
            return(sub);
        }
Example #10
0
        private void Remap(Model.SubModel sub)
        {
            List <int> newData = new List <int>();

            sub.IndexList.Clear();

            for (int i = 0; i < sub.VertexMap.Count; i += sub.indexStride)
            {
                bool found = false;
                for (int j = 0; j < newData.Count; j += sub.indexStride)
                {
                    found = true;
                    for (int k = 0; k < sub.indexStride; k++)
                    {
                        if (sub.VertexMap[i + k] != newData[j + k])
                        {
                            found = false;
                            break;
                        }
                    }
                    if (found)
                    {
                        sub.IndexList.Add(j / sub.indexStride);
                        break;
                    }
                }
                if (found)
                {
                    continue;
                }

                sub.IndexList.Add(newData.Count / sub.indexStride);
                for (int k = 0; k < sub.indexStride; k++)
                {
                    newData.Add(sub.VertexMap[i + k]);
                }
            }
            sub.VertexMap = newData;
        }