Skip to content

Commit d1928a1

Browse files
committed
SpriteBatch32 renderer using new transform
1 parent b335782 commit d1928a1

5 files changed

Lines changed: 59 additions & 40 deletions

File tree

v3/src/components/experimental-Transform-2.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ Transform.prototype.update = function (parentTransformMatrix)
165165

166166
var localm = this.localMatrix.loadIdentity();
167167
localm.translate(this.positionX, this.positionY);
168-
//localm.rotate(rotation);
168+
localm.rotate(rotation);
169169
localm.scale(this.scaleX, this.scaleY);
170170

171171
var local = localm.matrix;

v3/src/gameobjects/image/ImageWebGLRenderer.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@ var ImageWebGLRenderer = function (renderer, src, interpolationPercentage)
2121
renderer.setBlendMode(src.color._blendMode);
2222
renderer.spriteBatch.add(
2323
frame,
24-
transform._anchorX, transform._anchorY,
25-
transform.world.tx, transform.world.ty,
26-
transform._worldScaleX, transform._worldScaleY,
27-
transform._worldRotation,
24+
src.anchorX, src.anchorY,
25+
transform.worldMatrix.matrix,
2826
src.color._glTint
2927
);
3028
};

v3/src/renderer/webgl/batches/sprite/SpriteBatch32.js

