예제 #1
0
    private void _CalculateBounds()
    {
        Bounds bounds            = new Bounds();
        int    numLODs           = LODs.Count;
        bool   boundsInitialized = false;

        for (int LODindex = 0; LODindex < numLODs; ++LODindex)
        {
            LODSet lodSet     = LODs[LODindex];
            int    numObjects = lodSet.LODEntries.Count;
            for (int objectIndex = 0; objectIndex < numObjects; ++objectIndex)
            {
                GameObject lodObject   = lodSet.LODEntries[objectIndex].gameObject;
                Renderer   lodRenderer = lodObject ? lodObject.GetComponent <Renderer>() : null;
                if (lodRenderer && lodRenderer.bounds.extents != Vector3.zero)
                {
                    if (!boundsInitialized)
                    {
                        bounds            = lodRenderer.bounds;
                        boundsInitialized = true;
                    }
                    else
                    {
                        bounds.Encapsulate(lodRenderer.bounds);
                    }
                }
            }
        }
        boundsOffset  = transform.worldToLocalMatrix.MultiplyPoint(bounds.center);
        boundsRadius  = bounds.extents.magnitude;
        boundsUpdated = true;
    }
예제 #2
0
    void _ActivateLOD(int lodIndex)
    {
        toHide.Clear();
        toShow.Clear();

        if (activeLOD >= 0 && activeLOD < LODs.Count)
        {
            LODSet set        = LODs[activeLOD];
            int    numEntries = set.LODEntries.Count;
            for (int entryIndex = 0; entryIndex < numEntries; ++entryIndex)
            {
                LODEntry entry = set.LODEntries[entryIndex];
                if (entry.gameObject)
                {
                    toHide.Add(entry.gameObject);
                }
            }
        }

        if (lodIndex >= 0 && lodIndex < LODs.Count)
        {
            LODSet set        = LODs[lodIndex];
            int    numEntries = set.LODEntries.Count;
            for (int entryIndex = 0; entryIndex < numEntries; ++entryIndex)
            {
                LODEntry entry = set.LODEntries[entryIndex];
                if (entry.gameObject)
                {
                    toHide.Remove(entry.gameObject);
                    toShow.Add(entry);
                }
            }
        }

        int numHidden = toHide.Count;

        for (int hiddenIndex = 0; hiddenIndex < numHidden; ++hiddenIndex)
        {
            toHide[hiddenIndex].SetActive(false);
        }

        int numShown = toShow.Count;

        for (int showIndex = 0; showIndex < numShown; ++showIndex)
        {
            LODEntry entry = toShow[showIndex];
            entry.gameObject.SetActive(true);
            if (entry.lightmapSource)
            {
                Renderer entryRenderer = entry.gameObject.GetComponent <Renderer>();
                if (entryRenderer)
                {
                    entryRenderer.lightmapIndex = entry.lightmapSource.lightmapIndex;
#if UNITY_4_0 || UNITY_4_1 || UNITY_4_2 || UNITY_4_3 || UNITY_4_4 || UNITY_4_5 || UNITY_4_6
                    entryRenderer.lightmapTilingOffset = entry.lightmapSource.lightmapTilingOffset;
#else
                    entryRenderer.lightmapScaleOffset = entry.lightmapSource.lightmapScaleOffset;
#endif
                }
            }
        }

        activeLOD = lodIndex;

        if (CullSiblings != 0 &&
            ((activeLOD == -1 && !siblingsDisabled) || (activeLOD != -1 && siblingsDisabled)))
        {
            siblingsDisabled = (activeLOD == -1);

            if ((CullSiblings & SiblinglFlags.Behaviors) != 0)
            {
                MonoBehaviour[] siblingBehaviors = gameObject.GetComponents <MonoBehaviour>();
                int             numBehaviors     = siblingBehaviors.Length;
                for (int behaviorIndex = 0; behaviorIndex < numBehaviors; ++behaviorIndex)
                {
                    MonoBehaviour behavior = siblingBehaviors[behaviorIndex];
                    if (behavior != this && behavior != cachedMember)
                    {
                        behavior.enabled = !siblingsDisabled;
                    }
                }
            }

            if ((CullSiblings & SiblinglFlags.Renderers) != 0)
            {
                Renderer[] siblingRenderers = gameObject.GetComponents <Renderer>();
                int        numRenderers     = siblingRenderers.Length;
                for (int rendererIndex = 0; rendererIndex < numRenderers; ++rendererIndex)
                {
                    siblingRenderers[rendererIndex].enabled = !siblingsDisabled;
                }
            }

            if ((CullSiblings & SiblinglFlags.Lights) != 0)
            {
                Light[] siblingLights = gameObject.GetComponents <Light>();
                int     numLights     = siblingLights.Length;
                for (int lightIndex = 0; lightIndex < numLights; ++lightIndex)
                {
                    siblingLights[lightIndex].enabled = !siblingsDisabled;
                }
            }

            if ((CullSiblings & SiblinglFlags.Colliders) != 0)
            {
                Collider[] siblingColliders = gameObject.GetComponents <Collider>();
                int        numColliders     = siblingColliders.Length;
                for (int colliderIndex = 0; colliderIndex < numColliders; ++colliderIndex)
                {
                    siblingColliders[colliderIndex].enabled = !siblingsDisabled;
                }
            }

            if ((CullSiblings & SiblinglFlags.RigidBodies) != 0)
            {
                Rigidbody[] siblingRBs = gameObject.GetComponents <Rigidbody>();
                int         numRBs     = siblingRBs.Length;
                for (int rbIndex = 0; rbIndex < numRBs; ++rbIndex)
                {
                    if (siblingsDisabled)
                    {
                        siblingRBs[rbIndex].Sleep();
                    }
                    else
                    {
                        siblingRBs[rbIndex].WakeUp();
                    }
                }
            }
        }

        cachedMember.ForceUpdate(true);
    }