示例#1
0
        public void GenerateBinary(FileSettings settings, string[] args)
        {
            ResFile resFile = new ResFile();
            string  output  = "";
            string  input   = "";

            for (int i = 0; i < args.Length; i++)
            {
                if (args[i] == "-i" || args[i] == "--input")
                {
                    input = args[i + 1];
                }
                if (args[i] == "-o" || args[i] == "--output")
                {
                    output = args[i + 1];
                }
            }

            if (input == string.Empty)
            {
                Console.WriteLine("Must have an input (-i) file path");
                return;
            }
            if (output == string.Empty)
            {
                Console.WriteLine("Must have an output (-o) file path");
                return;
            }

            STGenericScene scene = new STGenericScene();

            string ext = Path.GetExtension(input);

            switch (ext)
            {
            case ".dae":
                scene = Collada.ColladaReader.Read(input);
                break;

            case ".fbx":
                break;
            }

            ConvertScene(resFile, scene);
            resFile.Save(output);
        }
示例#2
0
        private void ConvertScene(ResFile resFile, STGenericScene scene)
        {
            foreach (var model in scene.Models)
            {
                Model fmdl = new Model();
                fmdl.Name = model.Name;
                foreach (var mat in model.Materials)
                {
                    Material fmat = new Material();
                    fmat.Name = mat.Name;
                    fmdl.Materials.Add(fmat);
                    fmdl.MaterialDict.Add(fmat.Name);
                }

                fmdl.Skeleton = new Skeleton();
                foreach (var bone in model.Skeleton.Bones)
                {
                    fmdl.Skeleton.BoneDict.Add(bone.Name);
                    fmdl.Skeleton.Bones.Add(new Bone()
                    {
                        FlagsRotation     = BoneFlagsRotation.EulerXYZ,
                        FlagsTransform    = SetBoneFlags(bone),
                        Name              = bone.Name,
                        RigidMatrixIndex  = -1, //Gets calculated after
                        SmoothMatrixIndex = -1, //Gets calculated after
                        ParentIndex       = (short)bone.ParentIndex,
                        Position          = new Vector3F(
                            bone.Position.X,
                            bone.Position.Y,
                            bone.Position.Z),
                        Scale = new Vector3F(
                            bone.Scale.X,
                            bone.Scale.Y,
                            bone.Scale.Z),
                        Rotation = new Vector4F(
                            bone.EulerRotation.X,
                            bone.EulerRotation.Y,
                            bone.EulerRotation.Z,
                            1.0f),
                        Visible = true,
                    });
                }

                List <int> smoothSkinningIndices = new List <int>();
                List <int> rigidSkinningIndices  = new List <int>();

                //Determine the rigid and smooth bone skinning
                foreach (var mesh in model.Meshes)
                {
                    Console.WriteLine("mesh " + mesh.Name);

                    var numSkinning = CalculateSkinCount(mesh.Vertices);
                    foreach (var vertex in mesh.Vertices)
                    {
                        foreach (var bone in vertex.BoneWeights)
                        {
                            var bn = fmdl.Skeleton.Bones.Where(x => x.Name == bone.Bone).FirstOrDefault();
                            if (bn != null)
                            {
                                int index = fmdl.Skeleton.Bones.IndexOf(bn);

                                //Rigid skinning
                                if (numSkinning == 1)
                                {
                                    bn.RigidMatrixIndex = (short)index;
                                    if (!rigidSkinningIndices.Contains(index))
                                    {
                                        rigidSkinningIndices.Add(index);
                                    }
                                }
                                else
                                {
                                    bn.SmoothMatrixIndex = (short)index;
                                    if (!smoothSkinningIndices.Contains(index))
                                    {
                                        smoothSkinningIndices.Add(index);
                                    }
                                }
                            }
                        }
                    }
                }

                List <int> skinningIndices = new List <int>();
                skinningIndices.AddRange(smoothSkinningIndices);
                skinningIndices.AddRange(rigidSkinningIndices);

                fmdl.Skeleton.MatrixToBoneList = new List <ushort>();
                for (int i = 0; i < skinningIndices.Count; i++)
                {
                    fmdl.Skeleton.MatrixToBoneList.Add((ushort)skinningIndices[i]);
                }

                foreach (var mesh in model.Meshes)
                {
                    var settings = new MeshSettings()
                    {
                        UseBoneIndices = true,
                        UseBoneWeights = true,
                        UseNormal      = true,
                        UseTexCoord    = new bool[5] {
                            true, true, true, true, true,
                        },
                        UseColor = new bool[5] {
                            true, true, true, true, true,
                        },
                    };

                    var names = fmdl.Shapes.Select(x => x.Name).ToList();

                    Shape fshp = new Shape();
                    fshp.Name = Utility.RenameDuplicateString(names, mesh.Name, 0, 2);
                    //    fshp.MaterialIndex = (ushort)mesh.PolygonGroups[0].MaterialIndex;
                    fshp.MaterialIndex = 0;

                    var boundingBox = CalculateBoundingBox(mesh.Vertices);
                    fshp.SubMeshBoundings.Add(boundingBox);
                    fshp.SubMeshBoundings.Add(boundingBox);
                    fshp.SubMeshBoundingIndices.Add(0);
                    fshp.RadiusArray.Add((float)(boundingBox.Center.Length + boundingBox.Extent.Length));
                    fshp.SubMeshBoundingNodes.Add(new BoundingNode()
                    {
                        LeftChildIndex  = 0,
                        RightChildIndex = 0,
                        NextSibling     = 0,
                        SubMeshIndex    = 0,
                        Unknown         = 0,
                        SubMeshCount    = 1,
                    });

                    try
                    {
                        VertexBuffer buffer = GenerateVertexBuffer(mesh, settings, fmdl.Skeleton);
                        fshp.VertexBufferIndex = (ushort)fmdl.VertexBuffers.Count;
                        fshp.VertexSkinCount   = (byte)buffer.VertexSkinCount;
                        fmdl.VertexBuffers.Add(buffer);
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("Failed to generate vertex buffer! \n " + ex.ToString());
                    }


                    Mesh bMesh = new Mesh();
                    bMesh.PrimitiveType = PrimitiveType.Triangles;

                    IndexFormat Format = IndexFormat.UInt16;
                    if (mesh.Faces.Any(x => x > ushort.MaxValue))
                    {
                        Format = IndexFormat.UInt32;
                    }

                    bMesh.SetIndices(mesh.Faces, Format);
                    bMesh.SubMeshes.Add(new SubMesh()
                    {
                        Offset = 0,
                        Count  = (uint)mesh.Faces.Count,
                    });
                    fshp.Meshes.Add(bMesh);

                    fmdl.Shapes.Add(fshp);
                    fmdl.ShapeDict.Add(fshp.Name);
                }

                resFile.Models.Add(fmdl);
            }
        }