void tUpdate()
 {
     foreach (Object targetObject in targets)
     {
         MeshSimplify meshSimplify = targetObject as MeshSimplify;
         //+ADD添加应用Job
         for (int ichild = 0; ichild < meshSimplify.transform.childCount; ichild++)
         {
             Transform    childTrans = meshSimplify.transform.GetChild(ichild);
             MeshSimplify simplify   = childTrans.GetComponent <MeshSimplify>();
             if (simplify != null && simplify.GetSimplifyComputeHeap() != null)
             {
                 ComputeHeap computeHeap = simplify.GetSimplifyComputeHeap();
                 if (!computeHeap.IsHandle())
                 {
                     EditorUtility.DisplayProgressBar("ComputeMeshJobExcute", childTrans.gameObject.name, 0f);
                 }
                 if (computeHeap.GetJobHandleIsComplete() && !computeHeap.IsHandle())
                 {
                     EditorUtility.DisplayDialog("Done", "Done", "Done");
                     EditorUtility.ClearProgressBar();
                     computeHeap.JobComputeComplete();
                 }
             }
         }
     }
 }
 void Update()
 {
     if (isClickButton)
     {
         for (int i = 0; i < computeMeshGoList.Count; i++)
         {
             EditorUtility.DisplayProgressBar("OnComputeMesh", "mesh|" + computeMeshGoList[i] + " " + index, (float)index / computeMeshGoList.Count);
             GameObject go = computeMeshGoList[i];
             if (go == null)
             {
                 continue;
             }
             for (int ichild = 0; ichild < go.transform.childCount; ichild++)
             {
                 Transform    childTrans = go.transform.GetChild(ichild);
                 MeshSimplify simplify   = childTrans.GetComponent <MeshSimplify>();
                 if (simplify != null && simplify.GetSimplifyComputeHeap() != null)
                 {
                     ComputeHeap computeHeap = simplify.GetSimplifyComputeHeap();
                     if (computeHeap.GetJobHandleIsComplete())
                     {
                         computeHeap.JobComputeComplete();
                         index++;
                     }
                 }
             }
         }
     }
     if (index != 0 && index == computeMeshGoList.Count)
     {
         isClickButton = false;
         index         = 0;
         EditorUtility.ClearProgressBar();
         Debug.Log("<color=yellow>所有的模型数据计算完成————————</color>");
     }
 }
