babylon.tools.js 28 KB

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