예제 #1
0
    private static void ComputeMeshRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren, Simplifier.ProgressDelegate progress = null)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                if (meshSimplify.m_meshSimplifier != null)
                {
                    if (meshSimplify.m_simplifiedMesh)
                    {
                        meshSimplify.m_simplifiedMesh.Clear();
                    }

                    float fAmount = meshSimplify.m_fVertexAmount;

                    if (meshSimplify.m_bOverrideRootSettings == false && meshSimplify.m_meshSimplifyRoot != null)
                    {
                        fAmount = meshSimplify.m_meshSimplifyRoot.m_fVertexAmount;
                    }

                    if (meshSimplify.m_simplifiedMesh == null)
                    {
                        meshSimplify.m_simplifiedMesh = new Mesh();
                    }

                    meshSimplify.ConfigureSimplifier();

                    IEnumerator enumerator = meshSimplify.m_meshSimplifier.ComputeMeshWithVertexCount(gameObject, meshSimplify.m_simplifiedMesh, Mathf.RoundToInt(fAmount * meshSimplify.m_meshSimplifier.GetOriginalMeshUniqueVertexCount()), meshSimplify.name + " Simplified", progress);

                    while (enumerator.MoveNext())
                    {
                        if (Simplifier.Cancelled)
                        {
                            return;
                        }
                    }

                    if (Simplifier.Cancelled)
                    {
                        return;
                    }
                }
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                ComputeMeshRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren, progress);

                if (Simplifier.Cancelled)
                {
                    return;
                }
            }
        }
    }
    private static void ComputeDataRecursive(MeshSimplify root, GameObject gameObject, bool bRecurseIntoChildren, Simplifier.ProgressDelegate progress = null)
    {
        MeshSimplify meshSimplify = gameObject.GetComponent <MeshSimplify>();

        if (meshSimplify == null && root.m_bGenerateIncludeChildren)
        {
            if (MeshUtil.HasValidMeshData(gameObject))
            {
                meshSimplify = gameObject.AddComponent <MeshSimplify>();
                meshSimplify.m_meshSimplifyRoot = root;
                root.m_listDependentChildren.Add(meshSimplify);
            }
        }

        if (meshSimplify != null)
        {
            if (IsRootOrBelongsToTree(meshSimplify, root))
            {
                meshSimplify.FreeData(false);

                MeshFilter meshFilter = meshSimplify.GetComponent <MeshFilter>();

                if (meshFilter != null && meshFilter.sharedMesh != null)
                {
                    if (meshFilter.sharedMesh.vertexCount > 0)
                    {
                        if (meshSimplify.m_originalMesh == null)
                        {
                            meshSimplify.m_originalMesh = meshFilter.sharedMesh;
                        }

                        Simplifier[] simplifiers = meshSimplify.GetComponents <Simplifier>();

                        for (int c = 0; c < simplifiers.Length; c++)
                        {
                            if (Application.isEditor && Application.isPlaying == false)
                            {
                                DestroyImmediate(simplifiers[c]);
                            }
                            else
                            {
                                Destroy(simplifiers[c]);
                            }
                        }

                        meshSimplify.m_meshSimplifier           = meshSimplify.gameObject.AddComponent <Simplifier>();
                        meshSimplify.m_meshSimplifier.hideFlags = HideFlags.HideInInspector;
                        meshSimplify.ConfigureSimplifier();

                        IEnumerator enumerator = meshSimplify.m_meshSimplifier.ProgressiveMesh(gameObject, meshSimplify.m_originalMesh, root.m_aRelevanceSpheres, meshSimplify.name, progress);

                        while (enumerator.MoveNext())
                        {
                            if (Simplifier.Cancelled)
                            {
                                return;
                            }
                        }

                        if (Simplifier.Cancelled)
                        {
                            return;
                        }
                    }
                }
                else
                {
                    SkinnedMeshRenderer skin = meshSimplify.GetComponent <SkinnedMeshRenderer>();

                    if (skin != null)
                    {
                        if (skin.sharedMesh.vertexCount > 0)
                        {
                            if (meshSimplify.m_originalMesh == null)
                            {
                                meshSimplify.m_originalMesh = skin.sharedMesh;
                            }

                            Simplifier[] simplifiers = meshSimplify.GetComponents <Simplifier>();

                            for (int c = 0; c < simplifiers.Length; c++)
                            {
                                if (Application.isEditor && Application.isPlaying == false)
                                {
                                    DestroyImmediate(simplifiers[c]);
                                }
                                else
                                {
                                    Destroy(simplifiers[c]);
                                }
                            }

                            meshSimplify.m_meshSimplifier           = meshSimplify.gameObject.AddComponent <Simplifier>();
                            meshSimplify.m_meshSimplifier.hideFlags = HideFlags.HideInInspector;
                            meshSimplify.ConfigureSimplifier();

                            IEnumerator enumerator = meshSimplify.m_meshSimplifier.ProgressiveMesh(gameObject, meshSimplify.m_originalMesh, root.m_aRelevanceSpheres, meshSimplify.name, progress);

                            while (enumerator.MoveNext())
                            {
                                if (Simplifier.Cancelled)
                                {
                                    return;
                                }
                            }

                            if (Simplifier.Cancelled)
                            {
                                return;
                            }
                        }
                    }
                }

                meshSimplify.m_bDataDirty = false;
            }
        }

        if (bRecurseIntoChildren)
        {
            for (int nChild = 0; nChild < gameObject.transform.childCount; nChild++)
            {
                ComputeDataRecursive(root, gameObject.transform.GetChild(nChild).gameObject, bRecurseIntoChildren, progress);

                if (Simplifier.Cancelled)
                {
                    return;
                }
            }
        }
    }
