ToolsTab.ts 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. import { CubeTexture, Engine, EnvironmentTextureTools, Nullable, Scene, Tools, VideoRecorder } from "babylonjs";
  2. import { Helpers } from "../helpers/Helpers";
  3. import { Inspector } from "../Inspector";
  4. import { Tab } from "./Tab";
  5. import { TabBar } from "./TabBar";
  6. export class ToolsTab extends Tab {
  7. private _inspector: Inspector;
  8. private _scene: Scene;
  9. private _videoRecorder: Nullable<VideoRecorder> = null;
  10. constructor(tabbar: TabBar, insp: Inspector) {
  11. super(tabbar, 'Tools');
  12. this._inspector = insp;
  13. this._scene = this._inspector.scene;
  14. // Build the tools panel: a div that will contains all tools
  15. this._panel = Helpers.CreateDiv('tab-panel') as HTMLDivElement;
  16. this._panel.classList.add("tools-panel")
  17. let title = Helpers.CreateDiv('tool-title1', this._panel);
  18. let versionSpan = Helpers.CreateElement('span');
  19. versionSpan.textContent = `js v${Engine.Version} - Tools`;
  20. title.appendChild(versionSpan);
  21. // Environment block
  22. title = Helpers.CreateDiv('tool-title2', this._panel);
  23. title.textContent = "Environment Texture (.dds, .env)";
  24. {
  25. let errorElemm = Inspector.DOCUMENT.createElement('div');
  26. errorElemm.className = "tool-label-error";
  27. errorElemm.style.display = "none";
  28. let elemValue = Helpers.CreateDiv(null, this._panel);
  29. let inputElement = Inspector.DOCUMENT.createElement('input');
  30. inputElement.className = "tool-input";
  31. inputElement.type = "file";
  32. inputElement.accept = ".dds, .env";
  33. inputElement.onchange = (event: any) => {
  34. var files: File[] = event.target.files;
  35. let file: Nullable<File> = null;
  36. if (files && files.length) {
  37. file = files[0];
  38. }
  39. if (!file) {
  40. errorElemm.style.display = "block";
  41. errorElemm.textContent = "Please, select a file first."
  42. return;
  43. }
  44. let isFileDDS = file.name.toLowerCase().indexOf(".dds") > 0;
  45. let isFileEnv = file.name.toLowerCase().indexOf(".env") > 0;
  46. if (!isFileDDS && !isFileEnv) {
  47. errorElemm.style.display = "block";
  48. errorElemm.textContent = "Please, select a dds or env file."
  49. return;
  50. }
  51. Tools.ReadFile(file, data => {
  52. var blob = new Blob([data], { type: "octet/stream" });
  53. var url = URL.createObjectURL(blob);
  54. if (isFileDDS) {
  55. this._scene.environmentTexture = CubeTexture.CreateFromPrefilteredData(url, this._scene, ".dds");
  56. errorElemm.style.display = "none";
  57. }
  58. else {
  59. this._scene.environmentTexture = new CubeTexture(url, this._scene,
  60. undefined, undefined, undefined,
  61. () => {
  62. errorElemm.style.display = "none";
  63. },
  64. (message) => {
  65. if (message) {
  66. errorElemm.style.display = "block";
  67. errorElemm.textContent = message;
  68. }
  69. },
  70. undefined, undefined,
  71. ".env");
  72. }
  73. }, undefined, true);
  74. };
  75. elemValue.appendChild(inputElement);
  76. if (!this._scene.getEngine().premultipliedAlpha) {
  77. elemValue = Helpers.CreateDiv(null, this._panel);
  78. inputElement = Inspector.DOCUMENT.createElement('input');
  79. inputElement.value = "Compress current texture to .env";
  80. inputElement.className = "tool-input";
  81. inputElement.type = "button";
  82. inputElement.onclick = () => {
  83. if (!this._scene.environmentTexture) {
  84. errorElemm.style.display = "block";
  85. errorElemm.textContent = "You must load an environment texture first.";
  86. return;
  87. }
  88. if (this._scene.activeCamera) {
  89. EnvironmentTextureTools.CreateEnvTextureAsync(<CubeTexture>this._scene.environmentTexture)
  90. .then((buffer: ArrayBuffer) => {
  91. var blob = new Blob([buffer], { type: "octet/stream" });
  92. Tools.Download(blob, "environment.env");
  93. errorElemm.style.display = "none";
  94. })
  95. .catch((error: any) => {
  96. errorElemm.style.display = "block";
  97. errorElemm.textContent = error;
  98. });
  99. }
  100. else {
  101. errorElemm.style.display = "block";
  102. errorElemm.textContent = "An active camera is required.";
  103. }
  104. };
  105. elemValue.appendChild(inputElement);
  106. }
  107. this._panel.appendChild(errorElemm);
  108. }
  109. title = Helpers.CreateDiv('tool-title2', this._panel);
  110. title.textContent = "Capture";
  111. {
  112. let elemValue = Helpers.CreateDiv(null, this._panel);
  113. let inputElement = Inspector.DOCUMENT.createElement('input');
  114. inputElement.value = "Take Screenshot";
  115. inputElement.type = "button";
  116. inputElement.className = "tool-input";
  117. inputElement.onclick = () => {
  118. if (this._scene.activeCamera) {
  119. Tools.CreateScreenshot(this._scene.getEngine(), this._scene.activeCamera, { precision: 0.5 });
  120. }
  121. };
  122. elemValue.appendChild(inputElement);
  123. if (VideoRecorder && VideoRecorder.IsSupported(this._scene.getEngine())) {
  124. let videoRecorderElement = Inspector.DOCUMENT.createElement('input');
  125. videoRecorderElement.value = "Start Recording Video";
  126. videoRecorderElement.type = "button";
  127. videoRecorderElement.className = "tool-input";
  128. videoRecorderElement.onclick = () => {
  129. if (!this._videoRecorder) {
  130. this._videoRecorder = new VideoRecorder(this._scene.getEngine());
  131. }
  132. if (this._videoRecorder!.isRecording) {
  133. this._videoRecorder!.stopRecording();
  134. }
  135. else {
  136. videoRecorderElement.value = "Stop Recording Video";
  137. this._videoRecorder!.startRecording().then(() => {
  138. videoRecorderElement.value = "Start Recording Video";
  139. });
  140. }
  141. };
  142. elemValue.appendChild(videoRecorderElement);
  143. }
  144. }
  145. }
  146. public dispose() {
  147. // Nothing to dispose
  148. }
  149. }