|
| 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; |
0 commit comments