예제 #3
0
    private IEnumerator ComputeMeshWithVertices(float fAmount)
    {
        Simplifier.CoroutineFrameMiliseconds = 20;

        foreach (KeyValuePair <GameObject, Material[]> pair in m_objectMaterials)
        {
            MeshSimplify        meshSimplify = pair.Key.GetComponent <MeshSimplify>();
            MeshFilter          meshFilter   = pair.Key.GetComponent <MeshFilter>();
            SkinnedMeshRenderer skin         = pair.Key.GetComponent <SkinnedMeshRenderer>();

            if (meshSimplify == null)
            {
                meshSimplify = pair.Key.AddComponent <MeshSimplify>();
                meshSimplify.m_meshSimplifyRoot = m_selectedMeshSimplify;
                m_selectedMeshSimplify.m_listDependentChildren.Add(meshSimplify);
            }

            if (meshSimplify.MeshSimplifier == null)
            {
                meshSimplify.MeshSimplifier           = meshSimplify.gameObject.AddComponent <Simplifier>();
                meshSimplify.MeshSimplifier.hideFlags = HideFlags.HideInInspector;
                meshSimplify.ConfigureSimplifier();
            }

            if (meshSimplify && MeshSimplify.HasValidMeshData(pair.Key))
            {
                Mesh newMesh = null;

                if (meshFilter != null)
                {
                    newMesh = Mesh.Instantiate(meshFilter.sharedMesh);
                }
                else if (skin != null)
                {
                    newMesh = Mesh.Instantiate(skin.sharedMesh);
                }

                if (meshSimplify.HasData() == false)
                {
                    meshSimplify.GetMeshSimplifier().CoroutineEnded = false;

                    StartCoroutine(meshSimplify.GetMeshSimplifier().ProgressiveMesh(pair.Key, meshFilter != null ? meshFilter.sharedMesh : skin.sharedMesh, null, meshSimplify.name, Progress));

                    while (meshSimplify.GetMeshSimplifier().CoroutineEnded == false)
                    {
                        yield return(null);
                    }
                }

                if (meshSimplify.GetMeshSimplifier() != null)
                {
                    meshSimplify.GetMeshSimplifier().CoroutineEnded = false;

                    StartCoroutine(meshSimplify.GetMeshSimplifier().ComputeMeshWithVertexCount(pair.Key, newMesh, Mathf.RoundToInt(fAmount * meshSimplify.GetMeshSimplifier().GetOriginalMeshUniqueVertexCount()), meshSimplify.name, Progress));

                    while (meshSimplify.GetMeshSimplifier().CoroutineEnded == false)
                    {
                        yield return(null);
                    }

                    if (meshFilter != null)
                    {
                        meshFilter.mesh = newMesh;
                    }
                    else if (skin != null)
                    {
                        skin.sharedMesh = newMesh;
                    }

                    meshSimplify.m_simplifiedMesh = newMesh;
                }
            }
        }

        m_bFinished = true;
    }