|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <script src="//cdn.jsdelivr.net/npm/phaser@4.0.0/dist/phaser.js"></script> |
| 8 | + <script src="../dist/iife/spine-phaser-v4.js"></script> |
| 9 | + <link rel="stylesheet" href="../../index.css" /> |
| 10 | + <title>Spine Phaser Batching Context Test</title> |
| 11 | + </head> |
| 12 | + |
| 13 | + <body class="p-4 flex flex-col items-center"> |
| 14 | + <h1>Batching context test</h1> |
| 15 | + </body> |
| 16 | + <script> |
| 17 | + class BatchingContextTest extends Phaser.Scene { |
| 18 | + preload() { |
| 19 | + this.load.spineBinary("spineboy-data", "/assets/spineboy-pro.skel"); |
| 20 | + this.load.spineAtlas("spineboy-atlas", "/assets/spineboy-pma.atlas"); |
| 21 | + } |
| 22 | + |
| 23 | + create() { |
| 24 | + const camera = this.cameras.main; |
| 25 | + camera.setForceComposite(true); // Forces rendering through a framebuffer. |
| 26 | + camera.filters.internal.addVignette(0.5, 0.5, 0.8, 0.25); |
| 27 | + camera.filters.external.addPixelate(1); |
| 28 | + camera.flash(300, 255, 255, 255, false); |
| 29 | + camera.shake(500, 0.002); |
| 30 | + |
| 31 | + this.expectedSpineDrawCalls = 5; |
| 32 | + |
| 33 | + this.debugText = document.createElement("pre"); |
| 34 | + this.debugText.style.position = "absolute"; |
| 35 | + this.debugText.style.left = "8px"; |
| 36 | + this.debugText.style.top = "8px"; |
| 37 | + this.debugText.style.margin = "0"; |
| 38 | + this.debugText.style.padding = "4px 6px"; |
| 39 | + this.debugText.style.color = "#ffffff"; |
| 40 | + this.debugText.style.background = "rgba(0, 0, 0, 0.75)"; |
| 41 | + this.debugText.style.font = "12px monospace"; |
| 42 | + this.debugText.style.pointerEvents = "none"; |
| 43 | + document.body.appendChild(this.debugText); |
| 44 | + this.events.once(Phaser.Scenes.Events.SHUTDOWN, () => this.debugText.remove()); |
| 45 | + |
| 46 | + const makeSpineboy = (x, y, scale = 0.25) => { |
| 47 | + const spineboy = this.add.spine( |
| 48 | + x, |
| 49 | + y, |
| 50 | + "spineboy-data", |
| 51 | + "spineboy-atlas" |
| 52 | + ); |
| 53 | + spineboy.scale = scale; |
| 54 | + spineboy.animationState.setAnimation(0, "walk", true); |
| 55 | + return spineboy; |
| 56 | + }; |
| 57 | + |
| 58 | + // Top-level adjacent Spine objects, with skipped objects between them. |
| 59 | + // The invisible objects should not break the Spine batch. |
| 60 | + for (let i = 0; i < 5; i++) { |
| 61 | + makeSpineboy(100 + i * 120, 520, 0.25); |
| 62 | + this.add.rectangle(100 + i * 120 + 45, 520, 20, 20, 0xff00ff).setVisible(false); |
| 63 | + } |
| 64 | + |
| 65 | + // A visible Phaser object intentionally breaks the Spine batch. |
| 66 | + this.add.rectangle(395, 520, 30, 220, 0x00ffff, 0.35); |
| 67 | + |
| 68 | + // Container children: skipped children inside containers/layers should not break batching. |
| 69 | + const container = this.add.container(120, 250); |
| 70 | + for (let i = 0; i < 4; i++) { |
| 71 | + const spineboy = makeSpineboy(i * 120, 0, 0.22); |
| 72 | + const hiddenBlock = this.add.rectangle(i * 120 + 45, 0, 24, 24, 0xff0000).setVisible(false); |
| 73 | + container.add([spineboy, hiddenBlock]); |
| 74 | + } |
| 75 | + container.setRotation(0.12); |
| 76 | + |
| 77 | + // Filtered Spine objects render through object framebuffers. |
| 78 | + // These exercise the filtered drawing context and matrix path. |
| 79 | + for (let i = 0; i < 3; i++) { |
| 80 | + const filtered = makeSpineboy(220 + i * 140, 120, 0.2).enableFilters(); |
| 81 | + filtered.filters.internal.addGlow(0xffff00, 2, 0, 1); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + update(time) { |
| 86 | + if (this.debugText) { |
| 87 | + this.debugText.textContent = |
| 88 | + (() => { |
| 89 | + const drawCalls = spine.PolygonBatcher.getAndResetGlobalDrawCalls(); |
| 90 | + const expectedDrawCalls = this.expectedSpineDrawCalls; |
| 91 | + const status = drawCalls <= expectedDrawCalls ? "OK" : "TOO MANY"; |
| 92 | + this.debugText.style.color = drawCalls <= expectedDrawCalls ? "#80ff80" : "#ff8080"; |
| 93 | + return "Spine draw calls: " + drawCalls + " / expected <= " + expectedDrawCalls + " (" + status + ")" + |
| 94 | + "\nCamera forceComposite + internal/external filters" + |
| 95 | + "\nContainer + invisible skipped children"; |
| 96 | + })(); |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + new Phaser.Game({ |
| 102 | + type: Phaser.AUTO, |
| 103 | + width: 800, |
| 104 | + height: 600, |
| 105 | + type: Phaser.WEBGL, |
| 106 | + scene: [BatchingContextTest], |
| 107 | + plugins: { |
| 108 | + scene: [ |
| 109 | + { |
| 110 | + key: "spine.SpinePlugin", |
| 111 | + plugin: spine.SpinePlugin, |
| 112 | + mapping: "spine", |
| 113 | + }, |
| 114 | + ], |
| 115 | + }, |
| 116 | + }); |
| 117 | + </script> |
| 118 | +</html> |
0 commit comments