babylon.debugLayer.js 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var DebugLayer = (function () {
  4. function DebugLayer(scene) {
  5. var _this = this;
  6. this._transformationMatrix = BABYLON.Matrix.Identity();
  7. this._enabled = false;
  8. this._labelsEnabled = false;
  9. this._displayStatistics = true;
  10. this._displayTree = false;
  11. this._displayLogs = false;
  12. this._skeletonViewers = new Array();
  13. this._identityMatrix = BABYLON.Matrix.Identity();
  14. this.axisRatio = 0.02;
  15. this.accentColor = "orange";
  16. this._scene = scene;
  17. this._syncPositions = function () {
  18. var engine = _this._scene.getEngine();
  19. var canvasRect = engine.getRenderingCanvasClientRect();
  20. if (_this._showUI) {
  21. _this._statsDiv.style.left = (canvasRect.width - 410) + "px";
  22. _this._statsDiv.style.top = (canvasRect.height - 290) + "px";
  23. _this._statsDiv.style.width = "400px";
  24. _this._statsDiv.style.height = "auto";
  25. _this._statsSubsetDiv.style.maxHeight = "240px";
  26. _this._optionsDiv.style.left = "0px";
  27. _this._optionsDiv.style.top = "10px";
  28. _this._optionsDiv.style.width = "200px";
  29. _this._optionsDiv.style.height = "auto";
  30. _this._optionsSubsetDiv.style.maxHeight = (canvasRect.height - 225) + "px";
  31. _this._logDiv.style.left = "0px";
  32. _this._logDiv.style.top = (canvasRect.height - 170) + "px";
  33. _this._logDiv.style.width = "600px";
  34. _this._logDiv.style.height = "160px";
  35. _this._treeDiv.style.left = (canvasRect.width - 310) + "px";
  36. _this._treeDiv.style.top = "10px";
  37. _this._treeDiv.style.width = "300px";
  38. _this._treeDiv.style.height = "auto";
  39. _this._treeSubsetDiv.style.maxHeight = (canvasRect.height - 340) + "px";
  40. }
  41. _this._globalDiv.style.left = canvasRect.left + "px";
  42. _this._globalDiv.style.top = canvasRect.top + "px";
  43. _this._drawingCanvas.style.left = "0px";
  44. _this._drawingCanvas.style.top = "0px";
  45. _this._drawingCanvas.style.width = engine.getRenderWidth() + "px";
  46. _this._drawingCanvas.style.height = engine.getRenderHeight() + "px";
  47. var devicePixelRatio = window.devicePixelRatio || 1;
  48. var context = _this._drawingContext;
  49. var backingStoreRatio = context.webkitBackingStorePixelRatio ||
  50. context.mozBackingStorePixelRatio ||
  51. context.msBackingStorePixelRatio ||
  52. context.oBackingStorePixelRatio ||
  53. context.backingStorePixelRatio || 1;
  54. _this._ratio = devicePixelRatio / backingStoreRatio;
  55. _this._drawingCanvas.width = engine.getRenderWidth() * _this._ratio;
  56. _this._drawingCanvas.height = engine.getRenderHeight() * _this._ratio;
  57. };
  58. this._onCanvasClick = function (evt) {
  59. _this._clickPosition = {
  60. x: evt.clientX * _this._ratio,
  61. y: evt.clientY * _this._ratio
  62. };
  63. };
  64. this._syncUI = function () {
  65. if (_this._showUI) {
  66. if (_this._displayStatistics) {
  67. _this._displayStats();
  68. _this._statsDiv.style.display = "";
  69. }
  70. else {
  71. _this._statsDiv.style.display = "none";
  72. }
  73. if (_this._displayLogs) {
  74. _this._logDiv.style.display = "";
  75. }
  76. else {
  77. _this._logDiv.style.display = "none";
  78. }
  79. if (_this._displayTree) {
  80. _this._treeDiv.style.display = "";
  81. if (_this._needToRefreshMeshesTree) {
  82. _this._needToRefreshMeshesTree = false;
  83. _this._refreshMeshesTreeContent();
  84. }
  85. }
  86. else {
  87. _this._treeDiv.style.display = "none";
  88. }
  89. }
  90. };
  91. this._syncData = function () {
  92. if (_this._labelsEnabled || !_this._showUI) {
  93. _this._camera.getViewMatrix().multiplyToRef(_this._camera.getProjectionMatrix(), _this._transformationMatrix);
  94. _this._drawingContext.clearRect(0, 0, _this._drawingCanvas.width, _this._drawingCanvas.height);
  95. var engine = _this._scene.getEngine();
  96. var viewport = _this._camera.viewport;
  97. var globalViewport = viewport.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  98. // Meshes
  99. var meshes = _this._camera.getActiveMeshes();
  100. var index;
  101. var projectedPosition;
  102. for (index = 0; index < meshes.length; index++) {
  103. var mesh = meshes.data[index];
  104. var position = mesh.getBoundingInfo().boundingSphere.center;
  105. projectedPosition = BABYLON.Vector3.Project(position, mesh.getWorldMatrix(), _this._transformationMatrix, globalViewport);
  106. if (mesh.renderOverlay || _this.shouldDisplayAxis && _this.shouldDisplayAxis(mesh)) {
  107. _this._renderAxis(projectedPosition, mesh, globalViewport);
  108. }
  109. if (!_this.shouldDisplayLabel || _this.shouldDisplayLabel(mesh)) {
  110. _this._renderLabel(mesh.name, projectedPosition, 12, function () { mesh.renderOverlay = !mesh.renderOverlay; }, function () { return mesh.renderOverlay ? 'red' : 'black'; });
  111. }
  112. }
  113. // Cameras
  114. var cameras = _this._scene.cameras;
  115. for (index = 0; index < cameras.length; index++) {
  116. var camera = cameras[index];
  117. if (camera === _this._camera) {
  118. continue;
  119. }
  120. projectedPosition = BABYLON.Vector3.Project(BABYLON.Vector3.Zero(), camera.getWorldMatrix(), _this._transformationMatrix, globalViewport);
  121. if (!_this.shouldDisplayLabel || _this.shouldDisplayLabel(camera)) {
  122. _this._renderLabel(camera.name, projectedPosition, 12, function () {
  123. _this._camera.detachControl(engine.getRenderingCanvas());
  124. _this._camera = camera;
  125. _this._camera.attachControl(engine.getRenderingCanvas());
  126. }, function () { return "purple"; });
  127. }
  128. }
  129. // Lights
  130. var lights = _this._scene.lights;
  131. for (index = 0; index < lights.length; index++) {
  132. var light = lights[index];
  133. if (light.position) {
  134. projectedPosition = BABYLON.Vector3.Project(light.getAbsolutePosition(), _this._identityMatrix, _this._transformationMatrix, globalViewport);
  135. if (!_this.shouldDisplayLabel || _this.shouldDisplayLabel(light)) {
  136. _this._renderLabel(light.name, projectedPosition, -20, function () {
  137. light.setEnabled(!light.isEnabled());
  138. }, function () { return light.isEnabled() ? "orange" : "gray"; });
  139. }
  140. }
  141. }
  142. }
  143. _this._clickPosition = undefined;
  144. };
  145. }
  146. DebugLayer.prototype._refreshMeshesTreeContent = function () {
  147. while (this._treeSubsetDiv.hasChildNodes()) {
  148. this._treeSubsetDiv.removeChild(this._treeSubsetDiv.lastChild);
  149. }
  150. // Add meshes
  151. var sortedArray = this._scene.meshes.slice(0, this._scene.meshes.length);
  152. sortedArray.sort(function (a, b) {
  153. if (a.name === b.name) {
  154. return 0;
  155. }
  156. return (a.name > b.name) ? 1 : -1;
  157. });
  158. for (var index = 0; index < sortedArray.length; index++) {
  159. var mesh = sortedArray[index];
  160. if (!mesh.isEnabled()) {
  161. continue;
  162. }
  163. this._generateAdvancedCheckBox(this._treeSubsetDiv, mesh.name, mesh.getTotalVertices() + " verts", mesh.isVisible, function (element, m) {
  164. m.isVisible = element.checked;
  165. }, mesh);
  166. }
  167. };
  168. DebugLayer.prototype._renderSingleAxis = function (zero, unit, unitText, label, color) {
  169. this._drawingContext.beginPath();
  170. this._drawingContext.moveTo(zero.x, zero.y);
  171. this._drawingContext.lineTo(unit.x, unit.y);
  172. this._drawingContext.strokeStyle = color;
  173. this._drawingContext.lineWidth = 4;
  174. this._drawingContext.stroke();
  175. this._drawingContext.font = "normal 14px Segoe UI";
  176. this._drawingContext.fillStyle = color;
  177. this._drawingContext.fillText(label, unitText.x, unitText.y);
  178. };
  179. DebugLayer.prototype._renderAxis = function (projectedPosition, mesh, globalViewport) {
  180. var position = mesh.getBoundingInfo().boundingSphere.center;
  181. var worldMatrix = mesh.getWorldMatrix();
  182. var unprojectedVector = BABYLON.Vector3.UnprojectFromTransform(projectedPosition.add(new BABYLON.Vector3(this._drawingCanvas.width * this.axisRatio, 0, 0)), globalViewport.width, globalViewport.height, worldMatrix, this._transformationMatrix);
  183. var unit = (unprojectedVector.subtract(position)).length();
  184. var xAxis = BABYLON.Vector3.Project(position.add(new BABYLON.Vector3(unit, 0, 0)), worldMatrix, this._transformationMatrix, globalViewport);
  185. var xAxisText = BABYLON.Vector3.Project(position.add(new BABYLON.Vector3(unit * 1.5, 0, 0)), worldMatrix, this._transformationMatrix, globalViewport);
  186. this._renderSingleAxis(projectedPosition, xAxis, xAxisText, "x", "#FF0000");
  187. var yAxis = BABYLON.Vector3.Project(position.add(new BABYLON.Vector3(0, unit, 0)), worldMatrix, this._transformationMatrix, globalViewport);
  188. var yAxisText = BABYLON.Vector3.Project(position.add(new BABYLON.Vector3(0, unit * 1.5, 0)), worldMatrix, this._transformationMatrix, globalViewport);
  189. this._renderSingleAxis(projectedPosition, yAxis, yAxisText, "y", "#00FF00");
  190. var zAxis = BABYLON.Vector3.Project(position.add(new BABYLON.Vector3(0, 0, unit)), worldMatrix, this._transformationMatrix, globalViewport);
  191. var zAxisText = BABYLON.Vector3.Project(position.add(new BABYLON.Vector3(0, 0, unit * 1.5)), worldMatrix, this._transformationMatrix, globalViewport);
  192. this._renderSingleAxis(projectedPosition, zAxis, zAxisText, "z", "#0000FF");
  193. };
  194. DebugLayer.prototype._renderLabel = function (text, projectedPosition, labelOffset, onClick, getFillStyle) {
  195. if (projectedPosition.z > 0 && projectedPosition.z < 1.0) {
  196. this._drawingContext.font = "normal 12px Segoe UI";
  197. var textMetrics = this._drawingContext.measureText(text);
  198. var centerX = projectedPosition.x - textMetrics.width / 2;
  199. var centerY = projectedPosition.y;
  200. var clientRect = this._drawingCanvas.getBoundingClientRect();
  201. if (this._showUI && this._isClickInsideRect(clientRect.left * this._ratio + centerX - 5, clientRect.top * this._ratio + centerY - labelOffset - 12, textMetrics.width + 10, 17)) {
  202. onClick();
  203. }
  204. this._drawingContext.beginPath();
  205. this._drawingContext.rect(centerX - 5, centerY - labelOffset - 12, textMetrics.width + 10, 17);
  206. this._drawingContext.fillStyle = getFillStyle();
  207. this._drawingContext.globalAlpha = 0.5;
  208. this._drawingContext.fill();
  209. this._drawingContext.globalAlpha = 1.0;
  210. this._drawingContext.strokeStyle = '#FFFFFF';
  211. this._drawingContext.lineWidth = 1;
  212. this._drawingContext.stroke();
  213. this._drawingContext.fillStyle = "#FFFFFF";
  214. this._drawingContext.fillText(text, centerX, centerY - labelOffset);
  215. this._drawingContext.beginPath();
  216. this._drawingContext.arc(projectedPosition.x, centerY, 5, 0, 2 * Math.PI, false);
  217. this._drawingContext.fill();
  218. }
  219. };
  220. DebugLayer.prototype._isClickInsideRect = function (x, y, width, height) {
  221. if (!this._clickPosition) {
  222. return false;
  223. }
  224. if (this._clickPosition.x < x || this._clickPosition.x > x + width) {
  225. return false;
  226. }
  227. if (this._clickPosition.y < y || this._clickPosition.y > y + height) {
  228. return false;
  229. }
  230. return true;
  231. };
  232. DebugLayer.prototype.isVisible = function () {
  233. return this._enabled;
  234. };
  235. DebugLayer.prototype.hide = function () {
  236. if (!this._enabled) {
  237. return;
  238. }
  239. this._enabled = false;
  240. var engine = this._scene.getEngine();
  241. this._scene.unregisterBeforeRender(this._syncData);
  242. this._scene.unregisterAfterRender(this._syncUI);
  243. this._rootElement.removeChild(this._globalDiv);
  244. this._scene.forceShowBoundingBoxes = false;
  245. this._scene.forceWireframe = false;
  246. BABYLON.StandardMaterial.DiffuseTextureEnabled = true;
  247. BABYLON.StandardMaterial.AmbientTextureEnabled = true;
  248. BABYLON.StandardMaterial.SpecularTextureEnabled = true;
  249. BABYLON.StandardMaterial.EmissiveTextureEnabled = true;
  250. BABYLON.StandardMaterial.BumpTextureEnabled = true;
  251. BABYLON.StandardMaterial.OpacityTextureEnabled = true;
  252. BABYLON.StandardMaterial.ReflectionTextureEnabled = true;
  253. BABYLON.StandardMaterial.LightmapTextureEnabled = true;
  254. BABYLON.StandardMaterial.RefractionTextureEnabled = true;
  255. this._scene.shadowsEnabled = true;
  256. this._scene.particlesEnabled = true;
  257. this._scene.postProcessesEnabled = true;
  258. this._scene.collisionsEnabled = true;
  259. this._scene.lightsEnabled = true;
  260. this._scene.texturesEnabled = true;
  261. this._scene.lensFlaresEnabled = true;
  262. this._scene.proceduralTexturesEnabled = true;
  263. this._scene.renderTargetsEnabled = true;
  264. this._scene.probesEnabled = true;
  265. engine.getRenderingCanvas().removeEventListener("click", this._onCanvasClick);
  266. this._clearSkeletonViewers();
  267. };
  268. DebugLayer.prototype._clearSkeletonViewers = function () {
  269. for (var index = 0; index < this._skeletonViewers.length; index++) {
  270. this._skeletonViewers[index].dispose();
  271. }
  272. this._skeletonViewers = [];
  273. };
  274. DebugLayer.prototype.show = function (showUI, camera, rootElement) {
  275. if (showUI === void 0) { showUI = true; }
  276. if (camera === void 0) { camera = null; }
  277. if (rootElement === void 0) { rootElement = null; }
  278. if (this._enabled) {
  279. return;
  280. }
  281. this._enabled = true;
  282. if (camera) {
  283. this._camera = camera;
  284. }
  285. else {
  286. this._camera = this._scene.activeCamera;
  287. }
  288. this._showUI = showUI;
  289. var engine = this._scene.getEngine();
  290. this._globalDiv = document.createElement("div");
  291. this._rootElement = rootElement || document.body;
  292. this._rootElement.appendChild(this._globalDiv);
  293. this._generateDOMelements();
  294. engine.getRenderingCanvas().addEventListener("click", this._onCanvasClick);
  295. this._syncPositions();
  296. this._scene.registerBeforeRender(this._syncData);
  297. this._scene.registerAfterRender(this._syncUI);
  298. };
  299. DebugLayer.prototype._clearLabels = function () {
  300. this._drawingContext.clearRect(0, 0, this._drawingCanvas.width, this._drawingCanvas.height);
  301. for (var index = 0; index < this._scene.meshes.length; index++) {
  302. var mesh = this._scene.meshes[index];
  303. mesh.renderOverlay = false;
  304. }
  305. };
  306. DebugLayer.prototype._generateheader = function (root, text) {
  307. var header = document.createElement("div");
  308. header.innerHTML = text + "&nbsp;";
  309. header.style.textAlign = "right";
  310. header.style.width = "100%";
  311. header.style.color = "white";
  312. header.style.backgroundColor = "Black";
  313. header.style.padding = "5px 5px 4px 0px";
  314. header.style.marginLeft = "-5px";
  315. header.style.fontWeight = "bold";
  316. root.appendChild(header);
  317. };
  318. DebugLayer.prototype._generateTexBox = function (root, title, color) {
  319. var label = document.createElement("label");
  320. label.style.display = "inline";
  321. label.innerHTML = title;
  322. label.style.color = color;
  323. root.appendChild(label);
  324. root.appendChild(document.createElement("br"));
  325. };
  326. DebugLayer.prototype._generateAdvancedCheckBox = function (root, leftTitle, rightTitle, initialState, task, tag) {
  327. if (tag === void 0) { tag = null; }
  328. var label = document.createElement("label");
  329. label.style.display = "inline";
  330. var boundingBoxesCheckbox = document.createElement("input");
  331. boundingBoxesCheckbox.type = "checkbox";
  332. boundingBoxesCheckbox.checked = initialState;
  333. boundingBoxesCheckbox.style.display = "inline";
  334. boundingBoxesCheckbox.style.margin = "0px 5px 0px 0px";
  335. boundingBoxesCheckbox.style.verticalAlign = "sub";
  336. boundingBoxesCheckbox.addEventListener("change", function (evt) {
  337. task(evt.target, tag);
  338. });
  339. label.appendChild(boundingBoxesCheckbox);
  340. var container = document.createElement("span");
  341. var leftPart = document.createElement("span");
  342. var rightPart = document.createElement("span");
  343. rightPart.style.cssFloat = "right";
  344. leftPart.innerHTML = leftTitle;
  345. rightPart.innerHTML = rightTitle;
  346. rightPart.style.fontSize = "12px";
  347. rightPart.style.maxWidth = "200px";
  348. container.appendChild(leftPart);
  349. container.appendChild(rightPart);
  350. label.appendChild(container);
  351. root.appendChild(label);
  352. root.appendChild(document.createElement("br"));
  353. };
  354. DebugLayer.prototype._generateCheckBox = function (root, title, initialState, task, tag) {
  355. if (tag === void 0) { tag = null; }
  356. var label = document.createElement("label");
  357. label.style.display = "inline";
  358. var checkBox = document.createElement("input");
  359. checkBox.type = "checkbox";
  360. checkBox.checked = initialState;
  361. checkBox.style.display = "inline";
  362. checkBox.style.margin = "0px 5px 0px 0px";
  363. checkBox.style.verticalAlign = "sub";
  364. checkBox.addEventListener("change", function (evt) {
  365. task(evt.target, tag);
  366. });
  367. label.appendChild(checkBox);
  368. label.appendChild(document.createTextNode(title));
  369. root.appendChild(label);
  370. root.appendChild(document.createElement("br"));
  371. };
  372. DebugLayer.prototype._generateButton = function (root, title, task, tag) {
  373. if (tag === void 0) { tag = null; }
  374. var button = document.createElement("button");
  375. button.innerHTML = title;
  376. button.style.height = "24px";
  377. button.style.width = "150px";
  378. button.style.marginBottom = "5px";
  379. button.style.color = "#444444";
  380. button.style.border = "1px solid white";
  381. button.className = "debugLayerButton";
  382. button.addEventListener("click", function (evt) {
  383. task(evt.target, tag);
  384. });
  385. root.appendChild(button);
  386. root.appendChild(document.createElement("br"));
  387. };
  388. DebugLayer.prototype._generateRadio = function (root, title, name, initialState, task, tag) {
  389. if (tag === void 0) { tag = null; }
  390. var label = document.createElement("label");
  391. label.style.display = "inline";
  392. var boundingBoxesRadio = document.createElement("input");
  393. boundingBoxesRadio.type = "radio";
  394. boundingBoxesRadio.name = name;
  395. boundingBoxesRadio.checked = initialState;
  396. boundingBoxesRadio.style.display = "inline";
  397. boundingBoxesRadio.style.margin = "0px 5px 0px 0px";
  398. boundingBoxesRadio.style.verticalAlign = "sub";
  399. boundingBoxesRadio.addEventListener("change", function (evt) {
  400. task(evt.target, tag);
  401. });
  402. label.appendChild(boundingBoxesRadio);
  403. label.appendChild(document.createTextNode(title));
  404. root.appendChild(label);
  405. root.appendChild(document.createElement("br"));
  406. };
  407. DebugLayer.prototype._generateDOMelements = function () {
  408. var _this = this;
  409. this._globalDiv.id = "DebugLayer";
  410. this._globalDiv.style.position = "absolute";
  411. this._globalDiv.style.fontFamily = "Segoe UI, Arial";
  412. this._globalDiv.style.fontSize = "14px";
  413. this._globalDiv.style.color = "white";
  414. // Drawing canvas
  415. this._drawingCanvas = document.createElement("canvas");
  416. this._drawingCanvas.id = "DebugLayerDrawingCanvas";
  417. this._drawingCanvas.style.position = "absolute";
  418. this._drawingCanvas.style.pointerEvents = "none";
  419. this._drawingCanvas.style.backgroundColor = "transparent";
  420. this._drawingContext = this._drawingCanvas.getContext("2d");
  421. this._globalDiv.appendChild(this._drawingCanvas);
  422. if (this._showUI) {
  423. var background = "rgba(128, 128, 128, 0.4)";
  424. var border = "rgb(180, 180, 180) solid 1px";
  425. // Stats
  426. this._statsDiv = document.createElement("div");
  427. this._statsDiv.id = "DebugLayerStats";
  428. this._statsDiv.style.border = border;
  429. this._statsDiv.style.position = "absolute";
  430. this._statsDiv.style.background = background;
  431. this._statsDiv.style.padding = "0px 0px 0px 5px";
  432. this._generateheader(this._statsDiv, "STATISTICS");
  433. this._statsSubsetDiv = document.createElement("div");
  434. this._statsSubsetDiv.style.paddingTop = "5px";
  435. this._statsSubsetDiv.style.paddingBottom = "5px";
  436. this._statsSubsetDiv.style.overflowY = "auto";
  437. this._statsDiv.appendChild(this._statsSubsetDiv);
  438. // Tree
  439. this._treeDiv = document.createElement("div");
  440. this._treeDiv.id = "DebugLayerTree";
  441. this._treeDiv.style.border = border;
  442. this._treeDiv.style.position = "absolute";
  443. this._treeDiv.style.background = background;
  444. this._treeDiv.style.padding = "0px 0px 0px 5px";
  445. this._treeDiv.style.display = "none";
  446. this._generateheader(this._treeDiv, "MESHES TREE");
  447. this._treeSubsetDiv = document.createElement("div");
  448. this._treeSubsetDiv.style.paddingTop = "5px";
  449. this._treeSubsetDiv.style.paddingRight = "5px";
  450. this._treeSubsetDiv.style.overflowY = "auto";
  451. this._treeSubsetDiv.style.maxHeight = "300px";
  452. this._treeDiv.appendChild(this._treeSubsetDiv);
  453. this._needToRefreshMeshesTree = true;
  454. // Logs
  455. this._logDiv = document.createElement("div");
  456. this._logDiv.style.border = border;
  457. this._logDiv.id = "DebugLayerLogs";
  458. this._logDiv.style.position = "absolute";
  459. this._logDiv.style.background = background;
  460. this._logDiv.style.padding = "0px 0px 0px 5px";
  461. this._logDiv.style.display = "none";
  462. this._generateheader(this._logDiv, "LOGS");
  463. this._logSubsetDiv = document.createElement("div");
  464. this._logSubsetDiv.style.height = "127px";
  465. this._logSubsetDiv.style.paddingTop = "5px";
  466. this._logSubsetDiv.style.overflowY = "auto";
  467. this._logSubsetDiv.style.fontSize = "12px";
  468. this._logSubsetDiv.style.fontFamily = "consolas";
  469. this._logSubsetDiv.innerHTML = BABYLON.Tools.LogCache;
  470. this._logDiv.appendChild(this._logSubsetDiv);
  471. BABYLON.Tools.OnNewCacheEntry = function (entry) {
  472. _this._logSubsetDiv.innerHTML = entry + _this._logSubsetDiv.innerHTML;
  473. };
  474. // Options
  475. this._optionsDiv = document.createElement("div");
  476. this._optionsDiv.id = "DebugLayerOptions";
  477. this._optionsDiv.style.border = border;
  478. this._optionsDiv.style.position = "absolute";
  479. this._optionsDiv.style.background = background;
  480. this._optionsDiv.style.padding = "0px 0px 0px 5px";
  481. this._optionsDiv.style.overflowY = "auto";
  482. this._generateheader(this._optionsDiv, "OPTIONS");
  483. this._optionsSubsetDiv = document.createElement("div");
  484. this._optionsSubsetDiv.style.paddingTop = "5px";
  485. this._optionsSubsetDiv.style.paddingBottom = "5px";
  486. this._optionsSubsetDiv.style.overflowY = "auto";
  487. this._optionsSubsetDiv.style.maxHeight = "200px";
  488. this._optionsDiv.appendChild(this._optionsSubsetDiv);
  489. this._generateTexBox(this._optionsSubsetDiv, "<b>Windows:</b>", this.accentColor);
  490. this._generateCheckBox(this._optionsSubsetDiv, "Statistics", this._displayStatistics, function (element) { _this._displayStatistics = element.checked; });
  491. this._generateCheckBox(this._optionsSubsetDiv, "Logs", this._displayLogs, function (element) { _this._displayLogs = element.checked; });
  492. this._generateCheckBox(this._optionsSubsetDiv, "Meshes tree", this._displayTree, function (element) {
  493. _this._displayTree = element.checked;
  494. _this._needToRefreshMeshesTree = true;
  495. });
  496. this._optionsSubsetDiv.appendChild(document.createElement("br"));
  497. this._generateTexBox(this._optionsSubsetDiv, "<b>General:</b>", this.accentColor);
  498. this._generateCheckBox(this._optionsSubsetDiv, "Bounding boxes", this._scene.forceShowBoundingBoxes, function (element) { _this._scene.forceShowBoundingBoxes = element.checked; });
  499. this._generateCheckBox(this._optionsSubsetDiv, "Clickable labels", this._labelsEnabled, function (element) {
  500. _this._labelsEnabled = element.checked;
  501. if (!_this._labelsEnabled) {
  502. _this._clearLabels();
  503. }
  504. });
  505. this._generateCheckBox(this._optionsSubsetDiv, "Generate user marks (F12)", BABYLON.Tools.PerformanceLogLevel === BABYLON.Tools.PerformanceUserMarkLogLevel, function (element) {
  506. if (element.checked) {
  507. BABYLON.Tools.PerformanceLogLevel = BABYLON.Tools.PerformanceUserMarkLogLevel;
  508. }
  509. else {
  510. BABYLON.Tools.PerformanceLogLevel = BABYLON.Tools.PerformanceNoneLogLevel;
  511. }
  512. });
  513. ;
  514. this._optionsSubsetDiv.appendChild(document.createElement("br"));
  515. this._generateTexBox(this._optionsSubsetDiv, "<b>Rendering mode:</b>", this.accentColor);
  516. this._generateRadio(this._optionsSubsetDiv, "Solid", "renderMode", !this._scene.forceWireframe && !this._scene.forcePointsCloud, function (element) {
  517. if (element.checked) {
  518. _this._scene.forceWireframe = false;
  519. _this._scene.forcePointsCloud = false;
  520. }
  521. });
  522. this._generateRadio(this._optionsSubsetDiv, "Wireframe", "renderMode", this._scene.forceWireframe, function (element) {
  523. if (element.checked) {
  524. _this._scene.forceWireframe = true;
  525. _this._scene.forcePointsCloud = false;
  526. }
  527. });
  528. this._generateRadio(this._optionsSubsetDiv, "Point", "renderMode", this._scene.forcePointsCloud, function (element) {
  529. if (element.checked) {
  530. _this._scene.forceWireframe = false;
  531. _this._scene.forcePointsCloud = true;
  532. }
  533. });
  534. this._optionsSubsetDiv.appendChild(document.createElement("br"));
  535. this._generateTexBox(this._optionsSubsetDiv, "<b>Texture channels:</b>", this.accentColor);
  536. this._generateCheckBox(this._optionsSubsetDiv, "Diffuse", BABYLON.StandardMaterial.DiffuseTextureEnabled, function (element) { BABYLON.StandardMaterial.DiffuseTextureEnabled = element.checked; });
  537. this._generateCheckBox(this._optionsSubsetDiv, "Ambient", BABYLON.StandardMaterial.AmbientTextureEnabled, function (element) { BABYLON.StandardMaterial.AmbientTextureEnabled = element.checked; });
  538. this._generateCheckBox(this._optionsSubsetDiv, "Specular", BABYLON.StandardMaterial.SpecularTextureEnabled, function (element) { BABYLON.StandardMaterial.SpecularTextureEnabled = element.checked; });
  539. this._generateCheckBox(this._optionsSubsetDiv, "Emissive", BABYLON.StandardMaterial.EmissiveTextureEnabled, function (element) { BABYLON.StandardMaterial.EmissiveTextureEnabled = element.checked; });
  540. this._generateCheckBox(this._optionsSubsetDiv, "Bump", BABYLON.StandardMaterial.BumpTextureEnabled, function (element) { BABYLON.StandardMaterial.BumpTextureEnabled = element.checked; });
  541. this._generateCheckBox(this._optionsSubsetDiv, "Opacity", BABYLON.StandardMaterial.OpacityTextureEnabled, function (element) { BABYLON.StandardMaterial.OpacityTextureEnabled = element.checked; });
  542. this._generateCheckBox(this._optionsSubsetDiv, "Reflection", BABYLON.StandardMaterial.ReflectionTextureEnabled, function (element) { BABYLON.StandardMaterial.ReflectionTextureEnabled = element.checked; });
  543. this._generateCheckBox(this._optionsSubsetDiv, "Refraction", BABYLON.StandardMaterial.RefractionTextureEnabled, function (element) { BABYLON.StandardMaterial.RefractionTextureEnabled = element.checked; });
  544. this._generateCheckBox(this._optionsSubsetDiv, "Lightmap", BABYLON.StandardMaterial.LightmapTextureEnabled, function (element) { BABYLON.StandardMaterial.LightmapTextureEnabled = element.checked; });
  545. this._generateCheckBox(this._optionsSubsetDiv, "Fresnel", BABYLON.StandardMaterial.FresnelEnabled, function (element) { BABYLON.StandardMaterial.FresnelEnabled = element.checked; });
  546. this._optionsSubsetDiv.appendChild(document.createElement("br"));
  547. this._generateTexBox(this._optionsSubsetDiv, "<b>Options:</b>", this.accentColor);
  548. this._generateCheckBox(this._optionsSubsetDiv, "Animations", this._scene.animationsEnabled, function (element) { _this._scene.animationsEnabled = element.checked; });
  549. this._generateCheckBox(this._optionsSubsetDiv, "Collisions", this._scene.collisionsEnabled, function (element) { _this._scene.collisionsEnabled = element.checked; });
  550. this._generateCheckBox(this._optionsSubsetDiv, "Fog", this._scene.fogEnabled, function (element) { _this._scene.fogEnabled = element.checked; });
  551. this._generateCheckBox(this._optionsSubsetDiv, "Lens flares", this._scene.lensFlaresEnabled, function (element) { _this._scene.lensFlaresEnabled = element.checked; });
  552. this._generateCheckBox(this._optionsSubsetDiv, "Lights", this._scene.lightsEnabled, function (element) { _this._scene.lightsEnabled = element.checked; });
  553. this._generateCheckBox(this._optionsSubsetDiv, "Particles", this._scene.particlesEnabled, function (element) { _this._scene.particlesEnabled = element.checked; });
  554. this._generateCheckBox(this._optionsSubsetDiv, "Post-processes", this._scene.postProcessesEnabled, function (element) { _this._scene.postProcessesEnabled = element.checked; });
  555. this._generateCheckBox(this._optionsSubsetDiv, "Probes", this._scene.probesEnabled, function (element) { _this._scene.probesEnabled = element.checked; });
  556. this._generateCheckBox(this._optionsSubsetDiv, "Procedural textures", this._scene.proceduralTexturesEnabled, function (element) { _this._scene.proceduralTexturesEnabled = element.checked; });
  557. this._generateCheckBox(this._optionsSubsetDiv, "Render targets", this._scene.renderTargetsEnabled, function (element) { _this._scene.renderTargetsEnabled = element.checked; });
  558. this._generateCheckBox(this._optionsSubsetDiv, "Shadows", this._scene.shadowsEnabled, function (element) { _this._scene.shadowsEnabled = element.checked; });
  559. this._generateCheckBox(this._optionsSubsetDiv, "Skeletons", this._scene.skeletonsEnabled, function (element) { _this._scene.skeletonsEnabled = element.checked; });
  560. this._generateCheckBox(this._optionsSubsetDiv, "Sprites", this._scene.spritesEnabled, function (element) { _this._scene.spritesEnabled = element.checked; });
  561. this._generateCheckBox(this._optionsSubsetDiv, "Textures", this._scene.texturesEnabled, function (element) { _this._scene.texturesEnabled = element.checked; });
  562. if (BABYLON.AudioEngine && BABYLON.Engine.audioEngine.canUseWebAudio) {
  563. this._optionsSubsetDiv.appendChild(document.createElement("br"));
  564. this._generateTexBox(this._optionsSubsetDiv, "<b>Audio:</b>", this.accentColor);
  565. this._generateRadio(this._optionsSubsetDiv, "Headphones", "panningModel", this._scene.headphone, function (element) {
  566. if (element.checked) {
  567. _this._scene.headphone = true;
  568. }
  569. });
  570. this._generateRadio(this._optionsSubsetDiv, "Normal Speakers", "panningModel", !this._scene.headphone, function (element) {
  571. if (element.checked) {
  572. _this._scene.headphone = false;
  573. }
  574. });
  575. this._generateCheckBox(this._optionsSubsetDiv, "Disable audio", !this._scene.audioEnabled, function (element) {
  576. _this._scene.audioEnabled = !element.checked;
  577. });
  578. }
  579. this._optionsSubsetDiv.appendChild(document.createElement("br"));
  580. this._generateTexBox(this._optionsSubsetDiv, "<b>Viewers:</b>", this.accentColor);
  581. this._generateCheckBox(this._optionsSubsetDiv, "Skeletons", false, function (element) {
  582. if (!element.checked) {
  583. _this._clearSkeletonViewers();
  584. return;
  585. }
  586. for (var index = 0; index < _this._scene.meshes.length; index++) {
  587. var mesh = _this._scene.meshes[index];
  588. if (mesh.skeleton) {
  589. var found = false;
  590. for (var sIndex = 0; sIndex < _this._skeletonViewers.length; sIndex++) {
  591. if (_this._skeletonViewers[sIndex].skeleton === mesh.skeleton) {
  592. found = true;
  593. break;
  594. }
  595. }
  596. if (found) {
  597. continue;
  598. }
  599. var viewer = new BABYLON.Debug.SkeletonViewer(mesh.skeleton, mesh, _this._scene);
  600. viewer.isEnabled = true;
  601. _this._skeletonViewers.push(viewer);
  602. }
  603. }
  604. });
  605. this._optionsSubsetDiv.appendChild(document.createElement("br"));
  606. this._generateTexBox(this._optionsSubsetDiv, "<b>Tools:</b>", this.accentColor);
  607. this._generateButton(this._optionsSubsetDiv, "Dump rendertargets", function (element) { _this._scene.dumpNextRenderTargets = true; });
  608. this._generateButton(this._optionsSubsetDiv, "Run SceneOptimizer", function (element) { BABYLON.SceneOptimizer.OptimizeAsync(_this._scene); });
  609. this._generateButton(this._optionsSubsetDiv, "Log camera object", function (element) {
  610. if (_this._camera) {
  611. console.log(_this._camera);
  612. }
  613. else {
  614. console.warn("No camera defined, or debug layer created before camera creation!");
  615. }
  616. });
  617. this._optionsSubsetDiv.appendChild(document.createElement("br"));
  618. this._globalDiv.appendChild(this._statsDiv);
  619. this._globalDiv.appendChild(this._logDiv);
  620. this._globalDiv.appendChild(this._optionsDiv);
  621. this._globalDiv.appendChild(this._treeDiv);
  622. }
  623. };
  624. DebugLayer.prototype._displayStats = function () {
  625. var scene = this._scene;
  626. var engine = scene.getEngine();
  627. var glInfo = engine.getGlInfo();
  628. this._statsSubsetDiv.innerHTML = "Babylon.js v" + BABYLON.Engine.Version + " - <b>" + BABYLON.Tools.Format(engine.getFps(), 0) + " fps</b><br><br>"
  629. + "<div style='column-count: 2;-moz-column-count:2;-webkit-column-count:2'>"
  630. + "<b>Count</b><br>"
  631. + "Total meshes: " + scene.meshes.length + "<br>"
  632. + "Total lights: " + scene.lights.length + "<br>"
  633. + "Total vertices: " + scene.getTotalVertices() + "<br>"
  634. + "Total materials: " + scene.materials.length + "<br>"
  635. + "Total textures: " + scene.textures.length + "<br>"
  636. + "Active meshes: " + scene.getActiveMeshes().length + "<br>"
  637. + "Active indices: " + scene.getActiveIndices() + "<br>"
  638. + "Active bones: " + scene.getActiveBones() + "<br>"
  639. + "Active particles: " + scene.getActiveParticles() + "<br>"
  640. + "<b>Draw calls: " + engine.drawCalls + "</b><br><br>"
  641. + "<b>Duration</b><br>"
  642. + "Meshes selection:</i> " + BABYLON.Tools.Format(scene.getEvaluateActiveMeshesDuration()) + " ms<br>"
  643. + "Render Targets: " + BABYLON.Tools.Format(scene.getRenderTargetsDuration()) + " ms<br>"
  644. + "Particles: " + BABYLON.Tools.Format(scene.getParticlesDuration()) + " ms<br>"
  645. + "Sprites: " + BABYLON.Tools.Format(scene.getSpritesDuration()) + " ms<br><br>"
  646. + "Render: <b>" + BABYLON.Tools.Format(scene.getRenderDuration()) + " ms</b><br>"
  647. + "Frame: " + BABYLON.Tools.Format(scene.getLastFrameDuration()) + " ms<br>"
  648. + "Potential FPS: " + BABYLON.Tools.Format(1000.0 / scene.getLastFrameDuration(), 0) + "<br>"
  649. + "Resolution: " + engine.getRenderWidth() + "x" + engine.getRenderHeight() + "<br><br>"
  650. + "</div>"
  651. + "<div style='column-count: 2;-moz-column-count:2;-webkit-column-count:2'>"
  652. + "<b>Extensions</b><br>"
  653. + "Std derivatives: " + (engine.getCaps().standardDerivatives ? "Yes" : "No") + "<br>"
  654. + "Compressed textures: " + (engine.getCaps().s3tc ? "Yes" : "No") + "<br>"
  655. + "Hardware instances: " + (engine.getCaps().instancedArrays ? "Yes" : "No") + "<br>"
  656. + "Texture float: " + (engine.getCaps().textureFloat ? "Yes" : "No") + "<br><br>"
  657. + "32bits indices: " + (engine.getCaps().uintIndices ? "Yes" : "No") + "<br>"
  658. + "Fragment depth: " + (engine.getCaps().fragmentDepthSupported ? "Yes" : "No") + "<br>"
  659. + "High precision shaders: " + (engine.getCaps().highPrecisionShaderSupported ? "Yes" : "No") + "<br>"
  660. + "Draw buffers: " + (engine.getCaps().drawBuffersExtension ? "Yes" : "No") + "<br>"
  661. + "</div><br>"
  662. + "<div style='column-count: 2;-moz-column-count:2;-webkit-column-count:2'>"
  663. + "<b>Caps.</b><br>"
  664. + "Max textures units: " + engine.getCaps().maxTexturesImageUnits + "<br>"
  665. + "Max textures size: " + engine.getCaps().maxTextureSize + "<br>"
  666. + "Max anisotropy: " + engine.getCaps().maxAnisotropy + "<br>"
  667. + "<b>Info</b><br>"
  668. + "WebGL feature level: " + engine.webGLVersion + "<br>"
  669. + glInfo.version + "<br>"
  670. + "</div><br>"
  671. + glInfo.renderer + "<br>";
  672. if (this.customStatsFunction) {
  673. this._statsSubsetDiv.innerHTML += this.customStatsFunction();
  674. }
  675. };
  676. return DebugLayer;
  677. })();
  678. BABYLON.DebugLayer = DebugLayer;
  679. })(BABYLON || (BABYLON = {}));