示例#1
0
        public NullMeshObject AppendMeshObject(NullPrimitiveType meshType, int triangleCount, int vertexCount, bool includingNormal, bool includeTangent, bool includingVertexColor)
        {
            NullMeshObject meshObject = new NullMeshObject(CurrentVersion, meshType, triangleCount, vertexCount, includingNormal, includeTangent, includingVertexColor);

            MeshObjectList.Add(meshObject);
            return(meshObject);
        }
示例#2
0
        public bool RemoveMeshObject(NullMeshObject deletingOne, bool destroySource = true)
        {
            bool res = MeshObjectList.Remove(deletingOne);

            if (res && destroySource)
            {
                deletingOne.Clear();
            }
            return(true);
        }
 public bool BuildIndexedPrimitives(List <NullMergeIndex> indexMapping)
 {
     if (mVertexPosArray == null)
     {
         return(false);
     }
     mVertexPosArray = NullMeshObject.ReCreateCompactData(mVertexPosArray, indexMapping);
     mNormalArray    = NullMeshObject.ReCreateCompactData(mNormalArray, indexMapping);
     return(true);
 }
示例#4
0
        public bool BuildIndexedPrimitives(List <NullMergeIndex> indexMapping)
        {
            if (mUVArray.Count == 0)
            {
                return(false);
            }
            List <Vector2> newData = NullMeshObject.ReCreateCompactData(mUVArray, indexMapping);

            mUVArray = newData;
            return(true);
        }
示例#5
0
        public NullMeshObject AppendSkinObject(NullPrimitiveType meshType, int triangleCount, int vertexCount, bool includingNormal, bool includeTangent, bool includingVertexColor)
        {
            NullMeshObject mesh = null;

            switch (mWorkingMode)
            {
            case NullWorkingFlag.WF_SKELETON_MESHPIECE:
                mesh = mSkinObjectList.AppendMeshObject(meshType, triangleCount, vertexCount, includingNormal, includeTangent, includingVertexColor);
                mesh.SetMeshObjectHandle(mMeshObjectList.GetMeshObjectCount() + mSkinObjectList.GetMeshObjectIndex(mesh));
                break;
            }
            return(mesh);
        }
示例#6
0
        public int GetMeshObjectIndex(NullMeshObject mesh)
        {
            int index = 0xffff;

            for (int i = 0; i < MeshObjectList.Count; i++)
            {
                if (mesh == MeshObjectList[i])
                {
                    index = i;
                    break;
                }
            }
            return(index);
        }
示例#7
0
 public void DoAutoCenter(Vector3 center)
 {
     for (int i = 0; i < MeshObjectList.Count; i++)
     {
         NullMeshObject meshObject = MeshObjectList[i];
         for (int j = 0; j < meshObject.GetVertexCount(); j++)
         {
             Vector3 v = Vector3.zero;
             meshObject.GetVertex(j, ref v);
             v -= center;
             meshObject.SetVertex(j, v);
         }
     }
 }
示例#8
0
        public void GetCenter(ref Vector3 result)
        {
            result = Vector3.zero;
            int totalCount = 0;

            for (int i = 0; i < MeshObjectList.Count; i++)
            {
                NullMeshObject meshObject = MeshObjectList[i];
                meshObject.GetCenter(ref result);
                totalCount += meshObject.GetVertexCount();
            }
            if (totalCount == 0)
            {
                return;
            }
            result = result * (1.0f / totalCount);
        }
