babylon.debugLayer.ts 38 KB

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