Restore() private method

private Restore ( ) : void
return void
        public Texture EndPreview()
        {
            Unsupported.RestoreOverrideLightingSettings();

            m_SavedState.Restore();
            FinishFrame();
            return(m_RenderTexture);
        }
Example #2
0
        public static UnityTexture2D RenderStaticPreview(Sprite sprite, Color color, int width, int height, Matrix4x4 transform)
        {
            if (sprite == null)
            {
                return(null);
            }

            PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int)sprite.rect.width, (int)sprite.rect.height, ref width, ref height);

            SavedRenderTargetState savedRTState = new SavedRenderTargetState();

            RenderTexture tmp = RenderTexture.GetTemporary(width, height, 0, SystemInfo.GetGraphicsFormat(DefaultFormat.LDR));

            RenderTexture.active = tmp;
            GL.Clear(true, true, new Color(0f, 0f, 0f, 0.1f));

            previewSpriteDefaultMaterial.mainTexture = sprite.texture;
            previewSpriteDefaultMaterial.SetPass(0);

            RenderSpriteImmediate(sprite, color, transform);

            UnityTexture2D copy = new UnityTexture2D(width, height, TextureFormat.ARGB32, false);

            copy.hideFlags = HideFlags.HideAndDontSave;
            copy.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            copy.Apply();
            RenderTexture.ReleaseTemporary(tmp);

            savedRTState.Restore();
            return(copy);
        }
Example #3
0
 public static UnityEngine.Texture2D RenderStaticPreview(Sprite sprite, Color color, int width, int height, Matrix4x4 transform)
 {
     UnityEngine.Texture2D result;
     if (sprite == null)
     {
         result = null;
     }
     else
     {
         PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int)sprite.rect.width, (int)sprite.rect.height, ref width, ref height);
         SavedRenderTargetState savedRenderTargetState = new SavedRenderTargetState();
         RenderTexture          temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
         RenderTexture.active = temporary;
         GL.Clear(true, true, new Color(0f, 0f, 0f, 0.1f));
         SpriteUtility.previewSpriteDefaultMaterial.mainTexture = sprite.texture;
         SpriteUtility.previewSpriteDefaultMaterial.SetPass(0);
         SpriteUtility.RenderSpriteImmediate(sprite, color, transform);
         UnityEngine.Texture2D texture2D = new UnityEngine.Texture2D(width, height, TextureFormat.ARGB32, false);
         texture2D.hideFlags = HideFlags.HideAndDontSave;
         texture2D.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0);
         texture2D.Apply();
         RenderTexture.ReleaseTemporary(temporary);
         savedRenderTargetState.Restore();
         result = texture2D;
     }
     return(result);
 }
