babylon.tools.js 30 KB

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