Ejemplo n.º 1
0
        static ColladaGeometry GetGeometry(CL.UpAxisType up, CL.geometry geo, CL.library_materials matlib, CL.library_effects fxlib)
        {
            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          materialRef;
                ColladaMaterial material;
                if (item is CL.triangles)
                {
                    var triangles = (CL.triangles)item;
                    indexCount  = (int)(triangles.count * 3);
                    pRefs       = IntArray(triangles.p);
                    inputs      = triangles.input;
                    materialRef = 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;
                    materialRef = 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");
                        }
                    }
                    materialRef = plist.material;
                    inputs      = plist.input;
                    indexCount  = (int)(plist.count * 3);
                }
                if (indexCount == 0)
                {
                    continue;                  //Skip empty
                }
                material = ParseMaterial(materialRef, matlib, fxlib);
                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    = material
                });
            }
            conv.Indices   = indices.ToArray();
            conv.Vertices  = vertices.ToArray();
            conv.Drawcalls = drawcalls.ToArray();
            conv.CalculateDimensions();
            return(conv);
        }
Ejemplo n.º 2
0
        public static EditableUtf Generate(string iconName, LUtfNode textureNode, bool alpha)
        {
            var    modelFile    = new EditableUtf();
            var    unique       = IdSalt.New();
            string textureName  = $"data.icon.{iconName}.{unique}.tga";
            string materialName = $"data.icon.{iconName}.{unique}";
            string meshName     = $"data.icon.{iconName}.lod0-{unique}.vms";
            //VMeshLibrary
            var geom = new ColladaGeometry();

            geom.Vertices  = vertices;
            geom.Indices   = indices;
            geom.FVF       = D3DFVF.NORMAL | D3DFVF.XYZ | D3DFVF.TEX1;
            geom.Drawcalls = new[]
            {
                new ColladaDrawcall()
                {
                    StartVertex = 0, EndVertex = 3, Material = new ColladaMaterial()
                    {
                        Name = materialName
                    }, StartIndex = 0, TriCount = 2
                }
            };
            geom.CalculateDimensions();
            var vmsLib = new LUtfNode()
            {
                Name = "VMeshLibrary", Parent = modelFile.Root, Children = new List <LUtfNode>()
            };

            modelFile.Root.Children.Add(vmsLib);
            var vmsName = new LUtfNode()
            {
                Name = meshName, Parent = vmsLib, Children = new List <LUtfNode>()
            };

            vmsLib.Children.Add(vmsName);
            vmsName.Children.Add(new LUtfNode()
            {
                Name   = "VMeshData",
                Parent = vmsName,
                Data   = geom.VMeshData()
            });
            //VMeshPart
            var vmeshPart = new LUtfNode()
            {
                Name = "VMeshPart", Parent = modelFile.Root, Children = new List <LUtfNode>()
            };

            modelFile.Root.Children.Add(vmeshPart);
            vmeshPart.Children.Add(new LUtfNode()
            {
                Name = "VMeshRef", Parent = vmeshPart, Data = geom.VMeshRef(meshName)
            });

            //Texture
            var textureLibrary = new LUtfNode()
            {
                Name   = "Texture Library",
                Parent = modelFile.Root
            };

            textureLibrary.Children = new List <LUtfNode>();
            var clonedTex = textureNode.MakeCopy();

            clonedTex.Parent = textureLibrary;
            clonedTex.Name   = textureName;
            textureLibrary.Children.Add(clonedTex);
            modelFile.Root.Children.Add(textureLibrary);
            //Material
            var materialLibrary = new LUtfNode()
            {
                Name   = "Material Library",
                Parent = modelFile.Root
            };

            materialLibrary.Children = new List <LUtfNode>();
            var material = new LUtfNode()
            {
                Name   = materialName,
                Parent = materialLibrary
            };

            material.Children = new List <LUtfNode>();
            material.Children.Add(new LUtfNode()
            {
                Name   = "Type",
                Parent = material,
                Data   = Encoding.ASCII.GetBytes(alpha ? "DcDtOcOt" : "DcDt")
            });
            material.Children.Add(new LUtfNode()
            {
                Name   = "Dt_name",
                Parent = material,
                Data   = Encoding.ASCII.GetBytes(textureName)
            });
            material.Children.Add(new LUtfNode()
            {
                Name   = "Dt_flags",
                Parent = material,
                Data   = BitConverter.GetBytes((int)SamplerFlags.Default)
            });
            materialLibrary.Children.Add(material);
            modelFile.Root.Children.Add(materialLibrary);

            return(modelFile);
        }