Example #4
0
        internal static void ScaleImage(int w, int h, Texture2D inimage, Texture2D outimage, GUIStyle bgStyle)
        {
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
            SavedRenderTargetState savedRenderTargetState = new SavedRenderTargetState();

            if (AssetStorePreviewManager.s_RenderTexture != null && (AssetStorePreviewManager.s_RenderTexture.width != w || AssetStorePreviewManager.s_RenderTexture.height != h))
            {
                UnityEngine.Object.DestroyImmediate(AssetStorePreviewManager.s_RenderTexture);
                AssetStorePreviewManager.s_RenderTexture = null;
            }
            if (AssetStorePreviewManager.s_RenderTexture == null)
            {
                AssetStorePreviewManager.s_RenderTexture           = RenderTexture.GetTemporary(w, h, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                AssetStorePreviewManager.s_RenderTexture.hideFlags = HideFlags.HideAndDontSave;
            }
            RenderTexture renderTexture = AssetStorePreviewManager.s_RenderTexture;

            RenderTexture.active = renderTexture;
            Rect rect = new Rect(0f, 0f, (float)w, (float)h);

            EditorGUIUtility.SetRenderTextureNoViewport(renderTexture);
            GL.LoadOrtho();
            GL.LoadPixelMatrix(0f, (float)w, (float)h, 0f);
            ShaderUtil.rawViewportRect = new Rect(0f, 0f, (float)w, (float)h);
            ShaderUtil.rawScissorRect  = new Rect(0f, 0f, (float)w, (float)h);
            GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
            Rect screenRect = rect;

            if (inimage.width > inimage.height)
            {
                float num = screenRect.height * ((float)inimage.height / (float)inimage.width);
                screenRect.height = (float)((int)num);
                screenRect.y     += (float)((int)(num * 0.5f));
            }
            else
            {
                if (inimage.width < inimage.height)
                {
                    float num2 = screenRect.width * ((float)inimage.width / (float)inimage.height);
                    screenRect.width = (float)((int)num2);
                    screenRect.x    += (float)((int)(num2 * 0.5f));
                }
            }
            if (bgStyle != null && bgStyle.normal != null && bgStyle.normal.background != null)
            {
                Graphics.DrawTexture(rect, bgStyle.normal.background);
            }
            Graphics.DrawTexture(screenRect, inimage);
            outimage.ReadPixels(rect, 0, 0, false);
            outimage.Apply();
            outimage.hideFlags = HideFlags.HideAndDontSave;
            savedRenderTargetState.Restore();
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
        }
        /** Slow version of scaling an image but i doesn't
         *  matter since it is not performance critical
         */
        internal static void ScaleImage(int w, int h, Texture2D inimage, Texture2D outimage, GUIStyle bgStyle)
        {
            SavedRenderTargetState saved = new SavedRenderTargetState();

            if (s_RenderTexture != null && (s_RenderTexture.width != w || s_RenderTexture.height != h))
            {
                Object.DestroyImmediate(s_RenderTexture);
                s_RenderTexture = null;
            }

            if (s_RenderTexture == null)
            {
                s_RenderTexture           = RenderTexture.GetTemporary(w, h, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                s_RenderTexture.hideFlags = HideFlags.HideAndDontSave;
            }

            RenderTexture r = s_RenderTexture;

            RenderTexture.active = r;
            Rect rect = new Rect(0, 0, w, h);

            EditorGUIUtility.SetRenderTextureNoViewport(r);
            GL.LoadOrtho();
            GL.LoadPixelMatrix(0, w, h, 0);
            ShaderUtil.rawViewportRect = new Rect(0, 0, w, h);
            ShaderUtil.rawScissorRect  = new Rect(0, 0, w, h);
            GL.Clear(true, true, new Color(0, 0, 0, 0));

            Rect blitRect = rect;

            if (inimage.width > inimage.height)
            {
                float newHeight = (float)blitRect.height * ((float)inimage.height / (float)inimage.width);
                blitRect.height = (int)newHeight;
                blitRect.y     += (int)(newHeight * 0.5f);
            }
            else if (inimage.width < inimage.height)
            {
                float newWidth = (float)blitRect.width * ((float)inimage.width / (float)inimage.height);
                blitRect.width = (int)newWidth;
                blitRect.x    += (int)(newWidth * 0.5f);
            }
            if (bgStyle != null && bgStyle.normal != null && bgStyle.normal.background != null)
            {
                Graphics.DrawTexture(rect, bgStyle.normal.background);
            }

            Graphics.DrawTexture(blitRect, inimage);
            outimage.ReadPixels(rect, 0, 0, false);
            outimage.Apply();
            outimage.hideFlags = HideFlags.HideAndDontSave;
            saved.Restore();
        }
        internal static void ScaleImage(int w, int h, Texture2D inimage, Texture2D outimage, GUIStyle bgStyle)
        {
            SavedRenderTargetState state = new SavedRenderTargetState();

            if ((s_RenderTexture != null) && ((s_RenderTexture.width != w) || (s_RenderTexture.height != h)))
            {
                Object.DestroyImmediate(s_RenderTexture);
                s_RenderTexture = null;
            }
            if (s_RenderTexture == null)
            {
                s_RenderTexture           = RenderTexture.GetTemporary(w, h, 0x10, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                s_RenderTexture.hideFlags = HideFlags.HideAndDontSave;
            }
            RenderTexture rt = s_RenderTexture;

            RenderTexture.active = rt;
            Rect screenRect = new Rect(0f, 0f, (float)w, (float)h);

            EditorGUIUtility.SetRenderTextureNoViewport(rt);
            GL.LoadOrtho();
            GL.LoadPixelMatrix(0f, (float)w, (float)h, 0f);
            ShaderUtil.rawViewportRect = new Rect(0f, 0f, (float)w, (float)h);
            ShaderUtil.rawScissorRect  = new Rect(0f, 0f, (float)w, (float)h);
            GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
            Rect rect2 = screenRect;

            if (inimage.width > inimage.height)
            {
                float num = rect2.height * (((float)inimage.height) / ((float)inimage.width));
                rect2.height = (int)num;
                rect2.y     += (int)(num * 0.5f);
            }
            else if (inimage.width < inimage.height)
            {
                float num2 = rect2.width * (((float)inimage.width) / ((float)inimage.height));
                rect2.width = (int)num2;
                rect2.x    += (int)(num2 * 0.5f);
            }
            if (((bgStyle != null) && (bgStyle.normal != null)) && (bgStyle.normal.background != null))
            {
                Graphics.DrawTexture(screenRect, bgStyle.normal.background);
            }
            Graphics.DrawTexture(rect2, inimage);
            outimage.ReadPixels(screenRect, 0, 0, false);
            outimage.Apply();
            outimage.hideFlags = HideFlags.HideAndDontSave;
            state.Restore();
        }
Example #7
0
        private static Texture2D BuildPreviewTexture(int width, int height, Sprite sprite, Material spriteRendererMaterial)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                return(null);
            }
            float     num           = sprite.rect.width;
            float     num2          = sprite.rect.height;
            Texture2D spriteTexture = UnityEditor.Sprites.SpriteUtility.GetSpriteTexture(sprite, false);

            PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int)num, (int)num2, ref width, ref height);
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
            SavedRenderTargetState state = new SavedRenderTargetState();
            RenderTexture          temp  = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);

            RenderTexture.active = temp;
            GL.sRGBWrite         = QualitySettings.activeColorSpace == ColorSpace.Linear;
            GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
            Texture texture = null;
            Vector4 vector  = new Vector4(0f, 0f, 0f, 0f);
            bool    flag    = false;
            bool    flag2   = false;

            if (spriteRendererMaterial != null)
            {
                flag  = spriteRendererMaterial.HasProperty("_MainTex");
                flag2 = spriteRendererMaterial.HasProperty("_MainTex_TexelSize");
            }
            Material material = null;

            if (spriteRendererMaterial != null)
            {
                if (flag)
                {
                    texture = spriteRendererMaterial.GetTexture("_MainTex");
                    spriteRendererMaterial.SetTexture("_MainTex", spriteTexture);
                }
                if (flag2)
                {
                    vector = spriteRendererMaterial.GetVector("_MainTex_TexelSize");
                    spriteRendererMaterial.SetVector("_MainTex_TexelSize", TextureUtil.GetTexelSizeVector(spriteTexture));
                }
                spriteRendererMaterial.SetPass(0);
            }
            else
            {
                material = new Material(Shader.Find("Hidden/BlitCopy"))
                {
                    mainTexture = spriteTexture
                };
                material.SetPass(0);
            }
            float num3 = sprite.rect.width / sprite.bounds.size.x;

            Vector2[] vertices  = sprite.vertices;
            Vector2[] uv        = sprite.uv;
            ushort[]  triangles = sprite.triangles;
            Vector2   pivot     = sprite.pivot;

            GL.PushMatrix();
            GL.LoadOrtho();
            GL.Color(new Color(1f, 1f, 1f, 1f));
            GL.Begin(4);
            for (int i = 0; i < sprite.triangles.Length; i++)
            {
                ushort  index   = triangles[i];
                Vector2 vector3 = vertices[index];
                Vector2 vector4 = uv[index];
                GL.TexCoord(new Vector3(vector4.x, vector4.y, 0f));
                GL.Vertex3(((vector3.x * num3) + pivot.x) / num, ((vector3.y * num3) + pivot.y) / num2, 0f);
            }
            GL.End();
            GL.PopMatrix();
            GL.sRGBWrite = false;
            if (spriteRendererMaterial != null)
            {
                if (flag)
                {
                    spriteRendererMaterial.SetTexture("_MainTex", texture);
                }
                if (flag2)
                {
                    spriteRendererMaterial.SetVector("_MainTex_TexelSize", vector);
                }
            }
            Texture2D textured2 = new Texture2D(width, height, TextureFormat.ARGB32, false)
            {
                hideFlags = HideFlags.HideAndDontSave
            };

            textured2.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0);
            textured2.Apply();
            RenderTexture.ReleaseTemporary(temp);
            state.Restore();
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
            if (material != null)
            {
                UnityEngine.Object.DestroyImmediate(material);
            }
            return(textured2);
        }
