static private AdjustWidthAndHeightForStaticPreview ( int textureWidth, int textureHeight, int &width, int &height ) : void | ||
textureWidth | int | |
textureHeight | int | |
width | int | |
height | int | |
Résultat | void |
public sealed override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height) { TerrainLayer t = AssetDatabase.LoadMainAssetAtPath(assetPath) as TerrainLayer; if (t == null || t.diffuseTexture == null) { return(null); } int texwidth = t.diffuseTexture.width; int texheight = t.diffuseTexture.height; PreviewHelpers.AdjustWidthAndHeightForStaticPreview(texwidth, texheight, ref width, ref height); RenderTexture oldRT = RenderTexture.active; RenderTexture tempRT = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.ARGB32); Graphics.Blit(t.diffuseTexture, tempRT); Texture2D previewTexture = new Texture2D(width, height, TextureFormat.ARGB32, false); RenderTexture.active = tempRT; previewTexture.ReadPixels(new Rect(0, 0, width, height), 0, 0); previewTexture.Apply(); RenderTexture.ReleaseTemporary(tempRT); tempRT = null; RenderTexture.active = oldRT; return(previewTexture); }
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); }
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); }
public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.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)); } PreviewHelpers.AdjustWidthAndHeightForStaticPreview(target.width, target.height, ref width, ref height); EditorUtility.SetTemporarilyAllowIndieRenderTexture(true); 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; EditorUtility.SetTemporarilyAllowIndieRenderTexture(false); if ((materialForSpecialTexture != null) && Unsupported.IsDeveloperBuild()) { UnityEngine.Object.DestroyImmediate(materialForSpecialTexture); } return(textured); }
public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height) { if (!ShaderUtil.hardwareSupportsRectRenderTexture) { return((Texture2D)null); } Texture target1 = this.target as Texture; if (this.IsCubemap()) { return(this.m_CubemapPreview.RenderStaticPreview(target1, width, height)); } TextureImporter atPath = AssetImporter.GetAtPath(assetPath) as TextureImporter; if ((UnityEngine.Object)atPath != (UnityEngine.Object)null && atPath.spriteImportMode == SpriteImportMode.Polygon) { Sprite subAsset = subAssets[0] as Sprite; if ((bool)((UnityEngine.Object)subAsset)) { return(SpriteInspector.BuildPreviewTexture(width, height, subAsset, (Material)null, true)); } } PreviewHelpers.AdjustWidthAndHeightForStaticPreview(target1.width, target1.height, ref width, ref height); RenderTexture active = RenderTexture.active; Rect rawViewportRect = ShaderUtil.rawViewportRect; bool flag = !TextureUtil.GetLinearSampled(target1); RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, !flag ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.sRGB); Material material = EditorGUI.GetMaterialForSpecialTexture(target1); GL.sRGBWrite = QualitySettings.activeColorSpace == ColorSpace.Linear; if ((bool)((UnityEngine.Object)material)) { if (Unsupported.IsDeveloperBuild()) { material = new Material(material); } Graphics.Blit(target1, temporary, material); } else { Graphics.Blit(target1, temporary); } GL.sRGBWrite = false; RenderTexture.active = temporary; Texture2D target2 = this.target as Texture2D; Texture2D texture2D = !((UnityEngine.Object)target2 != (UnityEngine.Object)null) || !target2.alphaIsTransparency ? new Texture2D(width, height, TextureFormat.RGB24, false) : new Texture2D(width, height, TextureFormat.ARGB32, false); texture2D.ReadPixels(new Rect(0.0f, 0.0f, (float)width, (float)height), 0, 0); texture2D.Apply(); RenderTexture.ReleaseTemporary(temporary); EditorGUIUtility.SetRenderTextureNoViewport(active); ShaderUtil.rawViewportRect = rawViewportRect; if ((bool)((UnityEngine.Object)material) && Unsupported.IsDeveloperBuild()) { UnityEngine.Object.DestroyImmediate((UnityEngine.Object)material); } return(texture2D); }
public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height) { Texture2D result; if (!ShaderUtil.hardwareSupportsRectRenderTexture) { result = null; } else { Texture texture = base.target as Texture; if (this.IsCubemap()) { result = this.m_CubemapPreview.RenderStaticPreview(texture, width, height); } else { TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; if (textureImporter != null && textureImporter.textureType == TextureImporterType.Sprite && textureImporter.spriteImportMode == SpriteImportMode.Polygon) { if (subAssets.Length <= 0) { result = null; return(result); } Sprite sprite = subAssets[0] as Sprite; if (sprite) { result = SpriteInspector.BuildPreviewTexture(width, height, sprite, null, true); return(result); } } PreviewHelpers.AdjustWidthAndHeightForStaticPreview(texture.width, texture.height, ref width, ref height); RenderTexture active = RenderTexture.active; Rect rawViewportRect = ShaderUtil.rawViewportRect; RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear); Graphics.Blit(texture, temporary, EditorGUI.GetMaterialForSpecialTexture(texture, EditorGUIUtility.GUITextureBlit2SRGBMaterial)); RenderTexture.active = temporary; Texture2D texture2D = base.target as Texture2D; Texture2D texture2D2; if (texture2D != null && texture2D.alphaIsTransparency) { texture2D2 = new Texture2D(width, height, TextureFormat.RGBA32, false); } else { texture2D2 = new Texture2D(width, height, TextureFormat.RGB24, false); } texture2D2.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0); texture2D2.Apply(); RenderTexture.ReleaseTemporary(temporary); EditorGUIUtility.SetRenderTextureNoViewport(active); ShaderUtil.rawViewportRect = rawViewportRect; result = texture2D2; } } return(result); }
public override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height) { if (!ShaderUtil.hardwareSupportsRectRenderTexture) { return(null); } Texture texture = this.target as Texture; PreviewHelpers.AdjustWidthAndHeightForStaticPreview(texture.width, texture.height, ref width, ref height); EditorUtility.SetTemporarilyAllowIndieRenderTexture(true); RenderTexture active = RenderTexture.active; Rect rawViewportRect = ShaderUtil.rawViewportRect; RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, RenderTextureReadWrite.Linear); Material material = EditorGUI.GetMaterialForSpecialTexture(texture); if (material) { if (Unsupported.IsDeveloperBuild()) { material = new Material(material); } Graphics.Blit(texture, temporary, material); } else { Graphics.Blit(texture, temporary); } RenderTexture.active = temporary; Texture2D texture2D = this.target as Texture2D; Texture2D texture2D2; if (texture2D != null && texture2D.alphaIsTransparency) { texture2D2 = new Texture2D(width, height, TextureFormat.ARGB32, false); } else { texture2D2 = new Texture2D(width, height, TextureFormat.RGB24, false); } texture2D2.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0); texture2D2.Apply(); RenderTexture.ReleaseTemporary(temporary); EditorGUIUtility.SetRenderTextureNoViewport(active); ShaderUtil.rawViewportRect = rawViewportRect; EditorUtility.SetTemporarilyAllowIndieRenderTexture(false); if (material && Unsupported.IsDeveloperBuild()) { UnityEngine.Object.DestroyImmediate(material); } return(texture2D2); }
public sealed override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height) { Brush brush = AssetDatabase.LoadMainAssetAtPath(assetPath) as Brush; if (brush == null) { return(null); } if (brush.m_Mask == null) { brush.m_Mask = Brush.DefaultMask(); } PreviewHelpers.AdjustWidthAndHeightForStaticPreview(brush.m_Mask.width, brush.m_Mask.height, ref width, ref height); return(Brush.GenerateBrushTexture(brush.m_Mask, brush.m_Falloff, brush.m_RadiusScale, brush.m_BlackWhiteRemapMin, brush.m_BlackWhiteRemapMax, brush.m_InvertRemapRange, width, height, true)); }
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 override Texture2D RenderStaticPreview(string assetPath, UnityEngine.Object[] subAssets, int width, int height) { Texture2D result; if (!ShaderUtil.hardwareSupportsRectRenderTexture) { result = null; } else { Texture texture = base.target as Texture; if (this.IsCubemap()) { result = this.m_CubemapPreview.RenderStaticPreview(texture, width, height); } else { TextureImporter textureImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; if (textureImporter != null && textureImporter.textureType == TextureImporterType.Sprite && textureImporter.spriteImportMode == SpriteImportMode.Polygon) { Sprite sprite = subAssets[0] as Sprite; if (sprite) { result = SpriteInspector.BuildPreviewTexture(width, height, sprite, null, true); return(result); } } PreviewHelpers.AdjustWidthAndHeightForStaticPreview(texture.width, texture.height, ref width, ref height); RenderTexture active = RenderTexture.active; Rect rawViewportRect = ShaderUtil.rawViewportRect; bool flag = !TextureUtil.GetLinearSampled(texture); RenderTexture temporary = RenderTexture.GetTemporary(width, height, 0, RenderTextureFormat.Default, (!flag) ? RenderTextureReadWrite.Linear : RenderTextureReadWrite.sRGB); Material material = EditorGUI.GetMaterialForSpecialTexture(texture); GL.sRGBWrite = (QualitySettings.activeColorSpace == ColorSpace.Linear); if (material) { if (Unsupported.IsDeveloperBuild()) { material = new Material(material); } Graphics.Blit(texture, temporary, material); } else { Graphics.Blit(texture, temporary); } GL.sRGBWrite = false; RenderTexture.active = temporary; Texture2D texture2D = base.target as Texture2D; Texture2D texture2D2; if (texture2D != null && texture2D.alphaIsTransparency) { texture2D2 = new Texture2D(width, height, TextureFormat.RGBA32, false); } else { texture2D2 = new Texture2D(width, height, TextureFormat.RGB24, false); } texture2D2.ReadPixels(new Rect(0f, 0f, (float)width, (float)height), 0, 0); texture2D2.Apply(); RenderTexture.ReleaseTemporary(temporary); EditorGUIUtility.SetRenderTextureNoViewport(active); ShaderUtil.rawViewportRect = rawViewportRect; if (material && Unsupported.IsDeveloperBuild()) { UnityEngine.Object.DestroyImmediate(material); } result = texture2D2; } } return(result); }
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, null, QualitySettings.activeColorSpace == ColorSpace.Linear, false); 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); }
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 override Texture2D RenderStaticPreview(string assetPath, Object[] subAssets, int width, int height) { if (!ShaderUtil.hardwareSupportsRectRenderTexture) { return(null); } Texture texture = target as Texture; GraphicsFormat format = texture.graphicsFormat; if (!(GraphicsFormatUtility.IsIEEE754Format(format) || GraphicsFormatUtility.IsNormFormat(format))) { // Can't generate correct previews for non-float/norm formats. On Metal and Vulkan this even causes validation errors. return(null); } if (IsCubemap()) { return(m_CubemapPreview.RenderStaticPreview(texture, width, height, GetExposureValueForTexture(texture))); } if (IsTexture3D()) { return(m_Texture3DPreview.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(sprite, null, true, width, height)); } } else { return(null); } } PreviewHelpers.AdjustWidthAndHeightForStaticPreview(texture.width, texture.height, ref width, ref height); RenderTexture savedRT = RenderTexture.active; Rect savedViewport = ShaderUtil.rawViewportRect; var rt = texture as RenderTexture; if (rt != null) { rt.Create(); // Ensure RT is created. Otherwise the first attempted Blit will end up binding a dummy 2D Texture where it expects a 2D Texture Array. (validation errors observed on Vulkan/Metal) } RenderTexture tmp = RenderTexture.GetTemporary( width, height, 0, SystemInfo.GetGraphicsFormat(DefaultFormat.LDR)); Material mat = EditorGUI.GetMaterialForSpecialTexture(texture, null, QualitySettings.activeColorSpace == ColorSpace.Linear, false); 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); }
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((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); }