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.

This commit is contained in:
Richard Davey 2019-05-16 17:44:59 +01:00
parent f792fce9cb
commit e23a86d45a
3 changed files with 12 additions and 4 deletions

View file

@ -5,7 +5,8 @@
### Updates
* `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)
* 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.
* 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.
* 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.
### Bug Fixes

View file

@ -61,6 +61,7 @@ var BaseShader = new Class({
'uniform mat4 uProjectionMatrix;',
'uniform mat4 uViewMatrix;',
'uniform vec2 uResolution;',
'attribute vec2 inPosition;',
@ -68,7 +69,7 @@ var BaseShader = new Class({
'void main () {',
'gl_Position = uProjectionMatrix * uViewMatrix * vec4(inPosition, 1.0, 1.0);',
'fragCoord = vec2(inPosition.x, uViewMatrix[2][0] - inPosition.y);',
'fragCoord = vec2(inPosition.x, uResolution.y - inPosition.y);',
'}'
].join('\n');
}

View file

@ -350,13 +350,16 @@ var Shader = new Class({
var program = renderer.createProgram(this.shader.vertexSrc, this.shader.fragmentSrc);
// The default uniforms available within the vertex shader
renderer.setMatrix4(program, 'uViewMatrix', false, this.viewMatrix);
renderer.setMatrix4(program, 'uProjectionMatrix', false, this.projectionMatrix);
renderer.setFloat2(program, 'uResolution', this.width, this.height);
this.program = program;
var d = new Date();
// The default uniforms available within the fragment shader
var defaultUniforms = {
resolution: { type: '2f', value: { x: this.width, y: this.height }},
time: { type: '1f', value: 0 },
@ -831,9 +834,12 @@ var Shader = new Class({
vm[12] = vm[0] * x + vm[4] * y;
vm[13] = vm[1] * x + vm[5] * y;
this.renderer.setMatrix4(program, 'uViewMatrix', false, this.viewMatrix);
// Update vertex shader uniforms
// Update common uniforms
this.renderer.setMatrix4(program, 'uViewMatrix', false, this.viewMatrix);
this.renderer.setFloat2(program, 'uResolution', this.width, this.height);
// Update fragment shader uniforms
var uniforms = this.uniforms;
var res = uniforms.resolution;