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