Example #8
0
		private bool UpdateTextures(TextureAtlas atlas, List<TreeMaterial> materials)
		{
			if (!this.root.enableMaterialOptimize)
			{
				return false;
			}
			bool flag = this.optimizedSolidMaterial.GetTexture("_MainTex") != null && this.optimizedSolidMaterial.GetTexture("_BumpSpecMap") != null && this.optimizedSolidMaterial.GetTexture("_TranslucencyMap") != null && this.optimizedCutoutMaterial.GetTexture("_MainTex") != null && this.optimizedCutoutMaterial.GetTexture("_ShadowTex") != null && this.optimizedCutoutMaterial.GetTexture("_BumpSpecMap") != null && this.optimizedCutoutMaterial.GetTexture("_TranslucencyMap");
			UnityEngine.Object[] array = new UnityEngine.Object[materials.Count];
			for (int i = 0; i < materials.Count; i++)
			{
				array[i] = materials[i].material;
			}
			string text = InternalEditorUtility.CalculateHashForObjectsAndDependencies(array);
			text += atlas.GetHashCode();
			if (this.materialHash == text && flag)
			{
				this.UpdateShadowTexture(this.optimizedCutoutMaterial.GetTexture("_ShadowTex") as Texture2D, atlas.atlasWidth, atlas.atlasHeight);
				return false;
			}
			this.materialHash = text;
			int atlasWidth = atlas.atlasWidth;
			int atlasHeight = atlas.atlasHeight;
			int atlasPadding = atlas.atlasPadding;
			Texture2D texture2D = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
			Texture2D texture2D2 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.RGB24, true);
			Texture2D texture2D3 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
			Texture2D texture2D4 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
			texture2D.name = "diffuse";
			texture2D2.name = "shadow";
			texture2D3.name = "normal_specular";
			texture2D4.name = "translucency_gloss";
			SavedRenderTargetState savedRenderTargetState = new SavedRenderTargetState();
			EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
			RenderTexture temporary = RenderTexture.GetTemporary(atlasWidth, atlasHeight, 0, RenderTextureFormat.ARGB32);
			Color white = Color.white;
			Color color = new Color(0.03f, 0.5f, 0f, 0.5f);
			Color color2 = new Color(0f, 0f, 0f, 0f);
			Texture2D texture2D5 = new Texture2D(1, 1);
			texture2D5.SetPixel(0, 0, white);
			texture2D5.Apply();
			Texture2D texture2D6 = new Texture2D(1, 1);
			texture2D6.SetPixel(0, 0, white);
			texture2D6.Apply();
			Texture2D texture2D7 = new Texture2D(1, 1);
			texture2D7.SetPixel(0, 0, color);
			texture2D7.Apply();
			Texture2D texture2D8 = new Texture2D(1, 1);
			texture2D8.SetPixel(0, 0, color2);
			texture2D8.Apply();
			Texture2D texture2D9 = texture2D8;
			Texture2D texture2D10 = new Texture2D(1, 1);
			texture2D10.SetPixel(0, 0, Color.white);
			texture2D10.Apply();
			Material material = EditorGUIUtility.LoadRequired("Inspectors/TreeCreator/TreeTextureCombinerMaterial.mat") as Material;
			for (int j = 0; j < 4; j++)
			{
				RenderTexture.active = temporary;
				GL.LoadPixelMatrix(0f, (float)atlasWidth, 0f, (float)atlasHeight);
				material.SetVector("_TexSize", new Vector4((float)atlasWidth, (float)atlasHeight, 0f, 0f));
				switch (j)
				{
				case 0:
					GL.Clear(false, true, color);
					break;
				case 1:
					GL.Clear(false, true, color2);
					break;
				case 2:
					GL.Clear(false, true, color2);
					break;
				case 3:
					GL.Clear(false, true, color2);
					break;
				}
				for (int k = 0; k < atlas.nodes.Count; k++)
				{
					TextureAtlas.TextureNode textureNode = atlas.nodes[k];
					Rect packedRect = textureNode.packedRect;
					Texture texture = null;
					Texture texture2 = null;
					Color color3 = default(Color);
					switch (j)
					{
					case 0:
						texture = textureNode.normalTexture;
						texture2 = textureNode.shadowOffsetTexture;
						color3 = new Color(textureNode.shininess, 0f, 0f, 0f);
						if (texture == null)
						{
							texture = texture2D7;
						}
						if (texture2 == null)
						{
							texture2 = texture2D9;
						}
						break;
					case 1:
						texture = textureNode.diffuseTexture;
						color3 = textureNode.diffuseColor;
						if (texture == null)
						{
							texture = texture2D5;
						}
						break;
					case 2:
						texture = textureNode.translucencyTexture;
						texture2 = textureNode.glossTexture;
						if (texture == null)
						{
							texture = texture2D10;
						}
						if (texture2 == null)
						{
							texture2 = texture2D8;
						}
						break;
					case 3:
						texture2 = textureNode.diffuseTexture;
						if (texture2 == null)
						{
							texture2 = texture2D5;
						}
						break;
					}
					if (textureNode.tileV)
					{
						float x = packedRect.x;
						float num = (float)atlasPadding / 2f;
						for (float num2 = num; num2 > 0f; num2 -= 1f)
						{
							Rect rect = new Rect(packedRect);
							Rect rect2 = new Rect(packedRect);
							rect.x = x - num2;
							rect2.x = x + num2;
							this.DrawTexture(rect, texture, texture2, material, color3, j);
							this.DrawTexture(rect2, texture, texture2, material, color3, j);
						}
					}
					this.DrawTexture(packedRect, texture, texture2, material, color3, j);
				}
				switch (j)
				{
				case 0:
					texture2D3.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
					texture2D3.Apply(true);
					break;
				case 1:
					texture2D.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
					texture2D.Apply(true);
					break;
				case 2:
					texture2D4.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
					texture2D4.Apply(true);
					break;
				case 3:
					texture2D2.ReadPixels(new Rect(0f, 0f, (float)atlasWidth, (float)atlasHeight), 0, 0);
					texture2D2.Apply(true);
					break;
				}
			}
			savedRenderTargetState.Restore();
			this.optimizedSolidMaterial.SetPass(0);
			RenderTexture.ReleaseTemporary(temporary);
			UnityEngine.Object.DestroyImmediate(texture2D5);
			UnityEngine.Object.DestroyImmediate(texture2D6);
			UnityEngine.Object.DestroyImmediate(texture2D10);
			UnityEngine.Object.DestroyImmediate(texture2D8);
			UnityEngine.Object.DestroyImmediate(texture2D7);
			EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
			Texture2D[] array2 = new Texture2D[]
			{
				texture2D,
				texture2D3,
				texture2D4,
				texture2D2
			};
			array2 = TreeData.WriteOptimizedTextures(AssetDatabase.GetAssetPath(this), array2);
			UnityEngine.Object.DestroyImmediate(texture2D);
			UnityEngine.Object.DestroyImmediate(texture2D3);
			UnityEngine.Object.DestroyImmediate(texture2D4);
			UnityEngine.Object.DestroyImmediate(texture2D2);
			this.optimizedSolidMaterial.SetTexture("_MainTex", array2[0]);
			this.optimizedSolidMaterial.SetTexture("_BumpSpecMap", array2[1]);
			this.optimizedSolidMaterial.SetTexture("_TranslucencyMap", array2[2]);
			this.optimizedCutoutMaterial.SetTexture("_MainTex", array2[0]);
			this.optimizedCutoutMaterial.SetTexture("_BumpSpecMap", array2[1]);
			this.optimizedCutoutMaterial.SetTexture("_TranslucencyMap", array2[2]);
			this.optimizedCutoutMaterial.SetTexture("_ShadowTex", array2[3]);
			this.UpdateShadowTexture(array2[3], atlas.atlasWidth, atlas.atlasHeight);
			return true;
		}
