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 BABYLON.Mesh) {
  9. return null;
  10. }
  11. if (source instanceof BABYLON.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 BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  46. var maximum = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  47. for (var index = indexStart; index < indexStart + indexCount; index++) {
  48. var current = new BABYLON.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 BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  59. var maximum = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  60. for (var index = start; index < start + count; index++) {
  61. var current = new BABYLON.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) {
  254. var num = a - b;
  255. return -1.401298E-45 <= num && num <= 1.401298E-45;
  256. };
  257. Tools.DeepCopy = function (source, destination, doNotCopyList, mustCopyList) {
  258. for (var prop in source) {
  259. if (prop[0] === "_" && (!mustCopyList || mustCopyList.indexOf(prop) === -1)) {
  260. continue;
  261. }
  262. if (doNotCopyList && doNotCopyList.indexOf(prop) !== -1) {
  263. continue;
  264. }
  265. var sourceValue = source[prop];
  266. var typeOfSourceValue = typeof sourceValue;
  267. if (typeOfSourceValue == "function") {
  268. continue;
  269. }
  270. if (typeOfSourceValue == "object") {
  271. if (sourceValue instanceof Array) {
  272. destination[prop] = [];
  273. if (sourceValue.length > 0) {
  274. if (typeof sourceValue[0] == "object") {
  275. for (var index = 0; index < sourceValue.length; index++) {
  276. var clonedValue = cloneValue(sourceValue[index], destination);
  277. if (destination[prop].indexOf(clonedValue) === -1) {
  278. destination[prop].push(clonedValue);
  279. }
  280. }
  281. } else {
  282. destination[prop] = sourceValue.slice(0);
  283. }
  284. }
  285. } else {
  286. destination[prop] = cloneValue(sourceValue, destination);
  287. }
  288. } else {
  289. destination[prop] = sourceValue;
  290. }
  291. }
  292. };
  293. Tools.IsEmpty = function (obj) {
  294. for (var i in obj) {
  295. return false;
  296. }
  297. return true;
  298. };
  299. Tools.RegisterTopRootEvents = function (events) {
  300. for (var index = 0; index < events.length; index++) {
  301. var event = events[index];
  302. window.addEventListener(event.name, event.handler, false);
  303. try {
  304. if (window.parent) {
  305. window.parent.addEventListener(event.name, event.handler, false);
  306. }
  307. } catch (e) {
  308. // Silently fails...
  309. }
  310. }
  311. };
  312. Tools.UnregisterTopRootEvents = function (events) {
  313. for (var index = 0; index < events.length; index++) {
  314. var event = events[index];
  315. window.removeEventListener(event.name, event.handler);
  316. try {
  317. if (window.parent) {
  318. window.parent.removeEventListener(event.name, event.handler);
  319. }
  320. } catch (e) {
  321. // Silently fails...
  322. }
  323. }
  324. };
  325. Tools.CreateScreenshot = function (engine, camera, size) {
  326. var width;
  327. var height;
  328. var scene = camera.getScene();
  329. var previousCamera = null;
  330. if (scene.activeCamera !== camera) {
  331. previousCamera = scene.activeCamera;
  332. scene.activeCamera = camera;
  333. }
  334. //If a precision value is specified
  335. if (size.precision) {
  336. width = Math.round(engine.getRenderWidth() * size.precision);
  337. height = Math.round(width / engine.getAspectRatio(camera));
  338. size = { width: width, height: height };
  339. } else if (size.width && size.height) {
  340. width = size.width;
  341. height = size.height;
  342. } else if (size.width && !size.height) {
  343. width = size.width;
  344. height = Math.round(width / engine.getAspectRatio(camera));
  345. size = { width: width, height: height };
  346. } else if (size.height && !size.width) {
  347. height = size.height;
  348. width = Math.round(height * engine.getAspectRatio(camera));
  349. size = { width: width, height: height };
  350. } else if (!isNaN(size)) {
  351. height = size;
  352. width = size;
  353. } else {
  354. Tools.Error("Invalid 'size' parameter !");
  355. return;
  356. }
  357. //At this point size can be a number, or an object (according to engine.prototype.createRenderTargetTexture method)
  358. var texture = new BABYLON.RenderTargetTexture("screenShot", size, engine.scenes[0], false, false);
  359. texture.renderList = engine.scenes[0].meshes;
  360. texture.onAfterRender = function () {
  361. // Read the contents of the framebuffer
  362. var numberOfChannelsByLine = width * 4;
  363. var halfHeight = height / 2;
  364. //Reading datas from WebGL
  365. var data = engine.readPixels(0, 0, width, height);
  366. for (var i = 0; i < halfHeight; i++) {
  367. for (var j = 0; j < numberOfChannelsByLine; j++) {
  368. var currentCell = j + i * numberOfChannelsByLine;
  369. var targetLine = height - i - 1;
  370. var targetCell = j + targetLine * numberOfChannelsByLine;
  371. var temp = data[currentCell];
  372. data[currentCell] = data[targetCell];
  373. data[targetCell] = temp;
  374. }
  375. }
  376. // Create a 2D canvas to store the result
  377. if (!screenshotCanvas) {
  378. screenshotCanvas = document.createElement('canvas');
  379. }
  380. screenshotCanvas.width = width;
  381. screenshotCanvas.height = height;
  382. var context = screenshotCanvas.getContext('2d');
  383. // Copy the pixels to a 2D canvas
  384. var imageData = context.createImageData(width, height);
  385. imageData.data.set(data);
  386. context.putImageData(imageData, 0, 0);
  387. var base64Image = screenshotCanvas.toDataURL();
  388. //Creating a link if the browser have the download attribute on the a tag, to automatically start download generated image.
  389. if (("download" in document.createElement("a"))) {
  390. var a = window.document.createElement("a");
  391. a.href = base64Image;
  392. var date = new Date();
  393. var stringDate = date.getFullYear() + "/" + date.getMonth() + "/" + date.getDate() + "-" + date.getHours() + ":" + date.getMinutes();
  394. a.setAttribute("download", "screenshot-" + stringDate + ".png");
  395. window.document.body.appendChild(a);
  396. a.addEventListener("click", function () {
  397. a.parentElement.removeChild(a);
  398. });
  399. a.click();
  400. //Or opening a new tab with the image if it is not possible to automatically start download.
  401. } else {
  402. var newWindow = window.open("");
  403. var img = newWindow.document.createElement("img");
  404. img.src = base64Image;
  405. newWindow.document.body.appendChild(img);
  406. }
  407. };
  408. texture.render(true);
  409. texture.dispose();
  410. if (previousCamera) {
  411. scene.activeCamera = previousCamera;
  412. }
  413. };
  414. // XHR response validator for local file scenario
  415. Tools.ValidateXHRData = function (xhr, dataType) {
  416. if (typeof dataType === "undefined") { dataType = 7; }
  417. try {
  418. if (dataType & 1) {
  419. if (xhr.responseText && xhr.responseText.length > 0) {
  420. return true;
  421. } else if (dataType === 1) {
  422. return false;
  423. }
  424. }
  425. if (dataType & 2) {
  426. // Check header width and height since there is no "TGA" magic number
  427. var tgaHeader = BABYLON.Internals.TGATools.GetTGAHeader(xhr.response);
  428. if (tgaHeader.width && tgaHeader.height && tgaHeader.width > 0 && tgaHeader.height > 0) {
  429. return true;
  430. } else if (dataType === 2) {
  431. return false;
  432. }
  433. }
  434. if (dataType & 4) {
  435. // Check for the "DDS" magic number
  436. var ddsHeader = new Uint8Array(xhr.response, 0, 3);
  437. if (ddsHeader[0] == 68 && ddsHeader[1] == 68 && ddsHeader[2] == 83) {
  438. return true;
  439. } else {
  440. return false;
  441. }
  442. }
  443. } catch (e) {
  444. // Global protection
  445. }
  446. return false;
  447. };
  448. Object.defineProperty(Tools, "NoneLogLevel", {
  449. get: function () {
  450. return Tools._NoneLogLevel;
  451. },
  452. enumerable: true,
  453. configurable: true
  454. });
  455. Object.defineProperty(Tools, "MessageLogLevel", {
  456. get: function () {
  457. return Tools._MessageLogLevel;
  458. },
  459. enumerable: true,
  460. configurable: true
  461. });
  462. Object.defineProperty(Tools, "WarningLogLevel", {
  463. get: function () {
  464. return Tools._WarningLogLevel;
  465. },
  466. enumerable: true,
  467. configurable: true
  468. });
  469. Object.defineProperty(Tools, "ErrorLogLevel", {
  470. get: function () {
  471. return Tools._ErrorLogLevel;
  472. },
  473. enumerable: true,
  474. configurable: true
  475. });
  476. Object.defineProperty(Tools, "AllLogLevel", {
  477. get: function () {
  478. return Tools._MessageLogLevel | Tools._WarningLogLevel | Tools._ErrorLogLevel;
  479. },
  480. enumerable: true,
  481. configurable: true
  482. });
  483. Tools._AddLogEntry = function (entry) {
  484. Tools._LogCache = entry + Tools._LogCache;
  485. if (Tools.OnNewCacheEntry) {
  486. Tools.OnNewCacheEntry(entry);
  487. }
  488. };
  489. Tools._FormatMessage = function (message) {
  490. var padStr = function (i) {
  491. return (i < 10) ? "0" + i : "" + i;
  492. };
  493. var date = new Date();
  494. return "[" + padStr(date.getHours()) + ":" + padStr(date.getMinutes()) + ":" + padStr(date.getSeconds()) + "]: " + message;
  495. };
  496. Tools._LogDisabled = function (message) {
  497. // nothing to do
  498. };
  499. Tools._LogEnabled = function (message) {
  500. var formattedMessage = Tools._FormatMessage(message);
  501. console.log("BJS - " + formattedMessage);
  502. var entry = "<div style='color:white'>" + formattedMessage + "</div><br>";
  503. Tools._AddLogEntry(entry);
  504. };
  505. Tools._WarnDisabled = function (message) {
  506. // nothing to do
  507. };
  508. Tools._WarnEnabled = function (message) {
  509. var formattedMessage = Tools._FormatMessage(message);
  510. console.warn("BJS - " + formattedMessage);
  511. var entry = "<div style='color:orange'>" + formattedMessage + "</div><br>";
  512. Tools._AddLogEntry(entry);
  513. };
  514. Tools._ErrorDisabled = function (message) {
  515. // nothing to do
  516. };
  517. Tools._ErrorEnabled = function (message) {
  518. var formattedMessage = Tools._FormatMessage(message);
  519. console.error("BJS - " + formattedMessage);
  520. var entry = "<div style='color:red'>" + formattedMessage + "</div><br>";
  521. Tools._AddLogEntry(entry);
  522. };
  523. Object.defineProperty(Tools, "LogCache", {
  524. get: function () {
  525. return Tools._LogCache;
  526. },
  527. enumerable: true,
  528. configurable: true
  529. });
  530. Object.defineProperty(Tools, "LogLevels", {
  531. set: function (level) {
  532. if ((level & Tools.MessageLogLevel) === Tools.MessageLogLevel) {
  533. Tools.Log = Tools._LogEnabled;
  534. } else {
  535. Tools.Log = Tools._LogDisabled;
  536. }
  537. if ((level & Tools.WarningLogLevel) === Tools.WarningLogLevel) {
  538. Tools.Warn = Tools._WarnEnabled;
  539. } else {
  540. Tools.Warn = Tools._WarnDisabled;
  541. }
  542. if ((level & Tools.ErrorLogLevel) === Tools.ErrorLogLevel) {
  543. Tools.Error = Tools._ErrorEnabled;
  544. } else {
  545. Tools.Error = Tools._ErrorDisabled;
  546. }
  547. },
  548. enumerable: true,
  549. configurable: true
  550. });
  551. Object.defineProperty(Tools, "PerformanceNoneLogLevel", {
  552. get: function () {
  553. return Tools._PerformanceNoneLogLevel;
  554. },
  555. enumerable: true,
  556. configurable: true
  557. });
  558. Object.defineProperty(Tools, "PerformanceUserMarkLogLevel", {
  559. get: function () {
  560. return Tools._PerformanceUserMarkLogLevel;
  561. },
  562. enumerable: true,
  563. configurable: true
  564. });
  565. Object.defineProperty(Tools, "PerformanceConsoleLogLevel", {
  566. get: function () {
  567. return Tools._PerformanceConsoleLogLevel;
  568. },
  569. enumerable: true,
  570. configurable: true
  571. });
  572. Object.defineProperty(Tools, "PerformanceLogLevel", {
  573. set: function (level) {
  574. if ((level & Tools.PerformanceUserMarkLogLevel) === Tools.PerformanceUserMarkLogLevel) {
  575. Tools.StartPerformanceCounter = Tools._StartUserMark;
  576. Tools.EndPerformanceCounter = Tools._EndUserMark;
  577. return;
  578. }
  579. if ((level & Tools.PerformanceConsoleLogLevel) === Tools.PerformanceConsoleLogLevel) {
  580. Tools.StartPerformanceCounter = Tools._StartPerformanceConsole;
  581. Tools.EndPerformanceCounter = Tools._EndPerformanceConsole;
  582. return;
  583. }
  584. Tools.StartPerformanceCounter = Tools._StartPerformanceCounterDisabled;
  585. Tools.EndPerformanceCounter = Tools._EndPerformanceCounterDisabled;
  586. },
  587. enumerable: true,
  588. configurable: true
  589. });
  590. Tools._StartPerformanceCounterDisabled = function (counterName, condition) {
  591. };
  592. Tools._EndPerformanceCounterDisabled = function (counterName, condition) {
  593. };
  594. Tools._StartUserMark = function (counterName, condition) {
  595. if (typeof condition === "undefined") { condition = true; }
  596. if (!condition || !Tools._performance.mark) {
  597. return;
  598. }
  599. Tools._performance.mark(counterName + "-Begin");
  600. };
  601. Tools._EndUserMark = function (counterName, condition) {
  602. if (typeof condition === "undefined") { condition = true; }
  603. if (!condition || !Tools._performance.mark) {
  604. return;
  605. }
  606. Tools._performance.mark(counterName + "-End");
  607. Tools._performance.measure(counterName, counterName + "-Begin", counterName + "-End");
  608. };
  609. Tools._StartPerformanceConsole = function (counterName, condition) {
  610. if (typeof condition === "undefined") { condition = true; }
  611. if (!condition) {
  612. return;
  613. }
  614. Tools._StartUserMark(counterName, condition);
  615. if (console.time) {
  616. console.time(counterName);
  617. }
  618. };
  619. Tools._EndPerformanceConsole = function (counterName, condition) {
  620. if (typeof condition === "undefined") { condition = true; }
  621. if (!condition) {
  622. return;
  623. }
  624. Tools._EndUserMark(counterName, condition);
  625. if (console.time) {
  626. console.timeEnd(counterName);
  627. }
  628. };
  629. Object.defineProperty(Tools, "Now", {
  630. get: function () {
  631. if (window.performance && window.performance.now) {
  632. return window.performance.now();
  633. }
  634. return new Date().getTime();
  635. },
  636. enumerable: true,
  637. configurable: true
  638. });
  639. // Deprecated
  640. Tools.GetFps = function () {
  641. Tools.Warn("Tools.GetFps() is deprecated. Please use engine.getFps() instead");
  642. return 0;
  643. };
  644. Tools.BaseUrl = "";
  645. Tools.GetExponantOfTwo = function (value, max) {
  646. var count = 1;
  647. do {
  648. count *= 2;
  649. } while(count < value);
  650. if (count > max)
  651. count = max;
  652. return count;
  653. };
  654. Tools._NoneLogLevel = 0;
  655. Tools._MessageLogLevel = 1;
  656. Tools._WarningLogLevel = 2;
  657. Tools._ErrorLogLevel = 4;
  658. Tools._LogCache = "";
  659. Tools.Log = Tools._LogEnabled;
  660. Tools.Warn = Tools._WarnEnabled;
  661. Tools.Error = Tools._ErrorEnabled;
  662. Tools._PerformanceNoneLogLevel = 0;
  663. Tools._PerformanceUserMarkLogLevel = 1;
  664. Tools._PerformanceConsoleLogLevel = 2;
  665. Tools._performance = window.performance;
  666. Tools.StartPerformanceCounter = Tools._StartPerformanceCounterDisabled;
  667. Tools.EndPerformanceCounter = Tools._EndPerformanceCounterDisabled;
  668. return Tools;
  669. })();
  670. BABYLON.Tools = Tools;
  671. })(BABYLON || (BABYLON = {}));
  672. //# sourceMappingURL=babylon.tools.js.map