Skip to content

Commit

Permalink
Started caching materials and moved texture-changing methods to Textu…
Browse files Browse the repository at this point in the history
…reHelper

This shaved off 10-20 more ms.
  • Loading branch information
SolidAlloy authored and xsduan committed Feb 23, 2021
1 parent 3f96964 commit 47c9807
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 47 deletions.
49 changes: 4 additions & 45 deletions Editor/Icons/HierarchyFolderIcon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;
using UnityEngine.Experimental.Rendering;
using UnityHierarchyFolders.Runtime;
using Object = UnityEngine.Object;

Expand Down Expand Up @@ -74,46 +73,6 @@ private static void Startup()
EditorApplication.hierarchyWindowItemOnGUI += RefreshFolderIcons;
}

private static Texture2D GetTintedTexture(Texture2D original, Color tint, string name)
{
var material = new Material(Shader.Find("UI/Default")) { color = tint };
return GetTextureWithMaterial(original, material, name);
}

private static Texture2D GetWhiteTexture(Texture2D original, string name)
{
var material = new Material(Shader.Find("UI/Replace color")) { color = Color.white };
return GetTextureWithMaterial(original, material, name);
}

private static Texture2D GetTextureWithMaterial(Texture2D original, Material material, string name)
{
Texture2D newTexture;

using (new TextureHelper.SRGBWriteScope(true))
{
using (var temporary = new TextureHelper.TemporaryActiveTexture(original.width, original.height, 0))
{
GL.Clear(false, true, new Color(1f, 1f, 1f, 0f));

Graphics.Blit(original, temporary, material);

newTexture = new Texture2D(original.width, original.width, TextureFormat.ARGB32, false, true)
{
name = name,
filterMode = FilterMode.Bilinear,
hideFlags = HideFlags.DontSave
};

newTexture.ReadPixels(new Rect(0.0f, 0.0f, original.width, original.width), 0, 0);
newTexture.alphaIsTransparency = true;
newTexture.Apply();
}
}

return newTexture;
}

private static void InitIfNeeded()
{
if (_isInitialized) { return; }
Expand All @@ -124,8 +83,8 @@ private static void InitIfNeeded()
// We could use the actual white folder icons but I prefer the look of the tinted white folder icon
// To use the actual white version:
// texture = (Texture2D) EditorGUIUtility.IconContent($"{OpenedFolderPrefix | ClosedFolderPrefix} On Icon").image;
_openFolderSelectedTexture = GetWhiteTexture(_openFolderTexture, $"{_openedFolderPrefix} Icon White");
_closedFolderSelectedTexture = GetWhiteTexture(_closedFolderTexture, $"{_closedFolderPrefix} Icon White");
_openFolderSelectedTexture = TextureHelper.GetWhiteTexture(_openFolderTexture, $"{_openedFolderPrefix} Icon White");
_closedFolderSelectedTexture = TextureHelper.GetWhiteTexture(_closedFolderTexture, $"{_closedFolderPrefix} Icon White");

_coloredFolderIcons = new (Texture2D, Texture2D)[] { (_openFolderTexture, _closedFolderTexture) };

Expand All @@ -136,9 +95,9 @@ private static void InitIfNeeded()
int index = 1 + column + row * IconColumnCount;
var color = IconColors[column, row];

var openFolderIcon = GetTintedTexture(_openFolderSelectedTexture,
var openFolderIcon = TextureHelper.GetTintedTexture(_openFolderSelectedTexture,
color, $"{_openFolderSelectedTexture.name} {index}");
var closedFolderIcon = GetTintedTexture(_closedFolderSelectedTexture,
var closedFolderIcon = TextureHelper.GetTintedTexture(_closedFolderSelectedTexture,
color, $"{_closedFolderSelectedTexture.name} {index}");

ArrayUtility.Add(ref _coloredFolderIcons, (openFolderIcon, closedFolderIcon));
Expand Down
65 changes: 63 additions & 2 deletions Editor/Icons/TextureHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,69 @@ namespace UnityHierarchyFolders.Editor
using UnityEngine;

/// <summary>Helps to create new textures.</summary>
public static class TextureHelper
internal static class TextureHelper
{
private static readonly Color _fullyTransparent = new Color(1f, 1f, 1f, 0f);

private static Material _tintMaterial;
private static Material _colorReplaceMaterial;

public static Texture2D GetTintedTexture(Texture2D original, Color tint, string name)
{
return GetTextureWithMaterial(original, GetTintMaterial(tint), name);
}

public static Texture2D GetWhiteTexture(Texture2D original, string name)
{
return GetTextureWithMaterial(original, GetColorReplaceMaterial(Color.white), name);
}

private static Material GetTintMaterial(Color tint)
{
if (_tintMaterial == null)
_tintMaterial = new Material(Shader.Find("UI/Default"));

_tintMaterial.color = tint;
return _tintMaterial;
}

private static Material GetColorReplaceMaterial(Color color)
{
if (_colorReplaceMaterial == null)
_colorReplaceMaterial = new Material(Shader.Find("UI/Replace color"));

_colorReplaceMaterial.color = color;
return _colorReplaceMaterial;
}

private static Texture2D GetTextureWithMaterial(Texture2D original, Material material, string name)
{
Texture2D newTexture;

using (new SRGBWriteScope(true))
{
using (var temporary = new TemporaryActiveTexture(original.width, original.height, 0))
{
GL.Clear(false, true, _fullyTransparent);

Graphics.Blit(original, temporary, material);

newTexture = new Texture2D(original.width, original.width, TextureFormat.ARGB32, false, true)
{
name = name,
filterMode = FilterMode.Bilinear,
hideFlags = HideFlags.DontSave
};

newTexture.ReadPixels(new Rect(0f, 0f, original.width, original.width), 0, 0);
newTexture.alphaIsTransparency = true;
newTexture.Apply();
}
}

return newTexture;
}

/// <summary>
/// Temporarily sets <see cref="GL.sRGBWrite"/> to the passed value, then returns it back.
/// </summary>
Expand All @@ -22,7 +83,7 @@ public static class TextureHelper
{
private readonly bool _previousValue;

/// <summary>Temporarily sets <see cref="GL.sRGBWrite"/> to <paramref name=""/>, then executes the action.</summary>
/// <summary>Temporarily sets <see cref="GL.sRGBWrite"/> to <c>true</c>, then executes the action.</summary>
/// <param name="enableWrite"> Temporary value of <see cref="GL.sRGBWrite"/>. </param>
/// <example><code>
/// using (new SRGBWriteScope(true))
Expand Down

0 comments on commit 47c9807

Please sign in to comment.