[ts] Refactored the way we render multiple demos. All rendering is coalesced into a single rendering loop. Eats less CPU

This commit is contained in:
badlogic 2016-09-03 01:13:02 +02:00
parent 8faba2cdec
commit 73b5617b81
16 changed files with 84 additions and 43 deletions

View File

@ -757,10 +757,10 @@ declare module spine {
static BLUE: Color; static BLUE: Color;
static MAGENTA: Color; static MAGENTA: Color;
constructor(r?: number, g?: number, b?: number, a?: number); constructor(r?: number, g?: number, b?: number, a?: number);
set(r: number, g: number, b: number, a: number): void; set(r: number, g: number, b: number, a: number): this;
setFromColor(c: Color): void; setFromColor(c: Color): this;
setFromString(hex: string): void; setFromString(hex: string): this;
add(r: number, g: number, b: number, a: number): void; add(r: number, g: number, b: number, a: number): this;
clamp(): this; clamp(): this;
} }
class MathUtils { class MathUtils {
@ -1319,6 +1319,7 @@ declare module spine.webgl {
boneOriginColor: Color; boneOriginColor: Color;
attachmentLineColor: Color; attachmentLineColor: Color;
triangleLineColor: Color; triangleLineColor: Color;
pathColor: Color;
aabbColor: Color; aabbColor: Color;
drawBones: boolean; drawBones: boolean;
drawRegionAttachments: boolean; drawRegionAttachments: boolean;

View File

@ -4118,12 +4118,14 @@ var spine;
this.b = b; this.b = b;
this.a = a; this.a = a;
this.clamp(); this.clamp();
return this;
}; };
Color.prototype.setFromColor = function (c) { Color.prototype.setFromColor = function (c) {
this.r = c.r; this.r = c.r;
this.g = c.g; this.g = c.g;
this.b = c.b; this.b = c.b;
this.a = c.a; this.a = c.a;
return this;
}; };
Color.prototype.setFromString = function (hex) { Color.prototype.setFromString = function (hex) {
hex = hex.charAt(0) == '#' ? hex.substr(1) : hex; hex = hex.charAt(0) == '#' ? hex.substr(1) : hex;
@ -4131,6 +4133,7 @@ var spine;
this.g = parseInt(hex.substr(2, 2), 16) / 255.0; this.g = parseInt(hex.substr(2, 2), 16) / 255.0;
this.b = parseInt(hex.substr(4, 2), 16) / 255.0; this.b = parseInt(hex.substr(4, 2), 16) / 255.0;
this.a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0; this.a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0;
return this;
}; };
Color.prototype.add = function (r, g, b, a) { Color.prototype.add = function (r, g, b, a) {
this.r += r; this.r += r;
@ -4138,6 +4141,7 @@ var spine;
this.b += b; this.b += b;
this.a += a; this.a += a;
this.clamp(); this.clamp();
return this;
}; };
Color.prototype.clamp = function () { Color.prototype.clamp = function () {
if (this.r < 0) if (this.r < 0)
@ -6745,6 +6749,7 @@ var spine;
this.boneOriginColor = new spine.Color(0, 1, 0, 1); this.boneOriginColor = new spine.Color(0, 1, 0, 1);
this.attachmentLineColor = new spine.Color(0, 0, 1, 0.5); this.attachmentLineColor = new spine.Color(0, 0, 1, 0.5);
this.triangleLineColor = new spine.Color(1, 0.64, 0, 0.5); this.triangleLineColor = new spine.Color(1, 0.64, 0, 0.5);
this.pathColor = new spine.Color().setFromString("FF7F00");
this.aabbColor = new spine.Color(0, 1, 0, 0.5); this.aabbColor = new spine.Color(0, 1, 0, 0.5);
this.drawBones = true; this.drawBones = true;
this.drawRegionAttachments = true; this.drawRegionAttachments = true;
@ -6855,7 +6860,7 @@ var spine;
var nn = path.worldVerticesLength; var nn = path.worldVerticesLength;
var world = this.temp = spine.Utils.setArraySize(this.temp, nn, 0); var world = this.temp = spine.Utils.setArraySize(this.temp, nn, 0);
path.computeWorldVertices(slot, world); path.computeWorldVertices(slot, world);
var color = path.color; var color = this.pathColor;
var x1 = world[2], y1 = world[3], x2 = 0, y2 = 0; var x1 = world[2], y1 = world[3], x2 = 0, y2 = 0;
if (path.closed) { if (path.closed) {
shapes.setColor(color); shapes.setColor(color);

File diff suppressed because one or more lines are too long

View File

@ -731,10 +731,10 @@ declare module spine {
static BLUE: Color; static BLUE: Color;
static MAGENTA: Color; static MAGENTA: Color;
constructor(r?: number, g?: number, b?: number, a?: number); constructor(r?: number, g?: number, b?: number, a?: number);
set(r: number, g: number, b: number, a: number): void; set(r: number, g: number, b: number, a: number): this;
setFromColor(c: Color): void; setFromColor(c: Color): this;
setFromString(hex: string): void; setFromString(hex: string): this;
add(r: number, g: number, b: number, a: number): void; add(r: number, g: number, b: number, a: number): this;
clamp(): this; clamp(): this;
} }
class MathUtils { class MathUtils {

View File

@ -3941,12 +3941,14 @@ var spine;
this.b = b; this.b = b;
this.a = a; this.a = a;
this.clamp(); this.clamp();
return this;
}; };
Color.prototype.setFromColor = function (c) { Color.prototype.setFromColor = function (c) {
this.r = c.r; this.r = c.r;
this.g = c.g; this.g = c.g;
this.b = c.b; this.b = c.b;
this.a = c.a; this.a = c.a;
return this;
}; };
Color.prototype.setFromString = function (hex) { Color.prototype.setFromString = function (hex) {
hex = hex.charAt(0) == '#' ? hex.substr(1) : hex; hex = hex.charAt(0) == '#' ? hex.substr(1) : hex;
@ -3954,6 +3956,7 @@ var spine;
this.g = parseInt(hex.substr(2, 2), 16) / 255.0; this.g = parseInt(hex.substr(2, 2), 16) / 255.0;
this.b = parseInt(hex.substr(4, 2), 16) / 255.0; this.b = parseInt(hex.substr(4, 2), 16) / 255.0;
this.a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0; this.a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0;
return this;
}; };
Color.prototype.add = function (r, g, b, a) { Color.prototype.add = function (r, g, b, a) {
this.r += r; this.r += r;
@ -3961,6 +3964,7 @@ var spine;
this.b += b; this.b += b;
this.a += a; this.a += a;
this.clamp(); this.clamp();
return this;
}; };
Color.prototype.clamp = function () { Color.prototype.clamp = function () {
if (this.r < 0) if (this.r < 0)

File diff suppressed because one or more lines are too long

View File

@ -731,10 +731,10 @@ declare module spine {
static BLUE: Color; static BLUE: Color;
static MAGENTA: Color; static MAGENTA: Color;
constructor(r?: number, g?: number, b?: number, a?: number); constructor(r?: number, g?: number, b?: number, a?: number);
set(r: number, g: number, b: number, a: number): void; set(r: number, g: number, b: number, a: number): this;
setFromColor(c: Color): void; setFromColor(c: Color): this;
setFromString(hex: string): void; setFromString(hex: string): this;
add(r: number, g: number, b: number, a: number): void; add(r: number, g: number, b: number, a: number): this;
clamp(): this; clamp(): this;
} }
class MathUtils { class MathUtils {

View File

@ -3941,12 +3941,14 @@ var spine;
this.b = b; this.b = b;
this.a = a; this.a = a;
this.clamp(); this.clamp();
return this;
}; };
Color.prototype.setFromColor = function (c) { Color.prototype.setFromColor = function (c) {
this.r = c.r; this.r = c.r;
this.g = c.g; this.g = c.g;
this.b = c.b; this.b = c.b;
this.a = c.a; this.a = c.a;
return this;
}; };
Color.prototype.setFromString = function (hex) { Color.prototype.setFromString = function (hex) {
hex = hex.charAt(0) == '#' ? hex.substr(1) : hex; hex = hex.charAt(0) == '#' ? hex.substr(1) : hex;
@ -3954,6 +3956,7 @@ var spine;
this.g = parseInt(hex.substr(2, 2), 16) / 255.0; this.g = parseInt(hex.substr(2, 2), 16) / 255.0;
this.b = parseInt(hex.substr(4, 2), 16) / 255.0; this.b = parseInt(hex.substr(4, 2), 16) / 255.0;
this.a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0; this.a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0;
return this;
}; };
Color.prototype.add = function (r, g, b, a) { Color.prototype.add = function (r, g, b, a) {
this.r += r; this.r += r;
@ -3961,6 +3964,7 @@ var spine;
this.b += b; this.b += b;
this.a += a; this.a += a;
this.clamp(); this.clamp();
return this;
}; };
Color.prototype.clamp = function () { Color.prototype.clamp = function () {
if (this.r < 0) if (this.r < 0)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -731,10 +731,10 @@ declare module spine {
static BLUE: Color; static BLUE: Color;
static MAGENTA: Color; static MAGENTA: Color;
constructor(r?: number, g?: number, b?: number, a?: number); constructor(r?: number, g?: number, b?: number, a?: number);
set(r: number, g: number, b: number, a: number): void; set(r: number, g: number, b: number, a: number): this;
setFromColor(c: Color): void; setFromColor(c: Color): this;
setFromString(hex: string): void; setFromString(hex: string): this;
add(r: number, g: number, b: number, a: number): void; add(r: number, g: number, b: number, a: number): this;
clamp(): this; clamp(): this;
} }
class MathUtils { class MathUtils {
@ -1249,6 +1249,7 @@ declare module spine.webgl {
boneOriginColor: Color; boneOriginColor: Color;
attachmentLineColor: Color; attachmentLineColor: Color;
triangleLineColor: Color; triangleLineColor: Color;
pathColor: Color;
aabbColor: Color; aabbColor: Color;
drawBones: boolean; drawBones: boolean;
drawRegionAttachments: boolean; drawRegionAttachments: boolean;

View File

@ -3941,12 +3941,14 @@ var spine;
this.b = b; this.b = b;
this.a = a; this.a = a;
this.clamp(); this.clamp();
return this;
}; };
Color.prototype.setFromColor = function (c) { Color.prototype.setFromColor = function (c) {
this.r = c.r; this.r = c.r;
this.g = c.g; this.g = c.g;
this.b = c.b; this.b = c.b;
this.a = c.a; this.a = c.a;
return this;
}; };
Color.prototype.setFromString = function (hex) { Color.prototype.setFromString = function (hex) {
hex = hex.charAt(0) == '#' ? hex.substr(1) : hex; hex = hex.charAt(0) == '#' ? hex.substr(1) : hex;
@ -3954,6 +3956,7 @@ var spine;
this.g = parseInt(hex.substr(2, 2), 16) / 255.0; this.g = parseInt(hex.substr(2, 2), 16) / 255.0;
this.b = parseInt(hex.substr(4, 2), 16) / 255.0; this.b = parseInt(hex.substr(4, 2), 16) / 255.0;
this.a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0; this.a = (hex.length != 8 ? 255 : parseInt(hex.substr(6, 2), 16)) / 255.0;
return this;
}; };
Color.prototype.add = function (r, g, b, a) { Color.prototype.add = function (r, g, b, a) {
this.r += r; this.r += r;
@ -3961,6 +3964,7 @@ var spine;
this.b += b; this.b += b;
this.a += a; this.a += a;
this.clamp(); this.clamp();
return this;
}; };
Color.prototype.clamp = function () { Color.prototype.clamp = function () {
if (this.r < 0) if (this.r < 0)
@ -6322,6 +6326,7 @@ var spine;
this.boneOriginColor = new spine.Color(0, 1, 0, 1); this.boneOriginColor = new spine.Color(0, 1, 0, 1);
this.attachmentLineColor = new spine.Color(0, 0, 1, 0.5); this.attachmentLineColor = new spine.Color(0, 0, 1, 0.5);
this.triangleLineColor = new spine.Color(1, 0.64, 0, 0.5); this.triangleLineColor = new spine.Color(1, 0.64, 0, 0.5);
this.pathColor = new spine.Color().setFromString("FF7F00");
this.aabbColor = new spine.Color(0, 1, 0, 0.5); this.aabbColor = new spine.Color(0, 1, 0, 0.5);
this.drawBones = true; this.drawBones = true;
this.drawRegionAttachments = true; this.drawRegionAttachments = true;
@ -6432,7 +6437,7 @@ var spine;
var nn = path.worldVerticesLength; var nn = path.worldVerticesLength;
var world = this.temp = spine.Utils.setArraySize(this.temp, nn, 0); var world = this.temp = spine.Utils.setArraySize(this.temp, nn, 0);
path.computeWorldVertices(slot, world); path.computeWorldVertices(slot, world);
var color = path.color; var color = this.pathColor;
var x1 = world[2], y1 = world[3], x2 = 0, y2 = 0; var x1 = world[2], y1 = world[3], x2 = 0, y2 = 0;
if (path.closed) { if (path.closed) {
shapes.setColor(color); shapes.setColor(color);

File diff suppressed because one or more lines are too long

View File

@ -247,7 +247,7 @@ module spine {
update () { update () {
var now = Date.now() / 1000; var now = Date.now() / 1000;
this.delta = now -this.lastTime; this.delta = now - this.lastTime;
this.frameTime += this.delta; this.frameTime += this.delta;
this.totalTime += this.delta; this.totalTime += this.delta;
if (this.delta > this.maxDelta) this.delta = this.maxDelta; if (this.delta > this.maxDelta) this.delta = this.maxDelta;

View File

@ -5,29 +5,41 @@ var spineDemos;
spineDemos.NON_HOVER_COLOR_INNER = new spine.Color(0.478, 0, 0, 0.5); spineDemos.NON_HOVER_COLOR_INNER = new spine.Color(0.478, 0, 0, 0.5);
spineDemos.NON_HOVER_COLOR_OUTER = new spine.Color(1, 0, 0, 0.8); spineDemos.NON_HOVER_COLOR_OUTER = new spine.Color(1, 0, 0, 0.8);
spineDemos.assetManager = new spine.SharedAssetManager("http://esotericsoftware.com/demos/exports/"); spineDemos.assetManager = new spine.SharedAssetManager("http://esotericsoftware.com/demos/exports/");
spineDemos.setupRendering = function (canvas, renderFunc) { spineDemos.demos = [];
var isVisible = false; spineDemos.loopRunning = false;
function render () { var timeKeeper = new spine.TimeKeeper();
renderFunc(); var loop = function() {
if (isVisible) requestAnimationFrame(render); timeKeeper.update();
}; if (spineDemos.log) console.log(timeKeeper.delta + ", " + timeKeeper.framesPerSecond);
spineDemos.requestAnimationFrame(loop);
function viewportCheck () { var demos = spineDemos.demos;
var old = isVisible for (var i = 0; i < demos.length; i++) {
isVisible = spineDemos.isElementInViewport(canvas); var canvas = demos[i].canvas;
if (isVisible && old != isVisible) requestAnimationFrame(render); var renderFunc = demos[i].renderFunc;
if (spineDemos.isElementInViewport(canvas)) {
if (spineDemos.log) console.log("Rendering " + canvas.id);
renderFunc();
};
} }
}
window.addEventListener("DOMContentLoaded", viewportCheck, false); var setupLoop = function() {
window.addEventListener("load", viewportCheck, false); if (!spineDemos.loopRunning) {
window.addEventListener("resize", viewportCheck, false); loop();
window.addEventListener("scroll", viewportCheck, false); spineDemos.loopRunning = true;
}
}
viewportCheck(); spineDemos.setupRendering = function (canvas, renderFunc) {
requestAnimationFrame(render); setupLoop();
spineDemos.demos.push({canvas: canvas, renderFunc: renderFunc});
}; };
spineDemos.requestAnimationFrame = function(func) {
requestAnimationFrame(func);
}
spineDemos.isElementInViewport = function (canvas) { spineDemos.isElementInViewport = function (canvas) {
var rect = canvas.getBoundingClientRect(); var rect = canvas.getBoundingClientRect();
var x = 0, y = 0; var x = 0, y = 0;
@ -35,4 +47,13 @@ var spineDemos;
var height = (window.innerWidth || document.documentElement.clientWidth); var height = (window.innerWidth || document.documentElement.clientWidth);
return rect.left < x + width && rect.right > x && rect.top < y + height && rect.bottom > y; return rect.left < x + width && rect.right > x && rect.top < y + height && rect.bottom > y;
}; };
spineDemos.setupWebGLContext = function (canvas) {
config = {
alpha: false,
depth: false,
stencil: false
}
return gl = canvas.getContext("webgl", config) || canvas.getContext("experimental-webgl", config);
}
})(spineDemos || (spineDemos = { })); })(spineDemos || (spineDemos = { }));