@@ -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,81 +148,33 @@ 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 ) ;
160
-
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
- }
157
+ _stylePadding = ( ( ( _style & FontStyles . Bold ) == FontStyles . Bold ) ? _fontAsset . boldStyle : _fontAsset . normalStyle )
158
+ / 4.0f * _gradientScale * _ratioA ;
178
159
179
- // Set Padding based on selected font style
180
-
181
- #region Handle Style Padding
182
-
183
- if ( ( ( _style & FontStyles . Bold ) == FontStyles . Bold ) ) // Checks for any combination of Bold Style.
184
- {
185
- if ( _material . HasProperty ( ShaderUtilities . ID_GradientScale ) )
186
- {
187
- float gradientScale = _material . GetFloat ( ShaderUtilities . ID_GradientScale ) ;
188
- _stylePadding = _fontAsset . boldStyle / 4.0f * gradientScale * _material . GetFloat ( ShaderUtilities . ID_ScaleRatio_A ) ;
189
-
190
- // Clamp overall padding to Gradient Scale size.
191
- if ( _stylePadding + _padding > gradientScale )
192
- _padding = gradientScale - _stylePadding ;
193
- }
194
- else
195
- _stylePadding = 0 ;
196
- }
197
- else
198
- {
199
- if ( _material . HasProperty ( ShaderUtilities . ID_GradientScale ) )
200
- {
201
- float gradientScale = _material . GetFloat ( ShaderUtilities . ID_GradientScale ) ;
202
- _stylePadding = _fontAsset . normalStyle / 4.0f * gradientScale * _material . GetFloat ( ShaderUtilities . ID_ScaleRatio_A ) ;
203
-
204
- // Clamp overall padding to Gradient Scale size.
205
- if ( _stylePadding + _padding > gradientScale )
206
- _padding = gradientScale - _stylePadding ;
207
- }
208
- else
209
- _stylePadding = 0 ;
210
- }
211
-
212
- #endregion Handle Style Padding
160
+ // Clamp overall padding to Gradient Scale size.
161
+ if ( _stylePadding + _padding > _gradientScale )
162
+ _padding = _gradientScale - _stylePadding ;
213
163
}
214
164
215
165
override public void SetFormat ( TextFormat format , float fontSizeScale )
216
166
{
217
167
_format = format ;
218
168
219
- float size = format . size * fontSizeScale ;
169
+ float size = _format . size * fontSizeScale ;
220
170
if ( _format . specialStyle == TextFormat . SpecialStyle . Subscript || _format . specialStyle == TextFormat . SpecialStyle . Superscript )
171
+ {
221
172
size *= SupScale ;
173
+ }
222
174
223
175
_scale = size / _fontAsset . faceInfo . pointSize * _fontAsset . faceInfo . scale ;
224
176
_style = FontStyles . Normal ;
225
- if ( format . bold )
177
+ if ( _format . bold )
226
178
{
227
179
_style |= FontStyles . Bold ;
228
180
_fontWeight = FontWeight . Bold ;
@@ -233,11 +185,12 @@ override public void SetFormat(TextFormat format, float fontSizeScale)
233
185
_fontWeight = _defaultFontWeight ;
234
186
_boldMultiplier = 1.0f ;
235
187
}
236
-
237
- if ( format . italic )
188
+ if ( _format . italic )
189
+ {
238
190
_style |= FontStyles . Italic ;
191
+ }
239
192
240
- format . FillVertexColors ( vertexColors ) ;
193
+ _format . FillVertexColors ( vertexColors ) ;
241
194
}
242
195
243
196
override public bool GetGlyph ( char ch , out float width , out float height , out float baseline )
@@ -281,8 +234,8 @@ TMP_Character GetCharacterFromFontAsset(uint unicode, FontStyles fontStyle)
281
234
TMP_FontAsset actualAsset ;
282
235
#pragma warning restore
283
236
return TMP_FontAssetUtilities . GetCharacterFromFontAsset ( unicode , _fontAsset , true , fontStyle , _fontWeight ,
284
- out isAlternativeTypeface
285
- //,out actualAsset //old TMP version need this line
237
+ out isAlternativeTypeface
238
+ //,out actualAsset //old TMP version need this line
286
239
) ;
287
240
}
288
241
@@ -476,8 +429,8 @@ override public int DrawLine(float x, float y, float width, int fontSize, int ty
476
429
#region HANDLE UV0
477
430
478
431
// Calculate UV required to setup the 3 Quads for the Underline.
479
- Vector2 uv0 = new Vector2 ( ( _lineChar . glyph . glyphRect . x - _padding ) / _fontAsset . atlasWidth , ( _lineChar . glyph . glyphRect . y - _padding ) / _fontAsset . atlasHeight ) ; // bottom left
480
- Vector2 uv1 = new Vector2 ( uv0 . x , ( _lineChar . glyph . glyphRect . y + _lineChar . glyph . glyphRect . height + _padding ) / _fontAsset . atlasHeight ) ; // top left
432
+ Vector2 uv0 = new Vector2 ( ( _lineChar . glyph . glyphRect . x - _padding ) / _fontAsset . atlasWidth , ( _lineChar . glyph . glyphRect . y - _padding ) / _fontAsset . atlasHeight ) ; // bottom left
433
+ Vector2 uv1 = new Vector2 ( uv0 . x , ( _lineChar . glyph . glyphRect . y + _lineChar . glyph . glyphRect . height + _padding ) / _fontAsset . atlasHeight ) ; // top left
481
434
Vector2 uv2 = new Vector2 ( ( _lineChar . glyph . glyphRect . x - _padding + ( float ) _lineChar . glyph . glyphRect . width / 2 ) / _fontAsset . atlasWidth , uv1 . y ) ; // Mid Top Left
482
435
Vector2 uv3 = new Vector2 ( uv2 . x , uv0 . y ) ; // Mid Bottom Left
483
436
Vector2 uv4 = new Vector2 ( ( _lineChar . glyph . glyphRect . x + _padding + ( float ) _lineChar . glyph . glyphRect . width / 2 ) / _fontAsset . atlasWidth , uv1 . y ) ; // Mid Top Right
@@ -566,6 +519,82 @@ override public int GetLineHeight(int size)
566
519
{
567
520
return Mathf . RoundToInt ( _lineHeight * ( ( float ) size / _fontAsset . faceInfo . pointSize * _fontAsset . faceInfo . scale ) ) ;
568
521
}
522
+
523
+ float GetPadding ( )
524
+ {
525
+ Vector4 padding = Vector4 . zero ;
526
+ Vector4 maxPadding = Vector4 . zero ;
527
+
528
+ float faceDilate = _format . faceDilate * _ratioA ;
529
+ float faceSoftness = _format . outlineSoftness * _ratioA ;
530
+ float outlineThickness = _format . outline * _ratioA ;
531
+
532
+ float uniformPadding = outlineThickness + faceSoftness + faceDilate ;
533
+
534
+ // Underlay padding contribution
535
+ if ( _format . shadowOffset . x != 0 || _format . shadowOffset . y != 0 )
536
+ {
537
+ float offsetX = _format . shadowOffset . x * _ratioC ;
538
+ float offsetY = - _format . shadowOffset . y * _ratioC ;
539
+ float dilate = _format . faceDilate * _ratioC ;
540
+ float softness = _format . underlaySoftness * _ratioC ;
541
+
542
+ padding . x = Mathf . Max ( padding . x , faceDilate + dilate + softness - offsetX ) ;
543
+ padding . y = Mathf . Max ( padding . y , faceDilate + dilate + softness - offsetY ) ;
544
+ padding . z = Mathf . Max ( padding . z , faceDilate + dilate + softness + offsetX ) ;
545
+ padding . w = Mathf . Max ( padding . w , faceDilate + dilate + softness + offsetY ) ;
546
+ }
547
+
548
+ padding . x = Mathf . Max ( padding . x , uniformPadding ) ;
549
+ padding . y = Mathf . Max ( padding . y , uniformPadding ) ;
550
+ padding . z = Mathf . Max ( padding . z , uniformPadding ) ;
551
+ padding . w = Mathf . Max ( padding . w , uniformPadding ) ;
552
+
553
+ padding . x = Mathf . Min ( padding . x , 1 ) ;
554
+ padding . y = Mathf . Min ( padding . y , 1 ) ;
555
+ padding . z = Mathf . Min ( padding . z , 1 ) ;
556
+ padding . w = Mathf . Min ( padding . w , 1 ) ;
557
+
558
+ maxPadding . x = maxPadding . x < padding . x ? padding . x : maxPadding . x ;
559
+ maxPadding . y = maxPadding . y < padding . y ? padding . y : maxPadding . y ;
560
+ maxPadding . z = maxPadding . z < padding . z ? padding . z : maxPadding . z ;
561
+ maxPadding . w = maxPadding . w < padding . w ? padding . w : maxPadding . w ;
562
+
563
+ padding *= _gradientScale ;
564
+
565
+ // Set UniformPadding to the maximum value of any of its components.
566
+ uniformPadding = Mathf . Max ( padding . x , padding . y ) ;
567
+ uniformPadding = Mathf . Max ( padding . z , uniformPadding ) ;
568
+ uniformPadding = Mathf . Max ( padding . w , uniformPadding ) ;
569
+
570
+ return uniformPadding + 1.25f ;
571
+ }
572
+
573
+ // Scale Ratios to ensure property ranges are optimum in Material Editor
574
+ void UpdateShaderRatios ( )
575
+ {
576
+ _ratioA = _ratioB = _ratioC = 1 ;
577
+
578
+ bool isRatioEnabled = true ;
579
+ float clamp = 1 ;
580
+
581
+ float weight = Mathf . Max ( fontAsset . normalStyle , fontAsset . boldStyle ) / 4.0f ;
582
+ float range = ( weight + _format . faceDilate ) * ( _gradientScale - clamp ) ;
583
+
584
+ // Compute Ratio A
585
+ float t = Mathf . Max ( 1 , weight + _format . faceDilate + _format . outline + _format . outlineSoftness ) ;
586
+ _ratioA = isRatioEnabled ? ( _gradientScale - clamp ) / ( _gradientScale * t ) : 1 ;
587
+
588
+ // Compute Ratio B
589
+ // no glow support yet
590
+
591
+ // Compute Ratio C
592
+ if ( _format . shadowOffset . x != 0 || _format . shadowOffset . y != 0 )
593
+ {
594
+ t = Mathf . Max ( 1 , Mathf . Max ( Mathf . Abs ( _format . shadowOffset . x ) , Mathf . Abs ( - _format . shadowOffset . y ) ) + _format . faceDilate + _format . underlaySoftness ) ;
595
+ _ratioC = isRatioEnabled ? Mathf . Max ( 0 , _gradientScale - clamp - range ) / ( _gradientScale * t ) : 1 ;
596
+ }
597
+ }
569
598
}
570
599
}
571
600
0 commit comments