示例#9
0
        public bool BuildIndexedPrimitives()
        {
            bool res = false;

            if (mMeshObjectList != null)
            {
                List <NullMergeIndex> indexMapping = new List <NullMergeIndex>();
                for (int i = 0; i < mMeshObjectList.GetMeshObjectCount(); i++)
                {
                    NullMeshObject meshObject = mMeshObjectList[i];
                    res |= meshObject.BuildIndexedPrimitives(indexMapping);
                    if (meshObject.IsVertexAnimationMeshObject() && mVertexMorphAnimations != null)
                    {
                        for (int k = 0; k < mVertexMorphAnimations.GetAnimationCount(); k++)
                        {
                            NullVertexMorphAnimation animation = mVertexMorphAnimations[k];
                            for (int j = 0; j < animation.GetFrameCount(); j++)
                            {
                                NullVertexMorphAnimationFrame animationFrame = animation[j];
                                NullVertexMorphObject         morphObject    = animationFrame.FindMorphAnimationNodeByIndex(i);
                                Assert.IsTrue(morphObject != null, "");
                                res |= morphObject.BuildIndexedPrimitives(indexMapping);
                            }
                        }
                    }
                }
            }

            //try extract mesh-objects and skeleton-bindings
            if (mSkinObjectList != null)
            {
                for (int i = 0; i < mSkinObjectList.GetMeshObjectCount(); i++)
                {
                    List <NullMergeIndex> indexMapping = new List <NullMergeIndex>();
                    NullMeshObject        meshObject   = mSkinObjectList[i];
                    res |= meshObject.BuildIndexedPrimitives(indexMapping);
                    if (mSkeletonBinding != null && indexMapping.Count > 0)
                    {
                        NullSkeletonPiece skeletonPiece = mSkeletonBinding[i];
                        res |= skeletonPiece.BuildIndexedPrimitives(indexMapping);
                    }
                }
            }
            return(res);
        }
示例#10
0
        protected bool RotateMeshObjects(NullMeshObjects meshObjects, float angle)
        {
            if (meshObjects == null || meshObjects.GetVertexCount() == 0)
            {
                return(false);
            }
            int triangleCount = meshObjects.GetTriangleCount();

            if (triangleCount > 0)
            {
                //preparing data
                List <Vector3> vertices    = new List <Vector3>();
                List <Vector3> newVertices = new List <Vector3>();
                for (int i = 0; i < meshObjects.GetMeshObjectCount(); i++)
                {
                    NullMeshObject meshObject = meshObjects[i];
                    int            count      = meshObject.GetTriangleCount();
                    vertices.AddRange(meshObject.GetVertexData());
                }

                //do rotate calculation
                Vector3RotateCalculation(angle, vertices, newVertices, false);

                //update mesh objects vertices
                int index = 0;
                for (int i = 0; i < meshObjects.GetMeshObjectCount(); i++)
                {
                    NullMeshObject meshObject = meshObjects[i];
                    int            count      = meshObject.GetTriangleCount();
                    for (int j = 0; j < count; j++)
                    {
                        meshObject.SetVertex(j * 3 + 0, newVertices[index++]);
                        meshObject.SetVertex(j * 3 + 1, newVertices[index++]);
                        meshObject.SetVertex(j * 3 + 2, newVertices[index++]);
                    }
                }
                //delete temp buffer
                vertices.Clear();
                newVertices.Clear();
            }
            return(true);
        }
示例#11
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);
            }
        }
示例#12
0
        public bool ExtractToTriangles()
        {
            bool res = false;

            //try extract mesh-objects and vertex-morph animations
            if (mMeshObjectList != null)
            {
                for (int i = 0; i < mMeshObjectList.GetMeshObjectCount(); i++)
                {
                    List <Vector3Int> originalFaceIndices = new List <Vector3Int>();
                    NullMeshObject    meshObject          = mMeshObjectList[i];
                    res |= meshObject.ExtractToTriangles(originalFaceIndices);
                    if (meshObject.IsVertexAnimationMeshObject() && mVertexMorphAnimations != null)
                    {
                        for (int k = 0; k < mVertexMorphAnimations.GetAnimationCount(); k++)
                        {
                            NullVertexMorphAnimation animation = mVertexMorphAnimations[k];
                            for (int j = 0; j < animation.GetFrameCount(); j++)
                            {
                                NullVertexMorphAnimationFrame animationFrame = animation[j];
                                NullVertexMorphObject         morphObject    = animationFrame.FindMorphAnimationNodeByIndex(i);
                                Assert.IsTrue(morphObject != null, "");
                                switch (meshObject.GetMeshObjectType())
                                {
                                case NullPrimitiveType.MOT_INDEXED_PRIMITIVES:
                                    res |= morphObject.ExtractToTrianglesFromIndexedPrimitives(originalFaceIndices);
                                    break;
                                    //case NullPrimitiveType.MOT_TRIANGLE_STRIPS:
                                    //    res |= morphObject.ExtractToTrianglesFromStrips(meshObject.GetTriangleCount());
                                    //    break;
                                }
                            }
                        }
                    }
                    originalFaceIndices.Clear();
                }
            }

            //try extract mesh-objects and skeleton-bindings
            if (mSkinObjectList != null)
            {
                for (int i = 0; i < mSkinObjectList.GetMeshObjectCount(); i++)
                {
                    List <Vector3Int> originalFaceIndices = new List <Vector3Int>();
                    NullMeshObject    meshObject          = mSkinObjectList[i];
                    res |= meshObject.ExtractToTriangles(originalFaceIndices);
                    if (mSkeletonBinding != null && mSkeletonBinding.GetSkeletonBindingCount() > 0)
                    {
                        NullSkeletonPiece skeletonPiece = mSkeletonBinding[i];
                        switch (meshObject.GetMeshObjectType())
                        {
                        case NullPrimitiveType.MOT_INDEXED_PRIMITIVES:
                            res |= skeletonPiece.ExtractToTrianglesFromIndexedPrimitives(originalFaceIndices);
                            break;
                            //case HexMeshObject::MOT_TRIANGLE_STRIPS:
                            //    res |= skeletonPiece.ExtractToTrianglesFromStrips(meshObject.GetTriangleCount());
                            //    break;
                        }
                    }
                }
            }

            return(res);
        }
