Skip to content

Commit 6d21a4c

Browse files
committed
First working example of multi texture support
1 parent 1f72355 commit 6d21a4c

2 files changed

Lines changed: 29 additions & 15 deletions

File tree

src/pixi/renderers/webgl/shaders/PixiShader.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ PIXI.PixiShader.prototype.constructor = PIXI.PixiShader;
8080
PIXI.PixiShader.prototype.init = function()
8181
{
8282
var gl = this.gl;
83-
var MAX_TEXTURES = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
83+
this.MAX_TEXTURES = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
8484
var dynamicIfs = '\tif (vTextureIndex == 0.0) gl_FragColor = texture2D(uSamplerArray[0], vTextureCoord) * vColor;\n'
85-
for (var index = 1; index < MAX_TEXTURES; ++index)
85+
for (var index = 1; index < this.MAX_TEXTURES; ++index)
8686
{
8787
dynamicIfs += '\telse if (vTextureIndex == ' +
8888
index + '.0) gl_FragColor = texture2D(uSamplerArray[' +
@@ -94,7 +94,7 @@ PIXI.PixiShader.prototype.init = function()
9494
'varying vec2 vTextureCoord;',
9595
'varying vec4 vColor;',
9696
'varying float vTextureIndex;',
97-
'uniform sampler2D uSamplerArray[' + MAX_TEXTURES + '];',
97+
'uniform sampler2D uSamplerArray[' + this.MAX_TEXTURES + '];',
9898
'const vec4 PINK = vec4(1.0, 0.0, 1.0, 1.0);',
9999
'const vec4 GREEN = vec4(0.0, 1.0, 0.0, 1.0);',
100100
'void main(void) {',
@@ -121,7 +121,7 @@ PIXI.PixiShader.prototype.init = function()
121121
this.aTextureIndex = gl.getAttribLocation(program, 'aTextureIndex');
122122

123123
var indices = [];
124-
for (var i = 0; i < MAX_TEXTURES; ++i) {
124+
for (var i = 0; i < this.MAX_TEXTURES; ++i) {
125125
indices.push(i);
126126
}
127127
// NOTE:!!!

src/pixi/renderers/webgl/utils/WebGLSpriteBatch.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -139,16 +139,22 @@ PIXI.WebGLSpriteBatch = function () {
139139
* @type AbstractFilter
140140
*/
141141
this.defaultShader = null;
142+
143+
/**
144+
* @property textureArray
145+
* @type Array
146+
*/
147+
this.textureArray = [];
142148
};
143149

144150
/**
145151
* @method setContext
146152
* @param gl {WebGLContext} the current WebGL drawing context
147153
*/
148154
PIXI.WebGLSpriteBatch.prototype.setContext = function (gl) {
149-
var MAX_TEXTURES = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
155+
this.MAX_TEXTURES = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
150156
var dynamicIfs = '\tif (vTextureIndex == 0.0) gl_FragColor = texture2D(uSamplerArray[0], vTextureCoord) * vColor;\n'
151-
for (var index = 1; index < MAX_TEXTURES; ++index) {
157+
for (var index = 1; index < this.MAX_TEXTURES; ++index) {
152158
dynamicIfs += '\telse if (vTextureIndex == ' +
153159
index + '.0) gl_FragColor = texture2D(uSamplerArray[' +
154160
index + '], vTextureCoord) * vColor;\n'
@@ -160,7 +166,7 @@ PIXI.WebGLSpriteBatch.prototype.setContext = function (gl) {
160166
'varying vec2 vTextureCoord;',
161167
'varying vec4 vColor;',
162168
'varying float vTextureIndex;',
163-
'uniform sampler2D uSamplerArray[' + MAX_TEXTURES + '];',
169+
'uniform sampler2D uSamplerArray[' + this.MAX_TEXTURES + '];',
164170
'void main(void) {',
165171
dynamicIfs,
166172
'\telse gl_FragColor = texture2D(uSamplerArray[0], vTextureCoord) * vColor;',
@@ -216,6 +222,14 @@ PIXI.WebGLSpriteBatch.prototype.end = function () {
216222
*/
217223
PIXI.WebGLSpriteBatch.prototype.render = function (sprite, matrix) {
218224
var texture = sprite.texture;
225+
var baseTexture = texture.baseTexture;
226+
var gl = this.gl;
227+
if (this.textureArray[baseTexture.textureIndex] != baseTexture) {
228+
gl.activeTexture(gl.TEXTURE0 + baseTexture.textureIndex);
229+
gl.bindTexture(gl.TEXTURE_2D, baseTexture._glTextures[gl.id]);
230+
this.textureArray[baseTexture.textureIndex] = baseTexture;
231+
this.flush();
232+
}
219233

220234
// They provided an alternative rendering matrix, so use it
221235
var wt = sprite.worldTransform;
@@ -260,7 +274,7 @@ PIXI.WebGLSpriteBatch.prototype.render = function (sprite, matrix) {
260274
h1 = texture.frame.height * -aY;
261275
}
262276

263-
var i = this.currentBatchSize * this.vertexSize;//4 * this.vertSize;
277+
var i = this.currentBatchSize * this.vertexSize; //4 * this.vertSize;
264278
var tiOffset = this.currentBatchSize * 4;
265279
var resolution = texture.baseTexture.resolution;
266280
var textureIndex = texture.baseTexture.textureIndex;
@@ -347,6 +361,7 @@ PIXI.WebGLSpriteBatch.prototype.render = function (sprite, matrix) {
347361
PIXI.WebGLSpriteBatch.prototype.renderTilingSprite = function (sprite) {
348362
var texture = sprite.tilingTexture;
349363

364+
350365
// check texture..
351366
if (this.currentBatchSize >= this.size) {
352367
this.flush();
@@ -409,7 +424,7 @@ PIXI.WebGLSpriteBatch.prototype.renderTilingSprite = function (sprite) {
409424
var h0 = height * (1 - aY);
410425
var h1 = height * -aY;
411426

412-
var i = this.currentBatchSize * this.vertexSize;//4 * this.vertSize;
427+
var i = this.currentBatchSize * this.vertexSize; //4 * this.vertSize;
413428

414429
var resolution = texture.baseTexture.resolution;
415430

@@ -538,6 +553,7 @@ PIXI.WebGLSpriteBatch.prototype.flush = function () {
538553
} else {
539554
nextTexture = sprite.texture.baseTexture;
540555
}
556+
541557
nextBlendMode = sprite.blendMode;
542558
nextShader = sprite.shader || this.defaultShader;
543559

@@ -549,8 +565,10 @@ PIXI.WebGLSpriteBatch.prototype.flush = function () {
549565
if (skip && sprite.children.length > 0) {
550566
skip = false;
551567
}
552-
553-
if (( currentBaseTexture !== nextTexture && !skip) || blendSwap || shaderSwap) {
568+
//
569+
if (/*(currentBaseTexture != nextTexture && !skip) ||*/
570+
blendSwap ||
571+
shaderSwap) {
554572
this.renderBatch(currentBaseTexture, batchSize, start);
555573

556574
start = i;
@@ -627,13 +645,9 @@ PIXI.WebGLSpriteBatch.prototype.renderBatch = function (texture, size, startInde
627645
}
628646
} else {
629647
gl.activeTexture(gl.TEXTURE0 + texture.textureIndex);
630-
// bind the current texture
631648
gl.bindTexture(gl.TEXTURE_2D, texture._glTextures[gl.id]);
632649
}
633-
634-
// now draw those suckas!
635650
gl.drawElements(gl.TRIANGLES, size * 6, gl.UNSIGNED_SHORT, startIndex * 6 * 2);
636-
637651
// increment the draw count
638652
this.renderSession.drawCount++;
639653
};

0 commit comments

Comments
 (0)