babylon.tools.js 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. var BABYLON;
  2. (function (BABYLON) {
  3. // Screenshots
  4. var screenshotCanvas;
  5. var cloneValue = function (source, destinationObject) {
  6. if (!source)
  7. return null;
  8. if (source instanceof Mesh) {
  9. return null;
  10. }
  11. if (source instanceof SubMesh) {
  12. return source.clone(destinationObject);
  13. } else if (source.clone) {
  14. return source.clone();
  15. }
  16. return null;
  17. };
  18. var Tools = (function () {
  19. function Tools() {
  20. }
  21. Tools.GetFilename = function (path) {
  22. var index = path.lastIndexOf("/");
  23. if (index < 0)
  24. return path;
  25. return path.substring(index + 1);
  26. };
  27. Tools.GetDOMTextContent = function (element) {
  28. var result = "";
  29. var child = element.firstChild;
  30. while (child) {
  31. if (child.nodeType == 3) {
  32. result += child.textContent;
  33. }
  34. child = child.nextSibling;
  35. }
  36. return result;
  37. };
  38. Tools.ToDegrees = function (angle) {
  39. return angle * 180 / Math.PI;
  40. };
  41. Tools.ToRadians = function (angle) {
  42. return angle * Math.PI / 180;
  43. };
  44. Tools.ExtractMinAndMaxIndexed = function (positions, indices, indexStart, indexCount) {
  45. var minimum = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  46. var maximum = new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  47. for (var index = indexStart; index < indexStart + indexCount; index++) {
  48. var current = new Vector3(positions[indices[index] * 3], positions[indices[index] * 3 + 1], positions[indices[index] * 3 + 2]);
  49. minimum = BABYLON.Vector3.Minimize(current, minimum);
  50. maximum = BABYLON.Vector3.Maximize(current, maximum);
  51. }
  52. return {
  53. minimum: minimum,
  54. maximum: maximum
  55. };
  56. };
  57. Tools.ExtractMinAndMax = function (positions, start, count) {
  58. var minimum = new Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  59. var maximum = new Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  60. for (var index = start; index < start + count; index++) {
  61. var current = new Vector3(positions[index * 3], positions[index * 3 + 1], positions[index * 3 + 2]);
  62. minimum = BABYLON.Vector3.Minimize(current, minimum);
  63. maximum = BABYLON.Vector3.Maximize(current, maximum);
  64. }
  65. return {
  66. minimum: minimum,
  67. maximum: maximum
  68. };
  69. };
  70. Tools.MakeArray = function (obj, allowsNullUndefined) {
  71. if (allowsNullUndefined !== true && (obj === undefined || obj == null))
  72. return undefined;
  73. return Array.isArray(obj) ? obj : [obj];
  74. };
  75. // Misc.
  76. Tools.GetPointerPrefix = function () {
  77. var eventPrefix = "pointer";
  78. // Check if hand.js is referenced or if the browser natively supports pointer events
  79. if (!navigator.pointerEnabled) {
  80. eventPrefix = "mouse";
  81. }
  82. return eventPrefix;
  83. };
  84. Tools.QueueNewFrame = function (func) {
  85. if (window.requestAnimationFrame)
  86. window.requestAnimationFrame(func);
  87. else if (window.msRequestAnimationFrame)
  88. window.msRequestAnimationFrame(func);
  89. else if (window.webkitRequestAnimationFrame)
  90. window.webkitRequestAnimationFrame(func);
  91. else if (window.mozRequestAnimationFrame)
  92. window.mozRequestAnimationFrame(func);
  93. else if (window.oRequestAnimationFrame)
  94. window.oRequestAnimationFrame(func);
  95. else {
  96. window.setTimeout(func, 16);
  97. }
  98. };
  99. Tools.RequestFullscreen = function (element) {
  100. if (element.requestFullscreen)
  101. element.requestFullscreen();
  102. else if (element.msRequestFullscreen)
  103. element.msRequestFullscreen();
  104. else if (element.webkitRequestFullscreen)
  105. element.webkitRequestFullscreen();
  106. else if (element.mozRequestFullScreen)
  107. element.mozRequestFullScreen();
  108. };
  109. Tools.ExitFullscreen = function () {
  110. if (document.exitFullscreen) {
  111. document.exitFullscreen();
  112. } else if (document.mozCancelFullScreen) {
  113. document.mozCancelFullScreen();
  114. } else if (document.webkitCancelFullScreen) {
  115. document.webkitCancelFullScreen();
  116. } else if (document.msCancelFullScreen) {
  117. document.msCancelFullScreen();
  118. }
  119. };
  120. // External files
  121. Tools.CleanUrl = function (url) {
  122. url = url.replace(/#/mg, "%23");
  123. return url;
  124. };
  125. Tools.LoadImage = function (url, onload, onerror, database) {
  126. url = Tools.CleanUrl(url);
  127. var img = new Image();
  128. if (url.substr(0, 5) != "data:")
  129. img.crossOrigin = 'anonymous';
  130. img.onload = function () {
  131. onload(img);
  132. };
  133. img.onerror = function (err) {
  134. onerror(img, err);
  135. };
  136. var noIndexedDB = function () {
  137. img.src = url;
  138. };
  139. var loadFromIndexedDB = function () {
  140. database.loadImageFromDB(url, img);
  141. };
  142. //ANY database to do!
  143. if (database && database.enableTexturesOffline && BABYLON.Database.isUASupportingBlobStorage) {
  144. database.openAsync(loadFromIndexedDB, noIndexedDB);
  145. } else {
  146. if (url.indexOf("file:") === -1) {
  147. noIndexedDB();
  148. } else {
  149. try {
  150. var textureName = url.substring(5);
  151. var blobURL;
  152. try {
  153. blobURL = URL.createObjectURL(BABYLON.FilesInput.FilesTextures[textureName], { oneTimeOnly: true });
  154. } catch (ex) {
  155. // Chrome doesn't support oneTimeOnly parameter
  156. blobURL = URL.createObjectURL(BABYLON.FilesInput.FilesTextures[textureName]);
  157. }
  158. img.src = blobURL;
  159. } catch (e) {
  160. Tools.Log("Error while trying to load texture: " + textureName);
  161. img.src = null;
  162. }
  163. }
  164. }
  165. return img;
  166. };
  167. //ANY
  168. Tools.LoadFile = function (url, callback, progressCallBack, database, useArrayBuffer, onError) {
  169. url = Tools.CleanUrl(url);
  170. var noIndexedDB = function () {
  171. var request = new XMLHttpRequest();
  172. var loadUrl = Tools.BaseUrl + url;
  173. request.open('GET', loadUrl, true);
  174. if (useArrayBuffer) {
  175. request.responseType = "arraybuffer";
  176. }
  177. request.onprogress = progressCallBack;
  178. request.onreadystatechange = function () {
  179. if (request.readyState == 4) {
  180. if (request.status == 200 || BABYLON.Tools.ValidateXHRData(request, !useArrayBuffer ? 1 : 6)) {
  181. callback(!useArrayBuffer ? request.responseText : request.response);
  182. } else {
  183. if (onError) {
  184. onError();
  185. } else {
  186. throw new Error("Error status: " + request.status + " - Unable to load " + loadUrl);
  187. }
  188. }
  189. }
  190. };
  191. request.send(null);
  192. };
  193. var loadFromIndexedDB = function () {
  194. database.loadFileFromDB(url, callback, progressCallBack, noIndexedDB, useArrayBuffer);
  195. };
  196. if (url.indexOf("file:") !== -1) {
  197. var fileName = url.substring(5);
  198. BABYLON.Tools.ReadFile(BABYLON.FilesInput.FilesToLoad[fileName], callback, progressCallBack, true);
  199. } else {
  200. // Caching all files
  201. if (database && database.enableSceneOffline) {
  202. database.openAsync(loadFromIndexedDB, noIndexedDB);
  203. } else {
  204. noIndexedDB();
  205. }
  206. }
  207. };
  208. Tools.ReadFileAsDataURL = function (fileToLoad, callback, progressCallback) {
  209. var reader = new FileReader();
  210. reader.onload = function (e) {
  211. callback(e.target.result);
  212. };
  213. reader.onprogress = progressCallback;
  214. reader.readAsDataURL(fileToLoad);
  215. };
  216. Tools.ReadFile = function (fileToLoad, callback, progressCallBack, useArrayBuffer) {
  217. var reader = new FileReader();
  218. reader.onload = function (e) {
  219. callback(e.target.result);
  220. };
  221. reader.onprogress = progressCallBack;
  222. if (!useArrayBuffer) {
  223. // Asynchronous read
  224. reader.readAsText(fileToLoad);
  225. } else {
  226. reader.readAsArrayBuffer(fileToLoad);
  227. }
  228. };
  229. // Misc.
  230. Tools.Clamp = function (value, min, max) {
  231. if (typeof min === "undefined") { min = 0; }
  232. if (typeof max === "undefined") { max = 1; }
  233. return Math.min(max, Math.max(min, value));
  234. };
  235. Tools.Format = function (value, decimals) {
  236. if (typeof decimals === "undefined") { decimals = 2; }
  237. return value.toFixed(decimals);
  238. };
  239. Tools.CheckExtends = function (v, min, max) {
  240. if (v.x < min.x)
  241. min.x = v.x;
  242. if (v.y < min.y)
  243. min.y = v.y;
  244. if (v.z < min.z)
  245. min.z = v.z;
  246. if (v.x > max.x)
  247. max.x = v.x;
  248. if (v.y > max.y)
  249. max.y = v.y;
  250. if (v.z > max.z)
  251. max.z = v.z;
  252. };
  253. Tools.WithinEpsilon = function (a, b, epsilon) {
  254. if (typeof epsilon === "undefined") { epsilon = 1.401298E-45; }
  255. var num = a - b;
  256. return -epsilon <= num && num <= epsilon;
  257. };
  258. Tools.DeepCopy = function (source, destination, doNotCopyList, mustCopyList) {
  259. for (var prop in source) {
  260. if (prop[0] === "_" && (!mustCopyList || mustCopyList.indexOf(prop) === -1)) {
  261. continue;
  262. }
  263. if (doNotCopyList && doNotCopyList.indexOf(prop) !== -1) {
  264. continue;
  265. }
  266. var sourceValue = source[prop];
  267. var typeOfSourceValue = typeof sourceValue;
  268. if (typeOfSourceValue == "function") {
  269. continue;
  270. }
  271. if (typeOfSourceValue == "object") {
  272. if (sourceValue instanceof Array) {
  273. destination[prop] = [];
  274. if (sourceValue.length > 0) {
  275. if (typeof sourceValue[0] == "object") {
  276. for (var index = 0; index < sourceValue.length; index++) {
  277. var clonedValue = cloneValue(sourceValue[index], destination);
  278. if (destination[prop].indexOf(clonedValue) === -1) {
  279. destination[prop].push(clonedValue);
  280. }
  281. }
  282. } else {
  283. destination[prop] = sourceValue.slice(0);
  284. }
  285. }
  286. } else {
  287. destination[prop] = cloneValue(sourceValue, destination);
  288. }
  289. } else {
  290. destination[prop] = sourceValue;
  291. }
  292. }
  293. };
  294. Tools.IsEmpty = function (obj) {
  295. for (var i in obj) {
  296. return false;
  297. }
  298. return true;
  299. };
  300. Tools.RegisterTopRootEvents = function (events) {
  301. for (var index = 0; index < events.length; index++) {
  302. var event = events[index];
  303. window.addEventListener(event.name, event.handler, false);
  304. try {
  305. if (window.parent) {
  306. window.parent.addEventListener(event.name, event.handler, false);
  307. }
  308. } catch (e) {
  309. // Silently fails...
  310. }
  311. }
  312. };
  313. Tools.UnregisterTopRootEvents = function (events) {
  314. for (var index = 0; index < events.length; index++) {
  315. var event = events[index];
  316. window.removeEventListener(event.name, event.handler);
  317. try {
  318. if (window.parent) {
  319. window.parent.removeEventListener(event.name, event.handler);
  320. }
  321. } catch (e) {
  322. // Silently fails...
  323. }
  324. }
  325. };
  326. Tools.CreateScreenshot = function (engine, camera, size) {
  327. var width;
  328. var height;
  329. var scene = camera.getScene();
  330. var previousCamera = null;
  331. if (scene.activeCamera !== camera) {
  332. previousCamera = scene.activeCamera;
  333. scene.activeCamera = camera;
  334. }
  335. //If a precision value is specified
  336. if (size.precision) {
  337. width = Math.round(engine.getRenderWidth() * size.precision);
  338. height = Math.round(width / engine.getAspectRatio(camera));
  339. size = { width: width, height: height };
  340. } else if (size.width && size.height) {
  341. width = size.width;
  342. height = size.height;
  343. } else if (size.width && !size.height) {
  344. width = size.width;
  345. height = Math.round(width / engine.getAspectRatio(camera));
  346. size = { width: width, height: height };
  347. } else if (size.height && !size.width) {
  348. height = size.height;
  349. width = Math.round(height * engine.getAspectRatio(camera));
  350. size = { width: width, height: height };
  351. } else if (!isNaN(size)) {
  352. height = size;
  353. width = size;
  354. } else {
  355. Tools.Error("Invalid 'size' parameter !");
  356. return;
  357. }
  358. //At this point size can be a number, or an object (according to engine.prototype.createRenderTargetTexture method)
  359. var texture = new RenderTargetTexture("screenShot", size, engine.scenes[0], false, false);
  360. texture.renderList = engine.scenes[0].meshes;
  361. texture.onAfterRender = function () {
  362. // Read the contents of the framebuffer
  363. var numberOfChannelsByLine = width * 4;
  364. var halfHeight = height / 2;
  365. //Reading datas from WebGL
  366. var data = engine.readPixels(0, 0, width, height);
  367. for (var i = 0; i < halfHeight; i++) {
  368. for (var j = 0; j < numberOfChannelsByLine; j++) {
  369. var currentCell = j + i * numberOfChannelsByLine;
  370. var targetLine = height - i - 1;
  371. var targetCell = j + targetLine * numberOfChannelsByLine;
  372. var temp = data[currentCell];
  373. data[currentCell] = data[targetCell];
  374. data[targetCell] = temp;
  375. }
  376. }
  377. // Create a 2D canvas to store the result
  378. if (!screenshotCanvas) {
  379. screenshotCanvas = document.createElement('canvas');
  380. }
  381. screenshotCanvas.width = width;
  382. screenshotCanvas.height = height;
  383. var context = screenshotCanvas.getContext('2d');
  384. // Copy the pixels to a 2D canvas
  385. var imageData = context.createImageData(width, height);
  386. imageData.data.set(data);
  387. context.putImageData(imageData, 0, 0);
  388. var base64Image = screenshotCanvas.toDataURL();
  389. //Creating a link if the browser have the download attribute on the a tag, to automatically start download generated image.
  390. if (("download" in document.createElement("a"))) {
  391. var a = window.document.createElement("a");
  392. a.href = base64Image;
  393. var date = new Date();
  394. var stringDate = date.getFullYear() + "/" + date.getMonth() + "/" + date.getDate() + "-" + date.getHours() + ":" + date.getMinutes();
  395. a.setAttribute("download", "screenshot-" + stringDate + ".png");
  396. window.document.body.appendChild(a);
  397. a.addEventListener("click", function () {
  398. a.parentElement.removeChild(a);
  399. });
  400. a.click();
  401. //Or opening a new tab with the image if it is not possible to automatically start download.
  402. } else {
  403. var newWindow = window.open("");
  404. var img = newWindow.document.createElement("img");
  405. img.src = base64Image;
  406. newWindow.document.body.appendChild(img);
  407. }
  408. };
  409. texture.render(true);
  410. texture.dispose();
  411. if (previousCamera) {
  412. scene.activeCamera = previousCamera;
  413. }
  414. };
  415. // XHR response validator for local file scenario
  416. Tools.ValidateXHRData = function (xhr, dataType) {
  417. if (typeof dataType === "undefined") { dataType = 7; }
  418. try {
  419. if (dataType & 1) {
  420. if (xhr.responseText && xhr.responseText.length > 0) {
  421. return true;
  422. } else if (dataType === 1) {
  423. return false;
  424. }
  425. }
  426. if (dataType & 2) {
  427. // Check header width and height since there is no "TGA" magic number
  428. var tgaHeader = BABYLON.Internals.TGATools.GetTGAHeader(xhr.response);
  429. if (tgaHeader.width && tgaHeader.height && tgaHeader.width > 0 && tgaHeader.height > 0) {
  430. return true;
  431. } else if (dataType === 2) {
  432. return false;
  433. }
  434. }
  435. if (dataType & 4) {
  436. // Check for the "DDS" magic number
  437. var ddsHeader = new Uint8Array(xhr.response, 0, 3);
  438. if (ddsHeader[0] == 68 && ddsHeader[1] == 68 && ddsHeader[2] == 83) {
  439. return true;
  440. } else {
  441. return false;
  442. }
  443. }
  444. } catch (e) {
  445. // Global protection
  446. }
  447. return false;
  448. };
  449. Object.defineProperty(Tools, "NoneLogLevel", {
  450. get: function () {
  451. return Tools._NoneLogLevel;
  452. },
  453. enumerable: true,
  454. configurable: true
  455. });
  456. Object.defineProperty(Tools, "MessageLogLevel", {
  457. get: function () {
  458. return Tools._MessageLogLevel;
  459. },
  460. enumerable: true,
  461. configurable: true
  462. });
  463. Object.defineProperty(Tools, "WarningLogLevel", {
  464. get: function () {
  465. return Tools._WarningLogLevel;
  466. },
  467. enumerable: true,
  468. configurable: true
  469. });
  470. Object.defineProperty(Tools, "ErrorLogLevel", {
  471. get: function () {
  472. return Tools._ErrorLogLevel;
  473. },
  474. enumerable: true,
  475. configurable: true
  476. });
  477. Object.defineProperty(Tools, "AllLogLevel", {
  478. get: function () {
  479. return Tools._MessageLogLevel | Tools._WarningLogLevel | Tools._ErrorLogLevel;
  480. },
  481. enumerable: true,
  482. configurable: true
  483. });
  484. Tools._AddLogEntry = function (entry) {
  485. Tools._LogCache = entry + Tools._LogCache;
  486. if (Tools.OnNewCacheEntry) {
  487. Tools.OnNewCacheEntry(entry);
  488. }
  489. };
  490. Tools._FormatMessage = function (message) {
  491. var padStr = function (i) {
  492. return (i < 10) ? "0" + i : "" + i;
  493. };
  494. var date = new Date();
  495. return "[" + padStr(date.getHours()) + ":" + padStr(date.getMinutes()) + ":" + padStr(date.getSeconds()) + "]: " + message;
  496. };
  497. Tools._LogDisabled = function (message) {
  498. // nothing to do
  499. };
  500. Tools._LogEnabled = function (message) {
  501. var formattedMessage = Tools._FormatMessage(message);
  502. console.log("BJS - " + formattedMessage);
  503. var entry = "<div style='color:white'>" + formattedMessage + "</div><br>";
  504. Tools._AddLogEntry(entry);
  505. };
  506. Tools._WarnDisabled = function (message) {
  507. // nothing to do
  508. };
  509. Tools._WarnEnabled = function (message) {
  510. var formattedMessage = Tools._FormatMessage(message);
  511. console.warn("BJS - " + formattedMessage);
  512. var entry = "<div style='color:orange'>" + formattedMessage + "</div><br>";
  513. Tools._AddLogEntry(entry);
  514. };
  515. Tools._ErrorDisabled = function (message) {
  516. // nothing to do
  517. };
  518. Tools._ErrorEnabled = function (message) {
  519. var formattedMessage = Tools._FormatMessage(message);
  520. console.error("BJS - " + formattedMessage);
  521. var entry = "<div style='color:red'>" + formattedMessage + "</div><br>";
  522. Tools._AddLogEntry(entry);
  523. };
  524. Object.defineProperty(Tools, "LogCache", {
  525. get: function () {
  526. return Tools._LogCache;
  527. },
  528. enumerable: true,
  529. configurable: true
  530. });
  531. Object.defineProperty(Tools, "LogLevels", {
  532. set: function (level) {
  533. if ((level & Tools.MessageLogLevel) === Tools.MessageLogLevel) {
  534. Tools.Log = Tools._LogEnabled;
  535. } else {
  536. Tools.Log = Tools._LogDisabled;
  537. }
  538. if ((level & Tools.WarningLogLevel) === Tools.WarningLogLevel) {
  539. Tools.Warn = Tools._WarnEnabled;
  540. } else {
  541. Tools.Warn = Tools._WarnDisabled;
  542. }
  543. if ((level & Tools.ErrorLogLevel) === Tools.ErrorLogLevel) {
  544. Tools.Error = Tools._ErrorEnabled;
  545. } else {
  546. Tools.Error = Tools._ErrorDisabled;
  547. }
  548. },
  549. enumerable: true,
  550. configurable: true
  551. });
  552. Object.defineProperty(Tools, "PerformanceNoneLogLevel", {
  553. get: function () {
  554. return Tools._PerformanceNoneLogLevel;
  555. },
  556. enumerable: true,
  557. configurable: true
  558. });
  559. Object.defineProperty(Tools, "PerformanceUserMarkLogLevel", {
  560. get: function () {
  561. return Tools._PerformanceUserMarkLogLevel;
  562. },
  563. enumerable: true,
  564. configurable: true
  565. });
  566. Object.defineProperty(Tools, "PerformanceConsoleLogLevel", {
  567. get: function () {
  568. return Tools._PerformanceConsoleLogLevel;
  569. },
  570. enumerable: true,
  571. configurable: true
  572. });
  573. Object.defineProperty(Tools, "PerformanceLogLevel", {
  574. set: function (level) {
  575. if ((level & Tools.PerformanceUserMarkLogLevel) === Tools.PerformanceUserMarkLogLevel) {
  576. Tools.StartPerformanceCounter = Tools._StartUserMark;
  577. Tools.EndPerformanceCounter = Tools._EndUserMark;
  578. return;
  579. }
  580. if ((level & Tools.PerformanceConsoleLogLevel) === Tools.PerformanceConsoleLogLevel) {
  581. Tools.StartPerformanceCounter = Tools._StartPerformanceConsole;
  582. Tools.EndPerformanceCounter = Tools._EndPerformanceConsole;
  583. return;
  584. }
  585. Tools.StartPerformanceCounter = Tools._StartPerformanceCounterDisabled;
  586. Tools.EndPerformanceCounter = Tools._EndPerformanceCounterDisabled;
  587. },
  588. enumerable: true,
  589. configurable: true
  590. });
  591. Tools._StartPerformanceCounterDisabled = function (counterName, condition) {
  592. };
  593. Tools._EndPerformanceCounterDisabled = function (counterName, condition) {
  594. };
  595. Tools._StartUserMark = function (counterName, condition) {
  596. if (typeof condition === "undefined") { condition = true; }
  597. if (!condition || !Tools._performance.mark) {
  598. return;
  599. }
  600. Tools._performance.mark(counterName + "-Begin");
  601. };
  602. Tools._EndUserMark = function (counterName, condition) {
  603. if (typeof condition === "undefined") { condition = true; }
  604. if (!condition || !Tools._performance.mark) {
  605. return;
  606. }
  607. Tools._performance.mark(counterName + "-End");
  608. Tools._performance.measure(counterName, counterName + "-Begin", counterName + "-End");
  609. };
  610. Tools._StartPerformanceConsole = function (counterName, condition) {
  611. if (typeof condition === "undefined") { condition = true; }
  612. if (!condition) {
  613. return;
  614. }
  615. Tools._StartUserMark(counterName, condition);
  616. if (console.time) {
  617. console.time(counterName);
  618. }
  619. };
  620. Tools._EndPerformanceConsole = function (counterName, condition) {
  621. if (typeof condition === "undefined") { condition = true; }
  622. if (!condition) {
  623. return;
  624. }
  625. Tools._EndUserMark(counterName, condition);
  626. if (console.time) {
  627. console.timeEnd(counterName);
  628. }
  629. };
  630. Object.defineProperty(Tools, "Now", {
  631. get: function () {
  632. if (window.performance && window.performance.now) {
  633. return window.performance.now();
  634. }
  635. return new Date().getTime();
  636. },
  637. enumerable: true,
  638. configurable: true
  639. });
  640. // Deprecated
  641. Tools.GetFps = function () {
  642. Tools.Warn("Tools.GetFps() is deprecated. Please use engine.getFps() instead");
  643. return 0;
  644. };
  645. Tools.BaseUrl = "";
  646. Tools.GetExponantOfTwo = function (value, max) {
  647. var count = 1;
  648. do {
  649. count *= 2;
  650. } while(count < value);
  651. if (count > max)
  652. count = max;
  653. return count;
  654. };
  655. Tools._NoneLogLevel = 0;
  656. Tools._MessageLogLevel = 1;
  657. Tools._WarningLogLevel = 2;
  658. Tools._ErrorLogLevel = 4;
  659. Tools._LogCache = "";
  660. Tools.Log = Tools._LogEnabled;
  661. Tools.Warn = Tools._WarnEnabled;
  662. Tools.Error = Tools._ErrorEnabled;
  663. Tools._PerformanceNoneLogLevel = 0;
  664. Tools._PerformanceUserMarkLogLevel = 1;
  665. Tools._PerformanceConsoleLogLevel = 2;
  666. Tools._performance = window.performance;
  667. Tools.StartPerformanceCounter = Tools._StartPerformanceCounterDisabled;
  668. Tools.EndPerformanceCounter = Tools._EndPerformanceCounterDisabled;
  669. return Tools;
  670. })();
  671. BABYLON.Tools = Tools;
  672. })(BABYLON || (BABYLON = {}));