Skip to content

Commit f73d66a

Browse files
committed
Merge branch 'master' of https://github.com/photonstorm/phaser
2 parents a802914 + f294a17 commit f73d66a

14 files changed

Lines changed: 349 additions & 23 deletions

src/gameobjects/LightsManager.js

Lines changed: 240 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,240 @@
1+
var Class = require('../utils/Class');
2+
var Utils = require('../renderer/webgl/Utils');
3+
var LightPipeline = require('../renderer/webgl/pipelines/ForwardDiffuseLightPipeline');
4+
5+
var Light = new Class({
6+
7+
initialize:
8+
9+
function Light (x, y, radius, r, g, b, intensity)
10+
{
11+
this.x = x;
12+
this.y = y;
13+
this.radius = radius;
14+
this.r = r;
15+
this.g = g;
16+
this.b = b;
17+
this.intensity = intensity;
18+
this.scrollFactorX = 1.0;
19+
this.scrollFactorY = 1.0;
20+
},
21+
22+
set: function (x, y, radius, r, g, b, intensity)
23+
{
24+
this.x = x;
25+
this.y = y;
26+
this.radius = radius;
27+
this.r = r;
28+
this.g = g;
29+
this.b = b;
30+
this.intensity = intensity;
31+
this.scrollFactorX = 1.0;
32+
this.scrollFactorY = 1.0;
33+
},
34+
35+
setScrollFactor: function (x, y)
36+
{
37+
this.scrollFactorX = x;
38+
this.scrollFactorY = (y === undefined) ? x : y;
39+
return this;
40+
},
41+
42+
setColor: function (rgb)
43+
{
44+
var color = Utils.getFloatsFromUintRGB(rgb);
45+
46+
this.r = color[0];
47+
this.g = color[1];
48+
this.b = color[2];
49+
50+
return this;
51+
},
52+
53+
setIntensity: function (intensity)
54+
{
55+
this.intensity = intensity;
56+
57+
return this;
58+
},
59+
60+
setPosition: function (x, y)
61+
{
62+
this.x = x;
63+
this.y = y;
64+
65+
return this;
66+
},
67+
68+
setRadius: function (radius)
69+
{
70+
this.radius = radius;
71+
72+
return this;
73+
}
74+
75+
});
76+
77+
var LightsManager = new Class({
78+
79+
initialize:
80+
81+
function LightsManager()
82+
{
83+
this.lightPool = [];
84+
this.lights = [];
85+
this.culledLights = [];
86+
this.ambientColor = { r: 0.1, g: 0.1, b: 0.1 };
87+
this.active = false;
88+
},
89+
90+
enable: function ()
91+
{
92+
this.active = true;
93+
94+
return this;
95+
},
96+
97+
disable: function ()
98+
{
99+
this.active = false;
100+
101+
return this;
102+
},
103+
104+
shutdown: function ()
105+
{
106+
while (this.lights.length > 0)
107+
{
108+
this.lightPool.push(this.lights.pop());
109+
}
110+
111+
this.ambientColor = { r: 0.1, g: 0.1, b: 0.1 };
112+
this.culledLights.length = 0;
113+
this.lights.length = 0;
114+
115+
return this;
116+
},
117+
118+
destroy: function ()
119+
{
120+
this.shutdown();
121+
},
122+
123+
cull: function (camera)
124+
{
125+
var lights = this.lights;
126+
var culledLights = this.culledLights;
127+
var length = lights.length;
128+
var cameraCenterX = camera.x + camera.width / 2.0;
129+
var cameraCenterY = camera.y + camera.height / 2.0;
130+
var cameraRadius = (camera.width + camera.height) / 2.0;
131+
var point = { x: 0, y: 0 };
132+
var cameraMatrix = camera.matrix;
133+
var viewportHeight = this.systems.game.config.height;
134+
135+
culledLights.length = 0;
136+
137+
138+
for (var index = 0; index < length && culledLights.length < LightPipeline.LIGHT_COUNT; ++index)
139+
{
140+
var light = lights[index];
141+
142+
cameraMatrix.transformPoint(light.x, light.y, point);
143+
144+
// We'll just use bounding spheres to test
145+
// if lights should be rendered
146+
var dx = cameraCenterX - (point.x - (camera.scrollX * light.scrollFactorX * camera.zoom));
147+
var dy = cameraCenterY - (viewportHeight - (point.y - (camera.scrollY * light.scrollFactorY) * camera.zoom));
148+
var distance = Math.sqrt(dx * dx + dy * dy);
149+
150+
if (distance < light.radius + cameraRadius)
151+
{
152+
culledLights.push(lights[index]);
153+
}
154+
}
155+
156+
return culledLights;
157+
},
158+
159+
forEachLight: function (callback)
160+
{
161+
if (!callback)
162+
{
163+
return;
164+
}
165+
166+
var lights = this.lights;
167+
var length = lights.length;
168+
169+
for (var index = 0; index < length; ++index)
170+
{
171+
callback(lights[index]);
172+
}
173+
174+
return this;
175+
},
176+
177+
setAmbientColor: function (rgb)
178+
{
179+
var color = Utils.getFloatsFromUintRGB(rgb);
180+
181+
this.ambientColor.r = color[0];
182+
this.ambientColor.g = color[1];
183+
this.ambientColor.b = color[2];
184+
185+
return this;
186+
},
187+
188+
getMaxVisibleLights: function ()
189+
{
190+
return 10;
191+
},
192+
193+
getLightCount: function ()
194+
{
195+
return this.lights.length;
196+
},
197+
198+
addLight: function (x, y, radius, rgb, intensity)
199+
{
200+
var color = null;
201+
var light = null;
202+
203+
x = (x === undefined) ? 0.0 : x;
204+
y = (y === undefined) ? 0.0 : y;
205+
rgb = (rgb === undefined) ? 0xffffff : rgb;
206+
radius = (radius === undefined) ? 100.0 : radius;
207+
intensity = (intensity === undefined) ? 1.0 : intensity;
208+
209+
color = Utils.getFloatsFromUintRGB(rgb);
210+
light = null;
211+
212+
if (this.lightPool.length > 0)
213+
{
214+
light = this.lightPool.pop();
215+
light.set(x, y, radius, color[0], color[1], color[2], intensity);
216+
}
217+
else
218+
{
219+
light = new Light(x, y, radius, color[0], color[1], color[2], intensity);
220+
}
221+
222+
this.lights.push(light);
223+
224+
return light;
225+
},
226+
227+
removeLight: function (light)
228+
{
229+
var index = this.lights.indexOf(light);
230+
231+
if (index >= 0)
232+
{
233+
this.lightPool.push(light);
234+
this.lights.splice(index, 1);
235+
}
236+
}
237+
238+
});
239+
240+
module.exports = LightsManager;