예제 #3
0
        /////////////////////////////////////////////////////////////////////////////////////////////////
        // Public methods
        /////////////////////////////////////////////////////////////////////////////////////////////////

        public IEnumerator ProgressiveMesh(GameObject gameObject, Mesh sourceMesh, RelevanceSphere[] aRelevanceSpheres, string strProgressDisplayObjectName = "", ProgressDelegate progress = null)
        {
            computeHeapJob = new ComputeHeap();
            m_meshOriginal = sourceMesh;

            //Vector3[] aVerticesWorld = GetWorldVertices(gameObject);

            //if (aVerticesWorld == null)
            //{
            //  CoroutineEnded = true;
            //  yield break;
            //}
            int vertexCount = sourceMesh.vertexCount;

            m_aVertexMap         = new int[vertexCount];
            m_aVertexPermutation = new int[vertexCount];
            m_listVertices       = new List <Vertex>();
            m_aListTriangles     = new TriangleList[m_meshOriginal.subMeshCount];

            //                if (progress != null)
            //                {
            //                    progress("Preprocessing mesh: " + strProgressDisplayObjectName, "Building unique vertex data", 1.0f);
            //
            //                    if (Simplifier.Cancelled)
            //                    {
            //                        CoroutineEnded = true;
            //                        yield break;
            //                    }
            //                }

            //                m_meshUniqueVertices = new MeshUniqueVertices();
            //                m_meshUniqueVertices.BuildData(m_meshOriginal, gameObject);
            Vector3[]           worldVertices = new Vector3[sourceMesh.vertices.Length];
            SkinnedMeshRenderer skin;
            MeshFilter          filter;

            if ((skin = gameObject.GetComponent <SkinnedMeshRenderer>()) != null)
            {
#if UNITY_2018_1_OR_NEWER
                LocalToWorldTransformation.Transform(skin, worldVertices);
#else
                BoneWeight[] aBoneWeights = sourceMesh.boneWeights;
                Matrix4x4[]  aBindPoses   = sourceMesh.bindposes;
                Transform[]  aBones       = skin.bones;

                for (int nVertex = 0; nVertex < sourceMesh.vertices.Length; nVertex++)
                {
                    BoneWeight bw = aBoneWeights[nVertex];
                    Vector4    v  = sourceMesh.vertices [nVertex];
                    v.w = 1;
                    Vector3 v3World = aBones [bw.boneIndex0].localToWorldMatrix * aBindPoses [bw.boneIndex0] * v * bw.weight0
                                      + aBones [bw.boneIndex1].localToWorldMatrix * aBindPoses [bw.boneIndex1] * v * bw.weight1
                                      + aBones [bw.boneIndex2].localToWorldMatrix * aBindPoses [bw.boneIndex2] * v * bw.weight2
                                      + aBones [bw.boneIndex3].localToWorldMatrix * aBindPoses [bw.boneIndex3] * v * bw.weight3;

                    worldVertices [nVertex] = v3World;

                    if (progress != null && ((nVertex & 0xFF) == 0))
                    {
                        progress("Preprocessing mesh: " + strProgressDisplayObjectName, "Collecting vertex data", ((float)nVertex / (float)sourceMesh.vertices.Length));

                        if (Cancelled)
                        {
                            CoroutineEnded = true;
                            yield break;
                        }
                    }
                }
#endif
            }
            if ((filter = gameObject.GetComponent <MeshFilter>()) != null)
            {
#if UNITY_2018_1_OR_NEWER
                LocalToWorldTransformation.Transform(filter, worldVertices);
#else
                Matrix4x4 transformation = gameObject.transform.localToWorldMatrix;
                for (int nVertex = 0; nVertex < sourceMesh.vertices.Length; nVertex++)
                {
                    Vector4 v = sourceMesh.vertices [nVertex];
                    v.w = 1;
                    Vector3 v3World = transformation * v;

                    worldVertices [nVertex] = v3World;

                    if (progress != null && ((nVertex & 0xFF) == 0))
                    {
                        progress("Preprocessing mesh: " + strProgressDisplayObjectName, "Collecting vertex data", ((float)nVertex / (float)sourceMesh.vertices.Length));

                        if (Cancelled)
                        {
                            CoroutineEnded = true;
                            yield break;
                        }
                    }
                }
#endif
            }

            m_nOriginalMeshVertexCount = sourceMesh.vertexCount;//m_meshUniqueVertices.ListVertices.Count;
            m_fOriginalMeshSize        = Mathf.Max(m_meshOriginal.bounds.size.x, m_meshOriginal.bounds.size.y, m_meshOriginal.bounds.size.z);

            m_heap = Heap <Vertex> .CreateMinHeap();

            for (int i = 0; i < m_nOriginalMeshVertexCount; i++)
            {
                m_aVertexMap[i]         = -1;
                m_aVertexPermutation[i] = -1;
            }

            Vector2[] av2Mapping = m_meshOriginal.uv;

            AddVertices(sourceMesh.vertices, worldVertices, sourceMesh);

            int nTriangles = 0;
            //                ListIndices[] faceList = m_meshUniqueVertices.SubmeshesFaceList;
            for (int nSubMesh = 0; nSubMesh < m_meshOriginal.subMeshCount; nSubMesh++)
            {
                int[] anIndices = m_meshOriginal.GetTriangles(nSubMesh);
                //                    List<int> listIndices = faceList[nSubMesh].m_listIndices;
                m_aListTriangles[nSubMesh] = new TriangleList(anIndices.Length / 3);
                nTriangles = AddFaceListSubMesh(nSubMesh, anIndices, av2Mapping, nTriangles);
            }

            if (Application.isEditor && !Application.isPlaying)
            {
#if UNITY_2018_1_OR_NEWER
                float[] costs     = new float[m_listVertices.Count];
                int[]   collapses = new int[m_listVertices.Count];
                CostCompution.Compute(m_listVertices, m_aListTriangles, aRelevanceSpheres, m_bUseEdgeLength, m_bUseCurvature, BorderCurvature, m_fOriginalMeshSize, costs, collapses);

                computeHeapJob.Compute(m_listVertices, m_meshOriginal, m_aListTriangles, aRelevanceSpheres, costs, collapses, m_aVertexPermutation, m_aVertexMap, m_bUseEdgeLength, m_bUseCurvature, BorderCurvature, m_fOriginalMeshSize);
#else
                IEnumerator enumerator = ComputeAllEdgeCollapseCosts(strProgressDisplayObjectName, gameObject.transform, aRelevanceSpheres, progress);

                while (enumerator.MoveNext())
                {
                    if (Simplifier.Cancelled)
                    {
                        CoroutineEnded = true;
                        yield break;
                    }
                }

                for (int i = 0; i < m_listVertices.Count; i++)
                {
                    Vertex v = m_listVertices[i];
                    v.m_fObjDist = costs[i];
                    v.m_collapse = collapses[i] == -1 ? null : m_listVertices[collapses[i]];
                    m_heap.Insert(v);
                }
                int vertexNum = m_listVertices.Count;
                while (vertexNum-- > 0)
                {
                    //               if (progress != null && ((vertexNum & 0xFF) == 0))
                    //               {
                    //                   progress("Preprocessing mesh: " + strProgressDisplayObjectName, "Collapsing edges", 1.0f - ((float)vertexNum / (float)nVertices));

                    //                   if (Cancelled)
                    //                   {
                    //                       CoroutineEnded = true;
                    //                       yield break;
                    //                   }
                    //               }

                    //               if (sw.ElapsedMilliseconds > CoroutineFrameMiliseconds && CoroutineFrameMiliseconds > 0)
                    //               {
                    //                   yield return null;
                    //                   sw = Stopwatch.StartNew();
                    //}
                    Vertex mn = m_heap.ExtractTop();

                    //                    m_listVertexPermutationBack[m_listVertices.Count - 1] = mn.m_nID;
                    m_aVertexPermutation[mn.m_nID] = vertexNum;
                    m_aVertexMap[mn.m_nID]         = mn.m_collapse != null ? mn.m_collapse.m_nID : -1;
                    Collapse(mn, mn.m_collapse, gameObject.transform, aRelevanceSpheres);
                }
#endif
            }
            else
            {
#if UNITY_2018_1_OR_NEWER
                float[] costs     = new float[m_listVertices.Count];
                int[]   collapses = new int[m_listVertices.Count];
                CostCompution.Compute(m_listVertices, m_aListTriangles, aRelevanceSpheres, m_bUseEdgeLength, m_bUseCurvature, BorderCurvature, m_fOriginalMeshSize, costs, collapses);

                computeHeapJob.Compute(m_listVertices, m_meshOriginal, m_aListTriangles, aRelevanceSpheres, costs, collapses, m_aVertexPermutation, m_aVertexMap, m_bUseEdgeLength, m_bUseCurvature, BorderCurvature, m_fOriginalMeshSize);
#else
                yield return(StartCoroutine(ComputeAllEdgeCollapseCosts(strProgressDisplayObjectName, gameObject.transform, aRelevanceSpheres, progress)));

                for (int i = 0; i < m_listVertices.Count; i++)
                {
                    Vertex v = m_listVertices[i];
                    v.m_fObjDist = costs[i];
                    v.m_collapse = m_listVertices[collapses[i]];
                    m_heap.Insert(v);
                }
                int vertexNum = m_listVertices.Count;
                while (vertexNum-- > 0)
                {
                    //               if (progress != null && ((vertexNum & 0xFF) == 0))
                    //               {
                    //                   progress("Preprocessing mesh: " + strProgressDisplayObjectName, "Collapsing edges", 1.0f - ((float)vertexNum / (float)nVertices));

                    //                   if (Cancelled)
                    //                   {
                    //                       CoroutineEnded = true;
                    //                       yield break;
                    //                   }
                    //               }

                    //               if (sw.ElapsedMilliseconds > CoroutineFrameMiliseconds && CoroutineFrameMiliseconds > 0)
                    //               {
                    //                   yield return null;
                    //                   sw = Stopwatch.StartNew();
                    //}
                    Vertex mn = m_heap.ExtractTop();

                    //                    m_listVertexPermutationBack[m_listVertices.Count - 1] = mn.m_nID;
                    m_aVertexPermutation[mn.m_nID] = vertexNum;
                    m_aVertexMap[mn.m_nID]         = mn.m_collapse != null ? mn.m_collapse.m_nID : -1;
                    Collapse(mn, mn.m_collapse, gameObject.transform, aRelevanceSpheres);
                }
#endif
            }

            //int nVertices = m_listVertices.Count;

            //Stopwatch sw = Stopwatch.StartNew();


            //for (int nSubMesh = 0; nSubMesh < m_aListTriangles.Length; nSubMesh++)
            //{
            //    m_aListTriangles[nSubMesh].RemoveNull();
            //}
            CoroutineEnded = true;
            yield return(null);
        }