Skip to content

Commit e23a86d

Browse files
committed
The default BaseShader vertex shader has a new uniform uResolution which is set during the Shader init and load to be the size of the Game Object to which the shader is bound.
1 parent f792fce commit e23a86d

3 files changed

Lines changed: 11 additions & 3 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
### Updates
66

77
* `Zones` will now use the new `customHitArea` property introduced in 3.17 to avoid their hit areas from being resized if you specified your own custom hit area (thanks @rexrainbow)
8-
* The default `BaseShader` vertex shader will now set the `fragCoord` varying to be the viewport height minus the y inPosition. This will give the correct y axis in the fragment shader, causing 'inverted' shaders to display normally when using the default vertex code.
8+
* The default `BaseShader` vertex shader has a new uniform `uResolution` which is set during the Shader init and load to be the size of the Game Object to which the shader is bound.
9+
* The default `BaseShader` vertex shader will now set the `fragCoord` varying to be the Game Object height minus the y inPosition. This will give the correct y axis in the fragment shader, causing 'inverted' shaders to display normally when using the default vertex code.
910

1011
### Bug Fixes
1112

src/display/shader/BaseShader.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,15 @@ var BaseShader = new Class({
6161

6262
'uniform mat4 uProjectionMatrix;',
6363
'uniform mat4 uViewMatrix;',
64+
'uniform vec2 uResolution;',
6465

6566
'attribute vec2 inPosition;',
6667

6768
'varying vec2 fragCoord;',
6869

6970
'void main () {',
7071
'gl_Position = uProjectionMatrix * uViewMatrix * vec4(inPosition, 1.0, 1.0);',
71-
'fragCoord = vec2(inPosition.x, uViewMatrix[2][0] - inPosition.y);',
72+
'fragCoord = vec2(inPosition.x, uResolution.y - inPosition.y);',
7273
'}'
7374
].join('\n');
7475
}

src/gameobjects/shader/Shader.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,16 @@ var Shader = new Class({
350350

351351
var program = renderer.createProgram(this.shader.vertexSrc, this.shader.fragmentSrc);
352352

353+
// The default uniforms available within the vertex shader
353354
renderer.setMatrix4(program, 'uViewMatrix', false, this.viewMatrix);
354355
renderer.setMatrix4(program, 'uProjectionMatrix', false, this.projectionMatrix);
356+
renderer.setFloat2(program, 'uResolution', this.width, this.height);
355357

356358
this.program = program;
357359

358360
var d = new Date();
359361

362+
// The default uniforms available within the fragment shader
360363
var defaultUniforms = {
361364
resolution: { type: '2f', value: { x: this.width, y: this.height }},
362365
time: { type: '1f', value: 0 },
@@ -831,9 +834,12 @@ var Shader = new Class({
831834
vm[12] = vm[0] * x + vm[4] * y;
832835
vm[13] = vm[1] * x + vm[5] * y;
833836

837+
// Update vertex shader uniforms
838+
834839
this.renderer.setMatrix4(program, 'uViewMatrix', false, this.viewMatrix);
840+
this.renderer.setFloat2(program, 'uResolution', this.width, this.height);
835841

836-
// Update common uniforms
842+
// Update fragment shader uniforms
837843

838844
var uniforms = this.uniforms;
839845
var res = uniforms.resolution;

0 commit comments

Comments
 (0)