private bool IsActiveGameObjectInLightmap(GITextureType textureType)
        {
            if (isRealtimeLightmap)
            {
                return(m_ActiveGameObjectTextureHash == m_RealtimeTextureHash);
            }

            if (LightmapVisualizationUtility.IsAtlasTextureType(textureType))
            {
                return(m_ActiveGameObjectLightmapIndex == m_LightmapIndex);
            }

            return(m_ActiveGameObjectInstanceId == m_InstanceID);
        }
        private void UpdateCachedTexture(GITextureType textureType)
        {
            if (isIndexBased)
            {
                if (isRealtimeLightmap)
                {
                    Hash128[] mainHashes = Lightmapping.GetMainSystemHashes();

                    if (!m_RealtimeTextureHash.isValid || !mainHashes.Contains(m_RealtimeTextureHash))
                    {
                        m_RealtimeTextureHash = mainHashes.ElementAtOrDefault(m_LightmapIndex);
                    }
                }
            }
            else // instance based
            {
                if (isRealtimeLightmap)
                {
                    Hash128 systemHash;

                    if (!Lightmapping.GetInputSystemHash(m_InstanceID, out systemHash))
                    {
                        m_CachedTexture.textureAvailability = GITextureAvailability.GITextureNotAvailable;
                        return;
                    }
                    m_RealtimeTextureHash = systemHash;
                }
                else
                {
                    int lightmapIndex;

                    if (!Lightmapping.GetLightmapIndex(m_InstanceID, out lightmapIndex))
                    {
                        m_CachedTexture.textureAvailability = GITextureAvailability.GITextureNotAvailable;
                        return;
                    }

                    m_LightmapIndex = lightmapIndex;
                }
            }

            Hash128 contentHash = isRealtimeLightmap ? LightmapVisualizationUtility.GetRealtimeGITextureHash(m_RealtimeTextureHash, textureType) :
                                  LightmapVisualizationUtility.GetBakedGITextureHash(m_LightmapIndex, m_InstanceID, textureType);

            // if we need to fetch a new texture
            if (m_CachedTexture.texture == null || m_CachedTexture.type != textureType || m_CachedTexture.contentHash != contentHash || m_CachedTexture.contentHash == new Hash128())
            {
                m_CachedTexture = isRealtimeLightmap ?
                                  LightmapVisualizationUtility.GetRealtimeGITexture(m_RealtimeTextureHash, textureType) :
                                  LightmapVisualizationUtility.GetBakedGITexture(m_LightmapIndex, m_InstanceID, textureType);
            }

            if (!m_ShowUVOverlay)
            {
                return; // if we don't wanna show any overlay
            }
            if (m_CachedTexture.texture == null || m_CachedTexture.textureAvailability == GITextureAvailability.GITextureNotAvailable || m_CachedTexture.textureAvailability == GITextureAvailability.GITextureUnknown)
            {
                return; // if we dont have a texture
            }
            // fetch Renderers

            if (isRealtimeLightmap)
            {
                m_CachedTextureObjects = LightmapVisualizationUtility.GetRealtimeGITextureRenderers(m_RealtimeTextureHash);
            }
            else if (LightmapVisualizationUtility.IsAtlasTextureType(textureType))
            {
                m_CachedTextureObjects = LightmapVisualizationUtility.GetBakedGITextureRenderers(m_LightmapIndex);
            }
            else // if it's an instance based baked lightmap, we only have 1 object in it
            {
                m_CachedTextureObjects = new GameObject[] {}
            };
        }
        private void DrawPreview(Rect r)
        {
            if (r.height <= 0)
            {
                return;
            }

            if (m_ZoomablePreview == null)
            {
                m_ZoomablePreview = new ZoomableArea(true);

                m_ZoomablePreview.hRangeMin = 0.0f;
                m_ZoomablePreview.vRangeMin = 0.0f;

                m_ZoomablePreview.hRangeMax = 1.0f;
                m_ZoomablePreview.vRangeMax = 1.0f;

                m_ZoomablePreview.SetShownHRange(0, 1);
                m_ZoomablePreview.SetShownVRange(0, 1);

                m_ZoomablePreview.uniformScale    = true;
                m_ZoomablePreview.scaleWithWindow = true;
            }

            m_ZoomablePreview.rect = r;

            m_ZoomablePreview.BeginViewGUI();

            m_ZoomablePreview.hSlider = m_ZoomablePreview.shownArea.width < 1;
            m_ZoomablePreview.vSlider = m_ZoomablePreview.shownArea.height < 1;

            Rect          drawableArea = m_ZoomablePreview.drawRect;
            GITextureType textureType  = GetSelectedTextureType();

            UpdateCachedTexture(textureType);

            if (m_CachedTexture.textureAvailability == GITextureAvailability.GITextureNotAvailable || m_CachedTexture.textureAvailability == GITextureAvailability.GITextureUnknown)
            {
                if (!isRealtimeLightmap)
                {
                    if (!LightmapVisualizationUtility.IsAtlasTextureType(textureType) && isIndexBased)
                    {
                        GUI.Label(drawableArea, Styles.TextureNotAvailableBakedAlbedoEmissive, Styles.PreviewLabel);
                    }
                    else if (textureType == GITextureType.BakedShadowMask)
                    {
                        GUI.Label(drawableArea, Styles.TextureNotAvailableBakedShadowmask, Styles.PreviewLabel);
                    }
                    else
                    {
                        GUI.Label(drawableArea, Styles.TextureNotAvailableBaked, Styles.PreviewLabel);
                    }
                }
                else
                {
                    GUI.Label(drawableArea, Styles.TextureNotAvailableRealtime, Styles.PreviewLabel);
                }

                return;
            }

            if (m_CachedTexture.textureAvailability == GITextureAvailability.GITextureLoading && m_CachedTexture.texture == null)
            {
                GUI.Label(drawableArea, Styles.TextureLoading, Styles.PreviewLabel);
                return;
            }

            LightmapType lightmapType = LightmapVisualizationUtility.GetLightmapType(textureType);

            switch (Event.current.type)
            {
            // 'F' will zoom to uv bounds
            case EventType.ValidateCommand:
            case EventType.ExecuteCommand:

                if (Event.current.commandName == EventCommandNames.FrameSelected && IsActiveGameObjectInLightmap(textureType))
                {
                    // There are instance based baked textures where we don't get any STs and can't do the framing
                    if (!isRealtimeLightmap && !LightmapVisualizationUtility.IsAtlasTextureType(textureType))
                    {
                        break;
                    }

                    Vector4 lightmapTilingOffset = LightmapVisualizationUtility.GetLightmapTilingOffset(lightmapType);

                    Vector2 min = new Vector2(lightmapTilingOffset.z, lightmapTilingOffset.w);
                    Vector2 max = min + new Vector2(lightmapTilingOffset.x, lightmapTilingOffset.y);

                    min = Vector2.Max(min, Vector2.zero);
                    max = Vector2.Min(max, Vector2.one);

                    Texture2D texture     = m_CachedTexture.texture;
                    Rect      textureRect = new Rect(r.x, r.y, texture.width, texture.height);
                    textureRect = ResizeRectToFit(textureRect, drawableArea);

                    float offsetX = 0.0f, offsetY = 0.0f;

                    if (textureRect.height == drawableArea.height)
                    {
                        offsetX = (drawableArea.width - textureRect.width) / drawableArea.width;
                    }
                    else
                    {
                        offsetY = (drawableArea.height - textureRect.height) / drawableArea.height;
                    }

                    // Make sure that the focus rectangle is a even square
                    Rect rect = new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
                    rect.width = rect.height = Mathf.Max(rect.width, rect.height);
                    rect.x    -= (offsetX * min.x);
                    rect.y    += (offsetY * (1 - max.y));

                    m_ZoomablePreview.shownArea = rect;
                    Event.current.Use();
                }
                break;

            // Scale and draw texture and uv's
            case EventType.Repaint:
            {
                Texture2D texture = m_CachedTexture.texture;

                if (texture)
                {
                    Rect textureRect = new Rect(r.x, r.y, texture.width, texture.height);
                    textureRect = ResizeRectToFit(textureRect, drawableArea);
                    textureRect = ScaleRectByZoomableArea(textureRect, m_ZoomablePreview);

                    const int padding = 5;
                    textureRect.x      += padding;
                    textureRect.width  -= padding * 2;
                    textureRect.height -= padding;

                    // Texture shouldn't be filtered since it will make previewing really blurry
                    FilterMode prevMode = texture.filterMode;
                    texture.filterMode = FilterMode.Point;

                    LightmapVisualizationUtility.DrawTextureWithUVOverlay(texture,
                                                                          (m_ShowUVOverlay && IsActiveGameObjectInLightmap(textureType)) ? Selection.activeGameObject : null,
                                                                          m_ShowUVOverlay ? m_CachedTextureObjects : new GameObject[] {}, drawableArea, textureRect, textureType, exposure);
                    texture.filterMode = prevMode;
                }
            }
            break;
            }

            m_ZoomablePreview.EndViewGUI();
        }