Exemplo n.º 1
0
        private static BzSliceEdgeResult MakeEdgeResult(PolyMeshData polyMeshData)
        {
            var result = new BzSliceEdgeResult();

            result.vertices    = polyMeshData.vertices;
            result.normals     = polyMeshData.normals;
            result.boneWeights = polyMeshData.boneWeights;
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Generete and return mesh of polygon
        /// </summary>
        public PolyMeshData GetMeshData()
        {
            if (!Created)
            {
                throw new InvalidOperationException("You cannot get mesh if Created == False");
            }

            Vector2[] polyVertices2d;
            int[]     edgeValues;
            int[]     triangles;
            OptimizeData(out polyVertices2d, out edgeValues, out triangles);

            PolyMeshData meshData = new PolyMeshData();

            // triangles
            meshData.triangles = triangles;

            // vertices
            meshData.vertices = new Vector3[edgeValues.Length];
            for (int i = 0; i < edgeValues.Length; i++)
            {
                var     index = edgeValues[i];
                Vector3 v     = _meshData.Vertices[index];
                meshData.vertices[i] = v;
            }

            // normals
            meshData.normals = new Vector3[edgeValues.Length];
            for (int i = 0; i < meshData.triangles.Length; i += 3)
            {
                int i1 = meshData.triangles[i + 0];
                int i2 = meshData.triangles[i + 1];
                int i3 = meshData.triangles[i + 2];

                var v1 = meshData.vertices[i1];
                var v2 = meshData.vertices[i2];
                var v3 = meshData.vertices[i3];

                var dir1   = v2 - v1;
                var dir2   = v3 - v1;
                var normal = Vector3.Cross(dir1.normalized, dir2.normalized);

                meshData.normals[i1] += normal;
                meshData.normals[i2] += normal;
                meshData.normals[i3] += normal;
            }

            // normalize normals
            for (int i = 0; i < meshData.normals.Length; i++)
            {
                var n = Normalize(meshData.normals[i]);
#if DEBUG
                // TODO: move it to tests
                if (n.sqrMagnitude == 0f)
                {
                    throw new InvalidOperationException("meshData.normals[i].sqrMagnitude == 0f");
                }
#endif
                meshData.normals[i] = n;
            }

            // uv
            float wMax, wMin, hMax, hMin;
            var   first2D = polyVertices2d[0];
            wMax = wMin = first2D.x;
            hMax = hMin = first2D.y;
            for (int i = 1; i < polyVertices2d.Length; ++i)
            {
                var v = polyVertices2d[i];
                if (v.x > wMax)
                {
                    wMax = v.x;
                }
                if (v.x < wMin)
                {
                    wMin = v.x;
                }
                if (v.y > hMax)
                {
                    hMax = v.y;
                }
                if (v.y < hMin)
                {
                    hMin = v.y;
                }
            }
            float sizeX = wMax - wMin;
            float sizeY = hMax - hMin;
            float scale = Mathf.Max(sizeX, sizeY);

            Vector2 vMin = new Vector2(
                wMin + (sizeX - scale) / 2,
                hMin + (sizeY - scale) / 2);

            meshData.uv = new Vector2[polyVertices2d.Length];
            for (int i = 0; i < polyVertices2d.Length; ++i)
            {
                var v = polyVertices2d[i] - vMin;

                meshData.uv[i] = new Vector2(v.x / scale, v.y / scale);
            }

            // bone weights
            if (_meshData.BoneWeightsExists)
            {
                meshData.boneWeights = new BoneWeight[edgeValues.Length];
                for (int i = 0; i < edgeValues.Length; i++)
                {
                    int        index = edgeValues[i];
                    BoneWeight bw    = _meshData.BoneWeights[index];
                    meshData.boneWeights[i] = bw;
                }
            }
            else
            {
                meshData.boneWeights = new BoneWeight[0];
            }

            return(meshData);
        }