Example #9
0
        public static Texture2D BuildPreviewTexture(int width, int height, Sprite sprite, Material spriteRendererMaterial, bool isPolygon)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                return(null);
            }

            float spriteWidth  = sprite.rect.width;
            float spriteHeight = sprite.rect.height;

            Texture2D texture = UnityEditor.Sprites.SpriteUtility.GetSpriteTexture(sprite, false);

            // only adjust the preview texture size if the sprite is not in polygon mode.
            // In polygon mode, we are looking at a 4x4 texture will detailed mesh. It's better to maximize the display of it.
            if (!isPolygon)
            {
                PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int)spriteWidth, (int)spriteHeight, ref width, ref height);
            }

            SavedRenderTargetState savedRTState = new SavedRenderTargetState();

            RenderTexture tmp = RenderTexture.GetTemporary(
                width,
                height,
                0,
                RenderTextureFormat.Default,
                RenderTextureReadWrite.Linear);

            RenderTexture.active = tmp;

            GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));

            Texture _oldTexture      = null;
            Vector4 _oldTexelSize    = new Vector4(0, 0, 0, 0);
            bool    _matHasTexture   = false;
            bool    _matHasTexelSize = false;

            if (spriteRendererMaterial != null)
            {
                _matHasTexture   = spriteRendererMaterial.HasProperty("_MainTex");
                _matHasTexelSize = spriteRendererMaterial.HasProperty("_MainTex_TexelSize");
            }

            bool hasColors = sprite.HasVertexAttribute(VertexAttribute.Color);

            Material copyMaterial = null;

            if (spriteRendererMaterial != null)
            {
                if (_matHasTexture)
                {
                    _oldTexture = spriteRendererMaterial.GetTexture("_MainTex");
                    spriteRendererMaterial.SetTexture("_MainTex", texture);
                }

                if (_matHasTexelSize)
                {
                    _oldTexelSize = spriteRendererMaterial.GetVector("_MainTex_TexelSize");
                    spriteRendererMaterial.SetVector("_MainTex_TexelSize", TextureUtil.GetTexelSizeVector(texture));
                }

                spriteRendererMaterial.SetPass(0);
            }
            else
            {
                if (hasColors)
                {
                    SpriteUtility.previewSpriteDefaultMaterial.SetPass(0);
                }
                else if (texture != null)
                {
                    copyMaterial                   = new Material(Shader.Find("Hidden/BlitCopy"));
                    copyMaterial.mainTexture       = texture;
                    copyMaterial.mainTextureScale  = Vector2.one;
                    copyMaterial.mainTextureOffset = Vector2.zero;
                    copyMaterial.SetPass(0);
                }
            }

            float pixelsToUnits = sprite.rect.width / sprite.bounds.size.x;

            Vector2[] vertices  = sprite.vertices;
            Vector2[] uvs       = sprite.uv;
            ushort[]  triangles = sprite.triangles;
            Vector2   pivot     = sprite.pivot;

            NativeSlice <Color32>?colors = null;

            if (hasColors)
            {
                colors = sprite.GetVertexAttribute <Color32>(VertexAttribute.Color);
            }

            GL.PushMatrix();
            GL.LoadOrtho();
            GL.Color(new Color(1, 1, 1, 1));
            GL.Begin(GL.TRIANGLES);
            for (int i = 0; i < triangles.Length; ++i)
            {
                ushort  index  = triangles[i];
                Vector2 vertex = vertices[index];
                Vector2 uv     = uvs[index];
                GL.TexCoord(new Vector3(uv.x, uv.y, 0));
                if (colors != null)
                {
                    GL.Color(colors.Value[index]);
                }
                GL.Vertex3((vertex.x * pixelsToUnits + pivot.x) / spriteWidth, (vertex.y * pixelsToUnits + pivot.y) / spriteHeight, 0);
            }
            GL.End();
            GL.PopMatrix();


            if (spriteRendererMaterial != null)
            {
                if (_matHasTexture)
                {
                    spriteRendererMaterial.SetTexture("_MainTex", _oldTexture);
                }
                if (_matHasTexelSize)
                {
                    spriteRendererMaterial.SetVector("_MainTex_TexelSize", _oldTexelSize);
                }
            }

            var tmp2 = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);

            Graphics.Blit(tmp, tmp2, EditorGUIUtility.GUITextureBlit2SRGBMaterial);

            RenderTexture.active = tmp2;

            Texture2D copy = new Texture2D(width, height, TextureFormat.RGBA32, false);

            copy.hideFlags  = HideFlags.HideAndDontSave;
            copy.filterMode = texture != null ? texture.filterMode : FilterMode.Point;
            copy.anisoLevel = texture != null ? texture.anisoLevel : 0;
            copy.wrapMode   = texture != null ? texture.wrapMode : TextureWrapMode.Clamp;
            copy.ReadPixels(new Rect(0, 0, width, height), 0, 0);
            copy.Apply();
            RenderTexture.ReleaseTemporary(tmp);
            RenderTexture.ReleaseTemporary(tmp2);

            savedRTState.Restore();

            if (copyMaterial != null)
            {
                DestroyImmediate(copyMaterial);
            }

            return(copy);
        }
