by default multitexture is disabled

This commit is contained in:
Felipe Alfonso 2016-09-08 19:17:59 -03:00
parent 8a8e026ce3
commit 7350509f0b
2 changed files with 106 additions and 10 deletions

View file

@ -5,6 +5,7 @@
// this is where we store the webGL contexts for easy access.
PIXI.glContexts = [];
PIXI.instances = [];
PIXI._enableMultiTextureToggle = false;
/**
* The WebGLRenderer draws the stage and all its content onto a WebGL enabled canvas.
@ -178,6 +179,7 @@ PIXI.WebGLRenderer = function (game) {
*/
this.renderSession = {};
// Needed?
this.renderSession.game = this.game;
this.renderSession.gl = this.gl;
@ -210,13 +212,29 @@ PIXI.WebGLRenderer.prototype.initContext = function()
var gl = this.view.getContext('webgl', this._contextOptions) || this.view.getContext('experimental-webgl', this._contextOptions);
this.gl = gl;
this.maxTextures = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
if (!gl) {
// fail, not able to get a context
throw new Error('This browser does not support webGL. Try using the canvas renderer');
}
this.maxTextures = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
// HACK: Fill all texture units with empty 1x1 texture
// ---
if (PIXI._enableMultiTextureToggle)
{
var tempTexture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGB, 1, 1, 0, gl.RGB, gl.UNSIGNED_BYTE, null);
for (var index = 0; index < this.maxTextures; ++index)
{
gl.activeTexture(gl.TEXTURE0 + index);
gl.bindTexture(gl.TEXTURE_2D, tempTexture);
}
}
// ---
this.glContextId = gl.id = PIXI.WebGLRenderer.glContextId++;
PIXI.glContexts[this.glContextId] = gl;
@ -243,11 +261,15 @@ PIXI.WebGLRenderer.prototype.initContext = function()
};
PIXI.WebGLRenderer.prototype.setTexturePriority = function (textureNameCollection) {
if (!PIXI._enableMultiTextureToggle)
{
console.warn('Phaser: Can\'t call setTexturePriority if multi texture batching isn\'t enabled');
return;
}
var maxTextures = this.maxTextures;
var imageCache = this.game.cache._cache.image;
var imageName = null;
var gl = this.gl;
// We start from 1 because framebuffer texture uses unit 0.
for (var index = 0; index < textureNameCollection.length; ++index)
{
@ -505,4 +527,13 @@ PIXI.WebGLRenderer.prototype.mapBlendModes = function () {
};
PIXI.WebGLRenderer.prototype.getMaxTextureUnit = function() {
var gl = this.gl;
return gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
};
PIXI.enableMultiTexture = function() {
PIXI._enableMultiTextureToggle = true;
};
PIXI.WebGLRenderer.glContextId = 0;

View file

@ -72,13 +72,7 @@ PIXI.PixiShader = function(gl)
PIXI.PixiShader.prototype.constructor = PIXI.PixiShader;
/**
* Initialises the shader.
*
* @method init
*/
PIXI.PixiShader.prototype.init = function()
{
PIXI.PixiShader.prototype.initMultitexShader = function () {
var gl = this.gl;
this.MAX_TEXTURES = gl.getParameter(gl.MAX_TEXTURE_IMAGE_UNITS);
var dynamicIfs = '\tif (vTextureIndex == 0.0) gl_FragColor = texture2D(uSamplerArray[0], vTextureCoord) * vColor;\n'
@ -158,6 +152,77 @@ PIXI.PixiShader.prototype.init = function()
this.program = program;
};
PIXI.PixiShader.prototype.initDefaultShader = function () {
this.fragmentSrc = [
'precision lowp float;',
'varying vec2 vTextureCoord;',
'varying vec4 vColor;',
'varying float vTextureIndex;',
'uniform sampler2D uSampler;',
'void main(void) {',
' gl_FragColor = texture2D(uSampler, vTextureCoord) * vColor ;',
'}'
];
var gl = this.gl;
var program = PIXI.compileProgram(gl, this.vertexSrc || PIXI.PixiShader.defaultVertexSrc, this.fragmentSrc);
gl.useProgram(program);
// get and store the uniforms for the shader
this.uSampler = gl.getUniformLocation(program, 'uSampler');
this.projectionVector = gl.getUniformLocation(program, 'projectionVector');
this.offsetVector = gl.getUniformLocation(program, 'offsetVector');
this.dimensions = gl.getUniformLocation(program, 'dimensions');
// get and store the attributes
this.aVertexPosition = gl.getAttribLocation(program, 'aVertexPosition');
this.aTextureCoord = gl.getAttribLocation(program, 'aTextureCoord');
this.colorAttribute = gl.getAttribLocation(program, 'aColor');
this.aTextureIndex = gl.getAttribLocation(program, 'aTextureIndex');
// Begin worst hack eva //
// WHY??? ONLY on my chrome pixel the line above returns -1 when using filters?
// maybe its something to do with the current state of the gl context.
// I'm convinced this is a bug in the chrome browser as there is NO reason why this should be returning -1 especially as it only manifests on my chrome pixel
// If theres any webGL people that know why could happen please help :)
if(this.colorAttribute === -1)
{
this.colorAttribute = 2;
}
this.attributes = [this.aVertexPosition, this.aTextureCoord, this.colorAttribute, this.aTextureIndex];
// End worst hack eva //
// add those custom shaders!
for (var key in this.uniforms)
{
// get the uniform locations..
this.uniforms[key].uniformLocation = gl.getUniformLocation(program, key);
}
this.initUniforms();
this.program = program;
};
/**
* Initialises the shader.
*
* @method init
*/
PIXI.PixiShader.prototype.init = function()
{
if (PIXI._enableMultiTextureToggle) {
this.initMultitexShader();
} else {
this.initDefaultShader();
}
};
/**
* Initialises the shader uniform values.
*