1+ // This software includes third-party software subject to the associated copyrights, as follows:
2+ //
3+ // Name: SolidUtilities
4+ // Repo: https://github.com/SolidAlloy/SolidUtilities
5+ // License: MIT (https://github.com/SolidAlloy/SolidUtilities/blob/master/LICENSE)
6+ // Copyright (c) 2020 SolidAlloy
7+
8+ namespace UnityHierarchyFolders . Editor
9+ {
10+ using System ;
11+ using JetBrains . Annotations ;
12+ using UnityEngine ;
13+
14+ /// <summary>Helps to create new textures.</summary>
15+ public static class TextureHelper
16+ {
17+ /// <summary>
18+ /// Temporarily sets <see cref="GL.sRGBWrite"/> to the passed value, then returns it back.
19+ /// </summary>
20+ [ PublicAPI ]
21+ public readonly struct SRGBWriteScope : IDisposable
22+ {
23+ private readonly bool _previousValue ;
24+
25+ /// <summary>Temporarily sets <see cref="GL.sRGBWrite"/> to <paramref name=""/>, then executes the action.</summary>
26+ /// <param name="enableWrite"> Temporary value of <see cref="GL.sRGBWrite"/>. </param>
27+ /// <example><code>
28+ /// using (new SRGBWriteScope(true))
29+ /// {
30+ /// GL.Clear(false, true, new Color(1f, 1f, 1f, 0f));
31+ /// Graphics.Blit(Default, temporary, material);
32+ /// });
33+ /// </code></example>
34+ public SRGBWriteScope ( bool enableWrite )
35+ {
36+ _previousValue = GL . sRGBWrite ;
37+ GL . sRGBWrite = enableWrite ;
38+ }
39+
40+ public void Dispose ( )
41+ {
42+ GL . sRGBWrite = _previousValue ;
43+ }
44+ }
45+
46+ /// <summary>
47+ /// Creates a temporary texture, sets it as active in <see cref="RenderTexture.active"/>, then removes the changes
48+ /// and sets the previous active texture back automatically.
49+ /// </summary>
50+ /// <seealso cref="TemporaryRenderTexture"/>
51+ /// <example><code>
52+ /// using (var temporaryActiveTexture = new TemporaryActiveTexture(icon.width, icon.height, 0))
53+ /// {
54+ /// Graphics.Blit(icon, temporary, material);
55+ /// });
56+ /// </code></example>
57+ [ PublicAPI ]
58+ public class TemporaryActiveTexture : IDisposable
59+ {
60+ private readonly RenderTexture _previousActiveTexture ;
61+ private readonly TemporaryRenderTexture _value ;
62+
63+ /// <summary>
64+ /// Creates a temporary texture, sets it as active in <see cref="RenderTexture.active"/>, then removes it
65+ /// and sets the previous active texture back automatically.
66+ /// </summary>
67+ /// <param name="width">Width of the temporary texture in pixels.</param>
68+ /// <param name="height">Height of the temporary texture in pixels.</param>
69+ /// <param name="depthBuffer">Depth buffer of the temporary texture.</param>
70+ /// <seealso cref="TemporaryRenderTexture"/>
71+ /// <example><code>
72+ /// using (var temporaryActiveTexture = new TemporaryActiveTexture(icon.width, icon.height, 0))
73+ /// {
74+ /// Graphics.Blit(icon, temporary, material);
75+ /// });
76+ /// </code></example>
77+ public TemporaryActiveTexture ( int width , int height , int depthBuffer )
78+ {
79+ _previousActiveTexture = RenderTexture . active ;
80+ _value = new TemporaryRenderTexture ( width , height , depthBuffer ) ;
81+ RenderTexture . active = _value ;
82+ }
83+
84+ public static implicit operator RenderTexture ( TemporaryActiveTexture temporaryTexture ) => temporaryTexture . _value ;
85+
86+ public void Dispose ( )
87+ {
88+ _value . Dispose ( ) ;
89+ RenderTexture . active = _previousActiveTexture ;
90+ }
91+ }
92+
93+ /// <summary>Creates a temporary texture that can be used and then removed automatically.</summary>
94+ /// <seealso cref="TemporaryActiveTexture"/>
95+ /// <example><code>
96+ /// using (var temporaryTexture = new TemporaryRenderTexture(icon.width, icon.height, 0))
97+ /// {
98+ /// Graphics.Blit(icon, temporaryTexture, material);
99+ /// });
100+ /// </code></example>
101+ [ PublicAPI ]
102+ public class TemporaryRenderTexture : IDisposable
103+ {
104+ private readonly RenderTexture _value ;
105+
106+ /// <summary>Creates a temporary texture that can be used and then removed automatically.</summary>
107+ /// <param name="width">Width of the temporary texture in pixels.</param>
108+ /// <param name="height">Height of the temporary texture in pixels.</param>
109+ /// <param name="depthBuffer">Depth buffer of the temporary texture.</param>
110+ /// <seealso cref="TemporaryActiveTexture"/>
111+ /// <example><code>
112+ /// using (var temporaryTexture = new TemporaryRenderTexture(icon.width, icon.height, 0))
113+ /// {
114+ /// Graphics.Blit(icon, temporaryTexture, material);
115+ /// });
116+ /// </code></example>
117+ public TemporaryRenderTexture ( int width , int height , int depthBuffer )
118+ {
119+ _value = RenderTexture . GetTemporary ( width , height , depthBuffer ) ;
120+ }
121+
122+ public static implicit operator RenderTexture ( TemporaryRenderTexture temporaryRenderTexture ) => temporaryRenderTexture . _value ;
123+
124+ public void Dispose ( )
125+ {
126+ RenderTexture . ReleaseTemporary ( _value ) ;
127+ }
128+ }
129+ }
130+ }
0 commit comments