示例#1
0
        public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                return(null);
            }

            Texture texture = target as Texture;

            if (IsCubemap())
            {
                return(m_CubemapPreview.RenderStaticPreview(texture, width, height));
            }

            TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter;

            if (textureImporter != null && textureImporter.textureType == TextureImporterType.Sprite && textureImporter.spriteImportMode == SpriteImportMode.Polygon)
            {
                // If the texture importer is a Sprite of primitive, use the sprite inspector for generating preview/icon.
                if (subAssets.Length > 0)
                {
                    Sprite sprite = subAssets[0] as Sprite;
                    if (sprite)
                    {
                        return(SpriteInspector.BuildPreviewTexture(width, height, sprite, null, true));
                    }
                }
                else
                {
                    return(null);
                }
            }

            PreviewHelpers.AdjustWidthAndHeightForStaticPreview(texture.width, texture.height, ref width, ref height);

            RenderTexture savedRT       = RenderTexture.active;
            Rect          savedViewport = ShaderUtil.rawViewportRect;

            RenderTexture tmp = RenderTexture.GetTemporary(
                width, height,
                0,
                SystemInfo.GetGraphicsFormat(DefaultFormat.LDR));
            Material mat = EditorGUI.GetMaterialForSpecialTexture(texture);

            if (mat != null)
            {
                Graphics.Blit(texture, tmp, mat);
            }
            else
            {
                Graphics.Blit(texture, tmp);
            }

            RenderTexture.active = tmp;
            Texture2D copy;
            Texture2D tex2d = target as Texture2D;

            if (tex2d != null && tex2d.alphaIsTransparency)
            {
                copy = new Texture2D(width, height, TextureFormat.RGBA32, false);
            }
            else
            {
                copy = new Texture2D(width, height, TextureFormat.RGB24, false);
            }
            copy.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            copy.Apply();
            RenderTexture.ReleaseTemporary(tmp);

            EditorGUIUtility.SetRenderTextureNoViewport(savedRT);
            ShaderUtil.rawViewportRect = savedViewport;

            return(copy);
        }
示例#2
0
        public override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height)
        {
            Texture2D textured;

            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                return(null);
            }
            Texture target = this.target as Texture;

            if (this.IsCubemap())
            {
                return(this.m_CubemapPreview.RenderStaticPreview(target, width, height));
            }
            TextureImporter atPath = AssetImporter.GetAtPath(assetPath) as TextureImporter;

            if ((atPath != null) && (atPath.spriteImportMode == SpriteImportMode.Polygon))
            {
                Sprite sprite = subAssets[0] as Sprite;
                if (sprite != null)
                {
                    return(SpriteInspector.BuildPreviewTexture(width, height, sprite, null, true));
                }
            }
            PreviewHelpers.AdjustWidthAndHeightForStaticPreview(target.width, target.height, ref width, ref height);
            RenderTexture active                    = RenderTexture.active;
            Rect          rawViewportRect           = ShaderUtil.rawViewportRect;
            bool          flag                      = !TextureUtil.GetLinearSampled(target);
            RenderTexture dest                      = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, !flag ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.sRGB);
            Material      materialForSpecialTexture = EditorGUI.GetMaterialForSpecialTexture(target);

            GL.sRGBWrite = QualitySettings.activeColorSpace == ColorSpace.Linear;
            if (materialForSpecialTexture != null)
            {
                if (Unsupported.IsDeveloperBuild())
                {
                    materialForSpecialTexture = new Material(materialForSpecialTexture);
                }
                Graphics.Blit(target, dest, materialForSpecialTexture);
            }
            else
            {
                Graphics.Blit(target, dest);
            }
            GL.sRGBWrite         = false;
            RenderTexture.active = dest;
            Texture2D textured2 = this.target as Texture2D;

            if ((textured2 != null) && textured2.alphaIsTransparency)
            {
                textured = new Texture2D(width, height, TextureFormat.ARGB32, false);
            }
            else
            {
                textured = new Texture2D(width, height, TextureFormat.RGB24, false);
            }
            textured.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0);
            textured.Apply();
            RenderTexture.ReleaseTemporary(dest);
            EditorGUIUtility.SetRenderTextureNoViewport(active);
            ShaderUtil.rawViewportRect = rawViewportRect;
            if ((materialForSpecialTexture != null) && Unsupported.IsDeveloperBuild())
            {
                Object.DestroyImmediate(materialForSpecialTexture);
            }
            return(textured);
        }