Skip to content

Commit 7c4c439

Browse files
committed
Diffuse lighting shaders. Also made light layer rendering pass through
1 parent d4f0d02 commit 7c4c439

7 files changed

Lines changed: 140 additions & 33 deletions

File tree

src/gameobjects/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ var GameObjects = {
7373
if (WEBGL_RENDERER)
7474
{
7575
// WebGL only Game Objects
76-
//GameObjects.LightLayer = require('./lightlayer/LightLayer');
76+
GameObjects.LightLayer = require('./lightlayer/LightLayer');
7777
GameObjects.Mesh = require('./mesh/Mesh');
7878
GameObjects.Quad = require('./quad/Quad');
7979

80-
//GameObjects.Factories.LightLayer = require('./lightlayer/LightLayerFactory');
80+
GameObjects.Factories.LightLayer = require('./lightlayer/LightLayerFactory');
8181
GameObjects.Factories.Mesh = require('./mesh/MeshFactory');
8282
GameObjects.Factories.Quad = require('./quad/QuadFactory');
8383

84-
//GameObjects.Creators.LightLayer = require('./lightlayer/LightLayerCreator');
84+
GameObjects.Creators.LightLayer = require('./lightlayer/LightLayerCreator');
8585
GameObjects.Creators.Mesh = require('./mesh/MeshCreator');
8686
GameObjects.Creators.Quad = require('./quad/QuadCreator');
8787
}

src/gameobjects/lightlayer/LightLayer.js

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,9 @@ var Class = require('../../utils/Class');
33
var Components = require('../components');
44
var Const = require('./Const');
55
var GameObject = require('../GameObject');
6-
var GBufferShader = require('../../renderer/webgl/shaders/GBufferShader');
76
var Light = require('./Light');
8-
var LightFragmentShader = require('../../renderer/webgl/shaders/LightFragmentShader');
9-
var Phong2DShaderDeferred = require('../../renderer/webgl/shaders/Phong2DShaderDeferred');
107
var Render = require('./LightLayerRender');
118
var SpriteNormalPair = require('./SpriteNormalPair');
12-
var TexturedAndNormalizedTintedShader = require('../../renderer/webgl/shaders/TexturedAndNormalizedTintedShader');
13-
var VertexBuffer = require('../../renderer/webgl/resources/VertexBuffer');
14-
var WebGLSupportedExtensions = require('../../renderer/webgl/WebGLSupportedExtensions');
159

1610
// http://cpetry.github.io/NormalMap-Online/
1711

@@ -36,6 +30,7 @@ var LightLayer = new Class({
3630

3731
GameObject.call(this, scene, 'LightLayer');
3832

33+
this.renderer = scene.sys.game.renderer;
3934
this.passShader = null;
4035
this.gl = null;
4136
this.ambientLightColorR = 0.0;
@@ -48,24 +43,24 @@ var LightLayer = new Class({
4843
this._z = 0;
4944
this.setOrigin(0, 0);
5045

51-
scene.sys.game.renderer.onContextRestored(function (renderer) {
52-
_this.onContextRestored(renderer);
53-
});
46+
this.renderer.onContextRestored(function (renderer) {
47+
this.onContextRestored(renderer);
48+
}, this);
5449

55-
this.init(scene.sys.game.renderer, WebGLSupportedExtensions.has('WEBGL_draw_buffers'));
50+
this.init(scene.sys.game.renderer, this.renderer.hasExtension('WEBGL_draw_buffers'));
5651
},
5752

5853
onContextRestored: function (renderer)
5954
{
6055
/* It won't allow the use of drawBuffers on restored context */
6156
this.init(renderer, false);
62-
this.renderWebGL = require('./ForwardRenderer');
57+
//this.renderWebGL = require('./ForwardRenderer');
6358
this.lights.length = Math.min(this.lights.length, Const.MAX_LIGHTS);
6459
},
6560

6661
init: function (renderer, deferred)
6762
{
68-
var resourceManager = renderer.resourceManager;
63+
/*var resourceManager = renderer.resourceManager;
6964
7065
this._isDeferred = deferred;
7166
this.renderer = renderer;
@@ -151,7 +146,7 @@ var LightLayer = new Class({
151146
};
152147
}
153148
154-
/* Setup render targets */
149+
// Setup render targets
155150
this.gBufferFbo = gl.createFramebuffer();
156151
this.gBufferColorTex = gl.createTexture();
157152
this.gBufferNormalTex = gl.createTexture();
@@ -191,7 +186,7 @@ var LightLayer = new Class({
191186
192187
gl.bindFramebuffer(gl.FRAMEBUFFER, null);
193188
VertexBuffer.SetDirty();
194-
}
189+
}*/
195190
},
196191

197192
forEachLight: function (callback)
@@ -308,7 +303,7 @@ var LightLayer = new Class({
308303

309304
updateLights: function (renderer, camera, shader)
310305
{
311-
if (this.gl !== null)
306+
/*if (this.gl !== null)
312307
{
313308
var locations = this.lightsLocations;
314309
var lights = this.lights;
@@ -333,7 +328,7 @@ var LightLayer = new Class({
333328
gl.uniform3f(locations[index].position, point.x - (camera.scrollX * light.scrollFactorX * camera.zoom), height - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom), light.z);
334329
gl.uniform3f(locations[index].color, light.r, light.g, light.b);
335330
}
336-
}
331+
}*/
337332
}
338333

339334
});
Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1 @@
1-
var WebGLSupportedExtensions = require('../../renderer/webgl/WebGLSupportedExtensions');
2-
3-
module.exports = (function () {
4-
if (WebGLSupportedExtensions.has('WEBGL_draw_buffers'))
5-
{
6-
return require('./DeferredRenderer');
7-
}
8-
else
9-
{
10-
return require('./ForwardRenderer');
11-
}
12-
})();
13-
14-
1+
module.exports = function () {};
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#define SHADER_NAME PHASER_DEFERRED_DIFFUSE_FS
2+
3+
precision mediump float;
4+
5+
struct Light
6+
{
7+
vec3 position;
8+
vec3 color;
9+
float attenuation;
10+
float radius;
11+
};
12+
13+
const int kMaxLights = 50;
14+
15+
uniform vec4 uCamera; /* x, y, rotation, zoom */
16+
uniform vec2 uResolution;
17+
uniform sampler2D uMainSampler; // gbuffer color
18+
uniform sampler2D uNormSampler; // gbuffer normal
19+
uniform vec3 uAmbientLightColor;
20+
uniform Light uLights[kMaxLights];
21+
22+
void main()
23+
{
24+
vec2 uv = vec2(gl_FragCoord.xy / uResolution);
25+
vec3 finalColor = vec3(0.0, 0.0, 0.0);
26+
vec4 gbColor = texture2D(uMainSampler, uv);
27+
vec3 gbNormal = texture2D(uNormSampler, uv).rgb;
28+
vec3 normal = normalize(vec3(gbNormal * 2.0 - 1.0));
29+
vec2 res = vec2(min(uResolution.x, uResolution.y)) * uCamera.w;
30+
31+
for (int index = 0; index < kMaxLights; ++index)
32+
{
33+
Light light = uLights[index];
34+
vec3 lightDir = vec3((light.position.xy / res) - (gl_FragCoord.xy / res), light.position.z);
35+
vec3 lightNormal = normalize(lightDir);
36+
float distToSurf = length(lightDir) * uCamera.w;
37+
float diffuseFactor = max(dot(normal, lightNormal), 0.0);
38+
float radius = (light.radius / res.x * uCamera.w) * uCamera.w;
39+
float attenuation = clamp(1.0 - distToSurf * distToSurf / (radius * radius), 0.0, 1.0);
40+
vec3 diffuse = light.color * gbColor.rgb * diffuseFactor;
41+
finalColor += attenuation * diffuse;
42+
}
43+
44+
vec4 colorOutput = vec4(uAmbientLightColor + finalColor, gbColor.a);
45+
gl_FragColor = colorOutput;
46+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#define SHADER_NAME PHASER_DEFERRED_DIFFUSE_VS
2+
3+
precision mediump float;
4+
5+
attribute vec2 inPosition;
6+
7+
void main()
8+
{
9+
gl_Position = vec4(inPosition, 0.0, 1.0);
10+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#define SHADER_NAME PHASER_FORWARD_DIFFUSE_FS
2+
3+
precision mediump float;
4+
5+
struct Light
6+
{
7+
vec3 position;
8+
vec3 color;
9+
float attenuation;
10+
float radius;
11+
};
12+
13+
const int kMaxLights = 10;
14+
15+
uniform vec4 uCamera; /* x, y, rotation, zoom */
16+
uniform vec2 uResolution;
17+
uniform sampler2D uMainSampler;
18+
uniform sampler2D uNormSampler;
19+
uniform vec3 uAmbientLightColor;
20+
uniform Light uLights[kMaxLights];
21+
22+
varying vec2 outTexCoord;
23+
varying vec4 outTint;
24+
25+
void main()
26+
{
27+
vec3 finalColor = vec3(0.0, 0.0, 0.0);
28+
vec4 color = texture2D(uMainTexture, outTexCoord) * vec4(outTint.rgb * outTint.a, outTint.a);
29+
vec3 normalMap = texture2D(uNormTexture, outTexCoord).rgb;
30+
vec3 normal = normalize(vec3(normalMap * 2.0 - 1.0));
31+
vec2 res = vec2(min(uResolution.x, uResolution.y)) * uCamera.w;
32+
33+
for (int index = 0; index < kMaxLights; ++index)
34+
{
35+
Light light = uLights[index];
36+
vec3 lightDir = vec3((light.position.xy / res) - (gl_FragCoord.xy / res), light.position.z);
37+
vec3 lightNormal = normalize(lightDir);
38+
float distToSurf = length(lightDir) * uCamera.w;
39+
float diffuseFactor = max(dot(normal, lightNormal), 0.0);
40+
float radius = (light.radius / res.x * uCamera.w) * uCamera.w;
41+
float attenuation = clamp(1.0 - distToSurf * distToSurf / (radius * radius), 0.0, 1.0);
42+
vec3 diffuse = light.color * color.rgb * diffuseFactor;
43+
finalColor += attenuation * diffuse;
44+
}
45+
46+
vec4 colorOutput = vec4(uAmbientLightColor + finalColor, color.a);
47+
gl_FragColor = vec4(colorOutput.rgb * colorOutput.a, colorOutput.a);
48+
49+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#extension GL_EXT_draw_buffers : require
2+
3+
#define SHADER_NAME PHASER_GBUFFER_FS
4+
5+
precision mediump float;
6+
7+
uniform sampler2D uMainSampler;
8+
uniform sampler2D uNormSampler;
9+
10+
varying vec2 outTexCoord;
11+
varying vec4 outTint;
12+
13+
void main()
14+
{
15+
vec4 color = texture2D(uMainSampler, outTexCoord) * vec4(outTint.rgb * outTint.a, outTint.a);
16+
vec3 normal = texture2D(uNormSampler, outTexCoord).rgb;
17+
18+
gl_FragData[0] = color;
19+
gl_FragData[1] = vec4(normal, color.a);
20+
}

0 commit comments

Comments
 (0)