src/gameobjects/LightsPlugin.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
var Class = require('../utils/Class');
2+
var PluginManager = require('../plugins/PluginManager');
3+
var LightsManager = require('./LightsManager');
4+
5+
var LightsPlugin = new Class({
6+
7+
Extends: LightsManager,
8+
9+
initialize:
10+
11+
function LightsPlugin (scene)
12+
{
13+
this.scene = scene;
14+
15+
this.systems = scene.sys;
16+
17+
if (!scene.sys.settings.isBooted)
18+
{
19+
scene.sys.events.once('boot', this.boot, this);
20+
}
21+
22+
LightsManager.call(this);
23+
},
24+
25+
boot: function ()
26+
{
27+
var eventEmitter = this.systems.events;
28+
29+
eventEmitter.on('shutdown', this.shutdown, this);
30+
eventEmitter.on('destroy', this.destroy, this);
31+
},
32+
33+
destroy: function ()
34+
{
35+
this.shutdown();
36+
37+
this.scene = undefined;
38+
}
39+
40+
});
41+
42+
PluginManager.register('LightsPlugin', LightsPlugin, 'lights');
43+
44+
module.exports = LightsPlugin;

src/gameobjects/components/Pipeline.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ var Pipeline = {
3636
{
3737
this.pipeline = this.defaultPipeline;
3838
return (this.pipeline !== null);
39+
},
40+
41+
getPipelineName: function ()
42+
{
43+
return this.pipeline.name;
3944
}
4045

4146
};

src/gameobjects/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
var GameObjects = {
44

5+
LightsManager: require('./LightsManager'),
6+
LightsPlugin: require('./LightsPlugin'),
57
DisplayList: require('./DisplayList'),
68
DisplayListPlugin: require('./DisplayListPlugin'),
79
UpdateList: require('./UpdateList'),

src/plugins/DefaultScenePlugins.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ var DefaultScenePlugins = [
1212
'DataManagerPlugin',
1313
'InputPlugin',
1414
'Loader',
15-
'TweenManager'
15+
'TweenManager',
16+
'LightsPlugin'
1617

1718
];
1819

src/renderer/webgl/Utils.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,15 @@ module.exports = {
2626
return ((ua << 24) | (ub << 16) | (ug << 8) | ur) >>> 0;
2727
},
2828

29+
getFloatsFromUintRGB: function (rgb)
30+
{
31+
var ur = ((rgb >> 16)|0) & 0xff;
32+
var ug = ((rgb >> 8)|0) & 0xff;
33+
var ub = (rgb|0) & 0xff;
34+
35+
return [ ur / 255.0, ug / 255.0, ub / 255.0 ];
36+
},
37+
2938
getComponentCount: function (attributes)
3039
{
3140
var count = 0;

src/renderer/webgl/WebGLPipeline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ var WebGLPipeline = new Class({
77

88
function WebGLPipeline(config)
99
{
10-
this.name = config.name;
10+
this.name = 'WebGLPipeline';
1111
this.game = config.game;
1212
this.view = config.game.canvas;
1313
this.resolution = config.game.config.resolution;

src/renderer/webgl/WebGLRenderer.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ var WebGLRenderer = new Class({
231231
if (!this.hasPipeline(pipelineName)) this.pipelines[pipelineName] = pipelineInstance;
232232
else console.warn('Pipeline', pipelineName, ' already exists.');
233233

234+
pipelineInstance.name = pipelineName;
234235
this.pipelines[pipelineName].resize(this.width, this.height, this.game.config.resolution);
235236

236237
return this;

src/renderer/webgl/pipelines/BitmapMaskPipeline.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ var BitmapMaskPipeline = new Class({
1313
function BitmapMaskPipeline(game, gl, renderer)
1414
{
1515
WebGLPipeline.call(this, {
16-
name: 'BitmapMaskPipeline',
1716
game: game,
1817
gl: gl,
1918
renderer: renderer,

src/renderer/webgl/pipelines/FlatTintPipeline.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ var FlatTintPipeline = new Class({
4141
function FlatTintPipeline(game, gl, renderer)
4242
{
4343
WebGLPipeline.call(this, {
44-
name: 'FlatTintPipeline',
4544
game: game,
4645
gl: gl,
4746
renderer: renderer,

0 commit comments

Comments
 (0)