Beispiel #1
0
        static ColladaGeometry GetGeometry(CL.UpAxisType up, CL.geometry geo, bool isBlender)
        {
            var conv = new ColladaGeometry()
            {
                FVF = D3DFVF.XYZ
            };

            conv.Name = string.IsNullOrEmpty(geo.name) ? geo.id : geo.name;
            var msh = geo.Item as CL.mesh;

            if (msh == null)
            {
                return(null);
            }
            List <VertexPositionNormalDiffuseTextureTwo> vertices = new List <VertexPositionNormalDiffuseTextureTwo>();
            List <int>             hashes                    = new List <int>();
            List <ushort>          indices                   = new List <ushort>();
            List <ColladaDrawcall> drawcalls                 = new List <ColladaDrawcall>();
            Dictionary <string, GeometrySource> sources      = new Dictionary <string, GeometrySource>();
            Dictionary <string, float[]>        arrays       = new Dictionary <string, float[]>();
            Dictionary <string, GeometrySource> verticesRefs = new Dictionary <string, GeometrySource>();
            int placeHolderIdx = 0;

            //Get arrays
            foreach (var acc in msh.source)
            {
                var arr = acc.Item as CL.float_array;
                arrays.Add(arr.id, FloatArray(arr.Text));
            }
            //Accessors
            foreach (var acc in msh.source)
            {
                sources.Add(acc.id, new GeometrySource(acc, arrays));
            }
            //Process geometry
            foreach (var item in msh.Items)
            {
                if (!(item is CL.triangles || item is CL.polylist || item is CL.polygons))
                {
                    FLLog.Warning("Collada", "Ignoring " + item.GetType().Name + " element.");
                }
            }
            int totalTriangles = 0;

            foreach (var item in msh.Items.Where(x => x is CL.triangles || x is CL.polylist || x is CL.polygons))
            {
                if (item is CL.triangles)
                {
                    totalTriangles += (int)((CL.triangles)item).count;
                }
                else if (item is CL.polylist plist)
                {
                    totalTriangles += (int)plist.count;
                }
                else
                {
                    totalTriangles += (int)((CL.polygons)item).count;
                }
            }
            if (totalTriangles > 21845)
            {
                throw new Exception(string.Format(
                                        "Overflow!\nCollada geometry {0} has {1} triangles\nVMeshData has limit of 21845",
                                        string.IsNullOrEmpty(geo.name) ? geo.id : geo.name,
                                        totalTriangles));
            }
            foreach (var item in msh.Items.Where(x => x is CL.triangles || x is CL.polylist || x is CL.polygons))
            {
                CL.InputLocalOffset[] inputs;
                int[]  pRefs;
                int    indexCount;
                string material;
                if (item is CL.triangles)
                {
                    var triangles = (CL.triangles)item;
                    indexCount = (int)(triangles.count * 3);
                    pRefs      = IntArray(triangles.p);
                    inputs     = triangles.input;
                    material   = triangles.material;
                }
                else if (item is CL.polygons polygons)
                {
                    indexCount = (int)(polygons.count * 3);
                    int j = 0;
                    pRefs = new int[indexCount];
                    foreach (var arr in polygons.Items)
                    {
                        if (!(arr is string))
                        {
                            throw new Exception("Polygons: ph element unsupported");
                        }
                        var ints = IntArray((string)arr);
                        if (ints.Length != 3)
                        {
                            throw new Exception("Polygons: non-triangle geometry not supported");
                        }
                        pRefs[j]     = ints[0];
                        pRefs[j + 1] = ints[1];
                        pRefs[j + 2] = ints[2];
                        j           += 3;
                    }
                    inputs   = polygons.input;
                    material = polygons.material;
                }
                else
                {
                    var plist = (CL.polylist)item;
                    pRefs = IntArray(plist.p);
                    foreach (var c in IntArray(plist.vcount))
                    {
                        if (c != 3)
                        {
                            throw new Exception("Polylist: non-triangle geometry");
                        }
                    }
                    material   = plist.material;
                    inputs     = plist.input;
                    indexCount = (int)(plist.count * 3);
                }
                if (indexCount == 0)
                {
                    continue;                  //Skip empty
                }
                //Blender workaround #1 - their stuff is crap
                if (!string.IsNullOrEmpty(material) && isBlender && material.EndsWith("-material", StringComparison.InvariantCulture))
                {
                    material = material.Substring(0, material.Length - 9);
                }
                int pStride = 0;
                foreach (var input in inputs)
                {
                    pStride = Math.Max((int)input.offset, pStride);
                }
                pStride++;
                GeometrySource sourceXYZ = null; int offXYZ = int.MinValue;
                GeometrySource sourceNORMAL = null; int offNORMAL = int.MinValue;
                GeometrySource sourceCOLOR = null; int offCOLOR = int.MinValue;
                GeometrySource sourceUV1 = null; int offUV1 = int.MinValue;
                GeometrySource sourceUV2 = null; int offUV2 = int.MinValue;
                int            texCount = 0;
                int            startIdx = indices.Count;
                foreach (var input in inputs)
                {
                    switch (input.semantic)
                    {
                    case SEM_VERTEX:
                        if (CheckURI(input.source) != msh.vertices.id)
                        {
                            throw new Exception("VERTEX doesn't match mesh vertices");
                        }
                        foreach (var ip2 in msh.vertices.input)
                        {
                            switch (ip2.semantic)
                            {
                            case SEM_POSITION:
                                offXYZ    = (int)input.offset;
                                sourceXYZ = sources[CheckURI(ip2.source)];
                                break;

                            case SEM_NORMAL:
                                offNORMAL    = (int)input.offset;
                                sourceNORMAL = sources[CheckURI(ip2.source)];
                                conv.FVF    |= D3DFVF.NORMAL;
                                break;

                            case SEM_COLOR:
                                offCOLOR    = (int)input.offset;
                                sourceCOLOR = sources[CheckURI(ip2.source)];
                                conv.FVF   |= D3DFVF.DIFFUSE;
                                break;

                            case SEM_TEXCOORD:
                                if (texCount == 2)
                                {
                                    throw new Exception("Too many texcoords!");
                                }
                                if (texCount == 1)
                                {
                                    offUV2    = (int)input.offset;
                                    sourceUV2 = sources[CheckURI(ip2.source)];
                                    conv.FVF &= ~D3DFVF.TEX1;
                                    conv.FVF |= D3DFVF.TEX2;
                                }
                                else
                                {
                                    offUV1    = (int)input.offset;
                                    sourceUV1 = sources[CheckURI(ip2.source)];
                                    if ((conv.FVF & D3DFVF.TEX2) != D3DFVF.TEX2)
                                    {
                                        conv.FVF |= D3DFVF.TEX1;
                                    }
                                }
                                texCount++;
                                break;
                            }
                        }
                        break;

                    case SEM_POSITION:
                        offXYZ    = (int)input.offset;
                        sourceXYZ = sources[CheckURI(input.source)];
                        break;

                    case SEM_NORMAL:
                        offNORMAL    = (int)input.offset;
                        sourceNORMAL = sources[CheckURI(input.source)];
                        conv.FVF    |= D3DFVF.NORMAL;
                        break;

                    case SEM_COLOR:
                        offCOLOR    = (int)input.offset;
                        sourceCOLOR = sources[CheckURI(input.source)];
                        conv.FVF   |= D3DFVF.DIFFUSE;
                        break;

                    case SEM_TEXCOORD:
                        if (texCount == 2)
                        {
                            throw new Exception("Too many texcoords!");
                        }
                        if (texCount == 1)
                        {
                            offUV2    = (int)input.offset;
                            sourceUV2 = sources[CheckURI(input.source)];
                            conv.FVF &= ~D3DFVF.TEX1;
                            conv.FVF |= D3DFVF.TEX2;
                        }
                        else
                        {
                            offUV1    = (int)input.offset;
                            sourceUV1 = sources[CheckURI(input.source)];
                            if ((conv.FVF & D3DFVF.TEX2) != D3DFVF.TEX2)
                            {
                                conv.FVF |= D3DFVF.TEX1;
                            }
                        }
                        texCount++;
                        break;
                    }
                }
                int vertexOffset = vertices.Count;
                for (int i = 0; i < indexCount; i++)
                {
                    int idx  = i * pStride;
                    var vert = new VertexPositionNormalDiffuseTextureTwo(
                        VecAxis(up, sourceXYZ.GetXYZ(pRefs[idx + offXYZ])),
                        offNORMAL == int.MinValue ? Vector3.Zero : VecAxis(up, sourceNORMAL.GetXYZ(pRefs[idx + offNORMAL])),
                        offCOLOR == int.MinValue ? (uint)Color4.White.ToRgba() : (uint)sourceCOLOR.GetColor(pRefs[idx + offCOLOR]).ToRgba(),
                        offUV1 == int.MinValue ? Vector2.Zero : sourceUV1.GetUV(pRefs[idx + offUV1]),
                        offUV2 == int.MinValue ? Vector2.Zero : sourceUV2.GetUV(pRefs[idx + offUV2])
                        );
                    var hash    = HashVert(ref vert);
                    var vertIdx = FindDuplicate(hashes, vertices, vertexOffset, ref vert, hash);
                    if (indices.Count >= ushort.MaxValue)
                    {
                        throw new Exception("Too many indices");
                    }
                    if (vertIdx == -1)
                    {
                        if (vertices.Count + 1 >= ushort.MaxValue)
                        {
                            throw new Exception("Too many vertices");
                        }
                        indices.Add((ushort)(vertices.Count - vertexOffset));
                        vertices.Add(vert);
                        hashes.Add(hash);
                    }
                    else
                    {
                        indices.Add((ushort)(vertIdx - vertexOffset));
                    }
                }
                drawcalls.Add(new ColladaDrawcall()
                {
                    StartIndex  = startIdx,
                    StartVertex = vertexOffset,
                    EndVertex   = vertices.Count - 1,
                    TriCount    = (indices.Count - startIdx) / 3,
                    Material    = string.IsNullOrEmpty(material) ? (geo.name + "_mat" + placeHolderIdx++) : material
                });
            }
            conv.Indices   = indices.ToArray();
            conv.Vertices  = vertices.ToArray();
            conv.Drawcalls = drawcalls.ToArray();
            conv.CalculateDimensions();
            return(conv);
        }
