Beispiel #1
0
        private void CreateSkeletonMesh()
        {
            NullMeshFile meshFile = new NullMeshFile(NullWorkingFlag.WF_SKELETON_MESHPIECE);

            bool hadNormals  = mVertexNormals.Length > 0;
            bool hadTangents = mVertexTangents.Length > 0;
            bool hadColors   = mVertexColors.Length > 0;
            bool hadUv       = mVertexuvs.Length > 0;

            NullMeshObject meshObject  = meshFile.AppendSkinObject(NullPrimitiveType.MOT_INDEXED_PRIMITIVES, mVertexTriangles.Length, mVertexPoses.Length, true, true, true);
            NullUVGroup    uvGroup     = hadUv ? meshObject.GetOrCreateUVGroup(UVType.UVT_DEFAULT, mVertexPoses.Length) : null;
            int            vertexCount = mVertexPoses.Length;

            for (int i = 0; i < vertexCount; ++i)
            {
                meshObject.SetVertex(i, mVertexPoses[i]);
                if (hadColors)
                {
                    meshObject.SetVertexColor(i, Convert(mVertexColors[i]));
                }
                if (hadTangents)
                {
                    meshObject.SetTangent(i, mVertexTangents[i]);
                }
                if (hadNormals)
                {
                    meshObject.SetNormal(i, mVertexNormals[i]);
                }
                if (hadUv)
                {
                    uvGroup.SetUV(i, mVertexuvs[i]);
                }
            }

            for (int i = 0; i < mVertexTriangles.Length; i += 3)
            {
                int i1 = mVertexTriangles[i];
                int i2 = mVertexTriangles[i + 1];
                int i3 = mVertexTriangles[i + 2];
                meshObject.SetTriangleIndex(i, new Vector3Int(i1, i2, i3));
            }

            // 骨骼数
            NullNodeTree rootNode = meshFile.GetNodeTree();

            ConvertNodeTree(mRootBone, rootNode, mBindposes, mBones);

            // 权重绑定
            NullSkeletonBinding skeletonBinding = meshFile.GetSkeletonBinding();
            int pieceCount = 1; // 对应子模型,现在只限制一个子模型

            skeletonBinding.SetSkeletonName(mMesh.name);
            skeletonBinding.SetSkeletonBindingCount(pieceCount);
            for (int i = 0; i < pieceCount; ++i)
            {
                NullSkeletonPiece skeletonPiece = skeletonBinding[i];
                skeletonPiece.SetPieceHandle(i);
                ConvertSkeletonPiece(skeletonPiece, mBoneweights);
            }

            string fileName = OutPutDir + "/" + Target.name + ".hxs";

            using (NullMemoryStream stream = NullMemoryStream.WriteToFile(fileName))
            {
                meshFile.SaveToStream(stream);
            }
        }