Example #10
0
        public static Texture2D BuildPreviewTexture(int width, int height, Sprite sprite, Material spriteRendererMaterial, bool isPolygon)
        {
            Texture2D result;

            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                result = null;
            }
            else
            {
                float     width2        = sprite.rect.width;
                float     height2       = sprite.rect.height;
                Texture2D spriteTexture = UnityEditor.Sprites.SpriteUtility.GetSpriteTexture(sprite, false);
                if (!isPolygon)
                {
                    PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int)width2, (int)height2, ref width, ref height);
                }
                SavedRenderTargetState savedRenderTargetState = new SavedRenderTargetState();
                RenderTexture          temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear);
                RenderTexture.active = temporary;
                GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
                Texture value  = null;
                Vector4 vector = new Vector4(0f, 0f, 0f, 0f);
                bool    flag   = false;
                bool    flag2  = false;
                if (spriteRendererMaterial != null)
                {
                    flag  = spriteRendererMaterial.HasProperty("_MainTex");
                    flag2 = spriteRendererMaterial.HasProperty("_MainTex_TexelSize");
                }
                bool     flag3    = sprite.HasVertexAttribute(VertexAttribute.Color);
                Material material = null;
                if (spriteRendererMaterial != null)
                {
                    if (flag)
                    {
                        value = spriteRendererMaterial.GetTexture("_MainTex");
                        spriteRendererMaterial.SetTexture("_MainTex", spriteTexture);
                    }
                    if (flag2)
                    {
                        vector = spriteRendererMaterial.GetVector("_MainTex_TexelSize");
                        spriteRendererMaterial.SetVector("_MainTex_TexelSize", TextureUtil.GetTexelSizeVector(spriteTexture));
                    }
                    spriteRendererMaterial.SetPass(0);
                }
                else if (flag3)
                {
                    SpriteUtility.previewSpriteDefaultMaterial.SetPass(0);
                }
                else if (spriteTexture != null)
                {
                    material                   = new Material(Shader.Find("Hidden/BlitCopy"));
                    material.mainTexture       = spriteTexture;
                    material.mainTextureScale  = Vector2.one;
                    material.mainTextureOffset = Vector2.zero;
                    material.SetPass(0);
                }
                float                 num         = sprite.rect.width / sprite.bounds.size.x;
                Vector2[]             vertices    = sprite.vertices;
                Vector2[]             uv          = sprite.uv;
                ushort[]              triangles   = sprite.triangles;
                Vector2               pivot       = sprite.pivot;
                NativeSlice <Color32>?nativeSlice = null;
                if (flag3)
                {
                    nativeSlice = new NativeSlice <Color32>?(sprite.GetVertexAttribute(VertexAttribute.Color));
                }
                GL.PushMatrix();
                GL.LoadOrtho();
                GL.Color(new Color(1f, 1f, 1f, 1f));
                GL.Begin(4);
                for (int i = 0; i < triangles.Length; i++)
                {
                    ushort  num2    = triangles[i];
                    Vector2 vector2 = vertices[(int)num2];
                    Vector2 vector3 = uv[(int)num2];
                    GL.TexCoord(new Vector3(vector3.x, vector3.y, 0f));
                    if (nativeSlice.HasValue)
                    {
                        GL.Color(nativeSlice.Value[(int)num2]);
                    }
                    GL.Vertex3((vector2.x * num + pivot.x) / width2, (vector2.y * num + pivot.y) / height2, 0f);
                }
                GL.End();
                GL.PopMatrix();
                if (spriteRendererMaterial != null)
                {
                    if (flag)
                    {
                        spriteRendererMaterial.SetTexture("_MainTex", value);
                    }
                    if (flag2)
                    {
                        spriteRendererMaterial.SetVector("_MainTex_TexelSize", vector);
                    }
                }
                RenderTexture temporary2 = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
                Graphics.Blit(temporary, temporary2, EditorGUIUtility.GUITextureBlit2SRGBMaterial);
                RenderTexture.active = temporary2;
                Texture2D texture2D = new Texture2D(width, height, TextureFormat.RGBA32, false);
                texture2D.hideFlags  = HideFlags.HideAndDontSave;
                texture2D.filterMode = ((!(spriteTexture != null)) ? FilterMode.Point : spriteTexture.filterMode);
                texture2D.anisoLevel = ((!(spriteTexture != null)) ? 0 : spriteTexture.anisoLevel);
                texture2D.wrapMode   = ((!(spriteTexture != null)) ? TextureWrapMode.Clamp : spriteTexture.wrapMode);
                texture2D.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0);
                texture2D.Apply();
                RenderTexture.ReleaseTemporary(temporary);
                RenderTexture.ReleaseTemporary(temporary2);
                savedRenderTargetState.Restore();
                if (material != null)
                {
                    UnityEngine.Object.DestroyImmediate(material);
                }
                result = texture2D;
            }
            return(result);
        }
		internal static void ScaleImage(int w, int h, Texture2D inimage, Texture2D outimage, GUIStyle bgStyle)
		{
			EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
			SavedRenderTargetState savedRenderTargetState = new SavedRenderTargetState();
			if (AssetStorePreviewManager.s_RenderTexture != null && (AssetStorePreviewManager.s_RenderTexture.width != w || AssetStorePreviewManager.s_RenderTexture.height != h))
			{
				UnityEngine.Object.DestroyImmediate(AssetStorePreviewManager.s_RenderTexture);
				AssetStorePreviewManager.s_RenderTexture = null;
			}
			if (AssetStorePreviewManager.s_RenderTexture == null)
			{
				AssetStorePreviewManager.s_RenderTexture = RenderTexture.GetTemporary(w, h, 16, RenderTextureFormat.ARGB32, RenderTextureReadWrite.Linear);
				AssetStorePreviewManager.s_RenderTexture.hideFlags = HideFlags.HideAndDontSave;
			}
			RenderTexture renderTexture = AssetStorePreviewManager.s_RenderTexture;
			RenderTexture.active = renderTexture;
			Rect rect = new Rect(0f, 0f, (float)w, (float)h);
			EditorGUIUtility.SetRenderTextureNoViewport(renderTexture);
			GL.LoadOrtho();
			GL.LoadPixelMatrix(0f, (float)w, (float)h, 0f);
			ShaderUtil.rawViewportRect = new Rect(0f, 0f, (float)w, (float)h);
			ShaderUtil.rawScissorRect = new Rect(0f, 0f, (float)w, (float)h);
			GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
			Rect screenRect = rect;
			if (inimage.width > inimage.height)
			{
				float num = screenRect.height * ((float)inimage.height / (float)inimage.width);
				screenRect.height = (float)((int)num);
				screenRect.y += (float)((int)(num * 0.5f));
			}
			else
			{
				if (inimage.width < inimage.height)
				{
					float num2 = screenRect.width * ((float)inimage.width / (float)inimage.height);
					screenRect.width = (float)((int)num2);
					screenRect.x += (float)((int)(num2 * 0.5f));
				}
			}
			if (bgStyle != null && bgStyle.normal != null && bgStyle.normal.background != null)
			{
				Graphics.DrawTexture(rect, bgStyle.normal.background);
			}
			Graphics.DrawTexture(screenRect, inimage);
			outimage.ReadPixels(rect, 0, 0, false);
			outimage.Apply();
			outimage.hideFlags = HideFlags.HideAndDontSave;
			savedRenderTargetState.Restore();
			EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
		}
 public static Texture2D BuildPreviewTexture(int width, int height, Sprite sprite, Material spriteRendererMaterial, bool isPolygon)
 {
     if (!ShaderUtil.hardwareSupportsRectRenderTexture)
     {
         return null;
     }
     float num = sprite.rect.width;
     float num2 = sprite.rect.height;
     Texture2D spriteTexture = SpriteUtility.GetSpriteTexture(sprite, false);
     if (!isPolygon)
     {
         PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int) num, (int) num2, ref width, ref height);
     }
     SavedRenderTargetState state = new SavedRenderTargetState();
     RenderTexture temp = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
     RenderTexture.active = temp;
     GL.sRGBWrite = QualitySettings.activeColorSpace == ColorSpace.Linear;
     GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
     Texture texture = null;
     Vector4 vector = new Vector4(0f, 0f, 0f, 0f);
     bool flag = false;
     bool flag2 = false;
     if (spriteRendererMaterial != null)
     {
         flag = spriteRendererMaterial.HasProperty("_MainTex");
         flag2 = spriteRendererMaterial.HasProperty("_MainTex_TexelSize");
     }
     Material material = null;
     if (spriteRendererMaterial != null)
     {
         if (flag)
         {
             texture = spriteRendererMaterial.GetTexture("_MainTex");
             spriteRendererMaterial.SetTexture("_MainTex", spriteTexture);
         }
         if (flag2)
         {
             vector = spriteRendererMaterial.GetVector("_MainTex_TexelSize");
             spriteRendererMaterial.SetVector("_MainTex_TexelSize", TextureUtil.GetTexelSizeVector(spriteTexture));
         }
         spriteRendererMaterial.SetPass(0);
     }
     else
     {
         material = new Material(Shader.Find("Hidden/BlitCopy")) {
             mainTexture = spriteTexture,
             mainTextureScale = Vector2.one,
             mainTextureOffset = Vector2.zero
         };
         material.SetPass(0);
     }
     float num3 = sprite.rect.width / sprite.bounds.size.x;
     Vector2[] vertices = sprite.vertices;
     Vector2[] uv = sprite.uv;
     ushort[] triangles = sprite.triangles;
     Vector2 pivot = sprite.pivot;
     GL.PushMatrix();
     GL.LoadOrtho();
     GL.Color(new Color(1f, 1f, 1f, 1f));
     GL.Begin(4);
     for (int i = 0; i < triangles.Length; i++)
     {
         ushort index = triangles[i];
         Vector2 vector4 = vertices[index];
         Vector2 vector5 = uv[index];
         GL.TexCoord(new Vector3(vector5.x, vector5.y, 0f));
         GL.Vertex3(((vector4.x * num3) + pivot.x) / num, ((vector4.y * num3) + pivot.y) / num2, 0f);
     }
     GL.End();
     GL.PopMatrix();
     GL.sRGBWrite = false;
     if (spriteRendererMaterial != null)
     {
         if (flag)
         {
             spriteRendererMaterial.SetTexture("_MainTex", texture);
         }
         if (flag2)
         {
             spriteRendererMaterial.SetVector("_MainTex_TexelSize", vector);
         }
     }
     Texture2D textured3 = new Texture2D(width, height, TextureFormat.RGBA32, false) {
         hideFlags = HideFlags.HideAndDontSave,
         filterMode = spriteTexture.filterMode,
         anisoLevel = spriteTexture.anisoLevel,
         wrapMode = spriteTexture.wrapMode
     };
     textured3.ReadPixels(new Rect(0f, 0f, (float) width, (float) height), 0, 0);
     textured3.Apply();
     RenderTexture.ReleaseTemporary(temp);
     state.Restore();
     if (material != null)
     {
         Object.DestroyImmediate(material);
     }
     return textured3;
 }
