1- import { Matrix } from "@galacean/engine-math" ;
1+ import { Matrix , Vector2 } from "@galacean/engine-math" ;
22import { StaticInterfaceImplement } from "../../base/StaticInterfaceImplement" ;
3- import { SpriteRenderer } from "../sprite/SpriteRenderer" ;
43import { ISpriteAssembler } from "./ISpriteAssembler" ;
4+ import { ISpriteRenderer } from "./ISpriteRenderer" ;
55
66/**
7- * @internal
7+ * Assemble vertex data for the sprite renderer in sliced mode.
88 */
99@StaticInterfaceImplement < ISpriteAssembler > ( )
1010export class SlicedSpriteAssembler {
11- static _rectangleTriangles = [
11+ private static _rectangleTriangles = [
1212 0 , 1 , 4 , 1 , 5 , 4 , 1 , 2 , 5 , 2 , 6 , 5 , 2 , 3 , 6 , 3 , 7 , 6 , 4 , 5 , 8 , 5 , 9 , 8 , 5 , 6 , 9 , 6 , 10 , 9 , 6 , 7 , 10 , 7 , 11 , 10 , 8 ,
1313 9 , 12 , 9 , 13 , 12 , 9 , 10 , 13 , 10 , 14 , 13 , 10 , 11 , 14 , 11 , 15 , 14
1414 ] ;
15- static _worldMatrix = new Matrix ( ) ;
15+ private static _matrix = new Matrix ( ) ;
16+ private static _row = new Array < number > ( 4 ) ;
17+ private static _column = new Array < number > ( 4 ) ;
1618
17- static resetData ( renderer : SpriteRenderer ) : void {
19+ static resetData ( renderer : ISpriteRenderer ) : void {
1820 const manager = renderer . _getChunkManager ( ) ;
1921 const lastSubChunk = renderer . _subChunk ;
2022 lastSubChunk && manager . freeSubChunk ( lastSubChunk ) ;
@@ -23,14 +25,24 @@ export class SlicedSpriteAssembler {
2325 renderer . _subChunk = subChunk ;
2426 }
2527
26- static updatePositions ( renderer : SpriteRenderer ) : void {
27- const { width, height, sprite } = renderer ;
28+ static updatePositions (
29+ renderer : ISpriteRenderer ,
30+ worldMatrix : Matrix ,
31+ width : number ,
32+ height : number ,
33+ pivot : Vector2 ,
34+ flipX : boolean ,
35+ flipY : boolean ,
36+ referenceResolutionPerUnit : number = 1
37+ ) : void {
38+ const { sprite } = renderer ;
2839 const { border } = sprite ;
2940 // Update local positions.
3041 const spritePositions = sprite . _getPositions ( ) ;
3142 const { x : left , y : bottom } = spritePositions [ 0 ] ;
3243 const { x : right , y : top } = spritePositions [ 3 ] ;
33- const { width : expectWidth , height : expectHeight } = sprite ;
44+ const expectWidth = sprite . width * referenceResolutionPerUnit ;
45+ const expectHeight = sprite . height * referenceResolutionPerUnit ;
3446 const fixedLeft = expectWidth * border . x ;
3547 const fixedBottom = expectHeight * border . y ;
3648 const fixedRight = expectWidth * border . z ;
@@ -47,42 +59,36 @@ export class SlicedSpriteAssembler {
4759 // column
4860 // ------------------------
4961 // Calculate row and column.
50- let row : number [ ] , column : number [ ] ;
62+ const { _row : row , _column : column } = SlicedSpriteAssembler ;
5163 if ( fixedLeft + fixedRight > width ) {
5264 const widthScale = width / ( fixedLeft + fixedRight ) ;
53- row = [
54- expectWidth * left * widthScale ,
55- fixedLeft * widthScale ,
56- fixedLeft * widthScale ,
57- width - expectWidth * ( 1 - right ) * widthScale
58- ] ;
65+ ( row [ 0 ] = expectWidth * left * widthScale ) , ( row [ 1 ] = row [ 2 ] = fixedLeft * widthScale ) ;
66+ row [ 3 ] = width - expectWidth * ( 1 - right ) * widthScale ;
5967 } else {
60- row = [ expectWidth * left , fixedLeft , width - fixedRight , width - expectWidth * ( 1 - right ) ] ;
68+ ( row [ 0 ] = expectWidth * left ) , ( row [ 1 ] = fixedLeft ) , ( row [ 2 ] = width - fixedRight ) ;
69+ row [ 3 ] = width - expectWidth * ( 1 - right ) ;
6170 }
6271
6372 if ( fixedTop + fixedBottom > height ) {
6473 const heightScale = height / ( fixedTop + fixedBottom ) ;
65- column = [
66- expectHeight * bottom * heightScale ,
67- fixedBottom * heightScale ,
68- fixedBottom * heightScale ,
69- height - expectHeight * ( 1 - top ) * heightScale
70- ] ;
74+ ( column [ 0 ] = expectHeight * bottom * heightScale ) , ( column [ 1 ] = column [ 2 ] = fixedBottom * heightScale ) ;
75+ column [ 3 ] = height - expectHeight * ( 1 - top ) * heightScale ;
7176 } else {
72- column = [ expectHeight * bottom , fixedBottom , height - fixedTop , height - expectHeight * ( 1 - top ) ] ;
77+ ( column [ 0 ] = expectHeight * bottom ) , ( column [ 1 ] = fixedBottom ) , ( column [ 2 ] = height - fixedTop ) ;
78+ column [ 3 ] = height - expectHeight * ( 1 - top ) ;
7379 }
7480
7581 // Update renderer's worldMatrix.
76- const { x : pivotX , y : pivotY } = renderer . sprite . pivot ;
77- const localTransX = renderer . width * pivotX ;
78- const localTransY = renderer . height * pivotY ;
82+ const { x : pivotX , y : pivotY } = pivot ;
83+ const localTransX = width * pivotX ;
84+ const localTransY = height * pivotY ;
85+ // Position to World
86+ const modelMatrix = SlicedSpriteAssembler . _matrix ;
87+ const { elements : wE } = modelMatrix ;
7988 // Renderer's worldMatrix.
80- const worldMatrix = SlicedSpriteAssembler . _worldMatrix ;
81- const { elements : wE } = worldMatrix ;
82- // Parent's worldMatrix.
83- const { elements : pWE } = renderer . entity . transform . worldMatrix ;
84- const sx = renderer . flipX ? - 1 : 1 ;
85- const sy = renderer . flipY ? - 1 : 1 ;
89+ const { elements : pWE } = worldMatrix ;
90+ const sx = flipX ? - 1 : 1 ;
91+ const sy = flipY ? - 1 : 1 ;
8692 ( wE [ 0 ] = pWE [ 0 ] * sx ) , ( wE [ 1 ] = pWE [ 1 ] * sx ) , ( wE [ 2 ] = pWE [ 2 ] * sx ) ;
8793 ( wE [ 4 ] = pWE [ 4 ] * sy ) , ( wE [ 5 ] = pWE [ 5 ] * sy ) , ( wE [ 6 ] = pWE [ 6 ] * sy ) ;
8894 ( wE [ 8 ] = pWE [ 8 ] ) , ( wE [ 9 ] = pWE [ 9 ] ) , ( wE [ 10 ] = pWE [ 10 ] ) ;
@@ -112,13 +118,14 @@ export class SlicedSpriteAssembler {
112118 }
113119 }
114120
115- const { min, max } = renderer . _bounds ;
116- min . set ( row [ 0 ] , column [ 0 ] , 0 ) ;
117- max . set ( row [ 3 ] , column [ 3 ] , 0 ) ;
118- renderer . _bounds . transform ( worldMatrix ) ;
121+ // @ts -ignore
122+ const bounds = renderer . _bounds ;
123+ bounds . min . set ( row [ 0 ] , column [ 0 ] , 0 ) ;
124+ bounds . max . set ( row [ 3 ] , column [ 3 ] , 0 ) ;
125+ bounds . transform ( modelMatrix ) ;
119126 }
120127
121- static updateUVs ( renderer : SpriteRenderer ) : void {
128+ static updateUVs ( renderer : ISpriteRenderer ) : void {
122129 const subChunk = renderer . _subChunk ;
123130 const vertices = subChunk . chunk . vertices ;
124131 const spriteUVs = renderer . sprite . _getUVs ( ) ;
@@ -131,15 +138,16 @@ export class SlicedSpriteAssembler {
131138 }
132139 }
133140
134- static updateColor ( renderer : SpriteRenderer ) : void {
141+ static updateColor ( renderer : ISpriteRenderer , alpha : number ) : void {
135142 const subChunk = renderer . _subChunk ;
136143 const { r, g, b, a } = renderer . color ;
144+ const finalAlpha = a * alpha ;
137145 const vertices = subChunk . chunk . vertices ;
138146 for ( let i = 0 , o = subChunk . vertexArea . start + 5 ; i < 16 ; ++ i , o += 9 ) {
139147 vertices [ o ] = r ;
140148 vertices [ o + 1 ] = g ;
141149 vertices [ o + 2 ] = b ;
142- vertices [ o + 3 ] = a ;
150+ vertices [ o + 3 ] = finalAlpha ;
143151 }
144152 }
145153}
0 commit comments