@@ -27,10 +27,11 @@ Phaser.Renderer.WebGL.BatchManager = function (renderer)
2727 //
2828 // Position (vec2) = 4 * 2 bytes
2929 // UV (vec2) = 4 * 2 bytes
30- // Color (float) = 4 bytes
30+ // Tint Color (float) = 4 bytes
31+ // BG Color (float) = 4 bytes
3132 // Texture Index (float) OR tint (float) = 4 bytes
3233
33- this . vertSize = ( 4 * 2 ) + ( 4 * 2 ) + ( 4 ) ;
34+ this . vertSize = ( 4 * 2 ) + ( 4 * 2 ) + ( 4 ) + ( 4 ) ;
3435
3536 var numVerts = this . vertSize * this . maxBatchSize * 4 ;
3637
@@ -92,19 +93,22 @@ Phaser.Renderer.WebGL.BatchManager = function (renderer)
9293 this . vertexSrc = [
9394 'attribute vec2 aVertexPosition;' ,
9495 'attribute vec2 aTextureCoord;' ,
95- 'attribute vec4 aColor;' ,
96+ 'attribute vec4 aTintColor;' ,
97+ 'attribute vec4 aBgColor;' ,
9698
9799 'uniform vec2 projectionVector;' ,
98100
99101 'varying vec2 vTextureCoord;' ,
100- 'varying vec4 vColor;' ,
102+ 'varying vec4 vTintColor;' ,
103+ 'varying vec4 vBgColor;' ,
101104
102105 'const vec2 center = vec2(-1.0, 1.0);' ,
103106
104107 'void main(void) {' ,
105108 ' gl_Position = vec4((aVertexPosition / projectionVector) + center, 0.0, 1.0);' ,
106109 ' vTextureCoord = aTextureCoord;' , // pass the texture coordinate to the fragment shader, the GPU will interpolate the points
107- ' vColor = vec4(aColor.rgb * aColor.a, aColor.a);' ,
110+ ' vTintColor = vec4(aTintColor.rgb * aTintColor.a, aTintColor.a);' ,
111+ ' vBgColor = aBgColor;' ,
108112 '}'
109113 ] ;
110114
@@ -115,14 +119,20 @@ Phaser.Renderer.WebGL.BatchManager = function (renderer)
115119 */
116120 this . fragmentSrc = [
117121 'precision mediump float;' ,
122+
118123 'varying vec2 vTextureCoord;' , // the texture coords passed in from the vertex shader
119- 'varying vec4 vColor;' , // the color value passed in from the vertex shader (texture color + alpha + tint)
124+ 'varying vec4 vTintColor;' , // the color value passed in from the vertex shader (texture color + alpha + tint)
125+ 'varying vec4 vBgColor;' , // the bg color value passed in from the vertex shader
120126 'varying float vTextureIndex;' ,
127+
121128 'uniform sampler2D uSampler;' , // our texture
129+
122130 'const vec4 PINK = vec4(1.0, 0.0, 1.0, 1.0);' ,
131+
123132 'void main(void) {' ,
124- ' gl_FragColor = texture2D(uSampler, vTextureCoord) * vColor;' , // get the color from the texture
125- // ' gl_FragColor = PINK;', // debugging
133+ ' vec4 pixel = texture2D(uSampler, vTextureCoord) * vTintColor;' , // get the color from the texture
134+ ' if (pixel.a == 0.0) pixel = vBgColor;' , // if texture alpha is zero, use the bg color
135+ ' gl_FragColor = pixel;' ,
126136 '}'
127137 ] ;
128138
@@ -136,7 +146,10 @@ Phaser.Renderer.WebGL.BatchManager = function (renderer)
136146 // this.offsetVector;
137147
138148 // @type {GLint }
139- this . colorAttribute ;
149+ this . aTintColor ;
150+
151+ // @type {GLint }
152+ this . aBgColor ;
140153
141154 // @type {GLint }
142155 this . aTextureIndex ;
@@ -234,8 +247,9 @@ Phaser.Renderer.WebGL.BatchManager.prototype = {
234247 // Get and store the attributes
235248 this . aVertexPosition = gl . getAttribLocation ( program , 'aVertexPosition' ) ;
236249 this . aTextureCoord = gl . getAttribLocation ( program , 'aTextureCoord' ) ;
237- this . colorAttribute = gl . getAttribLocation ( program , 'aColor' ) ;
238250 // this.aTextureIndex = gl.getAttribLocation(program, 'aTextureIndex');
251+ this . aTintColor = gl . getAttribLocation ( program , 'aTintColor' ) ;
252+ this . aBgColor = gl . getAttribLocation ( program , 'aBgColor' ) ;
239253
240254 // Get and store the uniforms for the shader
241255 // this part is different for multi-textures
@@ -247,9 +261,12 @@ Phaser.Renderer.WebGL.BatchManager.prototype = {
247261 // texture coordinate
248262 gl . enableVertexAttribArray ( 1 ) ;
249263
250- // color attribute
264+ // tint color attribute
251265 gl . enableVertexAttribArray ( 2 ) ;
252266
267+ // bg color attribute
268+ gl . enableVertexAttribArray ( 3 ) ;
269+
253270 // texture index
254271 // gl.enableVertexAttribArray(3);
255272
@@ -470,6 +487,7 @@ Phaser.Renderer.WebGL.BatchManager.prototype = {
470487 positions [ i ++ ] = uvs . x0 ;
471488 positions [ i ++ ] = uvs . y0 ;
472489 colors [ i ++ ] = sprite . color . _glTint . topLeft + ( sprite . color . worldAlpha * 255 << 24 ) ;
490+ colors [ i ++ ] = sprite . color . _glBg ;
473491 // positions[i++] = textureIndex;
474492
475493 // Top Right vert (xy, uv, color)
@@ -478,6 +496,7 @@ Phaser.Renderer.WebGL.BatchManager.prototype = {
478496 positions [ i ++ ] = uvs . x1 ;
479497 positions [ i ++ ] = uvs . y1 ;
480498 colors [ i ++ ] = sprite . color . _glTint . topRight + ( sprite . color . worldAlpha * 255 << 24 ) ;
499+ colors [ i ++ ] = sprite . color . _glBg ;
481500 // positions[i++] = textureIndex;
482501
483502 // Bottom Right vert (xy, uv, color)
@@ -486,6 +505,7 @@ Phaser.Renderer.WebGL.BatchManager.prototype = {
486505 positions [ i ++ ] = uvs . x2 ;
487506 positions [ i ++ ] = uvs . y2 ;
488507 colors [ i ++ ] = sprite . color . _glTint . bottomRight + ( sprite . color . worldAlpha * 255 << 24 ) ;
508+ colors [ i ++ ] = sprite . color . _glBg ;
489509 // positions[i++] = textureIndex;
490510
491511 // Bottom Left vert (xy, uv, color)
@@ -494,6 +514,7 @@ Phaser.Renderer.WebGL.BatchManager.prototype = {
494514 positions [ i ++ ] = uvs . x3 ;
495515 positions [ i ++ ] = uvs . y3 ;
496516 colors [ i ++ ] = sprite . color . _glTint . bottomLeft + ( sprite . color . worldAlpha * 255 << 24 ) ;
517+ colors [ i ++ ] = sprite . color . _glBg ;
497518 // positions[i++] = textureIndex;
498519
499520 this . sprites [ this . currentBatchSize ++ ] = sprite ;
@@ -530,8 +551,11 @@ Phaser.Renderer.WebGL.BatchManager.prototype = {
530551 // set the texture coordinate
531552 gl . vertexAttribPointer ( this . aTextureCoord , 2 , gl . FLOAT , false , this . vertSize , 8 ) ;
532553
533- // color attributes will be interpreted as unsigned bytes and normalized
534- gl . vertexAttribPointer ( this . colorAttribute , 4 , gl . UNSIGNED_BYTE , true , this . vertSize , 16 ) ;
554+ // tint color attributes will be interpreted as unsigned bytes and normalized
555+ gl . vertexAttribPointer ( this . aTintColor , 4 , gl . UNSIGNED_BYTE , true , this . vertSize , 16 ) ;
556+
557+ // bg color attributes will be interpreted as unsigned bytes and normalized
558+ gl . vertexAttribPointer ( this . aBgColor , 4 , gl . UNSIGNED_BYTE , true , this . vertSize , 20 ) ;
535559
536560 // texture index
537561 // gl.vertexAttribPointer(this.aTextureIndex, 2, gl.FLOAT, false, this.vertSize, 20);
0 commit comments