Skip to content

Commit ea0c1ab

Browse files
committed
Fixed shader so blending is applied properly. Textures now have a premultiplied alpha property flag and unpacking property is set at creation.
1 parent caabe65 commit ea0c1ab

17 files changed

Lines changed: 65 additions & 14 deletions
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module.exports = {
22

33
MAX_LIGHTS: 10,
4-
DEFERRED_MAX_LIGHTS: 100
4+
DEFERRED_MAX_LIGHTS: 50
55

66
};

v3/src/renderer/webgl/ResourceManager.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,19 @@ var ResourceManager = new Class({
9393
}
9494
},
9595

96-
createTexture: function (mipLevel, minFilter, magFilter, wrapT, wrapS, format, pixels, width, height)
96+
createTexture: function (mipLevel, minFilter, magFilter, wrapT, wrapS, format, pixels, width, height, pma)
9797
{
9898
var gl = this.gl;
9999
var texture = gl.createTexture();
100100

101+
pma = (pma === undefined || pma === null) ? true : pma;
102+
101103
gl.bindTexture(gl.TEXTURE_2D, texture);
102104
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, minFilter);
103105
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, magFilter);
104106
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, wrapS);
105107
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, wrapT);
108+
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, pma);
106109

107110
if (pixels === null || pixels === undefined)
108111
{

v3/src/renderer/webgl/WebGLRenderer.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ var WebGLRenderer = new Class({
142142
gl.disable(gl.CULL_FACE);
143143
gl.enable(gl.BLEND);
144144
gl.clearColor(color.redGL, color.greenGL, color.blueGL, color.alphaGL);
145-
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
146145

147146
// Map Blend Modes
148147
this.blendModes = [];

v3/src/renderer/webgl/resources/Texture.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ var Texture = new Class({
44

55
initialize:
66

7-
function Texture (texture, width, height)
7+
function Texture (texture, width, height, pma)
88
{
99
this.texture = texture;
1010
this.width = width;
1111
this.height = height;
1212
this.isRenderTexture = false;
13+
this.isAlphaPremultiplied = pma;
1314
}
1415

1516
});

v3/src/renderer/webgl/shaders/GBufferShader.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var GBufferShader = function ()
22
{
33
var frag = [
44
'#extension GL_EXT_draw_buffers : require',
5+
6+
'// G-Buffer Shader',
57

68
'precision mediump float;',
79

@@ -17,7 +19,7 @@ var GBufferShader = function ()
1719
' vec4 spriteColor = texture2D(uMainTexture, v_tex_coord) * vec4(v_color, v_alpha);',
1820
' vec3 spriteNormal = texture2D(uNormTexture, v_tex_coord).rgb;',
1921

20-
' gl_FragData[0] = vec4(spriteColor.rgb * spriteColor.a, spriteColor.a);',
22+
' gl_FragData[0] = spriteColor;',
2123
' gl_FragData[1] = vec4(spriteNormal, spriteColor.a);',
2224
'}'
2325
];

v3/src/renderer/webgl/shaders/LightFragmentShader.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var LightFragmentShader = function (maxLights)
22
{
33
var frag = [
4+
'// Light Fragment Shader ',
5+
46
'precision mediump float;',
57

68
'struct Light',

v3/src/renderer/webgl/shaders/MaskShader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports = {
22
vert: [
3+
'// Mask Shader ',
4+
35
'precision mediump float;',
46
'attribute vec2 a_position;',
57
'void main()',
@@ -9,6 +11,8 @@ module.exports = {
911
''
1012
].join('\n'),
1113
frag: [
14+
'// Mask Shader ',
15+
1216
'precision mediump float;',
1317
'uniform vec2 u_resolution;',
1418
'uniform sampler2D u_main_sampler;',

v3/src/renderer/webgl/shaders/ParticleShader.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var ParticleShader = function ()
22
{
33
var vert = [
4+
'// Particle Shader',
5+
46
'precision mediump float;',
57
'uniform mat4 u_view_matrix;',
68

@@ -21,6 +23,8 @@ var ParticleShader = function ()
2123
];
2224

2325
var frag = [
26+
'// Particle Shader',
27+
2428
'precision mediump float;',
2529

2630
'uniform sampler2D u_main_sampler;',
@@ -31,7 +35,7 @@ var ParticleShader = function ()
3135
'void main()',
3236
'{',
3337
' vec4 sample_color = texture2D(u_main_sampler, v_tex_coord) * v_color;',
34-
' gl_FragColor = vec4(sample_color.rgb * sample_color.a, sample_color.a);',
38+
' gl_FragColor = sample_color;',
3539
'}'
3640
];
3741

v3/src/renderer/webgl/shaders/Phong2DShaderDeferred.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
var Phong2DShaderDeferred = function (maxLights)
22
{
33
var vert = [
4+
'// Phong 2D Shader Deferred',
5+
46
'precision mediump float;',
57
'attribute vec2 vertexPosition;',
68
'void main()',
@@ -10,6 +12,8 @@ var Phong2DShaderDeferred = function (maxLights)
1012
];
1113

1214
var frag = [
15+
'// Phong 2D Shader Deferred',
16+
1317
'precision mediump float;',
1418

1519
'struct Light',
@@ -50,7 +54,7 @@ var Phong2DShaderDeferred = function (maxLights)
5054
' }',
5155

5256
' vec4 color_output = vec4(uAmbientLightColor + finalColor, gbColor.a);',
53-
' gl_FragColor = vec4(color_output.rgb * color_output.a, color_output.a);',
57+
' gl_FragColor = color_output;',
5458
'}'
5559
];
5660

v3/src/renderer/webgl/shaders/TexturedAndAlphaShader.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
module.exports = {
22
vert: [
3+
'// Textured and Alpha Shader',
4+
35
'uniform mat4 u_view_matrix;',
46
'attribute vec2 a_position;',
57
'attribute vec2 a_tex_coord;',
@@ -13,13 +15,15 @@ module.exports = {
1315
'}'
1416
].join('\n'),
1517
frag: [
18+
'// Textured and Alpha Shader',
19+
1620
'precision mediump float;',
1721
'uniform sampler2D u_sampler2D;',
1822
'varying vec2 v_tex_coord;',
1923
'varying float v_alpha;',
2024
'void main() {',
2125
' vec4 output_color = texture2D(u_sampler2D, v_tex_coord);',
22-
' gl_FragColor = vec4(output_color.rgb * v_alpha * output_color.a, v_alpha * output_color.a);',
26+
' gl_FragColor = output_color;',
2327
'}'
2428
].join('\n')
2529
};

0 commit comments

Comments
 (0)