Skip to content

Commit 71e1206

Browse files
committed
Light Layer working on screen space
1 parent 2a3f966 commit 71e1206

6 files changed

Lines changed: 41 additions & 34 deletions

File tree

v3/src/checksum.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
var CHECKSUM = {
2-
build: '02d42da0-7886-11e7-9365-efeeaffc968c'
2+
build: 'c132b6c0-78a6-11e7-b6cf-f39dd7bd053c'
33
};
44
module.exports = CHECKSUM;

v3/src/gameobjects/lightlayer/ForwardRenderer.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,22 @@
11
var ForwardRenderer = function (renderer, lightLayer, interpolationPercentage, camera)
22
{
3-
if (this.renderMask !== this.renderFlags)
3+
var spriteList = lightLayer.sprites;
4+
var length = spriteList.length;
5+
var batch = renderer.spriteBatch;
6+
7+
if (this.renderMask !== this.renderFlags || length === 0)
48
{
59
return;
610
}
711

8-
var spriteList = lightLayer.sprites;
9-
var length = spriteList.length;
10-
var batch = renderer.spriteBatch;
12+
if (renderer.currentRenderer !== null)
13+
{
14+
renderer.currentRenderer.flush();
15+
}
1116

1217
batch.bind(lightLayer.passShader);
1318
batch.indexBufferObject.bind();
14-
lightLayer.updateLights();
19+
lightLayer.updateLights(renderer, camera);
1520

1621
for (var index = 0; index < length; ++index)
1722
{

v3/src/gameobjects/lightlayer/LightLayer.js

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,16 @@ var LightLayer = new Class({
1616
Mixins: [
1717
Components.Alpha,
1818
Components.BlendMode,
19-
Components.Flip,
20-
Components.GetBounds,
2119
Components.Origin,
2220
Components.RenderTarget,
23-
Components.ScaleMode,
2421
Components.ScrollFactor,
25-
Components.Size,
26-
Components.Transform,
2722
Components.Visible,
2823
Render
2924
],
3025

3126
initialize:
3227

33-
function LightLayer (scene, x, y, width, height)
28+
function LightLayer (scene)
3429
{
3530
GameObject.call(this, scene, 'LightLayer');
3631

@@ -62,6 +57,7 @@ var LightLayer = new Class({
6257
float attenuation;
6358
};
6459
60+
uniform vec2 uResolution;
6561
uniform sampler2D uMainTexture;
6662
uniform sampler2D uNormTexture;
6763
uniform vec3 uAmbientLightColor;
@@ -73,19 +69,31 @@ var LightLayer = new Class({
7369
7470
void main()
7571
{
76-
/* Just Pass through for now */
77-
vec4 finalColor = vec4(1.0, 0.0, 1.0, 1.0);
78-
vec4 normal = texture2D(uNormTexture, v_tex_coord);
79-
vec4 color = texture2D(uMainTexture, v_tex_coord);
80-
81-
finalColor = color * normal;
82-
83-
gl_FragColor = finalColor * vec4(v_color, v_alpha);
72+
vec3 finalColor = vec3(0.0, 0.0, 0.0);
73+
vec4 spriteColor = texture2D(uMainTexture, v_tex_coord) * vec4(v_color, v_alpha);
74+
vec3 spriteNormal = texture2D(uNormTexture, v_tex_coord).rgb;
75+
vec3 normal = normalize(vec3(spriteNormal * 2.0 - 1.0));
76+
77+
for (int index = 0; index < ` + Const.MAX_LIGHTS + `; ++index)
78+
{
79+
Light light = uLights[index];
80+
float lightY = uResolution.y - light.position.y;
81+
vec3 lightDir = vec3((vec2(light.position.x, lightY) / uResolution) - (gl_FragCoord.xy / uResolution), light.position.z);
82+
vec3 lightNormal = normalize(lightDir);
83+
float distToSurf = length(lightDir);
84+
float diffuseFactor = max(dot(normal, lightNormal), 0.0);
85+
float attenuation = 1.0 / (1.0 + light.attenuation * (distToSurf * distToSurf));
86+
vec3 diffuse = light.color * spriteColor.rgb * diffuseFactor;
87+
finalColor += attenuation * diffuse;
88+
}
89+
90+
gl_FragColor = vec4(uAmbientLightColor + finalColor, spriteColor.a);
8491
}
8592
`});
8693
this.ambientLightColorLoc = this.passShader.getUniformLocation('uAmbientLightColor');
8794
this.uMainTextureLoc = this.passShader.getUniformLocation('uMainTexture');
8895
this.uNormTextureLoc = this.passShader.getUniformLocation('uNormTexture');
96+
this.uResolutionLoc = this.passShader.getUniformLocation('uResolution');
8997

9098
this.passShader.setConstantInt1(this.uMainTextureLoc, 0);
9199
this.passShader.setConstantInt1(this.uNormTextureLoc, 1);
@@ -100,8 +108,6 @@ var LightLayer = new Class({
100108
}
101109
}
102110

103-
this.setPosition(x, y);
104-
this.setSize(width, height);
105111
this.setOrigin(0, 0);
106112
},
107113

@@ -176,7 +182,7 @@ var LightLayer = new Class({
176182
}
177183
},
178184

179-
updateLights: function()
185+
updateLights: function(renderer, camera)
180186
{
181187
if (this.gl !== null)
182188
{
@@ -186,14 +192,15 @@ var LightLayer = new Class({
186192
var gl = this.gl;
187193
var passShader = this.passShader;
188194

195+
passShader.setConstantFloat2(this.uResolutionLoc, renderer.width * camera.zoom, renderer.height * camera.zoom);
189196
passShader.setConstantFloat3(this.ambientLightColorLoc, this.ambientLightColorR, this.ambientLightColorG, this.ambientLightColorB);
190197

191198
for (var index = 0; index < length; ++index)
192199
{
193200
var light = lights[index];
194201
passShader.setConstantFloat3(locations[index].position, light.x, light.y, light.z);
195202
passShader.setConstantFloat3(locations[index].color, light.r, light.g, light.b);
196-
passShader.setConstantFloat3(locations[index].attenuation, light.attenuation);
203+
passShader.setConstantFloat1(locations[index].attenuation, light.attenuation);
197204
}
198205
}
199206
}

v3/src/gameobjects/lightlayer/LightLayerCreator.js

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@ var BuildGameObject = require('../BuildGameObject');
44

55
var LightLayerCreator = function (scene, config)
66
{
7-
var x = GetAdvancedValue(config, 'x', 0);
8-
var y = GetAdvancedValue(config, 'y', 0);
9-
var width = GetAdvancedValue(config, 'width', 512);
10-
var height = GetAdvancedValue(config, 'height', 512);
11-
12-
var pass = new LightLayer(scene, x, y, width, height);
7+
var pass = new LightLayer(scene);
138

149
BuildGameObject(scene, pass, config);
1510

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
var LightLayer = require('./LightLayer');
22

3-
var LightLayerFactory = function (scene, x, y, width, height)
3+
var LightLayerFactory = function (scene)
44
{
5-
return new LightLayer(scene, x, y, width, height);
5+
return new LightLayer(scene);
66
};
77

88
module.exports = LightLayerFactory;

v3/src/plugins/GameObjectFactory.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,9 @@ var GameObjectFactory = new Class({
130130
return this.displayList.add(ZoneFactory(this.scene, x, y, width, height));
131131
},
132132

133-
lightLayer: function (x, y, width, height)
133+
lightLayer: function ()
134134
{
135-
return this.displayList.add(LightLayerFactory(this.scene, x, y, width, height));
135+
return this.displayList.add(LightLayerFactory(this.scene));
136136
}
137137

138138
});

0 commit comments

Comments
 (0)