@@ -28,9 +28,13 @@ public class TMPFont : BaseFont
28
28
TextFormat _format ;
29
29
TMP_Character _char ;
30
30
TMP_Character _lineChar ;
31
- Material _material ;
32
31
MaterialManager _manager ;
33
32
33
+ float _gradientScale ;
34
+ float _ratioA ;
35
+ float _ratioB ;
36
+ float _ratioC ;
37
+
34
38
public TMPFont ( )
35
39
{
36
40
this . canTint = true ;
@@ -74,12 +78,6 @@ void Release()
74
78
mainTexture . Dispose ( ) ;
75
79
mainTexture = null ;
76
80
}
77
-
78
- if ( _material != null )
79
- {
80
- Material . DestroyImmediate ( _material ) ;
81
- _material = null ;
82
- }
83
81
}
84
82
85
83
void Init ( )
@@ -92,17 +90,11 @@ void Init()
92
90
_manager = mainTexture . GetMaterialManager ( this . shader ) ;
93
91
_manager . onCreateNewMaterial += OnCreateNewMaterial ;
94
92
95
- _material = new Material ( _fontAsset . material ) ; //copy
96
- _material . SetFloat ( ShaderUtilities . ID_TextureWidth , mainTexture . width ) ;
97
- _material . SetFloat ( ShaderUtilities . ID_TextureHeight , mainTexture . height ) ;
98
- _material . SetFloat ( ShaderUtilities . ID_GradientScale , fontAsset . atlasPadding + 1 ) ;
99
- _material . SetFloat ( ShaderUtilities . ID_WeightNormal , fontAsset . normalStyle ) ;
100
- _material . SetFloat ( ShaderUtilities . ID_WeightBold , fontAsset . boldStyle ) ;
101
-
102
93
// _ascent = _fontAsset.faceInfo.ascentLine;
103
94
// _lineHeight = _fontAsset.faceInfo.lineHeight;
104
95
_ascent = _fontAsset . faceInfo . pointSize ;
105
96
_lineHeight = _fontAsset . faceInfo . pointSize * 1.25f ;
97
+ _gradientScale = fontAsset . atlasPadding + 1 ;
106
98
107
99
_lineChar = GetCharacterFromFontAsset ( '_' , FontStyles . Normal ) ;
108
100
}
@@ -111,34 +103,42 @@ void OnCreateNewMaterial(Material mat)
111
103
{
112
104
mat . SetFloat ( ShaderUtilities . ID_TextureWidth , mainTexture . width ) ;
113
105
mat . SetFloat ( ShaderUtilities . ID_TextureHeight , mainTexture . height ) ;
114
- mat . SetFloat ( ShaderUtilities . ID_GradientScale , fontAsset . atlasPadding + 1 ) ;
106
+ mat . SetFloat ( ShaderUtilities . ID_GradientScale , _gradientScale ) ;
115
107
mat . SetFloat ( ShaderUtilities . ID_WeightNormal , fontAsset . normalStyle ) ;
116
108
mat . SetFloat ( ShaderUtilities . ID_WeightBold , fontAsset . boldStyle ) ;
117
109
}
118
110
119
111
override public void UpdateGraphics ( NGraphics graphics )
120
112
{
121
113
MaterialPropertyBlock block = graphics . materialPropertyBlock ;
114
+
115
+ UpdateShaderRatios ( ) ;
116
+ _padding = GetPadding ( ) ;
117
+
118
+ block . SetFloat ( ShaderUtilities . ID_ScaleRatio_A , _ratioA ) ;
119
+ block . SetFloat ( ShaderUtilities . ID_ScaleRatio_B , _ratioB ) ;
120
+ block . SetFloat ( ShaderUtilities . ID_ScaleRatio_C , _ratioC ) ;
121
+
122
+ block . SetFloat ( ShaderUtilities . ID_FaceDilate , _format . faceDilate ) ;
123
+ block . SetFloat ( ShaderUtilities . ID_OutlineSoftness , _format . outlineSoftness ) ;
124
+
122
125
if ( _format . outline > 0 )
123
126
{
124
127
graphics . ToggleKeyword ( "OUTLINE_ON" , true ) ;
125
- _material . EnableKeyword ( "OUTLINE_ON" ) ;
126
128
127
129
block . SetFloat ( ShaderUtilities . ID_OutlineWidth , _format . outline ) ;
128
130
block . SetColor ( ShaderUtilities . ID_OutlineColor , _format . outlineColor ) ;
129
131
}
130
132
else
131
133
{
132
134
graphics . ToggleKeyword ( "OUTLINE_ON" , false ) ;
133
- _material . DisableKeyword ( "OUTLINE_ON" ) ;
134
135
135
136
block . SetFloat ( ShaderUtilities . ID_OutlineWidth , 0 ) ;
136
137
}
137
138
138
139
if ( _format . shadowOffset . x != 0 || _format . shadowOffset . y != 0 )
139
140
{
140
141
graphics . ToggleKeyword ( "UNDERLAY_ON" , true ) ;
141
- _material . EnableKeyword ( "UNDERLAY_ON" ) ;
142
142
143
143
block . SetColor ( ShaderUtilities . ID_UnderlayColor , _format . shadowColor ) ;
144
144
block . SetFloat ( ShaderUtilities . ID_UnderlayOffsetX , _format . shadowOffset . x ) ;
@@ -148,78 +148,31 @@ override public void UpdateGraphics(NGraphics graphics)
148
148
else
149
149
{
150
150
graphics . ToggleKeyword ( "UNDERLAY_ON" , false ) ;
151
- _material . DisableKeyword ( "UNDERLAY_ON" ) ;
152
151
153
152
block . SetFloat ( ShaderUtilities . ID_UnderlayOffsetX , 0 ) ;
154
153
block . SetFloat ( ShaderUtilities . ID_UnderlayOffsetY , 0 ) ;
155
154
block . SetFloat ( ShaderUtilities . ID_UnderlaySoftness , 0 ) ;
156
155
}
157
156
158
- block . SetFloat ( ShaderUtilities . ID_FaceDilate , _format . faceDilate ) ;
159
- block . SetFloat ( ShaderUtilities . ID_OutlineSoftness , _format . outlineSoftness ) ;
157
+ _stylePadding = ( ( ( _style & FontStyles . Bold ) == FontStyles . Bold ) ? _fontAsset . boldStyle : _fontAsset . normalStyle )
158
+ / 4.0f * _gradientScale * _ratioA ;
160
159
161
- if ( _material . HasProperty ( ShaderUtilities . ID_ScaleRatio_A ) )
162
- {
163
- //ShaderUtilities.GetPadding does not support handle materialproperyblock, we have to use a temp material
164
- _material . SetFloat ( ShaderUtilities . ID_OutlineWidth , block . GetFloat ( ShaderUtilities . ID_OutlineWidth ) ) ;
165
- _material . SetFloat ( ShaderUtilities . ID_UnderlayOffsetX , block . GetFloat ( ShaderUtilities . ID_UnderlayOffsetX ) ) ;
166
- _material . SetFloat ( ShaderUtilities . ID_UnderlayOffsetY , block . GetFloat ( ShaderUtilities . ID_UnderlayOffsetY ) ) ;
167
- _material . SetFloat ( ShaderUtilities . ID_UnderlaySoftness , block . GetFloat ( ShaderUtilities . ID_UnderlaySoftness ) ) ;
168
- _material . SetFloat ( ShaderUtilities . ID_FaceDilate , block . GetFloat ( ShaderUtilities . ID_FaceDilate ) ) ;
169
- _material . SetFloat ( ShaderUtilities . ID_OutlineSoftness , block . GetFloat ( ShaderUtilities . ID_OutlineSoftness ) ) ;
170
-
171
- _padding = ShaderUtilities . GetPadding ( _material , false , false ) ;
172
-
173
- //and then set back the properteis
174
- block . SetFloat ( ShaderUtilities . ID_ScaleRatio_A , _material . GetFloat ( ShaderUtilities . ID_ScaleRatio_A ) ) ;
175
- block . SetFloat ( ShaderUtilities . ID_ScaleRatio_B , _material . GetFloat ( ShaderUtilities . ID_ScaleRatio_B ) ) ;
176
- block . SetFloat ( ShaderUtilities . ID_ScaleRatio_C , _material . GetFloat ( ShaderUtilities . ID_ScaleRatio_C ) ) ;
177
- }
178
-
179
- // Set Padding based on selected font style
180
- #region Handle Style Padding
181
- if ( ( ( _style & FontStyles . Bold ) == FontStyles . Bold ) ) // Checks for any combination of Bold Style.
182
- {
183
- if ( _material . HasProperty ( ShaderUtilities . ID_GradientScale ) )
184
- {
185
- float gradientScale = _material . GetFloat ( ShaderUtilities . ID_GradientScale ) ;
186
- _stylePadding = _fontAsset . boldStyle / 4.0f * gradientScale * _material . GetFloat ( ShaderUtilities . ID_ScaleRatio_A ) ;
187
-
188
- // Clamp overall padding to Gradient Scale size.
189
- if ( _stylePadding + _padding > gradientScale )
190
- _padding = gradientScale - _stylePadding ;
191
- }
192
- else
193
- _stylePadding = 0 ;
194
- }
195
- else
196
- {
197
- if ( _material . HasProperty ( ShaderUtilities . ID_GradientScale ) )
198
- {
199
- float gradientScale = _material . GetFloat ( ShaderUtilities . ID_GradientScale ) ;
200
- _stylePadding = _fontAsset . normalStyle / 4.0f * gradientScale * _material . GetFloat ( ShaderUtilities . ID_ScaleRatio_A ) ;
201
-
202
- // Clamp overall padding to Gradient Scale size.
203
- if ( _stylePadding + _padding > gradientScale )
204
- _padding = gradientScale - _stylePadding ;
205
- }
206
- else
207
- _stylePadding = 0 ;
208
- }
209
- #endregion Handle Style Padding
160
+ // Clamp overall padding to Gradient Scale size.
161
+ if ( _stylePadding + _padding > _gradientScale )
162
+ _padding = _gradientScale - _stylePadding ;
210
163
}
211
164
212
165
override public void SetFormat ( TextFormat format , float fontSizeScale )
213
166
{
214
167
_format = format ;
215
168
216
- float size = format . size * fontSizeScale ;
169
+ float size = _format . size * fontSizeScale ;
217
170
if ( _format . specialStyle == TextFormat . SpecialStyle . Subscript || _format . specialStyle == TextFormat . SpecialStyle . Superscript )
218
171
size *= SupScale ;
219
172
220
173
_scale = size / _fontAsset . faceInfo . pointSize * _fontAsset . faceInfo . scale ;
221
174
_style = FontStyles . Normal ;
222
- if ( format . bold )
175
+ if ( _format . bold )
223
176
{
224
177
_style |= FontStyles . Bold ;
225
178
_fontWeight = FontWeight . Bold ;
@@ -230,10 +183,10 @@ override public void SetFormat(TextFormat format, float fontSizeScale)
230
183
_fontWeight = _defaultFontWeight ;
231
184
_boldMultiplier = 1.0f ;
232
185
}
233
- if ( format . italic )
186
+ if ( _format . italic )
234
187
_style |= FontStyles . Italic ;
235
188
236
- format . FillVertexColors ( vertexColors ) ;
189
+ _format . FillVertexColors ( vertexColors ) ;
237
190
}
238
191
239
192
override public bool GetGlyph ( char ch , out float width , out float height , out float baseline )
@@ -540,6 +493,82 @@ override public int GetLineHeight(int size)
540
493
{
541
494
return Mathf . RoundToInt ( _lineHeight * ( ( float ) size / _fontAsset . faceInfo . pointSize * _fontAsset . faceInfo . scale ) ) ;
542
495
}
496
+
497
+ float GetPadding ( )
498
+ {
499
+ Vector4 padding = Vector4 . zero ;
500
+ Vector4 maxPadding = Vector4 . zero ;
501
+
502
+ float faceDilate = _format . faceDilate * _ratioA ;
503
+ float faceSoftness = _format . outlineSoftness * _ratioA ;
504
+ float outlineThickness = _format . outline * _ratioA ;
505
+
506
+ float uniformPadding = outlineThickness + faceSoftness + faceDilate ;
507
+
508
+ // Underlay padding contribution
509
+ if ( _format . shadowOffset . x != 0 || _format . shadowOffset . y != 0 )
510
+ {
511
+ float offsetX = _format . shadowOffset . x * _ratioC ;
512
+ float offsetY = - _format . shadowOffset . y * _ratioC ;
513
+ float dilate = _format . faceDilate * _ratioC ;
514
+ float softness = _format . underlaySoftness * _ratioC ;
515
+
516
+ padding . x = Mathf . Max ( padding . x , faceDilate + dilate + softness - offsetX ) ;
517
+ padding . y = Mathf . Max ( padding . y , faceDilate + dilate + softness - offsetY ) ;
518
+ padding . z = Mathf . Max ( padding . z , faceDilate + dilate + softness + offsetX ) ;
519
+ padding . w = Mathf . Max ( padding . w , faceDilate + dilate + softness + offsetY ) ;
520
+ }
521
+
522
+ padding . x = Mathf . Max ( padding . x , uniformPadding ) ;
523
+ padding . y = Mathf . Max ( padding . y , uniformPadding ) ;
524
+ padding . z = Mathf . Max ( padding . z , uniformPadding ) ;
525
+ padding . w = Mathf . Max ( padding . w , uniformPadding ) ;
526
+
527
+ padding . x = Mathf . Min ( padding . x , 1 ) ;
528
+ padding . y = Mathf . Min ( padding . y , 1 ) ;
529
+ padding . z = Mathf . Min ( padding . z , 1 ) ;
530
+ padding . w = Mathf . Min ( padding . w , 1 ) ;
531
+
532
+ maxPadding . x = maxPadding . x < padding . x ? padding . x : maxPadding . x ;
533
+ maxPadding . y = maxPadding . y < padding . y ? padding . y : maxPadding . y ;
534
+ maxPadding . z = maxPadding . z < padding . z ? padding . z : maxPadding . z ;
535
+ maxPadding . w = maxPadding . w < padding . w ? padding . w : maxPadding . w ;
536
+
537
+ padding *= _gradientScale ;
538
+
539
+ // Set UniformPadding to the maximum value of any of its components.
540
+ uniformPadding = Mathf . Max ( padding . x , padding . y ) ;
541
+ uniformPadding = Mathf . Max ( padding . z , uniformPadding ) ;
542
+ uniformPadding = Mathf . Max ( padding . w , uniformPadding ) ;
543
+
544
+ return uniformPadding + 1.25f ;
545
+ }
546
+
547
+ // Scale Ratios to ensure property ranges are optimum in Material Editor
548
+ void UpdateShaderRatios ( )
549
+ {
550
+ _ratioA = _ratioB = _ratioC = 1 ;
551
+
552
+ bool isRatioEnabled = true ;
553
+ float clamp = 1 ;
554
+
555
+ float weight = Mathf . Max ( fontAsset . normalStyle , fontAsset . boldStyle ) / 4.0f ;
556
+ float range = ( weight + _format . faceDilate ) * ( _gradientScale - clamp ) ;
557
+
558
+ // Compute Ratio A
559
+ float t = Mathf . Max ( 1 , weight + _format . faceDilate + _format . outline + _format . outlineSoftness ) ;
560
+ _ratioA = isRatioEnabled ? ( _gradientScale - clamp ) / ( _gradientScale * t ) : 1 ;
561
+
562
+ // Compute Ratio B
563
+ // no glow support yet
564
+
565
+ // Compute Ratio C
566
+ if ( _format . shadowOffset . x != 0 || _format . shadowOffset . y != 0 )
567
+ {
568
+ t = Mathf . Max ( 1 , Mathf . Max ( Mathf . Abs ( _format . shadowOffset . x ) , Mathf . Abs ( - _format . shadowOffset . y ) ) + _format . faceDilate + _format . underlaySoftness ) ;
569
+ _ratioC = isRatioEnabled ? Mathf . Max ( 0 , _gradientScale - clamp - range ) / ( _gradientScale * t ) : 1 ;
570
+ }
571
+ }
543
572
}
544
573
}
545
574
0 commit comments