private void RecalculateNormal()
        {
            int n = myTriangles.Length;

            for (int i = 0; i < n; ++i)
            {
                Tri f = myTriangles[i];
                if (f.deleted)
                {
                    continue;
                }
                f.RecalculateAvgNormals(smoothAngleDot);
            }
        }
        private void ComputeProgressiveMesh()
        {
            int i;
            int j;
            int n;
            Tri t;

            int vertexCount   = sharedVertices.Length;
            int triangleCount = sharedTriangles.Length;

            System.Array.Clear(myLODVertices, 0, myLODVertices.Length);
            for (i = 0; i < vertexCount; ++i)
            {
                Vector3 dv  = sharedVertices[i];
                bool    sel = false;

                n = selectedVertices.Count;
                for (j = 0; j < n; ++j)
                {
                    if (selectedVertices[j] == dv)
                    {
                        sel = true;
                        break;
                    }
                }
                myLODVertices[i] = new Vert(dv, i, sel);
            }

            // new myTris
            System.Array.Clear(myTriangles, 0, myTriangles.Length);
            int cnt = 0;

            for (i = 0; i < triangleCount; i += 3)
            {
                t = new Tri(cnt,
                            myLODVertices[sharedTriangles[i]],
                            myLODVertices[sharedTriangles[i + 1]],
                            myLODVertices[sharedTriangles[i + 2]],
                            originalUVs[originalTriangles[i]],
                            originalUVs[originalTriangles[i + 1]],
                            originalUVs[originalTriangles[i + 2]]);

                t.SetDefaultIndices(originalTriangles[i], originalTriangles[i + 1], originalTriangles[i + 2]);
                if (bRecalculateNormals)
                {
                    t.vn0 = t.vn1 = t.vn2 = t.normal;
                }
                else
                {
                    t.vn0 = originalNormals[originalTriangles[i]];
                    t.vn1 = originalNormals[originalTriangles[i + 1]];
                    t.vn2 = originalNormals[originalTriangles[i + 2]];
                }

                myTriangles[cnt] = t;
                ++cnt;
            }

            cache     = new List <Vert>();
            cacheSize = vertexCount;

            if (bRecalculateNormals)
            {
                RecalculateNormal();       // set normals for vertex.
            }
            ComputeAllEdgeCollapseCosts(); // cache all edge collapse costs
            //System.Array.Sort(cache, myComparer); // lower cost to the left.
            cache = cache.OrderBy(p => p.cost).ToList();

            collapseHistory = new History[vertexCount];

            currentcnt  = myLODVertices.Length + 1;
            searchIndex = 0;
            while (--currentcnt > 0)
            {
                CollapseTest();
            }


            // LOD Data size calculation
            n = collapseHistory.Length;
            int     tmpBytes = 0;
            History tmpHis;

            for (i = 0; i < n; ++i)
            {
                tmpHis    = collapseHistory[i];
                tmpBytes += (
                    tmpHis.removedTriangles.Count * 2 +
                    tmpHis.replacedVertex.Count * 14
                    ) * 4;
            }
            lodDataSize = tmpBytes;
        }
Beispiel #3
0
 public void RemoveFace(Tri f)
 {
     face.Remove(f);
 }
        public void Calculate(Mesh tmpMesh)
        {
            ProgressiveMesh(ratio);

            int     i;
            int     j;
            int     foundAt = -1;
            Vector3 v       = new Vector3();
            Vector3 vn      = new Vector3();
            Vector3 dvn     = new Vector3();
            Vector2 vuv     = new Vector2();
            //History his = new History();

            int cnt        = 0;
            int vertsCount = myLODVertices.Length;
            int trisCount  = myTriangles.Length;

            int reducedTriCount = 0;

            foreach (Tri t in myTriangles)
            {
                if (t.deleted)
                {
                    continue;
                }
                ++reducedTriCount;
            }

            int minTriCount = reducedTriCount * 3;

            int[]     tris    = new int[minTriCount];
            Vector3[] verts   = new Vector3[minTriCount];
            Vector2[] uvs     = new Vector2[minTriCount];
            Vector3[] norms   = new Vector3[minTriCount];
            int[]     indices = new int[minTriCount];

            for (i = 0; i < reducedTriCount; ++i)
            {
                Tri tri = myTriangles[triOrder[i]];

                int  cnt1 = cnt + 1;
                int  cnt2 = cnt + 2;
                Vert v0   = tri.v0;
                Vert v1   = tri.v1;
                Vert v2   = tri.v2;

                verts[cnt]  = v0.position;
                verts[cnt1] = v1.position;
                verts[cnt2] = v2.position;
                tris[cnt]   = cnt;
                tris[cnt1]  = cnt1;
                tris[cnt2]  = cnt2;
                uvs[cnt]    = tri.uv0;
                uvs[cnt1]   = tri.uv1;
                uvs[cnt2]   = tri.uv2;
                norms[cnt]  = tri.vn0;
                norms[cnt1] = tri.vn1;
                norms[cnt2] = tri.vn2;

                indices[cnt]  = tri.defaultIndex0;
                indices[cnt1] = tri.defaultIndex1;
                indices[cnt2] = tri.defaultIndex2;

                cnt += 3;
            }

            int            triNum      = tris.Length;
            List <Vector3> newVertices = new List <Vector3>();
            List <Vector2> newUVs      = new List <Vector2>();
            List <Vector3> newNormals  = new List <Vector3>();
            List <Vector3> newDNormals = new List <Vector3>();

            if (bRecalculateNormals)
            {
                for (i = 0; i < triNum; ++i)
                {
                    v   = verts[i];
                    vuv = uvs[i];
                    vn  = norms[i];
                    var n = newVertices.Count;
                    foundAt = -1;
                    for (j = 0; j < n; ++j)
                    {
                        if (newVertices[j] == v && newUVs[j] == vuv &&
                            Vector3.Dot(newNormals[j], vn) > smoothAngleDot)
                        {
                            foundAt = j; break;
                        }
                    }

                    if (foundAt != -1)
                    {
                        tris[i] = foundAt;
                    }
                    else
                    {
                        tris[i]        = n;
                        newVertices[n] = v;
                        newUVs[n]      = vuv;
                        newNormals[n]  = vn;
                        newDNormals[n] = dvn;
                    }
                }
            }
            else
            {
                for (i = 0; i < triNum; ++i)
                {
                    v   = verts[i];
                    vuv = uvs[i];
                    vn  = norms[i];
                    dvn = originalNormals[indices[i]];
                    int n = newVertices.Count;
                    foundAt = -1;
                    for (j = 0; j < n; ++j)
                    {
                        if (newVertices[j] == v && newUVs[j] == vuv && newDNormals[j] == dvn)
                        {
                            foundAt = j; break;
                        }
                    }

                    if (foundAt != -1)
                    {
                        tris[i] = foundAt;
                    }
                    else
                    {
                        tris[i] = n;
                        newVertices.Insert(n, v);
                        newUVs.Insert(n, vuv);
                        newNormals.Insert(n, vn);
                        newDNormals.Insert(n, dvn);
                    }
                }
            }

            finalVertices  = newVertices.ToArray();
            finalNormals   = newNormals.ToArray();
            finalUVs       = newUVs.ToArray();
            finalTriangles = tris;
        }
Beispiel #5
0
 public void AddFace(Tri f)
 {
     face.Add(f);
 }