Beispiel #2
0
        static ColladaGeometry GetGeometry(CL.UpAxisType up, CL.geometry geo)
        {
            var conv = new ColladaGeometry()
            {
                FVF = D3DFVF.XYZ
            };
            var msh = geo.Item as CL.mesh;

            if (msh == null)
            {
                return(null);
            }
            List <VertexPositionNormalDiffuseTextureTwo> vertices = new List <VertexPositionNormalDiffuseTextureTwo>();
            List <ushort>          indices                   = new List <ushort>();
            List <ColladaDrawcall> drawcalls                 = new List <ColladaDrawcall>();
            Dictionary <string, GeometrySource> sources      = new Dictionary <string, GeometrySource>();
            Dictionary <string, float[]>        arrays       = new Dictionary <string, float[]>();
            Dictionary <string, GeometrySource> verticesRefs = new Dictionary <string, GeometrySource>();

            //Get arrays
            foreach (var acc in msh.source)
            {
                var arr = acc.Item as CL.float_array;
                arrays.Add(arr.id, FloatArray(arr.Text));
            }
            //Accessors
            foreach (var acc in msh.source)
            {
                sources.Add(acc.id, new GeometrySource(acc, arrays));
            }
            //Process geometry
            if (msh.Items.Where(x => x is CL.triangles || x is CL.polylist).Count() != msh.Items.Length)
            {
                throw new Exception("Non-triangle geometry");
            }
            foreach (var item in msh.Items.Where(x => x is CL.triangles || x is CL.polylist))
            {
                CL.InputLocalOffset[] inputs;
                int[]  pRefs;
                int    triangleCount;
                string material;
                if (item is CL.triangles)
                {
                    var triangles = (CL.triangles)item;
                    pRefs         = IntArray(triangles.p);
                    inputs        = triangles.input;
                    triangleCount = (int)(triangles.count * 3);
                    material      = triangles.material;
                }
                else
                {
                    var plist = (CL.polylist)item;
                    pRefs = IntArray(plist.p);
                    foreach (var c in IntArray(plist.vcount))
                    {
                        if (c != 3)
                        {
                            throw new Exception("Polylist: non-triangle geometry");
                        }
                    }
                    material      = plist.material;
                    inputs        = plist.input;
                    triangleCount = (int)(plist.count * 3);
                }
                int pStride = 0;
                foreach (var input in inputs)
                {
                    pStride = Math.Max((int)input.offset, pStride);
                }
                pStride++;
                GeometrySource sourceXYZ = null; int offXYZ = int.MinValue;
                GeometrySource sourceNORMAL = null; int offNORMAL = int.MinValue;
                GeometrySource sourceCOLOR = null; int offCOLOR = int.MinValue;
                GeometrySource sourceUV1 = null; int offUV1 = int.MinValue;
                GeometrySource sourceUV2 = null; int offUV2 = int.MinValue;
                int            texCount = 0;
                int            startIdx = indices.Count;
                foreach (var input in inputs)
                {
                    switch (input.semantic)
                    {
                    case SEM_VERTEX:
                        if (CheckURI(input.source) != msh.vertices.id)
                        {
                            throw new Exception("VERTEX doesn't match mesh vertices");
                        }
                        foreach (var ip2 in msh.vertices.input)
                        {
                            switch (ip2.semantic)
                            {
                            case SEM_POSITION:
                                offXYZ    = (int)input.offset;
                                sourceXYZ = sources[CheckURI(ip2.source)];
                                break;

                            case SEM_NORMAL:
                                offNORMAL    = (int)input.offset;
                                sourceNORMAL = sources[CheckURI(ip2.source)];
                                conv.FVF    |= D3DFVF.NORMAL;
                                break;

                            case SEM_COLOR:
                                offCOLOR    = (int)input.offset;
                                sourceCOLOR = sources[CheckURI(ip2.source)];
                                conv.FVF   |= D3DFVF.DIFFUSE;
                                break;

                            case SEM_TEXCOORD:
                                if (texCount == 2)
                                {
                                    throw new Exception("Too many texcoords!");
                                }
                                if (texCount == 1)
                                {
                                    offUV2    = (int)input.offset;
                                    sourceUV2 = sources[CheckURI(ip2.source)];
                                    conv.FVF &= ~D3DFVF.TEX1;
                                    conv.FVF |= D3DFVF.TEX2;
                                }
                                else
                                {
                                    offUV1    = (int)input.offset;
                                    sourceUV1 = sources[CheckURI(ip2.source)];
                                    if ((conv.FVF & D3DFVF.TEX2) != D3DFVF.TEX2)
                                    {
                                        conv.FVF |= D3DFVF.TEX1;
                                    }
                                }
                                texCount++;
                                break;
                            }
                        }
                        break;

                    case SEM_POSITION:
                        offXYZ    = (int)input.offset;
                        sourceXYZ = sources[CheckURI(input.source)];
                        break;

                    case SEM_NORMAL:
                        offNORMAL    = (int)input.offset;
                        sourceNORMAL = sources[CheckURI(input.source)];
                        conv.FVF    |= D3DFVF.NORMAL;
                        break;

                    case SEM_COLOR:
                        offCOLOR    = (int)input.offset;
                        sourceCOLOR = sources[CheckURI(input.source)];
                        conv.FVF   |= D3DFVF.DIFFUSE;
                        break;

                    case SEM_TEXCOORD:
                        if (texCount == 2)
                        {
                            throw new Exception("Too many texcoords!");
                        }
                        if (texCount == 1)
                        {
                            offUV2    = (int)input.offset;
                            sourceUV2 = sources[CheckURI(input.source)];
                            conv.FVF &= ~D3DFVF.TEX1;
                            conv.FVF |= D3DFVF.TEX2;
                        }
                        else
                        {
                            offUV1    = (int)input.offset;
                            sourceUV1 = sources[CheckURI(input.source)];
                            if ((conv.FVF & D3DFVF.TEX2) != D3DFVF.TEX2)
                            {
                                conv.FVF |= D3DFVF.TEX1;
                            }
                        }
                        texCount++;
                        break;
                    }
                }
                for (int i = 0; i < triangleCount; i++)
                {
                    int idx  = i * pStride;
                    var vert = new VertexPositionNormalDiffuseTextureTwo(
                        VecAxis(up, sourceXYZ.GetXYZ(pRefs[idx + offXYZ])),
                        offNORMAL == int.MinValue ? Vector3.Zero : VecAxis(up, sourceNORMAL.GetXYZ(pRefs[idx + offNORMAL])),
                        offCOLOR == int.MinValue ? Color4.White : sourceCOLOR.GetColor(pRefs[idx + offCOLOR]),
                        offUV1 == int.MinValue ? Vector2.Zero : sourceUV1.GetUV(pRefs[idx + offUV1]),
                        offUV2 == int.MinValue ? Vector2.Zero : sourceUV2.GetUV(pRefs[idx + offUV2])
                        );
                    var vertIdx = vertices.IndexOf(vert);
                    if (indices.Count >= ushort.MaxValue)
                    {
                        throw new Exception("Too many indices");
                    }
                    if (vertIdx == -1)
                    {
                        if (vertices.Count + 1 >= ushort.MaxValue)
                        {
                            throw new Exception("Overflow");
                        }
                        indices.Add((ushort)vertices.Count);
                        vertices.Add(vert);
                    }
                    else
                    {
                        indices.Add((ushort)vertIdx);
                    }
                }
                drawcalls.Add(new ColladaDrawcall()
                {
                    Start    = startIdx,
                    TriCount = (indices.Count - startIdx) / 3,
                    Material = string.IsNullOrEmpty(material) ? "NullMaterial" : material
                });
            }
            conv.Indices   = indices.ToArray();
            conv.Vertices  = vertices.ToArray();
            conv.Drawcalls = drawcalls.ToArray();
            conv.CalculateDimensions();
            return(conv);
        }