Skip to content

Commit fd38987

Browse files
committed
Added ability to toggle multi-texture support at run-time.
1 parent 8de9921 commit fd38987

5 files changed

Lines changed: 146 additions & 77 deletions

File tree

src/core/Game.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -944,24 +944,24 @@ Phaser.Game.prototype = {
944944

945945
// this.updates.preUpdate();
946946

947-
this.scale.preUpdate();
948-
this.debug.preUpdate();
947+
// this.scale.preUpdate();
948+
// this.debug.preUpdate();
949949
// this.camera.preUpdate();
950-
this.physics.preUpdate();
950+
// this.physics.preUpdate();
951951
this.state.preUpdate(timeStep);
952-
this.plugins.preUpdate(timeStep);
952+
// this.plugins.preUpdate(timeStep);
953953
this.stage.preUpdate();
954954

955955
this.tweens.update();
956956
this.state.update();
957957
this.stage.update();
958958
// this.tweens.update();
959959

960-
this.sound.update();
961-
this.input.update();
962-
this.physics.update();
963-
this.particles.update();
964-
this.plugins.update();
960+
// this.sound.update();
961+
// this.input.update();
962+
// this.physics.update();
963+
// this.particles.update();
964+
// this.plugins.update();
965965

966966
this.stage.postUpdate();
967967
this.plugins.postUpdate();
@@ -1004,11 +1004,11 @@ Phaser.Game.prototype = {
10041004

10051005
this.renderer.render(this.stage);
10061006

1007-
this.plugins.render(elapsedTime);
1007+
// this.plugins.render(elapsedTime);
10081008

10091009
this.state.render(elapsedTime);
10101010

1011-
this.plugins.postRender(elapsedTime);
1011+
// this.plugins.postRender(elapsedTime);
10121012

10131013
this.updates.stop();
10141014
},

src/gameobjects/stage/Stage.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,13 @@ Phaser.Stage.prototype.preUpdate = function ()
149149
{
150150
this.worldAlpha = this.alpha;
151151

152-
this.currentRenderOrderID = 0;
152+
// this.currentRenderOrderID = 0;
153153

154154
// This can't loop in reverse, we need the renderOrderID to be in sequence
155155
for (var i = 0; i < this.children.list.length; i++)
156156
{
157157
this.children.list[i].preUpdate();
158158
}
159-
160159
};
161160

162161
/**

src/renderer/webgl/BatchManager.js

Lines changed: 103 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ()

src/renderer/webgl/WebGLRenderer.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,26 @@ Phaser.Renderer.WebGL.prototype = {
278278

279279
},
280280

281+
enableMultiTextureSupport: function (textureArray)
282+
{
283+
this.multiTexture = true;
284+
285+
// Recompile the batch
286+
this.batch.initMultiTexture();
287+
288+
if (Array.isArray(textureArray))
289+
{
290+
var index = 0;
291+
292+
for (var i = 0; i < textureArray.length; i++)
293+
{
294+
var texture = this.game.textures.get(textureArray[i]);
295+
296+
index = texture.setTextureIndex(index);
297+
}
298+
}
299+
},
300+
281301
resize: function (width, height)
282302
{
283303
this.width = width * this.game.resolution;
@@ -715,6 +735,15 @@ Phaser.Renderer.WebGL.prototype = {
715735
return shaderProgram;
716736
},
717737

738+
deleteProgram: function (program)
739+
{
740+
var gl = this.gl;
741+
742+
gl.deleteProgram(program);
743+
744+
return this;
745+
},
746+
718747
createEmptyTexture: function (width, height, scaleMode)
719748
{
720749
var gl = this.gl;

src/textures/Texture.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ Phaser.Texture.prototype = {
9494
{
9595
this.source[i].glTextureIndex = index;
9696

97+
console.log(this.source[i].image.currentSrc, 'index = ', index);
98+
9799
index++;
98100
}
99101

0 commit comments

Comments
 (0)