示例#13
0
        protected bool BuildTangentForMeshObjects(NullMeshObjects meshObjects, float thresHold, bool forceSmooth = false)
        {
            if (meshObjects == null || meshObjects.GetMeshObjectCount() == 0)
            {
                return(false);
            }

            int triangleCount = meshObjects.GetTriangleCount();

            if (triangleCount > 0)
            {
                //preparing data
                List <Vector3> vertices = new List <Vector3>();
                List <Vector2> uvs      = new List <Vector2>();

                List <Vector3> tangents          = Make <Vector3>(triangleCount * 3);
                List <Vector3> binormals         = Make <Vector3>(triangleCount * 3);
                List <byte>    smoothGroups      = Make <byte>(triangleCount);
                int            realTriangleCount = 0;

                for (int i = 0; i < meshObjects.GetMeshObjectCount(); i++)
                {
                    NullMeshObject meshObject = meshObjects[i];
                    if (meshObject.GetUVGroups().Count == 0 || meshObject.GetNormalData().Count == 0)
                    {
                        continue;
                    }
                    int count = meshObject.GetTriangleCount();
                    vertices.AddRange(meshObject.GetVertexData());
                    byte smoothGroup = meshObject.GetSmoothGroup();

                    if (forceSmooth && smoothGroup == 0)
                    {
                        smoothGroup = 1;
                    }
                    Set(smoothGroups, smoothGroup);
                    NullUVGroup uvGroup = meshObject.GetUVGroup(UVType.UVT_NORMAL_MAP);
                    if (uvGroup == null)
                    {
                        uvGroup = meshObject.GetUVGroup(UVType.UVT_DEFAULT);
                    }
                    uvs.AddRange(uvGroup.GetUVData());
                    realTriangleCount += count;
                }
                //do face-smoothing
                ComputeTengents(thresHold, vertices, realTriangleCount, smoothGroups, uvs, tangents, binormals);

                //update mesh objects normals
                for (int i = 0; i < meshObjects.GetMeshObjectCount(); i++)
                {
                    NullMeshObject meshObject = meshObjects[i];
                    if (meshObject.GetUVGroups().Count == 0 || meshObject.GetNormalData().Count == 0)
                    {
                        continue;
                    }
                    meshObject.BuildTangentArray();
                    int count = meshObject.GetTriangleCount();
                    for (int j = 0; j < count; j++)
                    {
                        //set tangent
                        meshObject.SetTangent(j * 3 + 0, tangents[j * 3]);
                        meshObject.SetTangent(j * 3 + 1, tangents[j * 3 + 1]);
                        meshObject.SetTangent(j * 3 + 2, tangents[j * 3 + 2]);

                        //set binormal
                        meshObject.SetBinormal(j * 3 + 0, binormals[j * 3]);
                        meshObject.SetBinormal(j * 3 + 1, binormals[j * 3 + 1]);
                        meshObject.SetBinormal(j * 3 + 2, binormals[j * 3 + 2]);
                    }
                }
                //delete temp buffer
                vertices.Clear();
                tangents.Clear();
                binormals.Clear();
                uvs.Clear();
                smoothGroups.Clear();
            }
            return(true);
        }