@@ -15,12 +15,37 @@ namespace Coffee.UIExtensions
1515 [ Icon ( "Packages/com.coffee.ui-dynamic-sampler/UIDynamicSamplerIcon.png" ) ]
1616 public class UIDynamicSampler : UIBehaviour , IMaterialModifier
1717 {
18+ public enum PreDownSamplingRate
19+ {
20+ None = 0 ,
21+ x2 = 2 ,
22+ x4 = 4 ,
23+ x8 = 8 ,
24+ x16 = 16 ,
25+ x32 = 32 ,
26+ }
27+
28+ /// <summary>
29+ /// Dynamic sampling will occur when the number of texels per screen pixel exceeds this value.
30+ /// </summary>
31+ [ Tooltip ( "Dynamic sampling will occur when the number of texels per screen pixel exceeds this value." ) ]
1832 [ Range ( 0.5f , 10f ) ]
1933 [ SerializeField ] private float m_SamplingThreshold = 2f ;
2034
35+ /// <summary>
36+ /// The scale of the dynamic texture relative to the rendering size. A larger value will require more memory.
37+ /// </summary>
38+ [ Tooltip (
39+ "The scale of the dynamic texture relative to the rendering size. A larger value will require more memory." ) ]
2140 [ Range ( 0.5f , 10f ) ]
2241 [ SerializeField ] private float m_ScaleFactor = 1.5f ;
2342
43+ /// <summary>
44+ /// The intensity of the blur applied by the downsampling buffer beforehand.
45+ /// </summary>
46+ [ Tooltip ( "The intensity of the blur applied by the downsampling buffer beforehand." ) ]
47+ [ SerializeField ] private PreDownSamplingRate m_PreDownSamplingRate = PreDownSamplingRate . None ;
48+
2449 private RenderTexture _dynamicTexture ;
2550 private Canvas . WillRenderCanvases _blit ;
2651 private Graphic _graphic ;
@@ -122,9 +147,20 @@ private void Blit()
122147 }
123148#endif
124149
150+ var texId = ( uint ) tex . GetInstanceID ( ) ;
151+ RenderTexture tmp = null ;
152+ if ( m_PreDownSamplingRate != PreDownSamplingRate . None )
153+ {
154+ var rate = ( int ) m_PreDownSamplingRate ;
155+ var preDownSamplingSize = new Vector2Int ( tex . width / rate , tex . height / rate ) ;
156+ tmp = RenderTexture . GetTemporary ( RenderTextureRepository . GetDescriptor ( preDownSamplingSize , false ) ) ;
157+ Graphics . Blit ( tex , tmp ) ;
158+ tex = tmp ;
159+ }
160+
125161 // Get or create render texture.
126162 var size = Vector2Int . RoundToInt ( rtSize * canvasScale * m_ScaleFactor * renderScale ) ;
127- var hash = new Hash128 ( ( uint ) tex . GetInstanceID ( ) , ( uint ) size . GetHashCode ( ) , 0 , 0 ) ;
163+ var hash = new Hash128 ( texId , ( uint ) size . GetHashCode ( ) , ( uint ) m_PreDownSamplingRate , 0 ) ;
128164 if ( ! RenderTextureRepository . Valid ( hash , _dynamicTexture ) )
129165 {
130166 RenderTextureRepository . Get ( hash , ref _dynamicTexture ,
@@ -138,6 +174,11 @@ private void Blit()
138174 }
139175
140176 graphic . canvasRenderer . SetTexture ( _dynamicTexture ) ;
177+
178+ if ( tmp )
179+ {
180+ RenderTexture . ReleaseTemporary ( tmp ) ;
181+ }
141182 }
142183 }
143184
0 commit comments