mirror of
https://github.com/liabru/matter-js.git
synced 2024-11-30 10:20:52 -05:00
improve Render.debug
This commit is contained in:
parent
f2ef3aadd8
commit
1753bf0904
1 changed files with 239 additions and 58 deletions
|
@ -32,6 +32,9 @@ var Mouse = require('../core/Mouse');
|
||||||
|| window.webkitCancelAnimationFrame || window.msCancelAnimationFrame;
|
|| window.webkitCancelAnimationFrame || window.msCancelAnimationFrame;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Render._goodFps = 30;
|
||||||
|
Render._goodDelta = 1000 / 60;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new renderer. The options parameter is an object that specifies any properties you wish to override the defaults.
|
* Creates a new renderer. The options parameter is an object that specifies any properties you wish to override the defaults.
|
||||||
* All properties have default values, and many are pre-calculated automatically based on other properties.
|
* All properties have default values, and many are pre-calculated automatically based on other properties.
|
||||||
|
@ -48,6 +51,18 @@ var Mouse = require('../core/Mouse');
|
||||||
canvas: null,
|
canvas: null,
|
||||||
mouse: null,
|
mouse: null,
|
||||||
frameRequestId: null,
|
frameRequestId: null,
|
||||||
|
timing: {
|
||||||
|
historySize: 60,
|
||||||
|
delta: 0,
|
||||||
|
deltaHistory: [],
|
||||||
|
lastTime: 0,
|
||||||
|
lastTimestamp: 0,
|
||||||
|
timestampElapsed: 0,
|
||||||
|
timestampElapsedHistory: [],
|
||||||
|
engineDeltaHistory: [],
|
||||||
|
engineElapsedHistory: [],
|
||||||
|
elapsedHistory: []
|
||||||
|
},
|
||||||
options: {
|
options: {
|
||||||
width: 800,
|
width: 800,
|
||||||
height: 600,
|
height: 600,
|
||||||
|
@ -121,7 +136,12 @@ var Mouse = require('../core/Mouse');
|
||||||
Render.run = function(render) {
|
Render.run = function(render) {
|
||||||
(function loop(time){
|
(function loop(time){
|
||||||
render.frameRequestId = _requestAnimationFrame(loop);
|
render.frameRequestId = _requestAnimationFrame(loop);
|
||||||
Render.world(render);
|
|
||||||
|
Render.world(render, time);
|
||||||
|
_updateTiming(render, time);
|
||||||
|
|
||||||
|
if (render.options.showDebug)
|
||||||
|
Render.debug(render, render.context, time);
|
||||||
})();
|
})();
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -289,13 +309,16 @@ var Mouse = require('../core/Mouse');
|
||||||
* @method world
|
* @method world
|
||||||
* @param {render} render
|
* @param {render} render
|
||||||
*/
|
*/
|
||||||
Render.world = function(render) {
|
Render.world = function(render, time) {
|
||||||
var engine = render.engine,
|
var startTime = Common.now(),
|
||||||
|
engine = render.engine,
|
||||||
world = engine.world,
|
world = engine.world,
|
||||||
canvas = render.canvas,
|
canvas = render.canvas,
|
||||||
context = render.context,
|
context = render.context,
|
||||||
options = render.options,
|
options = render.options,
|
||||||
allBodies = Composite.allBodies(world),
|
timing = render.timing;
|
||||||
|
|
||||||
|
var allBodies = Composite.allBodies(world),
|
||||||
allConstraints = Composite.allConstraints(world),
|
allConstraints = Composite.allConstraints(world),
|
||||||
background = options.wireframes ? options.wireframeBackground : options.background,
|
background = options.wireframes ? options.wireframeBackground : options.background,
|
||||||
bodies = [],
|
bodies = [],
|
||||||
|
@ -409,81 +432,190 @@ var Mouse = require('../core/Mouse');
|
||||||
if (options.showBroadphase && engine.broadphase.controller === Grid)
|
if (options.showBroadphase && engine.broadphase.controller === Grid)
|
||||||
Render.grid(render, engine.broadphase, context);
|
Render.grid(render, engine.broadphase, context);
|
||||||
|
|
||||||
if (options.showDebug)
|
|
||||||
Render.debug(render, context);
|
|
||||||
|
|
||||||
if (options.hasBounds) {
|
if (options.hasBounds) {
|
||||||
// revert view transforms
|
// revert view transforms
|
||||||
Render.endViewTransform(render);
|
Render.endViewTransform(render);
|
||||||
}
|
}
|
||||||
|
|
||||||
Events.trigger(render, 'afterRender', event);
|
Events.trigger(render, 'afterRender', event);
|
||||||
|
|
||||||
|
// log the time elapsed computing this update
|
||||||
|
timing.lastElapsed = Common.now() - startTime;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description
|
* Renders debug information.
|
||||||
* @private
|
* @private
|
||||||
* @method debug
|
* @method debug
|
||||||
* @param {render} render
|
* @param {render} render
|
||||||
* @param {RenderingContext} context
|
* @param {RenderingContext} context
|
||||||
|
* @param {Number} time
|
||||||
*/
|
*/
|
||||||
Render.debug = function(render, context) {
|
Render.debug = function(render, context, time) {
|
||||||
var c = context,
|
var engine = render.engine,
|
||||||
engine = render.engine,
|
|
||||||
world = engine.world,
|
world = engine.world,
|
||||||
metrics = engine.metrics,
|
|
||||||
options = render.options,
|
|
||||||
bodies = Composite.allBodies(world),
|
bodies = Composite.allBodies(world),
|
||||||
space = " ";
|
parts = 0,
|
||||||
|
width = 55,
|
||||||
|
height = 44,
|
||||||
|
x = 0,
|
||||||
|
y = 0;
|
||||||
|
|
||||||
if (engine.timing.timestamp - (render.debugTimestamp || 0) >= 500) {
|
// count parts
|
||||||
var text = "";
|
for (var i = 0; i < bodies.length; i += 1) {
|
||||||
|
parts += bodies[i].parts.length;
|
||||||
if (metrics.timing) {
|
|
||||||
text += "fps: " + Math.round(metrics.timing.fps) + space;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// @if DEBUG
|
// sections
|
||||||
if (metrics.extended) {
|
var sections = {
|
||||||
if (metrics.timing) {
|
'Part': parts,
|
||||||
text += "delta: " + metrics.timing.delta.toFixed(3) + space;
|
'Body': bodies.length,
|
||||||
text += "correction: " + metrics.timing.correction.toFixed(3) + space;
|
'Cons': Composite.allConstraints(world).length,
|
||||||
|
'Comp': Composite.allComposites(world).length,
|
||||||
|
'Pair': engine.pairs.list.length
|
||||||
|
};
|
||||||
|
|
||||||
|
// background
|
||||||
|
context.fillStyle = '#0e0f19';
|
||||||
|
context.fillRect(x, y, width * 5.5, height);
|
||||||
|
|
||||||
|
context.font = '12px Arial';
|
||||||
|
context.textBaseline = 'top';
|
||||||
|
context.textAlign = 'right';
|
||||||
|
|
||||||
|
// sections
|
||||||
|
for (var key in sections) {
|
||||||
|
var section = sections[key];
|
||||||
|
// label
|
||||||
|
context.fillStyle = '#aaa';
|
||||||
|
context.fillText(key, x + width, y + 8);
|
||||||
|
|
||||||
|
// value
|
||||||
|
context.fillStyle = '#eee';
|
||||||
|
context.fillText(section, x + width, y + 26);
|
||||||
|
|
||||||
|
x += width;
|
||||||
}
|
}
|
||||||
|
|
||||||
text += "bodies: " + bodies.length + space;
|
Render.timing(render, context, time);
|
||||||
|
};
|
||||||
|
|
||||||
if (engine.broadphase.controller === Grid)
|
/**
|
||||||
text += "buckets: " + metrics.buckets + space;
|
* Renders timing information.
|
||||||
|
* @private
|
||||||
|
* @method timing
|
||||||
|
* @param {render} render
|
||||||
|
* @param {RenderingContext} context
|
||||||
|
*/
|
||||||
|
Render.timing = function(render, context) {
|
||||||
|
var engine = render.engine,
|
||||||
|
timing = render.timing,
|
||||||
|
deltaHistory = timing.deltaHistory,
|
||||||
|
elapsedHistory = timing.elapsedHistory,
|
||||||
|
timestampElapsedHistory = timing.timestampElapsedHistory,
|
||||||
|
engineDeltaHistory = timing.engineDeltaHistory,
|
||||||
|
engineElapsedHistory = timing.engineElapsedHistory,
|
||||||
|
lastEngineDelta = engine.timing.lastDelta;
|
||||||
|
|
||||||
text += "\n";
|
var deltaMean = _mean(deltaHistory),
|
||||||
|
elapsedMean = _mean(elapsedHistory),
|
||||||
|
engineDeltaMean = _mean(engineDeltaHistory),
|
||||||
|
engineElapsedMean = _mean(engineElapsedHistory),
|
||||||
|
timestampElapsedMean = _mean(timestampElapsedHistory),
|
||||||
|
rateMean = (timestampElapsedMean / deltaMean) || 0,
|
||||||
|
fps = (1000 / deltaMean) || 0;
|
||||||
|
|
||||||
text += "collisions: " + metrics.collisions + space;
|
var graphHeight = 4,
|
||||||
text += "pairs: " + engine.pairs.list.length + space;
|
gap = 12,
|
||||||
text += "broad: " + metrics.broadEff + space;
|
width = 60,
|
||||||
text += "mid: " + metrics.midEff + space;
|
height = 34,
|
||||||
text += "narrow: " + metrics.narrowEff + space;
|
x = 10,
|
||||||
|
y = 69;
|
||||||
|
|
||||||
|
// background
|
||||||
|
context.fillStyle = '#0e0f19';
|
||||||
|
context.fillRect(0, 50, gap * 4 + width * 5 + 22, height);
|
||||||
|
|
||||||
|
// show FPS
|
||||||
|
Render.status(
|
||||||
|
context, x, y, width, graphHeight, deltaHistory.length,
|
||||||
|
Math.round(fps) + ' fps',
|
||||||
|
fps / Render._goodFps,
|
||||||
|
function(i) { return (deltaHistory[i] / deltaMean) - 1; }
|
||||||
|
);
|
||||||
|
|
||||||
|
// show engine delta
|
||||||
|
Render.status(
|
||||||
|
context, x + gap + width, y, width, graphHeight, engineDeltaHistory.length,
|
||||||
|
lastEngineDelta.toFixed(2) + ' dt',
|
||||||
|
Render._goodDelta / lastEngineDelta,
|
||||||
|
function(i) { return (engineDeltaHistory[i] / engineDeltaMean) - 1; }
|
||||||
|
);
|
||||||
|
|
||||||
|
// show engine update time
|
||||||
|
Render.status(
|
||||||
|
context, x + (gap + width) * 2, y, width, graphHeight, engineElapsedHistory.length,
|
||||||
|
engineElapsedMean.toFixed(2) + ' ut',
|
||||||
|
1 - (engineElapsedMean / Render._goodFps),
|
||||||
|
function(i) { return (engineElapsedHistory[i] / engineElapsedMean) - 1; }
|
||||||
|
);
|
||||||
|
|
||||||
|
// show render time
|
||||||
|
Render.status(
|
||||||
|
context, x + (gap + width) * 3, y, width, graphHeight, elapsedHistory.length,
|
||||||
|
elapsedMean.toFixed(2) + ' rt',
|
||||||
|
1 - (elapsedMean / Render._goodFps),
|
||||||
|
function(i) { return (elapsedHistory[i] / elapsedMean) - 1; }
|
||||||
|
);
|
||||||
|
|
||||||
|
// show effective speed
|
||||||
|
Render.status(
|
||||||
|
context, x + (gap + width) * 4, y, width, graphHeight, timestampElapsedHistory.length,
|
||||||
|
rateMean.toFixed(2) + ' x',
|
||||||
|
rateMean * rateMean * rateMean,
|
||||||
|
function(i) { return (((timestampElapsedHistory[i] / deltaHistory[i]) / rateMean) || 0) - 1; }
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Renders a label, indicator and a chart.
|
||||||
|
* @private
|
||||||
|
* @method status
|
||||||
|
* @param {RenderingContext} context
|
||||||
|
* @param {number} x
|
||||||
|
* @param {number} y
|
||||||
|
* @param {number} width
|
||||||
|
* @param {number} height
|
||||||
|
* @param {number} count
|
||||||
|
* @param {string} label
|
||||||
|
* @param {string} indicator
|
||||||
|
* @param {function} plotY
|
||||||
|
*/
|
||||||
|
Render.status = function(context, x, y, width, height, count, label, indicator, plotY) {
|
||||||
|
// background
|
||||||
|
context.strokeStyle = '#888';
|
||||||
|
context.fillStyle = '#444';
|
||||||
|
context.lineWidth = 1;
|
||||||
|
context.fillRect(x, y + 7, width, 1);
|
||||||
|
|
||||||
|
// chart
|
||||||
|
context.beginPath();
|
||||||
|
context.moveTo(x, y + 7 - height * Common.clamp(0.4 * plotY(0), -2, 2));
|
||||||
|
for (var i = 0; i < width; i += 1) {
|
||||||
|
context.lineTo(x + i, y + 7 - (i < count ? height * Common.clamp(0.4 * plotY(i), -2, 2) : 0));
|
||||||
}
|
}
|
||||||
// @endif
|
context.stroke();
|
||||||
|
|
||||||
render.debugString = text;
|
// indicator
|
||||||
render.debugTimestamp = engine.timing.timestamp;
|
context.fillStyle = 'hsl(' + Common.clamp(25 + 95 * indicator, 0, 120) + ',100%,60%)';
|
||||||
}
|
context.fillRect(x, y - 7, 4, 4);
|
||||||
|
|
||||||
if (render.debugString) {
|
// label
|
||||||
c.font = "12px Arial";
|
context.font = '12px Arial';
|
||||||
|
context.textBaseline = 'middle';
|
||||||
if (options.wireframes) {
|
context.textAlign = 'right';
|
||||||
c.fillStyle = 'rgba(255,255,255,0.5)';
|
context.fillStyle = '#eee';
|
||||||
} else {
|
context.fillText(label, x + width, y - 5);
|
||||||
c.fillStyle = 'rgba(0,0,0,0.5)';
|
|
||||||
}
|
|
||||||
|
|
||||||
var split = render.debugString.split('\n');
|
|
||||||
|
|
||||||
for (var i = 0; i < split.length; i++) {
|
|
||||||
c.fillText(split[i], 50, 50 + i * 18);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1322,7 +1454,56 @@ var Mouse = require('../core/Mouse');
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Description
|
* Updates render timing.
|
||||||
|
* @method _updateTiming
|
||||||
|
* @private
|
||||||
|
* @param {render} render
|
||||||
|
* @param {number} time
|
||||||
|
*/
|
||||||
|
var _updateTiming = function(render, time) {
|
||||||
|
var engine = render.engine,
|
||||||
|
timing = render.timing,
|
||||||
|
historySize = timing.historySize,
|
||||||
|
timestamp = engine.timing.timestamp;
|
||||||
|
|
||||||
|
timing.delta = time - timing.lastTime || Render._goodDelta;
|
||||||
|
timing.lastTime = time;
|
||||||
|
|
||||||
|
timing.timestampElapsed = timestamp - timing.lastTimestamp || 0;
|
||||||
|
timing.lastTimestamp = timestamp;
|
||||||
|
|
||||||
|
timing.deltaHistory.unshift(timing.delta);
|
||||||
|
timing.deltaHistory.length = Math.min(timing.deltaHistory.length, historySize);
|
||||||
|
|
||||||
|
timing.engineDeltaHistory.unshift(engine.timing.lastDelta);
|
||||||
|
timing.engineDeltaHistory.length = Math.min(timing.engineDeltaHistory.length, historySize);
|
||||||
|
|
||||||
|
timing.timestampElapsedHistory.unshift(timing.timestampElapsed);
|
||||||
|
timing.timestampElapsedHistory.length = Math.min(timing.timestampElapsedHistory.length, historySize);
|
||||||
|
|
||||||
|
timing.engineElapsedHistory.unshift(engine.timing.lastElapsed);
|
||||||
|
timing.engineElapsedHistory.length = Math.min(timing.engineElapsedHistory.length, historySize);
|
||||||
|
|
||||||
|
timing.elapsedHistory.unshift(timing.lastElapsed);
|
||||||
|
timing.elapsedHistory.length = Math.min(timing.elapsedHistory.length, historySize);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the mean value of the given numbers.
|
||||||
|
* @method _mean
|
||||||
|
* @private
|
||||||
|
* @param {Number[]} values
|
||||||
|
* @return {Number} the mean of given values
|
||||||
|
*/
|
||||||
|
var _mean = function(values) {
|
||||||
|
var result = 0;
|
||||||
|
for (var i = 0; i < values.length; i += 1) {
|
||||||
|
result += values[i];
|
||||||
|
}
|
||||||
|
return (result / values.length) || 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
* @method _createCanvas
|
* @method _createCanvas
|
||||||
* @private
|
* @private
|
||||||
* @param {} width
|
* @param {} width
|
||||||
|
|
Loading…
Reference in a new issue