babylon.debugLayer.js 39 KB

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