| 1 | class Scene |
| 2 | { |
| 3 | static idCount = 0; |
| 4 | id = null; |
| 5 | activeObjects = []; |
| 6 | position = null; |
| 7 | size = null; |
| 8 | real_position = null; |
| 9 | px = 0; py = 0; |
| 10 | real_size = null; |
| 11 | cameraPosition = null; |
| 12 | parentScene = null; |
| 13 | renderBackground = true; |
| 14 | quadTree = null; |
| 15 | |
| 16 | pendingRemoval = []; |
| 17 | _sortedActiveObjects = null; |
| 18 | _sortDirty = true; |
| 19 | /** Objects whose `layer` changed since the last draw-order rebuild. */ |
| 20 | _layerChanged = []; |
| 21 | /** |
| 22 | * If more than this many objects change layer between draws, fall back to a |
| 23 | * full in-place sort (cheaper than many splice reinserts on large lists). |
| 24 | */ |
| 25 | _layerChangeFullSortThreshold = 64; |
| 26 | |
| 27 | g_primary = false; |
| 28 | |
| 29 | static DisplayModes = { absolute: 0, relativeToParent: 1 }; |
| 30 | |
| 31 | displayMode = Scene.DisplayModes.absolute; |
| 32 | |
| 33 | cv_canvas = null; |
| 34 | glContext = null; |
| 35 | destroyed = false; |
| 36 | |
| 37 | // Legacy: posts applied to the "default" layer when that layer has none of its own. |
| 38 | postShaders = []; |
| 39 | postTime = 0; |
| 40 | |
| 41 | // Ordered list of RenderLayer. Index 0 is drawn first (back). |
| 42 | renderLayers = null; |
| 43 | |
| 44 | game = null; |
| 45 | |
| 46 | get primary () |
| 47 | { |
| 48 | return this.g_primary; |
| 49 | } |
| 50 | |
| 51 | constructor (x, y, width, height, displayMode = 0) |
| 52 | { |
| 53 | Scene.idCount++; |
| 54 | this.id = 'canvas_' + Scene.idCount; |
| 55 | |
| 56 | this.position = new vector(x, y); |
| 57 | this.size = new vector(width, height); |
| 58 | this.real_position = new vector(x, y); |
| 59 | this.real_size = new vector(width, height); |
| 60 | this.cameraPosition = new vector(0, 0); |
| 61 | this.displayMode = displayMode; |
| 62 | |
| 63 | |
| 64 | const body = document.body; |
| 65 | |
| 66 | body.insertAdjacentHTML('beforeend', '<canvas id="' + this.id + '" style="position: absolute; left: ' + x + 'px; top: ' + y + 'px;" width="' + width + '" height="' + height + '"></canvas>'); |
| 67 | |
| 68 | this.cv_canvas = document.getElementById(this.id); |
| 69 | this.glContext = new GLContext(this.cv_canvas); |
| 70 | this.postShaders = []; |
| 71 | this.postTime = 0; |
| 72 | this.renderLayers = [new RenderLayer(RenderLayer.DEFAULT_NAME)]; |
| 73 | |
| 74 | this.g_fixSize(); |
| 75 | this.quadTree = new QuadTree(this.real_size.x, this.real_size.y); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Replace the layer stack. Order in the array is draw order (first = back). |
| 80 | * Each entry: RenderLayer, or { name, postShaders?, composite? }. |
| 81 | */ |
| 82 | setLayers (layerConfigs) |
| 83 | { |
| 84 | if (!layerConfigs || !layerConfigs.length) |
| 85 | { |
| 86 | this.renderLayers = [new RenderLayer(RenderLayer.DEFAULT_NAME)]; |
| 87 | return this.renderLayers; |
| 88 | } |
| 89 | |
| 90 | const layers = []; |
| 91 | for (let i = 0; i < layerConfigs.length; i++) |
| 92 | { |
| 93 | const cfg = layerConfigs[i]; |
| 94 | if (!cfg) |
| 95 | { continue; } |
| 96 | |
| 97 | if (cfg instanceof RenderLayer) |
| 98 | { layers.push(cfg); } |
| 99 | else |
| 100 | { layers.push(new RenderLayer(cfg.name, cfg)); } |
| 101 | } |
| 102 | |
| 103 | if (!layers.length) |
| 104 | { layers.push(new RenderLayer(RenderLayer.DEFAULT_NAME)); } |
| 105 | |
| 106 | this.renderLayers = layers; |
| 107 | return this.renderLayers; |
| 108 | } |
| 109 | |
| 110 | getLayer (name) |
| 111 | { |
| 112 | const layers = this.renderLayers || []; |
| 113 | for (let i = 0; i < layers.length; i++) |
| 114 | { |
| 115 | if (layers[i].name === name) |
| 116 | { return layers[i]; } |
| 117 | } |
| 118 | |
| 119 | return null; |
| 120 | } |
| 121 | |
| 122 | setPrimary () |
| 123 | { |
| 124 | this.game.setPrimaryScene(this); |
| 125 | } |
| 126 | |
| 127 | onLoad () |
| 128 | { |
| 129 | |
| 130 | } |
| 131 | |
| 132 | loadChildScene (scene) |
| 133 | { |
| 134 | scene.parentScene = this; |
| 135 | this.game.loadScene(scene); |
| 136 | } |
| 137 | |
| 138 | destroy () |
| 139 | { |
| 140 | if (this.destroyed) |
| 141 | { return; } |
| 142 | |
| 143 | for (var i = 0; i < this.activeObjects.length; i++) |
| 144 | { |
| 145 | this.activeObjects[i].destroy(); |
| 146 | } |
| 147 | |
| 148 | this.destroyed = true; |
| 149 | |
| 150 | if (this.game) |
| 151 | { this.game.removeScene(this); } |
| 152 | |
| 153 | this.removeCanvas(); |
| 154 | |
| 155 | if (this.primary) |
| 156 | { |
| 157 | if (this.parentScene && !this.parentScene.destroyed) |
| 158 | { |
| 159 | this.parentScene.setPrimary(); |
| 160 | } |
| 161 | else if (this.game) |
| 162 | { |
| 163 | let promoted = null; |
| 164 | for (let i = 0; i < this.game.activeScenes.length; i++) |
| 165 | { |
| 166 | const candidate = this.game.activeScenes[i]; |
| 167 | if (candidate !== this && !candidate.destroyed) |
| 168 | { |
| 169 | promoted = candidate; |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (promoted) |
| 175 | { this.game.setPrimaryScene(promoted); } |
| 176 | else |
| 177 | { this.g_primary = false; } |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | this.g_primary = false; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | removeCanvas () |
| 187 | { |
| 188 | var elem = this.cv_canvas || document.getElementById(this.id); |
| 189 | if (elem && elem.parentNode) |
| 190 | { |
| 191 | elem.parentNode.removeChild(elem); |
| 192 | } |
| 193 | |
| 194 | this.cv_canvas = null; |
| 195 | this.glContext = null; |
| 196 | } |
| 197 | |
| 198 | backgroundColor = |
| 199 | { |
| 200 | r: 255, |
| 201 | g: 255, |
| 202 | b: 255 |
| 203 | }; |
| 204 | |
| 205 | get canvas () |
| 206 | { |
| 207 | return this.cv_canvas; |
| 208 | } |
| 209 | |
| 210 | get context () |
| 211 | { |
| 212 | return this.glContext ? this.glContext.gl : null; |
| 213 | } |
| 214 | |
| 215 | addObject (obj) |
| 216 | { |
| 217 | if (this.destroyed) |
| 218 | { return; } |
| 219 | |
| 220 | this.activeObjects.push(obj); |
| 221 | obj.scene = this; |
| 222 | obj.initQuadtree(this.quadTree); |
| 223 | obj.checkQuadtree(); |
| 224 | |
| 225 | if (this._sortedActiveObjects && !this._sortDirty && !this._layerChanged.length) |
| 226 | { this.#insertSortedByLayer(this._sortedActiveObjects, obj); } |
| 227 | else |
| 228 | { this._sortDirty = true; } |
| 229 | |
| 230 | obj.addedToScene && obj.addedToScene(); |
| 231 | |
| 232 | return obj; |
| 233 | } |
| 234 | |
| 235 | removeObject (obj) |
| 236 | { |
| 237 | if (this.activeObjects.indexOf(obj) >= 0) |
| 238 | { |
| 239 | if (!this.pendingRemoval.contains(obj)) |
| 240 | { this.pendingRemoval.push(obj); } |
| 241 | |
| 242 | this._sortDirty = true; |
| 243 | } |
| 244 | |
| 245 | if (this.quadTree === obj.quadTree) |
| 246 | { this.quadTree.remove(obj); } |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Called when an object's draw-priority `layer` changes. |
| 251 | * Batches small moves for splice-reinsert; floods into a full sort when many change. |
| 252 | */ |
| 253 | objectLayerChanged (obj) |
| 254 | { |
| 255 | if (!obj || this._sortDirty) |
| 256 | { |
| 257 | this._sortDirty = true; |
| 258 | return; |
| 259 | } |
| 260 | this._layerChanged.push(obj); |
| 261 | if (this._layerChanged.length > this._layerChangeFullSortThreshold) |
| 262 | { |
| 263 | this._sortDirty = true; |
| 264 | this._layerChanged.length = 0; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | /** Force a full draw-order rebuild on the next draw (e.g. bulk external edits). */ |
| 269 | markSortDirty () |
| 270 | { |
| 271 | this._sortDirty = true; |
| 272 | this._layerChanged.length = 0; |
| 273 | } |
| 274 | |
| 275 | #lowerBoundByLayer (sorted, layer) |
| 276 | { |
| 277 | let lo = 0; |
| 278 | let hi = sorted.length; |
| 279 | while (lo < hi) |
| 280 | { |
| 281 | const mid = (lo + hi) >> 1; |
| 282 | if (sorted[mid].layer < layer) |
| 283 | { lo = mid + 1; } |
| 284 | else |
| 285 | { hi = mid; } |
| 286 | } |
| 287 | return lo; |
| 288 | } |
| 289 | |
| 290 | #insertSortedByLayer (sorted, obj) |
| 291 | { |
| 292 | const at = this.#lowerBoundByLayer(sorted, obj.layer); |
| 293 | sorted.splice(at, 0, obj); |
| 294 | } |
| 295 | |
| 296 | #reinsertSortedByLayer (sorted, obj) |
| 297 | { |
| 298 | const from = sorted.indexOf(obj); |
| 299 | if (from >= 0) |
| 300 | { sorted.splice(from, 1); } |
| 301 | this.#insertSortedByLayer(sorted, obj); |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Ensure `_sortedActiveObjects` matches `activeObjects` ordered by `layer`. |
| 306 | * Avoids allocating a fresh array every frame (no `slice().sort()`). |
| 307 | */ |
| 308 | #ensureDrawOrder () |
| 309 | { |
| 310 | const active = this.activeObjects; |
| 311 | if (this._sortDirty || !this._sortedActiveObjects) |
| 312 | { |
| 313 | let sorted = this._sortedActiveObjects; |
| 314 | if (!sorted) |
| 315 | { |
| 316 | sorted = new Array(active.length); |
| 317 | this._sortedActiveObjects = sorted; |
| 318 | } |
| 319 | else if (sorted.length !== active.length) |
| 320 | { sorted.length = active.length; } |
| 321 | |
| 322 | for (let i = 0; i < active.length; i++) |
| 323 | { sorted[i] = active[i]; } |
| 324 | |
| 325 | sorted.sort((a, b) => a.layer - b.layer); |
| 326 | this._sortDirty = false; |
| 327 | this._layerChanged.length = 0; |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | if (!this._layerChanged.length) |
| 332 | { return; } |
| 333 | |
| 334 | const changed = this._layerChanged; |
| 335 | const sorted = this._sortedActiveObjects; |
| 336 | for (let i = 0; i < changed.length; i++) |
| 337 | { this.#reinsertSortedByLayer(sorted, changed[i]); } |
| 338 | changed.length = 0; |
| 339 | } |
| 340 | |
| 341 | update (deltaTime) |
| 342 | { |
| 343 | if (this.destroyed) |
| 344 | { return; } |
| 345 | |
| 346 | this.postTime += deltaTime; |
| 347 | |
| 348 | for (let i = 0; i < this.activeObjects.length; i++) |
| 349 | { |
| 350 | this.activeObjects[i].update(deltaTime); |
| 351 | } |
| 352 | |
| 353 | if (this.pendingRemoval.length) |
| 354 | { |
| 355 | for (let i = 0; i < this.pendingRemoval.length; i++) |
| 356 | { |
| 357 | const obj = this.pendingRemoval[i]; |
| 358 | const index = this.activeObjects.indexOf(obj); |
| 359 | if (index >= 0 && obj.destroyed) |
| 360 | { this.activeObjects.removeAt(index); } |
| 361 | } |
| 362 | |
| 363 | this.pendingRemoval.length = 0; |
| 364 | this._sortDirty = true; |
| 365 | this._layerChanged.length = 0; |
| 366 | } |
| 367 | |
| 368 | this.quadTree.update(deltaTime); |
| 369 | } |
| 370 | |
| 371 | g_fixSize () |
| 372 | { |
| 373 | if (this.displayMode == Scene.DisplayModes.relativeToParent) |
| 374 | { |
| 375 | if (this.parentScene) |
| 376 | { |
| 377 | this.real_position.x = this.parentScene.real_position.x + (this.position.x * this.parentScene.real_size.x); |
| 378 | this.real_position.y = this.parentScene.real_position.y + (this.position.y * this.parentScene.real_size.y); |
| 379 | |
| 380 | this.real_size.x = this.parentScene.real_size.x * this.size.x; |
| 381 | this.real_size.y = this.parentScene.real_size.y * this.size.y; |
| 382 | } |
| 383 | else |
| 384 | { |
| 385 | this.real_position.x = window.innerWidth * this.position.x; |
| 386 | this.real_position.y = window.innerHeight * this.position.y; |
| 387 | |
| 388 | this.real_size.x = window.innerWidth * this.size.x; |
| 389 | this.real_size.y = window.innerHeight * this.size.y; |
| 390 | } |
| 391 | } |
| 392 | else if (this.displayMode == Scene.DisplayModes.absolute) |
| 393 | { |
| 394 | this.real_position.x = this.position.x; |
| 395 | this.real_position.y = this.position.y; |
| 396 | |
| 397 | this.real_size.x = this.size.x; |
| 398 | this.real_size.y = this.size.y; |
| 399 | } |
| 400 | |
| 401 | const canvasW = Math.max(1, Math.round(this.real_size.x)); |
| 402 | const canvasH = Math.max(1, Math.round(this.real_size.y)); |
| 403 | let resized = false; |
| 404 | |
| 405 | if (this.canvas.width != canvasW) |
| 406 | { |
| 407 | this.canvas.width = canvasW; |
| 408 | resized = true; |
| 409 | } |
| 410 | |
| 411 | if (this.canvas.height != canvasH) |
| 412 | { |
| 413 | this.canvas.height = canvasH; |
| 414 | resized = true; |
| 415 | } |
| 416 | |
| 417 | // Changing canvas dimensions resets WebGL — rebuild helper/buffers/programs. |
| 418 | if (resized || !this.glContext) |
| 419 | { this.glContext = new GLContext(this.cv_canvas); } |
| 420 | else |
| 421 | { this.glContext.setSize(this.canvas.width, this.canvas.height); } |
| 422 | |
| 423 | if (this.px != this.real_position.x) |
| 424 | { |
| 425 | this.px = this.real_position.x; |
| 426 | this.canvas.style.left = this.real_position.x + "px"; |
| 427 | } |
| 428 | |
| 429 | if (this.py != this.real_position.y) |
| 430 | { |
| 431 | this.py = this.real_position.y; |
| 432 | this.canvas.style.top = this.real_position.y + "px"; |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | normalizePostShaderList (list) |
| 437 | { |
| 438 | if (!list || !list.length) |
| 439 | { return []; } |
| 440 | |
| 441 | const passes = []; |
| 442 | for (let i = 0; i < list.length; i++) |
| 443 | { |
| 444 | const entry = list[i]; |
| 445 | if (!entry) |
| 446 | { continue; } |
| 447 | |
| 448 | if (entry instanceof Shader || (entry.vertSource && entry.fragSource && entry.getProgramInfo)) |
| 449 | { |
| 450 | passes.push({ shader: entry, uniforms: null }); |
| 451 | } |
| 452 | else if (entry.shader) |
| 453 | { |
| 454 | const pass = { shader: entry.shader, uniforms: entry.uniforms || null }; |
| 455 | if (entry.scale != null) |
| 456 | { pass.scale = entry.scale; } |
| 457 | passes.push(pass); |
| 458 | } |
| 459 | } |
| 460 | |
| 461 | return passes; |
| 462 | } |
| 463 | |
| 464 | normalizePostShaders () |
| 465 | { |
| 466 | return this.normalizePostShaderList(this.postShaders); |
| 467 | } |
| 468 | |
| 469 | getPostPassesForLayer (layer) |
| 470 | { |
| 471 | const fromLayer = this.normalizePostShaderList(layer.postShaders); |
| 472 | if (fromLayer.length) |
| 473 | { return fromLayer; } |
| 474 | |
| 475 | // Back-compat: scene.postShaders apply to the default layer only. |
| 476 | if (layer.name === RenderLayer.DEFAULT_NAME) |
| 477 | { return this.normalizePostShaders(); } |
| 478 | |
| 479 | return []; |
| 480 | } |
| 481 | |
| 482 | draw () |
| 483 | { |
| 484 | if (this.destroyed || !this.glContext) |
| 485 | { return; } |
| 486 | |
| 487 | this.g_fixSize(); |
| 488 | |
| 489 | const gl = this.glContext; |
| 490 | const layers = this.renderLayers && this.renderLayers.length |
| 491 | ? this.renderLayers |
| 492 | : [new RenderLayer(RenderLayer.DEFAULT_NAME)]; |
| 493 | |
| 494 | // Canvas clear (background behind all layers). |
| 495 | gl.endOffscreenPass(); |
| 496 | if (this.renderBackground) |
| 497 | { |
| 498 | gl.clear( |
| 499 | this.backgroundColor.r, |
| 500 | this.backgroundColor.g, |
| 501 | this.backgroundColor.b, |
| 502 | 1 |
| 503 | ); |
| 504 | } |
| 505 | else |
| 506 | { |
| 507 | gl.clearTransparent(); |
| 508 | } |
| 509 | |
| 510 | this.#ensureDrawOrder(); |
| 511 | const sortedActiveObjects = this._sortedActiveObjects; |
| 512 | |
| 513 | // Bucket visible objects once so empty bloom layers can be skipped entirely. |
| 514 | const byLayer = Object.create(null); |
| 515 | for (let i = 0; i < sortedActiveObjects.length; i++) |
| 516 | { |
| 517 | const obj = sortedActiveObjects[i]; |
| 518 | if (obj.destroyed || !obj.renderer || !obj.renderer.visible) |
| 519 | { continue; } |
| 520 | |
| 521 | const name = obj.renderer.layerName; |
| 522 | let bucket = byLayer[name]; |
| 523 | if (!bucket) |
| 524 | { |
| 525 | bucket = []; |
| 526 | byLayer[name] = bucket; |
| 527 | } |
| 528 | |
| 529 | bucket.push(obj); |
| 530 | } |
| 531 | |
| 532 | const builtins = { |
| 533 | u_resolution: [gl._lastWidth, gl._lastHeight], |
| 534 | u_time: this.postTime |
| 535 | }; |
| 536 | |
| 537 | for (let li = 0; li < layers.length; li++) |
| 538 | { |
| 539 | const renderLayer = layers[li]; |
| 540 | const layerObjects = byLayer[renderLayer.name]; |
| 541 | if (!layerObjects || !layerObjects.length) |
| 542 | { continue; } |
| 543 | |
| 544 | gl.beginOffscreenPass(); |
| 545 | gl.clearTransparent(); |
| 546 | |
| 547 | for (let i = 0; i < layerObjects.length; i++) |
| 548 | { layerObjects[i].draw(this.context); } |
| 549 | |
| 550 | const postPasses = this.getPostPassesForLayer(renderLayer); |
| 551 | let texture = gl.colorTextures[0]; |
| 552 | |
| 553 | if (postPasses.length) |
| 554 | { |
| 555 | texture = gl.applyPostShaders(postPasses, builtins, { toCanvas: false }); |
| 556 | } |
| 557 | else |
| 558 | { |
| 559 | gl.endOffscreenPass(); |
| 560 | } |
| 561 | |
| 562 | gl.blitToCanvas(texture, renderLayer.composite || "alpha"); |
| 563 | } |
| 564 | } |
| 565 | } |