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); }
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); }
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); }
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); }