Skip to content

Commit c8f7761

Browse files
committed
Added selection of multi texture for the rest of PIXI shaders
1 parent 58f74ec commit c8f7761

2 files changed

Lines changed: 131 additions & 89 deletions

File tree

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

Lines changed: 61 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,49 @@ PIXI.PixiFastShader = function (gl) {
2828
*/
2929
this.program = null;
3030

31-
var gl = this.gl;
32-
this.MAX_TEXTURES = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
33-
var dynamicIfs = '\tif (vTextureIndex == 0.0) gl_FragColor = texture2D(uSamplerArray[0], vTextureCoord) * vColor;\n'
34-
for (var index = 1; index < this.MAX_TEXTURES; ++index)
35-
{
36-
dynamicIfs += '\telse if (vTextureIndex == ' +
37-
index + '.0) gl_FragColor = texture2D(uSamplerArray[' +
38-
index + '], vTextureCoord) * vColor;\n'
39-
}
40-
41-
/**
42-
* The fragment shader.
43-
* @property fragmentSrc
44-
* @type Array
45-
*/
46-
this.fragmentSrc = [
47-
'// PixiFastShader Fragment Shader.',
48-
'precision lowp float;',
49-
'varying vec2 vTextureCoord;',
50-
'varying float vColor;',
51-
'varying float vTextureIndex;',
52-
'uniform sampler2D uSamplerArray[' + this.MAX_TEXTURES + '];',
53-
'const vec4 PINK = vec4(1.0, 0.0, 1.0, 1.0);',
54-
'const vec4 GREEN = vec4(0.0, 1.0, 0.0, 1.0);',
55-
'void main(void) {',
56-
dynamicIfs,
57-
'else gl_FragColor = PINK;',
58-
'}'
59-
];
31+
if (PIXI._enableMultiTextureToggle) {
32+
var gl = this.gl;
33+
this.MAX_TEXTURES = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
34+
var dynamicIfs = '\tif (vTextureIndex == 0.0) gl_FragColor = texture2D(uSamplerArray[0], vTextureCoord) * vColor;\n'
35+
for (var index = 1; index < this.MAX_TEXTURES; ++index)
36+
{
37+
dynamicIfs += '\telse if (vTextureIndex == ' +
38+
index + '.0) gl_FragColor = texture2D(uSamplerArray[' +
39+
index + '], vTextureCoord) * vColor;\n'
40+
}
41+
42+
/**
43+
* The fragment shader.
44+
* @property fragmentSrc
45+
* @type Array
46+
*/
47+
this.fragmentSrc = [
48+
'// PixiFastShader Fragment Shader.',
49+
'precision lowp float;',
50+
'varying vec2 vTextureCoord;',
51+
'varying float vColor;',
52+
'varying float vTextureIndex;',
53+
'uniform sampler2D uSamplerArray[' + this.MAX_TEXTURES + '];',
54+
'const vec4 PINK = vec4(1.0, 0.0, 1.0, 1.0);',
55+
'const vec4 GREEN = vec4(0.0, 1.0, 0.0, 1.0);',
56+
'void main(void) {',
57+
dynamicIfs,
58+
'else gl_FragColor = PINK;',
59+
'}'
60+
];
61+
} else {
62+
this.fragmentSrc = [
63+
'// PixiFastShader Fragment Shader.',
64+
'precision lowp float;',
65+
'varying vec2 vTextureCoord;',
66+
'varying float vColor;',
67+
'varying float vTextureIndex;',
68+
'uniform sampler2D uSampler;',
69+
'void main(void) {',
70+
' gl_FragColor = texture2D(uSampler, vTextureCoord) * vColor;'
71+
'}'
72+
];
73+
}
6074

6175
/**
6276
* The vertex shader.
@@ -122,22 +136,26 @@ PIXI.PixiFastShader.prototype.init = function () {
122136
gl.useProgram(program);
123137

124138
// get and store the uniforms for the shader
125-
this.uSampler = gl.getUniformLocation(program, 'uSamplerArray[0]');
126-
127-
var indices = [];
128-
// HACK: we bind an empty texture to avoid WebGL warning spam.
129-
var tempTexture = gl.createTexture();
130-
gl.activeTexture(gl.TEXTURE0);
131-
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
132-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, null);
133-
for (var i = 0; i < this.MAX_TEXTURES; ++i) {
134-
gl.activeTexture(gl.TEXTURE0 + i);
139+
this.uSampler = PIXI._enableMultiTextureToggle ?
140+
gl.getUniformLocation(program, 'uSamplerArray[0]') :
141+
gl.getUniformLocation(program, 'uSampler');
142+
143+
if (PIXI._enableMultiTextureToggle) {
144+
var indices = [];
145+
// HACK: we bind an empty texture to avoid WebGL warning spam.
146+
var tempTexture = gl.createTexture();
147+
gl.activeTexture(gl.TEXTURE0);
135148
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
136-
indices.push(i);
149+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, null);
150+
for (var i = 0; i < this.MAX_TEXTURES; ++i) {
151+
gl.activeTexture(gl.TEXTURE0 + i);
152+
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
153+
indices.push(i);
154+
}
155+
gl.activeTexture(gl.TEXTURE0);
156+
gl.uniform1iv(this.uSampler, indices);
137157
}
138-
gl.activeTexture(gl.TEXTURE0);
139-
gl.uniform1iv(this.uSampler, indices);
140-
158+
141159
this.projectionVector = gl.getUniformLocation(program, 'projectionVector');
142160
this.offsetVector = gl.getUniformLocation(program, 'offsetVector');
143161
this.dimensions = gl.getUniformLocation(program, 'dimensions');

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

Lines changed: 70 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -29,38 +29,58 @@ PIXI.StripShader = function(gl)
2929
*/
3030
this.program = null;
3131

32-
var gl = this.gl;
33-
this.MAX_TEXTURES = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
34-
var dynamicIfs = '\tif (vTextureIndex == 0.0) gl_FragColor = texture2D(uSamplerArray[0], vTextureCoord);\n'
35-
for (var index = 1; index < this.MAX_TEXTURES; ++index)
36-
{
37-
dynamicIfs += '\telse if (vTextureIndex == ' +
38-
index + '.0) gl_FragColor = texture2D(uSamplerArray[' +
39-
index + '], vTextureCoord) ;\n'
32+
if (PIXI._enableMultiTextureToggle) {
33+
var gl = this.gl;
34+
this.MAX_TEXTURES = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
35+
var dynamicIfs = '\tif (vTextureIndex == 0.0) gl_FragColor = texture2D(uSamplerArray[0], vTextureCoord);\n'
36+
for (var index = 1; index < this.MAX_TEXTURES; ++index)
37+
{
38+
dynamicIfs += '\telse if (vTextureIndex == ' +
39+
index + '.0) gl_FragColor = texture2D(uSamplerArray[' +
40+
index + '], vTextureCoord) ;\n'
41+
}
42+
43+
44+
/**
45+
* The fragment shader.
46+
* @property fragmentSrc
47+
* @type Array
48+
*/
49+
this.fragmentSrc = [
50+
'//StripShader Fragment Shader.',
51+
'precision mediump float;',
52+
'varying vec2 vTextureCoord;',
53+
'varying float vTextureIndex;',
54+
// 'varying float vColor;',
55+
'uniform float alpha;',
56+
'uniform sampler2D uSamplerArray[' + this.MAX_TEXTURES + '];',
57+
'const vec4 PINK = vec4(1.0, 0.0, 1.0, 1.0);',
58+
'const vec4 GREEN = vec4(0.0, 1.0, 0.0, 1.0);',
59+
'void main(void) {',
60+
dynamicIfs,
61+
'else gl_FragColor = PINK;',
62+
'}'
63+
];
64+
} else {
65+
/**
66+
* The fragment shader.
67+
* @property fragmentSrc
68+
* @type Array
69+
*/
70+
this.fragmentSrc = [
71+
'//StripShader Fragment Shader.',
72+
'precision mediump float;',
73+
'varying vec2 vTextureCoord;',
74+
'varying float vTextureIndex;',
75+
// 'varying float vColor;',
76+
'uniform float alpha;',
77+
'uniform sampler2D uSampler;',
78+
'void main(void) {',
79+
' gl_FragColor = texture2D(uSampler, vTextureCoord);',
80+
'}'
81+
];
4082
}
4183

42-
43-
/**
44-
* The fragment shader.
45-
* @property fragmentSrc
46-
* @type Array
47-
*/
48-
this.fragmentSrc = [
49-
'//StripShader Fragment Shader.',
50-
'precision mediump float;',
51-
'varying vec2 vTextureCoord;',
52-
'varying float vTextureIndex;',
53-
// 'varying float vColor;',
54-
'uniform float alpha;',
55-
'uniform sampler2D uSamplerArray[' + this.MAX_TEXTURES + '];',
56-
'const vec4 PINK = vec4(1.0, 0.0, 1.0, 1.0);',
57-
'const vec4 GREEN = vec4(0.0, 1.0, 0.0, 1.0);',
58-
'void main(void) {',
59-
dynamicIfs,
60-
'else gl_FragColor = PINK;',
61-
'}'
62-
];
63-
6484
/**
6585
* The vertex shader.
6686
* @property vertexSrc
@@ -107,23 +127,27 @@ PIXI.StripShader.prototype.init = function()
107127
gl.useProgram(program);
108128

109129
// get and store the uniforms for the shader
110-
// get and store the uniforms for the shader
111-
this.uSampler = gl.getUniformLocation(program, 'uSamplerArray[0]');
112-
113-
var indices = [];
114-
// HACK: we bind an empty texture to avoid WebGL warning spam.
115-
var tempTexture = gl.createTexture();
116-
gl.activeTexture(gl.TEXTURE0);
117-
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
118-
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, null);
119-
for (var i = 0; i < this.MAX_TEXTURES; ++i) {
120-
gl.activeTexture(gl.TEXTURE0 + i);
121-
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
122-
indices.push(i);
123-
}
124-
gl.activeTexture(gl.TEXTURE0);
125-
gl.uniform1iv(this.uSampler, indices);
130+
this.uSampler = PIXI._enableMultiTextureToggle ?
131+
gl.getUniformLocation(program, 'uSamplerArray[0]') :
132+
gl.getUniformLocation(program, 'uSampler');
126133

134+
135+
if (PIXI._enableMultiTextureToggle) {
136+
var indices = [];
137+
// HACK: we bind an empty texture to avoid WebGL warning spam.
138+
var tempTexture = gl.createTexture();
139+
gl.activeTexture(gl.TEXTURE0);
140+
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
141+
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, null);
142+
for (var i = 0; i < this.MAX_TEXTURES; ++i) {
143+
gl.activeTexture(gl.TEXTURE0 + i);
144+
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
145+
indices.push(i);
146+
}
147+
gl.activeTexture(gl.TEXTURE0);
148+
gl.uniform1iv(this.uSampler, indices);
149+
}
150+
127151
this.projectionVector = gl.getUniformLocation(program, 'projectionVector');
128152
this.offsetVector = gl.getUniformLocation(program, 'offsetVector');
129153
this.colorAttribute = gl.getAttribLocation(program, 'aColor');

0 commit comments

Comments
 (0)