@@ -109,8 +109,7 @@ Phaser.Renderer.WebGL.BatchManager = function (renderer, batchSize)
109109 'uniform sampler2D uSampler;' , // our texture
110110
111111 'void main(void) {' ,
112- ' vec4 pixel = texture2D(uSampler, vTextureCoord);' , // get the color from the texture
113- // ' vec4 pixel = texture2D(uSampler, vTextureCoord) * vTintColor;', // get the color from the texture
112+ ' vec4 pixel = texture2D(uSampler, vTextureCoord) * vTintColor;' , // get the color from the texture
114113 // ' if (pixel.a == 0.0) pixel = vBgColor;', // if texture alpha is zero, use the bg color
115114 ' gl_FragColor = pixel;' ,
116115 '}'
@@ -216,104 +215,144 @@ Phaser.Renderer.WebGL.BatchManager.prototype = {
216215 // Set the source of the buffer data (this.vertices array)
217216 gl . bufferData ( gl . ARRAY_BUFFER , this . vertices , gl . DYNAMIC_DRAW ) ;
218217
219- var fragmentSrc = this . fragmentSrc ;
220-
221218 if ( this . renderer . multiTexture )
222219 {
223- var multiFrag = [
224- 'precision lowp float;' ,
225-
226- 'varying vec2 vTextureCoord;' , // the texture coords passed in from the vertex shader
227- 'varying vec4 vTintColor;' , // the color value passed in from the vertex shader (texture color + alpha + tint)
228- 'varying vec4 vBgColor;' , // the bg color value passed in from the vertex shader
229- 'varying float vTextureIndex;' ,
230-
231- 'uniform sampler2D uSamplerArray[' + this . renderer . maxTextures + '];' ,
232-
233- 'const vec4 PINK = vec4(1.0, 0.0, 1.0, 1.0);' ,
234-
235- 'void main(void) {' ,
236-
237- ' vec4 pixel;' ,
238- ' if (vTextureIndex == 0.0) pixel = texture2D(uSamplerArray[0], vTextureCoord);'
239- ] ;
240-
241- for ( i = 1 ; i < this . renderer . maxTextures ; i ++ )
242- {
243- multiFrag . push ( ' else if (vTextureIndex == ' + i + '.0) pixel = texture2D(uSamplerArray[' + i + '], vTextureCoord);' ) ;
244- }
245-
246- multiFrag = multiFrag . concat ( [
247- ' else pixel = PINK;' ,
248-
249- ' pixel *= vTintColor;' ,
250- ' if (pixel.a == 0.0) pixel = vBgColor;' , // if texture alpha is zero, use the bg color
251- ' gl_FragColor = pixel;' ,
252- '}'
253- ] ) ;
254-
255- this . multiTextureFragmentSrc = multiFrag ;
256-
257- fragmentSrc = this . multiTextureFragmentSrc ;
220+ this . initMultiTexture ( ) ;
258221 }
222+ else
223+ {
224+ this . initSingleTexture ( ) ;
225+ }
226+ } ,
259227
260- // Compile the Shader
261- var program = this . renderer . compileProgram ( this . vertexSrc , fragmentSrc ) ;
262-
263- // Set Shader
264- gl . useProgram ( program ) ;
228+ initAttributes : function ( )
229+ {
230+ var gl = this . gl ;
231+ var program = this . program ;
265232
266233 // Get and store the attributes
267234
268235 // vertex position
269236 this . aVertexPosition = gl . getAttribLocation ( program , 'aVertexPosition' ) ;
270237 gl . enableVertexAttribArray ( this . aVertexPosition ) ;
271238
239+ // texture coordinate
272240 this . aTextureCoord = gl . getAttribLocation ( program , 'aTextureCoord' ) ;
273241 gl . enableVertexAttribArray ( this . aTextureCoord ) ;
274242
243+ // texture index
275244 this . aTextureIndex = gl . getAttribLocation ( program , 'aTextureIndex' ) ;
276245 gl . enableVertexAttribArray ( this . aTextureIndex ) ;
277246
247+ // tint / pixel color
278248 this . aTintColor = gl . getAttribLocation ( program , 'aTintColor' ) ;
279249 gl . enableVertexAttribArray ( this . aTintColor ) ;
280250
251+ // background pixel color
281252 this . aBgColor = gl . getAttribLocation ( program , 'aBgColor' ) ;
282253 gl . enableVertexAttribArray ( this . aBgColor ) ;
283254
284- // Get and store the uniforms for the shader
285- if ( this . renderer . multiTexture )
255+ // The projection vector (middle of the game world)
256+ this . projectionVector = gl . getUniformLocation ( program , 'projectionVector' ) ;
257+
258+ // The offset vector (camera shake)
259+ this . offsetVector = gl . getUniformLocation ( program , 'offsetVector' ) ;
260+ } ,
261+
262+ initSingleTexture : function ( )
263+ {
264+ console . log ( 'initSingleTexture' ) ;
265+
266+ var gl = this . gl ;
267+
268+ // Shader already exists
269+ if ( this . program )
286270 {
287- // Bind empty multi-textures to avoid WebGL spam
288- var indices = [ ] ;
271+ this . renderer . deleteProgram ( this . program ) ;
272+ }
273+
274+ // Compile the Shader
275+ this . program = this . renderer . compileProgram ( this . vertexSrc , this . fragmentSrc ) ;
289276
290- var tempTexture = this . renderer . createEmptyTexture ( 1 , 1 , 0 ) ;
277+ // Set Shader
278+ gl . useProgram ( this . program ) ;
291279
292- for ( i = 0 ; i < this . renderer . maxTextures ; i ++ )
293- {
294- gl . activeTexture ( gl . TEXTURE0 + i ) ;
280+ this . initAttributes ( ) ;
295281
296- gl . bindTexture ( gl . TEXTURE_2D , tempTexture ) ;
282+ this . uSampler = gl . getUniformLocation ( this . program , 'uSampler' ) ;
283+ } ,
297284
298- indices . push ( i ) ;
299- }
285+ initMultiTexture : function ( )
286+ {
287+ console . log ( 'initMultiTexture' ) ;
288+
289+ var gl = this . gl ;
290+
291+ var multiFrag = [
292+ 'precision lowp float;' ,
300293
301- this . uSampler = gl . getUniformLocation ( program , 'uSamplerArray[0]' ) ;
294+ 'varying vec2 vTextureCoord;' , // the texture coords passed in from the vertex shader
295+ 'varying vec4 vTintColor;' , // the color value passed in from the vertex shader (texture color + alpha + tint)
296+ 'varying vec4 vBgColor;' , // the bg color value passed in from the vertex shader
297+ 'varying float vTextureIndex;' ,
302298
303- gl . uniform1iv ( this . uSampler , indices ) ;
299+ 'uniform sampler2D uSamplerArray[' + this . renderer . maxTextures + '];' ,
300+
301+ 'const vec4 PINK = vec4(1.0, 0.0, 1.0, 1.0);' ,
302+
303+ 'void main(void) {' ,
304+
305+ ' vec4 pixel;' ,
306+ ' if (vTextureIndex == 0.0) pixel = texture2D(uSamplerArray[0], vTextureCoord);'
307+ ] ;
308+
309+ for ( var i = 1 ; i < this . renderer . maxTextures ; i ++ )
310+ {
311+ multiFrag . push ( ' else if (vTextureIndex == ' + i + '.0) pixel = texture2D(uSamplerArray[' + i + '], vTextureCoord);' ) ;
304312 }
305- else
313+
314+ multiFrag = multiFrag . concat ( [
315+ ' else pixel = PINK;' ,
316+
317+ ' pixel *= vTintColor;' ,
318+ // ' if (pixel.a == 0.0) pixel = vBgColor;', // if texture alpha is zero, use the bg color
319+ ' gl_FragColor = pixel;' ,
320+ '}'
321+ ] ) ;
322+
323+ this . multiTextureFragmentSrc = multiFrag ;
324+
325+ // Shader already exists
326+ if ( this . program )
306327 {
307- this . uSampler = gl . getUniformLocation ( program , 'uSampler' ) ;
328+ this . renderer . deleteProgram ( this . program ) ;
308329 }
309330
310- // The projection vector (middle of the game world)
311- this . projectionVector = gl . getUniformLocation ( program , 'projectionVector' ) ;
331+ // Compile the Shader
332+ this . program = this . renderer . compileProgram ( this . vertexSrc , this . multiTextureFragmentSrc ) ;
312333
313- // The offset vector (camera shake)
314- this . offsetVector = gl . getUniformLocation ( program , 'offsetVector' ) ;
334+ // Set Shader
335+ gl . useProgram ( this . program ) ;
336+
337+ this . initAttributes ( ) ;
338+
339+ // Bind empty multi-textures to avoid WebGL spam
340+ var indices = [ ] ;
341+
342+ var tempTexture = this . renderer . createEmptyTexture ( 1 , 1 , 0 ) ;
343+
344+ for ( i = 0 ; i < this . renderer . maxTextures ; i ++ )
345+ {
346+ gl . activeTexture ( gl . TEXTURE0 + i ) ;
347+
348+ gl . bindTexture ( gl . TEXTURE_2D , tempTexture ) ;
349+
350+ indices . push ( i ) ;
351+ }
352+
353+ this . uSampler = gl . getUniformLocation ( this . program , 'uSamplerArray[0]' ) ;
315354
316- this . program = program ;
355+ gl . uniform1iv ( this . uSampler , indices ) ;
317356 } ,
318357
319358 begin : function ( )
0 commit comments