Lines changed: 44 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,21 @@ SpriteBatch32.prototype = {
8383
var indexBufferObject = CreateBuffer(gl, gl.ELEMENT_ARRAY_BUFFER, gl.STATIC_DRAW, null, indexDataBuffer.getByteCapacity());
8484

8585
var attribArray = [
86+
//CreateAttribDesc(gl, program, 'a_position', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 0),
87+
//CreateAttribDesc(gl, program, 'a_tex_coord', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 8),
88+
//CreateAttribDesc(gl, program, 'a_translate', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 16),
89+
//CreateAttribDesc(gl, program, 'a_scale', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 24),
90+
//CreateAttribDesc(gl, program, 'a_rotation', 1, gl.FLOAT, false, CONST.VERTEX_SIZE, 32),
91+
//CreateAttribDesc(gl, program, 'a_color', 3, 5121, true, CONST.VERTEX_SIZE, 36)
8692
CreateAttribDesc(gl, program, 'a_position', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 0),
8793
CreateAttribDesc(gl, program, 'a_tex_coord', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 8),
88-
CreateAttribDesc(gl, program, 'a_translate', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 16),
89-
CreateAttribDesc(gl, program, 'a_scale', 2, gl.FLOAT, false, CONST.VERTEX_SIZE, 24),
90-
CreateAttribDesc(gl, program, 'a_rotation', 1, gl.FLOAT, false, CONST.VERTEX_SIZE, 32),
91-
CreateAttribDesc(gl, program, 'a_color', 3, 5121, true, CONST.VERTEX_SIZE, 36)
94+
CreateAttribDesc(gl, program, 'transform_a', 1, gl.FLOAT, false, CONST.VERTEX_SIZE, 16),
95+
CreateAttribDesc(gl, program, 'transform_b', 1, gl.FLOAT, false, CONST.VERTEX_SIZE, 20),
96+
CreateAttribDesc(gl, program, 'transform_c', 1, gl.FLOAT, false, CONST.VERTEX_SIZE, 24),
97+
CreateAttribDesc(gl, program, 'transform_d', 1, gl.FLOAT, false, CONST.VERTEX_SIZE, 28),
98+
CreateAttribDesc(gl, program, 'transform_tx', 1, gl.FLOAT, false, CONST.VERTEX_SIZE, 32),
99+
CreateAttribDesc(gl, program, 'transform_ty', 1, gl.FLOAT, false, CONST.VERTEX_SIZE, 36),
100+
CreateAttribDesc(gl, program, 'a_color', 3, gl.UNSIGNED_BYTE, true, CONST.VERTEX_SIZE, 40)
92101
];
93102

94103
var vertexArray = new VertexArray(CreateBuffer(gl, gl.ARRAY_BUFFER, gl.STREAM_DRAW, null, vertexDataBuffer.getByteCapacity()), attribArray);
@@ -137,7 +146,7 @@ SpriteBatch32.prototype = {
137146
return (this.vertexDataBuffer.getByteLength() >= this.vertexDataBuffer.getByteCapacity());
138147
},
139148

140-
add: function (frame, anchorX, anchorY, translateX, translateY, scaleX, scaleY, rotation, vertexColor)
149+
add: function (frame, anchorX, anchorY, matrix, vertexColor)
141150
{
142151
this.manager.setBatch(this, frame.texture.source[frame.sourceIndex].glTexture);
143152

@@ -152,49 +161,59 @@ SpriteBatch32.prototype = {
152161
var height = frame.height;
153162
var x = width * -anchorX + frame.x;
154163
var y = height * -anchorY + frame.y;
164+
var a = matrix[0];
165+
var b = matrix[1];
166+
var c = matrix[2];
167+
var d = matrix[3];
168+
var tx = matrix[4];
169+
var ty = matrix[5];
155170

156171
vertexBufferF32[vertexOffset++] = x;
157172
vertexBufferF32[vertexOffset++] = y;
158173
vertexBufferF32[vertexOffset++] = uvs.x0;
159174
vertexBufferF32[vertexOffset++] = uvs.y0;
160-
vertexBufferF32[vertexOffset++] = translateX;
161-
vertexBufferF32[vertexOffset++] = translateY;
162-
vertexBufferF32[vertexOffset++] = scaleX;
163-
vertexBufferF32[vertexOffset++] = scaleY;
164-
vertexBufferF32[vertexOffset++] = rotation;
175+
vertexBufferF32[vertexOffset++] = a;
176+
vertexBufferF32[vertexOffset++] = b;
177+
vertexBufferF32[vertexOffset++] = c;
178+
vertexBufferF32[vertexOffset++] = d;
179+
vertexBufferF32[vertexOffset++] = tx;
180+
vertexBufferF32[vertexOffset++] = ty;
165181
vertexBufferU32[vertexOffset++] = vertexColor.topLeft;
166182

167183
vertexBufferF32[vertexOffset++] = x;
168184
vertexBufferF32[vertexOffset++] = y + height;
169185
vertexBufferF32[vertexOffset++] = uvs.x1;
170186
vertexBufferF32[vertexOffset++] = uvs.y1;
171-
vertexBufferF32[vertexOffset++] = translateX;
172-
vertexBufferF32[vertexOffset++] = translateY;
173-
vertexBufferF32[vertexOffset++] = scaleX;
174-
vertexBufferF32[vertexOffset++] = scaleY;
175-
vertexBufferF32[vertexOffset++] = rotation;
187+
vertexBufferF32[vertexOffset++] = a;
188+
vertexBufferF32[vertexOffset++] = b;
189+
vertexBufferF32[vertexOffset++] = c;
190+
vertexBufferF32[vertexOffset++] = d;
191+
vertexBufferF32[vertexOffset++] = tx;
192+
vertexBufferF32[vertexOffset++] = ty;
176193
vertexBufferU32[vertexOffset++] = vertexColor.bottomLeft;
177194

178195
vertexBufferF32[vertexOffset++] = x + width;
179196
vertexBufferF32[vertexOffset++] = y + height;
180197
vertexBufferF32[vertexOffset++] = uvs.x2;
181198
vertexBufferF32[vertexOffset++] = uvs.y2;
182-
vertexBufferF32[vertexOffset++] = translateX;
183-
vertexBufferF32[vertexOffset++] = translateY;
184-
vertexBufferF32[vertexOffset++] = scaleX;
185-
vertexBufferF32[vertexOffset++] = scaleY;
186-
vertexBufferF32[vertexOffset++] = rotation;
199+
vertexBufferF32[vertexOffset++] = a;
200+
vertexBufferF32[vertexOffset++] = b;
201+
vertexBufferF32[vertexOffset++] = c;
202+
vertexBufferF32[vertexOffset++] = d;
203+
vertexBufferF32[vertexOffset++] = tx;
204+
vertexBufferF32[vertexOffset++] = ty;
187205
vertexBufferU32[vertexOffset++] = vertexColor.bottomRight;
188206

189207
vertexBufferF32[vertexOffset++] = x + width;
190208
vertexBufferF32[vertexOffset++] = y;
191209
vertexBufferF32[vertexOffset++] = uvs.x3;
192210
vertexBufferF32[vertexOffset++] = uvs.y3;
193-
vertexBufferF32[vertexOffset++] = translateX;
194-
vertexBufferF32[vertexOffset++] = translateY;
195-
vertexBufferF32[vertexOffset++] = scaleX;
196-
vertexBufferF32[vertexOffset++] = scaleY;
197-
vertexBufferF32[vertexOffset++] = rotation;
211+
vertexBufferF32[vertexOffset++] = a;
212+
vertexBufferF32[vertexOffset++] = b;
213+
vertexBufferF32[vertexOffset++] = c;
214+
vertexBufferF32[vertexOffset++] = d;
215+
vertexBufferF32[vertexOffset++] = tx;
216+
vertexBufferF32[vertexOffset++] = ty;
198217
vertexBufferU32[vertexOffset++] = vertexColor.topRight;
199218

200219
this.elementCount += CONST.SPRITE_INDEX_COUNT;

v3/src/renderer/webgl/batches/sprite/VertexShader.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,20 @@ module.exports = [
22
'uniform mat4 u_view_matrix;',
33
'attribute vec2 a_position;',
44
'attribute vec2 a_tex_coord;',
5-
'attribute vec2 a_translate;',
6-
'attribute vec2 a_scale;',
5+
'attribute float transform_a;',
6+
'attribute float transform_b;',
7+
'attribute float transform_c;',
8+
'attribute float transform_d;',
9+
'attribute float transform_tx;',
10+
'attribute float transform_ty;',
711
'attribute vec3 a_color;',
8-
'attribute float a_rotation;',
912
'varying vec2 v_tex_coord;',
1013
'varying vec3 v_color;',
1114
'void main () {',
12-
' float t_cos = cos(a_rotation);',
13-
' float t_sin = sin(a_rotation);',
14-
' vec2 t_position = (a_position );',
15-
' t_position = vec2(t_position.x * t_cos - t_position.y * t_sin, t_position.x * t_sin + t_position.y * t_cos);',
16-
' gl_Position = u_view_matrix * vec4((t_position * a_scale) + a_translate, 1.0, 1.0);',
15+
' vec2 t_position;',
16+
' t_position.x = a_position.x * transform_a + a_position.y * transform_c + transform_tx;',
17+
' t_position.y = a_position.x * transform_b + a_position.y * transform_d + transform_ty;',
18+
' gl_Position = u_view_matrix * vec4(t_position, 1.0, 1.0);',
1719
' v_tex_coord = a_tex_coord;',
1820
' v_color = a_color;',
1921
'}'

v3/src/renderer/webgl/batches/sprite/const.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ var VertexShader = require('./VertexShader');
44
var CONST = {
55

66
// VERTEX_SIZE = (sizeof(vec2) * 4) + (sizeof(float) + sizeof(uint32))
7-
VERTEX_SIZE: 40,
7+
VERTEX_SIZE: 44,
88
INDEX_SIZE: 2,
99
SPRITE_VERTEX_COUNT: 4,
1010
SPRITE_INDEX_COUNT: 6,
1111

1212
// How many 32-bit components does the vertex have.
13-
SPRITE_VERTEX_COMPONENT_COUNT: 10,
13+
SPRITE_VERTEX_COMPONENT_COUNT: 11,
1414

1515
// Can't be bigger since index are 16-bit
1616
MAX_SPRITES: 10000,

0 commit comments

Comments
 (0)