@@ -9,7 +9,6 @@ class MeshKey : PoolObject, IEquatable<MeshKey> {
9
9
public float scale ;
10
10
11
11
public MeshKey ( ) {
12
-
13
12
}
14
13
15
14
public static MeshKey create ( long textBlobId , float scale ) {
@@ -73,7 +72,6 @@ class MeshInfo : PoolObject {
73
72
long _timeToLive ;
74
73
75
74
public MeshInfo ( ) {
76
-
77
75
}
78
76
79
77
public static MeshInfo create ( MeshKey key , uiMeshMesh mesh , long textureVersion , int timeToLive = 5 ) {
@@ -99,27 +97,26 @@ public void touch(long timeTolive = 5) {
99
97
}
100
98
}
101
99
102
-
100
+
103
101
class TextBlobMesh : PoolObject {
104
-
105
102
static readonly Dictionary < MeshKey , MeshInfo > _meshes = new Dictionary < MeshKey , MeshInfo > ( ) ;
106
103
static long _frameCount = 0 ;
107
-
108
- public TextBlob textBlob ;
104
+
105
+ public TextBlob ? textBlob ;
109
106
public float scale ;
110
107
public uiMatrix3 matrix ;
111
-
108
+
112
109
uiMeshMesh _mesh ;
113
110
bool _resolved ;
114
111
115
112
public TextBlobMesh ( ) {
116
113
}
117
114
118
115
public override void clear ( ) {
119
- this . textBlob = null ;
120
116
ObjectPool < uiMeshMesh > . release ( this . _mesh ) ;
121
117
this . _mesh = null ;
122
118
this . _resolved = false ;
119
+ this . textBlob = null ;
123
120
}
124
121
125
122
public static TextBlobMesh create ( TextBlob textBlob , float scale , uiMatrix3 matrix ) {
@@ -129,7 +126,7 @@ public static TextBlobMesh create(TextBlob textBlob, float scale, uiMatrix3 matr
129
126
newMesh . matrix = matrix ;
130
127
return newMesh ;
131
128
}
132
-
129
+
133
130
public static long frameCount {
134
131
get { return _frameCount ; }
135
132
}
@@ -139,6 +136,7 @@ public static int meshCount {
139
136
}
140
137
141
138
static List < MeshKey > _keysToRemove = new List < MeshKey > ( ) ;
139
+
142
140
public static void tickNextFrame ( ) {
143
141
_frameCount ++ ;
144
142
D . assert ( _keysToRemove . Count == 0 ) ;
@@ -148,11 +146,11 @@ public static void tickNextFrame() {
148
146
}
149
147
}
150
148
151
- foreach ( var key in _keysToRemove )
152
- {
149
+ foreach ( var key in _keysToRemove ) {
153
150
ObjectPool < MeshInfo > . release ( _meshes [ key ] ) ;
154
151
_meshes . Remove ( key ) ;
155
152
}
153
+
156
154
_keysToRemove . Clear ( ) ;
157
155
}
158
156
@@ -162,58 +160,59 @@ public uiMeshMesh resolveMesh() {
162
160
}
163
161
164
162
this . _resolved = true ;
165
-
166
- var style = this . textBlob . style ;
167
-
168
- var text = this . textBlob . text ;
169
- var key = MeshKey . create ( this . textBlob . instanceId , this . scale ) ;
163
+
164
+ var style = this . textBlob . Value . style ;
165
+
166
+ var text = this . textBlob . Value . text ;
167
+ var key = MeshKey . create ( this . textBlob . Value . instanceId , this . scale ) ;
170
168
var fontInfo = FontManager . instance . getOrCreate ( style . fontFamily , style . fontWeight , style . fontStyle ) ;
171
169
var font = fontInfo . font ;
172
-
170
+
173
171
_meshes . TryGetValue ( key , out var meshInfo ) ;
174
172
if ( meshInfo != null && meshInfo . textureVersion == fontInfo . textureVersion ) {
175
173
ObjectPool < MeshKey > . release ( key ) ;
176
174
meshInfo . touch ( ) ;
177
175
this . _mesh = meshInfo . mesh . transform ( this . matrix ) ;
178
176
return this . _mesh ;
179
177
}
180
-
178
+
181
179
// Handling Emoji
182
- char startingChar = text [ this . textBlob . textOffset ] ;
180
+ char startingChar = text [ this . textBlob . Value . textOffset ] ;
183
181
if ( char . IsHighSurrogate ( startingChar ) || EmojiUtils . isSingleCharEmoji ( startingChar ) ) {
184
182
var vert = ObjectPool < uiList < Vector3 > > . alloc ( ) ;
185
183
var tri = ObjectPool < uiList < int > > . alloc ( ) ;
186
184
var uvCoord = ObjectPool < uiList < Vector2 > > . alloc ( ) ;
187
-
185
+
188
186
var metrics = FontMetrics . fromFont ( font , style . UnityFontSize ) ;
189
187
var minMaxRect = EmojiUtils . getMinMaxRect ( style . fontSize , metrics . ascent , metrics . descent ) ;
190
188
var minX = minMaxRect . left ;
191
189
var maxX = minMaxRect . right ;
192
190
var minY = minMaxRect . top ;
193
191
var maxY = minMaxRect . bottom ;
194
192
195
- for ( int i = 0 ; i < this . textBlob . textSize ; i ++ ) {
196
- char a = text [ this . textBlob . textOffset + i ] ;
193
+ for ( int i = 0 ; i < this . textBlob . Value . textSize ; i ++ ) {
194
+ char a = text [ this . textBlob . Value . textOffset + i ] ;
197
195
int code = a ;
198
196
if ( char . IsHighSurrogate ( a ) ) {
199
- D . assert ( i + 1 < this . textBlob . textSize ) ;
200
- D . assert ( this . textBlob . textOffset + i + 1 < this . textBlob . text . Length ) ;
201
- char b = text [ this . textBlob . textOffset + i + 1 ] ;
197
+ D . assert ( i + 1 < this . textBlob . Value . textSize ) ;
198
+ D . assert ( this . textBlob . Value . textOffset + i + 1 < this . textBlob . Value . text . Length ) ;
199
+ char b = text [ this . textBlob . Value . textOffset + i + 1 ] ;
202
200
D . assert ( char . IsLowSurrogate ( b ) ) ;
203
201
code = char . ConvertToUtf32 ( a , b ) ;
204
- } else if ( char . IsLowSurrogate ( a ) || EmojiUtils . isEmptyEmoji ( a ) ) {
202
+ }
203
+ else if ( char . IsLowSurrogate ( a ) || EmojiUtils . isEmptyEmoji ( a ) ) {
205
204
continue ;
206
205
}
206
+
207
207
var uvRect = EmojiUtils . getUVRect ( code ) ;
208
208
209
- var pos = this . textBlob . positions [ i ] ;
209
+ var positionX = this . textBlob . Value . getPositionX ( i ) ;
210
210
211
211
int baseIndex = vert . Count ;
212
- vert . Add ( new Vector3 ( pos . x + minX , pos . y + minY , 0 ) ) ;
213
- vert . Add ( new Vector3 ( pos . x + maxX , pos . y + minY , 0 ) ) ;
214
- vert . Add ( new Vector3 ( pos . x + maxX , pos . y + maxY , 0 ) ) ;
215
- vert . Add ( new Vector3 ( pos . x + minX , pos . y + maxY , 0 ) ) ;
216
-
212
+ vert . Add ( new Vector3 ( positionX + minX , minY , 0 ) ) ;
213
+ vert . Add ( new Vector3 ( positionX + maxX , minY , 0 ) ) ;
214
+ vert . Add ( new Vector3 ( positionX + maxX , maxY , 0 ) ) ;
215
+ vert . Add ( new Vector3 ( positionX + minX , maxY , 0 ) ) ;
217
216
tri . Add ( baseIndex ) ;
218
217
tri . Add ( baseIndex + 1 ) ;
219
218
tri . Add ( baseIndex + 2 ) ;
@@ -225,58 +224,61 @@ public uiMeshMesh resolveMesh() {
225
224
uvCoord . Add ( uvRect . topRight . toVector ( ) ) ;
226
225
uvCoord . Add ( uvRect . topLeft . toVector ( ) ) ;
227
226
228
- if ( char . IsHighSurrogate ( a ) ) i ++ ;
227
+ if ( char . IsHighSurrogate ( a ) ) {
228
+ i ++ ;
229
+ }
229
230
}
230
-
231
+
231
232
uiMeshMesh meshMesh = uiMeshMesh . create ( null , vert , tri , uvCoord ) ;
232
-
233
+
233
234
if ( _meshes . ContainsKey ( key ) ) {
234
235
ObjectPool < MeshInfo > . release ( _meshes [ key ] ) ;
235
236
_meshes . Remove ( key ) ;
236
237
}
238
+
237
239
_meshes [ key ] = MeshInfo . create ( key , meshMesh , 0 ) ;
238
240
239
241
this . _mesh = meshMesh . transform ( this . matrix ) ;
240
242
return this . _mesh ;
241
243
}
242
-
243
- var length = this . textBlob . textSize ;
244
+
245
+ var length = this . textBlob . Value . textSize ;
244
246
var fontSizeToLoad = Mathf . CeilToInt ( style . UnityFontSize * this . scale ) ;
245
247
246
248
var vertices = ObjectPool < uiList < Vector3 > > . alloc ( ) ;
247
249
vertices . SetCapacity ( length * 4 ) ;
248
-
250
+
249
251
var triangles = ObjectPool < uiList < int > > . alloc ( ) ;
250
252
triangles . SetCapacity ( length * 6 ) ;
251
253
252
254
var uv = ObjectPool < uiList < Vector2 > > . alloc ( ) ;
253
255
uv . SetCapacity ( length * 4 ) ;
254
-
256
+
255
257
for ( int charIndex = 0 ; charIndex < length ; ++ charIndex ) {
256
- var ch = text [ charIndex + this . textBlob . textOffset ] ;
258
+ var ch = text [ charIndex + this . textBlob . Value . textOffset ] ;
257
259
// first char as origin for mesh position
258
- var position = this . textBlob . positions [ charIndex ] ;
260
+ var positionX = this . textBlob . Value . getPositionX ( charIndex ) ;
259
261
if ( LayoutUtils . isWordSpace ( ch ) || LayoutUtils . isLineEndSpace ( ch ) || ch == '\t ' ) {
260
262
continue ;
261
263
}
262
264
263
265
if ( fontSizeToLoad == 0 ) {
264
266
continue ;
265
267
}
266
-
268
+
267
269
font . getGlyphInfo ( ch , out var glyphInfo , fontSizeToLoad , style . UnityFontStyle ) ;
268
-
270
+
269
271
var minX = glyphInfo . minX / this . scale ;
270
272
var maxX = glyphInfo . maxX / this . scale ;
271
273
var minY = - glyphInfo . maxY / this . scale ;
272
274
var maxY = - glyphInfo . minY / this . scale ;
273
275
274
276
var baseIndex = vertices . Count ;
275
277
276
- vertices . Add ( new Vector3 ( ( position . x + minX ) , ( position . y + minY ) , 0 ) ) ;
277
- vertices . Add ( new Vector3 ( ( position . x + maxX ) , ( position . y + minY ) , 0 ) ) ;
278
- vertices . Add ( new Vector3 ( ( position . x + maxX ) , ( position . y + maxY ) , 0 ) ) ;
279
- vertices . Add ( new Vector3 ( ( position . x + minX ) , ( position . y + maxY ) , 0 ) ) ;
278
+ vertices . Add ( new Vector3 ( positionX + minX , minY , 0 ) ) ;
279
+ vertices . Add ( new Vector3 ( positionX + maxX , minY , 0 ) ) ;
280
+ vertices . Add ( new Vector3 ( positionX + maxX , maxY , 0 ) ) ;
281
+ vertices . Add ( new Vector3 ( positionX + minX , maxY , 0 ) ) ;
280
282
281
283
triangles . Add ( baseIndex ) ;
282
284
triangles . Add ( baseIndex + 1 ) ;
@@ -301,11 +303,12 @@ public uiMeshMesh resolveMesh() {
301
303
}
302
304
303
305
uiMeshMesh mesh = vertices . Count > 0 ? uiMeshMesh . create ( null , vertices , triangles , uv ) : null ;
304
-
306
+
305
307
if ( _meshes . ContainsKey ( key ) ) {
306
308
ObjectPool < MeshInfo > . release ( _meshes [ key ] ) ;
307
309
_meshes . Remove ( key ) ;
308
310
}
311
+
309
312
_meshes [ key ] = MeshInfo . create ( key , mesh , fontInfo . textureVersion ) ;
310
313
311
314
this . _mesh = mesh . transform ( this . matrix ) ;
0 commit comments