示例#1
0
        /// <summary>
        /// Creates and returns a list of vertex groups for the specified mesh.
        /// </summary>
        public static List <MeshVertexGroup> Create(Mesh mesh)
        {
            if (!mesh.isReadable)
            {
                return(new List <MeshVertexGroup>());
            }

            // These variables holds the number of groups per world unit. It's probably worth
            // experimenting with these values, but there is no correct value that you can set.
            // The bigger the values, the bigger the number of vertices which can exist in one
            // group. The smaller the value, the bigger the number of vertex groups. It all
            // depends on the kind of meshes you are dealing with. Setting this to 2, seems to
            // provide reasonably good results.
            const float numberOfGroupsPerWorldUnitX = 2.0f;
            const float numberOfGroupsPerWorldUnitY = 2.0f;
            const float numberOfGroupsPerWorldUnitZ = 2.0f;

            // Cache needed data
            Bounds  meshBounds     = mesh.bounds;
            Vector3 meshBoundsSize = meshBounds.size;

            Vector3[] meshVertices = mesh.vertices;

            // Calculate the vertec group size on all axes
            float vertexGroupSizeX = meshBoundsSize.x / numberOfGroupsPerWorldUnitX;
            float vertexGroupSizeY = meshBoundsSize.y / numberOfGroupsPerWorldUnitY;
            float vertexGroupSizeZ = meshBoundsSize.z / numberOfGroupsPerWorldUnitZ;

            var vertexGroupIndexMappings = new Dictionary <VertexGroupIndices, MeshVertexGroup>();

            for (int vIndex = 0; vIndex < meshVertices.Length; ++vIndex)
            {
                Vector3 vertex = meshVertices[vIndex];

                int groupIndexX = Mathf.FloorToInt(vertex.x / vertexGroupSizeX);
                int groupIndexY = Mathf.FloorToInt(vertex.y / vertexGroupSizeY);
                int groupIndexZ = Mathf.FloorToInt(vertex.z / vertexGroupSizeZ);

                VertexGroupIndices vertGroupIndices = new VertexGroupIndices(groupIndexX, groupIndexY, groupIndexZ);
                if (vertexGroupIndexMappings.ContainsKey(vertGroupIndices))
                {
                    vertexGroupIndexMappings[vertGroupIndices].AddVertex(vertex);
                }
                else
                {
                    MeshVertexGroup meshVertexGroup = new MeshVertexGroup();
                    meshVertexGroup.AddVertex(vertex);
                    vertexGroupIndexMappings.Add(vertGroupIndices, meshVertexGroup);
                }
            }
            if (vertexGroupIndexMappings.Count == 0)
            {
                return(new List <MeshVertexGroup>());
            }

            var meshVertexGroups = new List <MeshVertexGroup>(vertexGroupIndexMappings.Count);

            foreach (var pair in vertexGroupIndexMappings)
            {
                MeshVertexGroup vertGroup = pair.Value;
                vertGroup.Close();

                meshVertexGroups.Add(vertGroup);
            }

            return(meshVertexGroups);
        }
示例#2
0
        public Geoset1300(BinaryReader br)
        {
            TotalSize = br.ReadUInt32();
            long end = TotalSize + br.BaseStream.Position;

            //Vertices
            if (br.HasTag("VRTX"))
            {
                NrOfVertices = br.ReadUInt32();
                for (int i = 0; i < NrOfVertices; i++)
                {
                    Vertices.Add(new CVector3(br));
                }
            }

            //Normals
            if (br.HasTag("NRMS"))
            {
                NrOfNormals = br.ReadUInt32();
                for (int i = 0; i < NrOfNormals; i++)
                {
                    Normals.Add(new CVector3(br));
                }
            }

            //TexCoords
            if (br.HasTag("UVAS"))
            {
                NrOfTexCoords = br.ReadUInt32();                 //Amount of groups
                for (int i = 0; i < NrOfNormals * NrOfTexCoords; i++)
                {
                    TexCoords.Add(new CVector2(br));
                }
            }

            //Face Group Type
            if (br.HasTag("PTYP"))
            {
                NrOfFaceTypeGroups = br.ReadUInt32();
                FaceTypes.AddRange(br.ReadBytes((int)NrOfFaceTypeGroups));
            }

            //Face Groups
            if (br.HasTag("PCNT"))
            {
                NrOfFaceGroups = br.ReadUInt32();
                for (int i = 0; i < NrOfFaceGroups; i++)
                {
                    FaceGroups.Add(br.ReadUInt32());
                }
            }

            //Indexes
            if (br.HasTag("PVTX"))
            {
                NrOfFaceVertices = br.ReadUInt32();
                for (int i = 0; i < NrOfFaceVertices / 3; i++)
                {
                    FaceVertices.Add(new CVertex(br));
                }
            }

            //Vertex Groups
            if (br.HasTag("GNDX"))
            {
                NrOfVertexGroupIndices = br.ReadUInt32();
                VertexGroupIndices.AddRange(br.ReadBytes((int)NrOfVertexGroupIndices));
            }

            //Matrix Groups
            if (br.HasTag("MTGC"))
            {
                NrOfMatrixGroups = br.ReadUInt32();
                for (int i = 0; i < NrOfMatrixGroups; i++)
                {
                    MatrixGroups.Add(br.ReadUInt32());
                }
            }

            //Matrix Indexes
            if (br.HasTag("MATS"))
            {
                NrOfMatrixIndexes = br.ReadUInt32();
                for (int i = 0; i < NrOfMatrixIndexes; i++)
                {
                    MatrixIndexes.Add(br.ReadUInt32());
                }
            }

            //Bone Indexes
            if (br.HasTag("BIDX"))
            {
                NrOfBoneIndexes = br.ReadUInt32();
                for (int i = 0; i < NrOfBoneIndexes; i++)
                {
                    BoneIndexes.Add(br.ReadUInt32());
                }
            }

            //Bone Weights
            if (br.HasTag("BWGT"))
            {
                NrOfBoneWeights = br.ReadUInt32();
                for (int i = 0; i < NrOfBoneWeights; i++)
                {
                    BoneWeights.Add(br.ReadUInt32());
                }
            }

            MaterialId     = br.ReadUInt32();
            SelectionGroup = br.ReadUInt32();
            Unselectable   = br.ReadUInt32() == 1;
            Bounds         = new CExtent(br);

            //Extents
            NrOfExtents = br.ReadUInt32();
            for (int i = 0; i < NrOfExtents; i++)
            {
                Extents.Add(new CExtent(br));
            }

            //Grouped Vertices
            for (int i = 0; i < NrOfVertices; i++)
            {
                if (!GroupedVertices.ContainsKey(VertexGroupIndices[i]))
                {
                    GroupedVertices.Add(VertexGroupIndices[i], new List <CVector3>());
                }

                GroupedVertices[VertexGroupIndices[i]].Add(Vertices[i]);
            }
        }