1
1
import { Camera } from "../Camera" ;
2
2
import { Engine } from "../Engine" ;
3
3
import { Layer } from "../Layer" ;
4
+ import { Utils } from "../Utils" ;
4
5
import { RenderQueueType , Shader } from "../shader" ;
5
6
import { ShaderMacroCollection } from "../shader/ShaderMacroCollection" ;
6
7
import { RenderContext } from "./RenderContext" ;
@@ -194,7 +195,7 @@ export class RenderQueue {
194
195
* Sort the elements.
195
196
*/
196
197
sort ( compareFunc : Function ) : void {
197
- this . _quickSort ( this . elements , 0 , this . elements . length , compareFunc ) ;
198
+ Utils . _quickSort ( this . elements , 0 , this . elements . length , compareFunc ) ;
198
199
}
199
200
200
201
/**
@@ -204,107 +205,4 @@ export class RenderQueue {
204
205
_initSpriteBatcher ( engine : Engine ) : void {
205
206
this . _spriteBatcher = new SpriteBatcher ( engine ) ;
206
207
}
207
-
208
- /**
209
- * @remarks
210
- * Modified based on v8.
211
- * https://github.com/v8/v8/blob/7.2-lkgr/src/js/array.js
212
- */
213
- private _quickSort < T > ( a : T [ ] , from : number , to : number , compareFunc : Function ) : void {
214
- while ( true ) {
215
- // Insertion sort is faster for short arrays.
216
- if ( to - from <= 10 ) {
217
- this . _insertionSort ( a , from , to , compareFunc ) ;
218
- return ;
219
- }
220
- const third_index = ( from + to ) >> 1 ;
221
- // Find a pivot as the median of first, last and middle element.
222
- let v0 = a [ from ] ;
223
- let v1 = a [ to - 1 ] ;
224
- let v2 = a [ third_index ] ;
225
- const c01 = compareFunc ( v0 , v1 ) ;
226
- if ( c01 > 0 ) {
227
- // v1 < v0, so swap them.
228
- const tmp = v0 ;
229
- v0 = v1 ;
230
- v1 = tmp ;
231
- } // v0 <= v1.
232
- const c02 = compareFunc ( v0 , v2 ) ;
233
- if ( c02 >= 0 ) {
234
- // v2 <= v0 <= v1.
235
- const tmp = v0 ;
236
- v0 = v2 ;
237
- v2 = v1 ;
238
- v1 = tmp ;
239
- } else {
240
- // v0 <= v1 && v0 < v2
241
- const c12 = compareFunc ( v1 , v2 ) ;
242
- if ( c12 > 0 ) {
243
- // v0 <= v2 < v1
244
- const tmp = v1 ;
245
- v1 = v2 ;
246
- v2 = tmp ;
247
- }
248
- }
249
- // v0 <= v1 <= v2
250
- a [ from ] = v0 ;
251
- a [ to - 1 ] = v2 ;
252
- const pivot = v1 ;
253
- let low_end = from + 1 ; // Upper bound of elements lower than pivot.
254
- let high_start = to - 1 ; // Lower bound of elements greater than pivot.
255
- a [ third_index ] = a [ low_end ] ;
256
- a [ low_end ] = pivot ;
257
-
258
- // From low_end to i are elements equal to pivot.
259
- // From i to high_start are elements that haven't been compared yet.
260
- partition: for ( let i = low_end + 1 ; i < high_start ; i ++ ) {
261
- let element = a [ i ] ;
262
- let order = compareFunc ( element , pivot ) ;
263
- if ( order < 0 ) {
264
- a [ i ] = a [ low_end ] ;
265
- a [ low_end ] = element ;
266
- low_end ++ ;
267
- } else if ( order > 0 ) {
268
- do {
269
- high_start -- ;
270
- if ( high_start == i ) break partition;
271
- const top_elem = a [ high_start ] ;
272
- order = compareFunc ( top_elem , pivot ) ;
273
- } while ( order > 0 ) ;
274
- a [ i ] = a [ high_start ] ;
275
- a [ high_start ] = element ;
276
- if ( order < 0 ) {
277
- element = a [ i ] ;
278
- a [ i ] = a [ low_end ] ;
279
- a [ low_end ] = element ;
280
- low_end ++ ;
281
- }
282
- }
283
- }
284
- if ( to - high_start < low_end - from ) {
285
- this . _quickSort ( a , high_start , to , compareFunc ) ;
286
- to = low_end ;
287
- } else {
288
- this . _quickSort ( a , from , low_end , compareFunc ) ;
289
- from = high_start ;
290
- }
291
- }
292
- }
293
-
294
- private _insertionSort < T > ( a : T [ ] , from : number , to : number , compareFunc : Function ) : void {
295
- for ( let i = from + 1 ; i < to ; i ++ ) {
296
- let j ;
297
- const element = a [ i ] ;
298
- for ( j = i - 1 ; j >= from ; j -- ) {
299
- const tmp = a [ j ] ;
300
- const order = compareFunc ( tmp , element ) ;
301
- if ( order > 0 ) {
302
- a [ j + 1 ] = tmp ;
303
- } else {
304
- break ;
305
- }
306
- }
307
- a [ j + 1 ] = element ;
308
- }
309
- }
310
208
}
0 commit comments