Example #13
0
        private bool UpdateTextures(TextureAtlas atlas, List<TreeMaterial> materials)
        {
            if (!this.root.enableMaterialOptimize)
            {
                return false;
            }
            bool flag = ((((this.optimizedSolidMaterial.GetTexture("_MainTex") != null) && (this.optimizedSolidMaterial.GetTexture("_BumpSpecMap") != null)) && ((this.optimizedSolidMaterial.GetTexture("_TranslucencyMap") != null) && (this.optimizedCutoutMaterial.GetTexture("_MainTex") != null))) && ((this.optimizedCutoutMaterial.GetTexture("_ShadowTex") != null) && (this.optimizedCutoutMaterial.GetTexture("_BumpSpecMap") != null))) && ((bool) this.optimizedCutoutMaterial.GetTexture("_TranslucencyMap"));
            UnityEngine.Object[] objects = new UnityEngine.Object[materials.Count];
            for (int i = 0; i < materials.Count; i++)
            {
                objects[i] = materials[i].material;
            }
            string str = InternalEditorUtility.CalculateHashForObjectsAndDependencies(objects) + atlas.GetHashCode();
            if ((this.materialHash == str) && flag)
            {
                this.UpdateShadowTexture(this.optimizedCutoutMaterial.GetTexture("_ShadowTex") as Texture2D, atlas.atlasWidth, atlas.atlasHeight);
                return false;
            }
            this.materialHash = str;
            int atlasWidth = atlas.atlasWidth;
            int atlasHeight = atlas.atlasHeight;
            int atlasPadding = atlas.atlasPadding;
            Texture2D textured = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
            Texture2D textured2 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.RGB24, true);
            Texture2D textured3 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
            Texture2D textured4 = new Texture2D(atlasWidth, atlasHeight, TextureFormat.ARGB32, true);
            textured.name = "diffuse";
            textured2.name = "shadow";
            textured3.name = "normal_specular";
            textured4.name = "translucency_gloss";
            SavedRenderTargetState state = new SavedRenderTargetState();
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
            RenderTexture temp = RenderTexture.GetTemporary(atlasWidth, atlasHeight, 0, RenderTextureFormat.ARGB32);
            Color white = Color.white;
            Color color = new Color(0.03f, 0.5f, 0f, 0.5f);
            Color color3 = new Color(0f, 0f, 0f, 0f);
            Texture2D textured5 = new Texture2D(1, 1);
            textured5.SetPixel(0, 0, white);
            textured5.Apply();
            Texture2D textured6 = new Texture2D(1, 1);
            textured6.SetPixel(0, 0, white);
            textured6.Apply();
            Texture2D textured7 = new Texture2D(1, 1);
            textured7.SetPixel(0, 0, color);
            textured7.Apply();
            Texture2D textured8 = new Texture2D(1, 1);
            textured8.SetPixel(0, 0, color3);
            textured8.Apply();
            Texture2D textured9 = textured8;
            Texture2D textured10 = new Texture2D(1, 1);
            textured10.SetPixel(0, 0, Color.white);
            textured10.Apply();
            Material material = EditorGUIUtility.LoadRequired("Inspectors/TreeCreator/TreeTextureCombinerMaterial.mat") as Material;
            for (int j = 0; j < 4; j++)
            {
                RenderTexture.active = temp;
                GL.LoadPixelMatrix(0f, (float) atlasWidth, 0f, (float) atlasHeight);
                material.SetVector("_TexSize", new Vector4((float) atlasWidth, (float) atlasHeight, 0f, 0f));
                switch (j)
                {
                    case 0:
                        GL.Clear(false, true, color);
                        break;

                    case 1:
                        GL.Clear(false, true, color3);
                        break;

                    case 2:
                        GL.Clear(false, true, color3);
                        break;

                    case 3:
                        GL.Clear(false, true, color3);
                        break;
                }
                for (int k = 0; k < atlas.nodes.Count; k++)
                {
                    TextureAtlas.TextureNode node = atlas.nodes[k];
                    Rect packedRect = node.packedRect;
                    Texture rgbTexture = null;
                    Texture alphaTexture = null;
                    Color diffuseColor = new Color();
                    switch (j)
                    {
                        case 0:
                            rgbTexture = node.normalTexture;
                            alphaTexture = node.shadowOffsetTexture;
                            diffuseColor = new Color(node.shininess, 0f, 0f, 0f);
                            if (rgbTexture == null)
                            {
                                rgbTexture = textured7;
                            }
                            if (alphaTexture == null)
                            {
                                alphaTexture = textured9;
                            }
                            break;

                        case 1:
                            rgbTexture = node.diffuseTexture;
                            diffuseColor = node.diffuseColor;
                            if (rgbTexture == null)
                            {
                                rgbTexture = textured5;
                            }
                            break;

                        case 2:
                            rgbTexture = node.translucencyTexture;
                            alphaTexture = node.glossTexture;
                            if (rgbTexture == null)
                            {
                                rgbTexture = textured10;
                            }
                            if (alphaTexture == null)
                            {
                                alphaTexture = textured8;
                            }
                            break;

                        case 3:
                            alphaTexture = node.diffuseTexture;
                            if (alphaTexture == null)
                            {
                                alphaTexture = textured5;
                            }
                            break;
                    }
                    if (node.tileV)
                    {
                        float x = packedRect.x;
                        float num8 = ((float) atlasPadding) / 2f;
                        for (float m = num8; m > 0f; m--)
                        {
                            Rect rect = new Rect(packedRect);
                            Rect rect3 = new Rect(packedRect);
                            rect.x = x - m;
                            rect3.x = x + m;
                            this.DrawTexture(rect, rgbTexture, alphaTexture, material, diffuseColor, j);
                            this.DrawTexture(rect3, rgbTexture, alphaTexture, material, diffuseColor, j);
                        }
                    }
                    this.DrawTexture(packedRect, rgbTexture, alphaTexture, material, diffuseColor, j);
                }
                switch (j)
                {
                    case 0:
                        textured3.ReadPixels(new Rect(0f, 0f, (float) atlasWidth, (float) atlasHeight), 0, 0);
                        textured3.Apply(true);
                        break;

                    case 1:
                        textured.ReadPixels(new Rect(0f, 0f, (float) atlasWidth, (float) atlasHeight), 0, 0);
                        textured.Apply(true);
                        break;

                    case 2:
                        textured4.ReadPixels(new Rect(0f, 0f, (float) atlasWidth, (float) atlasHeight), 0, 0);
                        textured4.Apply(true);
                        break;

                    case 3:
                        textured2.ReadPixels(new Rect(0f, 0f, (float) atlasWidth, (float) atlasHeight), 0, 0);
                        textured2.Apply(true);
                        break;
                }
            }
            state.Restore();
            this.optimizedSolidMaterial.SetPass(0);
            RenderTexture.ReleaseTemporary(temp);
            UnityEngine.Object.DestroyImmediate(textured5);
            UnityEngine.Object.DestroyImmediate(textured6);
            UnityEngine.Object.DestroyImmediate(textured10);
            UnityEngine.Object.DestroyImmediate(textured8);
            UnityEngine.Object.DestroyImmediate(textured7);
            EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
            Texture2D[] textures = new Texture2D[] { textured, textured3, textured4, textured2 };
            textures = WriteOptimizedTextures(AssetDatabase.GetAssetPath(this), textures);
            UnityEngine.Object.DestroyImmediate(textured);
            UnityEngine.Object.DestroyImmediate(textured3);
            UnityEngine.Object.DestroyImmediate(textured4);
            UnityEngine.Object.DestroyImmediate(textured2);
            this.optimizedSolidMaterial.SetTexture("_MainTex", textures[0]);
            this.optimizedSolidMaterial.SetTexture("_BumpSpecMap", textures[1]);
            this.optimizedSolidMaterial.SetTexture("_TranslucencyMap", textures[2]);
            this.optimizedCutoutMaterial.SetTexture("_MainTex", textures[0]);
            this.optimizedCutoutMaterial.SetTexture("_BumpSpecMap", textures[1]);
            this.optimizedCutoutMaterial.SetTexture("_TranslucencyMap", textures[2]);
            this.optimizedCutoutMaterial.SetTexture("_ShadowTex", textures[3]);
            this.UpdateShadowTexture(textures[3], atlas.atlasWidth, atlas.atlasHeight);
            return true;
        }
        public static Texture2D BuildPreviewTexture(int width, int height, Sprite sprite, Material spriteRendererMaterial, bool isPolygon)
        {
            if (!ShaderUtil.hardwareSupportsRectRenderTexture)
            {
                return((Texture2D)null);
            }
            float     width1        = sprite.rect.width;
            float     height1       = sprite.rect.height;
            Texture2D spriteTexture = UnityEditor.Sprites.SpriteUtility.GetSpriteTexture(sprite, false);

            if (!isPolygon)
            {
                PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int)width1, (int)height1, ref width, ref height);
            }
            SavedRenderTargetState renderTargetState = new SavedRenderTargetState();
            RenderTexture          temporary         = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);

            RenderTexture.active = temporary;
            GL.sRGBWrite         = QualitySettings.activeColorSpace == ColorSpace.Linear;
            GL.Clear(true, true, new Color(0.0f, 0.0f, 0.0f, 0.0f));
            Texture texture = (Texture)null;
            Vector4 vector  = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
            bool    flag1   = false;
            bool    flag2   = false;

            if ((Object)spriteRendererMaterial != (Object)null)
            {
                flag1 = spriteRendererMaterial.HasProperty("_MainTex");
                flag2 = spriteRendererMaterial.HasProperty("_MainTex_TexelSize");
            }
            Material material = (Material)null;

            if ((Object)spriteRendererMaterial != (Object)null)
            {
                if (flag1)
                {
                    texture = spriteRendererMaterial.GetTexture("_MainTex");
                    spriteRendererMaterial.SetTexture("_MainTex", (Texture)spriteTexture);
                }
                if (flag2)
                {
                    vector = spriteRendererMaterial.GetVector("_MainTex_TexelSize");
                    spriteRendererMaterial.SetVector("_MainTex_TexelSize", TextureUtil.GetTexelSizeVector((Texture)spriteTexture));
                }
                spriteRendererMaterial.SetPass(0);
            }
            else
            {
                material                   = new Material(Shader.Find("Hidden/BlitCopy"));
                material.mainTexture       = (Texture)spriteTexture;
                material.mainTextureScale  = Vector2.one;
                material.mainTextureOffset = Vector2.zero;
                material.SetPass(0);
            }
            float num1 = sprite.rect.width / sprite.bounds.size.x;

            Vector2[] vertices  = sprite.vertices;
            Vector2[] uv        = sprite.uv;
            ushort[]  triangles = sprite.triangles;
            Vector2   pivot     = sprite.pivot;

            GL.PushMatrix();
            GL.LoadOrtho();
            GL.Color(new Color(1f, 1f, 1f, 1f));
            GL.Begin(4);
            for (int index = 0; index < triangles.Length; ++index)
            {
                ushort  num2      = triangles[index];
                Vector2 vector2_1 = vertices[(int)num2];
                Vector2 vector2_2 = uv[(int)num2];
                GL.TexCoord(new Vector3(vector2_2.x, vector2_2.y, 0.0f));
                GL.Vertex3((vector2_1.x * num1 + pivot.x) / width1, (vector2_1.y * num1 + pivot.y) / height1, 0.0f);
            }
            GL.End();
            GL.PopMatrix();
            GL.sRGBWrite = false;
            if ((Object)spriteRendererMaterial != (Object)null)
            {
                if (flag1)
                {
                    spriteRendererMaterial.SetTexture("_MainTex", texture);
                }
                if (flag2)
                {
                    spriteRendererMaterial.SetVector("_MainTex_TexelSize", vector);
                }
            }
            Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);

            texture2D.hideFlags = HideFlags.HideAndDontSave;
            texture2D.ReadPixels(new Rect(0.0f, 0.0f, (float)width, (float)height), 0, 0);
            texture2D.Apply();
            RenderTexture.ReleaseTemporary(temporary);
            renderTargetState.Restore();
            if ((Object)material != (Object)null)
            {
                Object.DestroyImmediate((Object)material);
            }
            return(texture2D);
        }
		private static Texture2D BuildPreviewTexture(int width, int height, Sprite sprite, Material spriteRendererMaterial)
		{
			if (!ShaderUtil.hardwareSupportsRectRenderTexture)
			{
				return null;
			}
			float width2 = sprite.rect.width;
			float height2 = sprite.rect.height;
			Texture2D spriteTexture = UnityEditor.Sprites.SpriteUtility.GetSpriteTexture(sprite, false);
			PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int)width2, (int)height2, ref width, ref height);
			EditorUtility.SetTemporarilyAllowIndieRenderTexture(true);
			SavedRenderTargetState savedRenderTargetState = new SavedRenderTargetState();
			RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
			RenderTexture.active = temporary;
			GL.sRGBWrite = (QualitySettings.activeColorSpace == ColorSpace.Linear);
			GL.Clear(true, true, new Color(0f, 0f, 0f, 0f));
			Texture texture = null;
			Vector4 vector = new Vector4(0f, 0f, 0f, 0f);
			bool flag = false;
			bool flag2 = false;
			if (spriteRendererMaterial != null)
			{
				flag = spriteRendererMaterial.HasProperty("_MainTex");
				flag2 = spriteRendererMaterial.HasProperty("_MainTex_TexelSize");
			}
			Material material = null;
			if (spriteRendererMaterial != null)
			{
				if (flag)
				{
					texture = spriteRendererMaterial.GetTexture("_MainTex");
					spriteRendererMaterial.SetTexture("_MainTex", spriteTexture);
				}
				if (flag2)
				{
					vector = spriteRendererMaterial.GetVector("_MainTex_TexelSize");
					spriteRendererMaterial.SetVector("_MainTex_TexelSize", TextureUtil.GetTexelSizeVector(spriteTexture));
				}
				spriteRendererMaterial.SetPass(0);
			}
			else
			{
				material = new Material(Shader.Find("Hidden/BlitCopy"));
				material.mainTexture = spriteTexture;
				material.SetPass(0);
			}
			float num = sprite.rect.width / sprite.bounds.size.x;
			Vector2[] vertices = sprite.vertices;
			Vector2[] uv = sprite.uv;
			ushort[] triangles = sprite.triangles;
			Vector2 pivot = sprite.pivot;
			GL.PushMatrix();
			GL.LoadOrtho();
			GL.Color(new Color(1f, 1f, 1f, 1f));
			GL.Begin(4);
			for (int i = 0; i < sprite.triangles.Length; i++)
			{
				ushort num2 = triangles[i];
				Vector2 vector2 = vertices[(int)num2];
				Vector2 vector3 = uv[(int)num2];
				GL.TexCoord(new Vector3(vector3.x, vector3.y, 0f));
				GL.Vertex3((vector2.x * num + pivot.x) / width2, (vector2.y * num + pivot.y) / height2, 0f);
			}
			GL.End();
			GL.PopMatrix();
			GL.sRGBWrite = false;
			if (spriteRendererMaterial != null)
			{
				if (flag)
				{
					spriteRendererMaterial.SetTexture("_MainTex", texture);
				}
				if (flag2)
				{
					spriteRendererMaterial.SetVector("_MainTex_TexelSize", vector);
				}
			}
			Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
			texture2D.hideFlags = HideFlags.HideAndDontSave;
			texture2D.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0);
			texture2D.Apply();
			RenderTexture.ReleaseTemporary(temporary);
			savedRenderTargetState.Restore();
			EditorUtility.SetTemporarilyAllowIndieRenderTexture(false);
			if (material != null)
			{
				UnityEngine.Object.DestroyImmediate(material);
			}
			return texture2D;
		}
 public static Texture2D BuildPreviewTexture(int width, int height, Sprite sprite, Material spriteRendererMaterial, bool isPolygon)
 {
   if (!ShaderUtil.hardwareSupportsRectRenderTexture)
     return (Texture2D) null;
   float width1 = sprite.rect.width;
   float height1 = sprite.rect.height;
   Texture2D spriteTexture = UnityEditor.Sprites.SpriteUtility.GetSpriteTexture(sprite, false);
   if (!isPolygon)
     PreviewHelpers.AdjustWidthAndHeightForStaticPreview((int) width1, (int) height1, ref width, ref height);
   SavedRenderTargetState renderTargetState = new SavedRenderTargetState();
   RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Default);
   RenderTexture.active = temporary;
   GL.sRGBWrite = QualitySettings.activeColorSpace == ColorSpace.Linear;
   GL.Clear(true, true, new Color(0.0f, 0.0f, 0.0f, 0.0f));
   Texture texture = (Texture) null;
   Vector4 vector = new Vector4(0.0f, 0.0f, 0.0f, 0.0f);
   bool flag1 = false;
   bool flag2 = false;
   if ((Object) spriteRendererMaterial != (Object) null)
   {
     flag1 = spriteRendererMaterial.HasProperty("_MainTex");
     flag2 = spriteRendererMaterial.HasProperty("_MainTex_TexelSize");
   }
   Material material = (Material) null;
   if ((Object) spriteRendererMaterial != (Object) null)
   {
     if (flag1)
     {
       texture = spriteRendererMaterial.GetTexture("_MainTex");
       spriteRendererMaterial.SetTexture("_MainTex", (Texture) spriteTexture);
     }
     if (flag2)
     {
       vector = spriteRendererMaterial.GetVector("_MainTex_TexelSize");
       spriteRendererMaterial.SetVector("_MainTex_TexelSize", TextureUtil.GetTexelSizeVector((Texture) spriteTexture));
     }
     spriteRendererMaterial.SetPass(0);
   }
   else
   {
     material = new Material(Shader.Find("Hidden/BlitCopy"));
     material.mainTexture = (Texture) spriteTexture;
     material.mainTextureScale = Vector2.one;
     material.mainTextureOffset = Vector2.zero;
     material.SetPass(0);
   }
   float num1 = sprite.rect.width / sprite.bounds.size.x;
   Vector2[] vertices = sprite.vertices;
   Vector2[] uv = sprite.uv;
   ushort[] triangles = sprite.triangles;
   Vector2 pivot = sprite.pivot;
   GL.PushMatrix();
   GL.LoadOrtho();
   GL.Color(new Color(1f, 1f, 1f, 1f));
   GL.Begin(4);
   for (int index = 0; index < triangles.Length; ++index)
   {
     ushort num2 = triangles[index];
     Vector2 vector2_1 = vertices[(int) num2];
     Vector2 vector2_2 = uv[(int) num2];
     GL.TexCoord(new Vector3(vector2_2.x, vector2_2.y, 0.0f));
     GL.Vertex3((vector2_1.x * num1 + pivot.x) / width1, (vector2_1.y * num1 + pivot.y) / height1, 0.0f);
   }
   GL.End();
   GL.PopMatrix();
   GL.sRGBWrite = false;
   if ((Object) spriteRendererMaterial != (Object) null)
   {
     if (flag1)
       spriteRendererMaterial.SetTexture("_MainTex", texture);
     if (flag2)
       spriteRendererMaterial.SetVector("_MainTex_TexelSize", vector);
   }
   Texture2D texture2D = new Texture2D(width, height, TextureFormat.ARGB32, false);
   texture2D.hideFlags = HideFlags.HideAndDontSave;
   texture2D.ReadPixels(new Rect(0.0f, 0.0f, (float) width, (float) height), 0, 0);
   texture2D.Apply();
   RenderTexture.ReleaseTemporary(temporary);
   renderTargetState.Restore();
   if ((Object) material != (Object) null)
     Object.DestroyImmediate((Object) material);
   return texture2D;
 }