babylon.tools.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. "use strict";
  2. var BABYLON = BABYLON || {};
  3. (function () {
  4. BABYLON.Tools = BABYLON.Tools || {};
  5. BABYLON.Tools.GetFilename = function (path) {
  6. var index = path.lastIndexOf("/");
  7. if (index < 0)
  8. return path;
  9. return path.substring(index + 1);
  10. };
  11. BABYLON.Tools.GetDOMTextContent = function(element) {
  12. var result = "";
  13. var child = element.firstChild;
  14. while (child) {
  15. if (child.nodeType == 3) {
  16. result += child.textContent;
  17. }
  18. child = child.nextSibling;
  19. }
  20. return result;
  21. };
  22. BABYLON.Tools.ToDegrees = function(angle) {
  23. return angle * 180 / Math.PI;
  24. };
  25. BABYLON.Tools.ToRadians = function(angle) {
  26. return angle * Math.PI / 180;
  27. };
  28. BABYLON.Tools.ExtractMinAndMax = function (positions, start, count) {
  29. var minimum = new BABYLON.Vector3(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE);
  30. var maximum = new BABYLON.Vector3(-Number.MAX_VALUE, -Number.MAX_VALUE, -Number.MAX_VALUE);
  31. for (var index = start; index < start + count; index++) {
  32. var current = new BABYLON.Vector3(positions[index * 3], positions[index * 3 + 1], positions[index * 3 + 2]);
  33. minimum = BABYLON.Vector3.Minimize(current, minimum);
  34. maximum = BABYLON.Vector3.Maximize(current, maximum);
  35. }
  36. return {
  37. minimum: minimum,
  38. maximum: maximum
  39. };
  40. };
  41. BABYLON.Tools.MakeArray = function (obj, allowsNullUndefined) {
  42. if (allowsNullUndefined !== true && (obj === undefined || obj == null))
  43. return undefined;
  44. return Array.isArray(obj) ? obj : [obj];
  45. };
  46. // Smart array
  47. BABYLON.Tools.SmartArray = function(capacity) {
  48. this.data = new Array(capacity);
  49. this.length = 0;
  50. };
  51. BABYLON.Tools.SmartArray.prototype.push = function(value) {
  52. this.data[this.length++] = value;
  53. if (this.length > this.data.length) {
  54. this.data.length *= 2;
  55. }
  56. };
  57. BABYLON.Tools.SmartArray.prototype.pushNoDuplicate = function(value) {
  58. if (this.indexOf(value) > -1) {
  59. return;
  60. }
  61. this.push(value);
  62. };
  63. BABYLON.Tools.SmartArray.prototype.sort = function (compareFn) {
  64. this.data.sort(compareFn);
  65. };
  66. BABYLON.Tools.SmartArray.prototype.reset = function() {
  67. this.length = 0;
  68. };
  69. BABYLON.Tools.SmartArray.prototype.concat = function (array) {
  70. if (array.length === 0) {
  71. return;
  72. }
  73. if (this.length + array.length > this.data.length) {
  74. this.data.length = (this.length + array.length) * 2;
  75. }
  76. for (var index = 0; index < array.length; index++) {
  77. this.data[this.length++] = (array.data || array)[index];
  78. }
  79. };
  80. BABYLON.Tools.SmartArray.prototype.concatWithNoDuplicate = function (array) {
  81. if (array.length === 0) {
  82. return;
  83. }
  84. if (this.length + array.length > this.data.length) {
  85. this.data.length = (this.length + array.length) * 2;
  86. }
  87. for (var index = 0; index < array.length; index++) {
  88. var item = (array.data || array)[index];
  89. var pos = this.data.indexOf(item);
  90. if (pos === -1 ||pos >= this.length) {
  91. this.data[this.length++] = item;
  92. }
  93. }
  94. };
  95. BABYLON.Tools.SmartArray.prototype.indexOf = function (value) {
  96. var position = this.data.indexOf(value);
  97. if (position >= this.length) {
  98. return -1;
  99. }
  100. return position;
  101. };
  102. // Misc.
  103. BABYLON.Tools.GetPointerPrefix = function() {
  104. var eventPrefix = "pointer";
  105. // Check if hand.js is referenced or if the browser natively supports pointer events
  106. if (!navigator.pointerEnabled) {
  107. eventPrefix = "mouse";
  108. }
  109. return eventPrefix;
  110. };
  111. BABYLON.Tools.QueueNewFrame = function (func) {
  112. if (window.requestAnimationFrame)
  113. window.requestAnimationFrame(func);
  114. else if (window.msRequestAnimationFrame)
  115. window.msRequestAnimationFrame(func);
  116. else if (window.webkitRequestAnimationFrame)
  117. window.webkitRequestAnimationFrame(func);
  118. else if (window.mozRequestAnimationFrame)
  119. window.mozRequestAnimationFrame(func);
  120. else if (window.oRequestAnimationFrame)
  121. window.oRequestAnimationFrame(func);
  122. else {
  123. window.setTimeout(func, 16);
  124. }
  125. };
  126. BABYLON.Tools.RequestFullscreen = function (element) {
  127. if (element.requestFullscreen)
  128. element.requestFullscreen();
  129. else if (element.msRequestFullscreen)
  130. element.msRequestFullscreen();
  131. else if (element.webkitRequestFullscreen)
  132. element.webkitRequestFullscreen();
  133. else if (element.mozRequestFullScreen)
  134. element.mozRequestFullScreen();
  135. };
  136. BABYLON.Tools.ExitFullscreen = function () {
  137. if (document.exitFullscreen) {
  138. document.exitFullscreen();
  139. }
  140. else if (document.mozCancelFullScreen) {
  141. document.mozCancelFullScreen();
  142. }
  143. else if (document.webkitCancelFullScreen) {
  144. document.webkitCancelFullScreen();
  145. }
  146. else if (document.msCancelFullScreen) {
  147. document.msCancelFullScreen();
  148. }
  149. };
  150. // External files
  151. BABYLON.Tools.BaseUrl = "";
  152. BABYLON.Tools.LoadImage = function (url, onload, onerror, database) {
  153. var img = new Image();
  154. img.crossOrigin = 'anonymous';
  155. img.onload = function () {
  156. onload(img);
  157. };
  158. img.onerror = function (err) {
  159. onerror(img, err);
  160. };
  161. var noIndexedDB = function () {
  162. img.src = url;
  163. };
  164. var loadFromIndexedDB = function () {
  165. database.loadImageFromDB(url, img);
  166. };
  167. if (database && database.enableTexturesOffline && BABYLON.Database.isUASupportingBlobStorage) {
  168. database.openAsync(loadFromIndexedDB, noIndexedDB);
  169. }
  170. else {
  171. if (url.indexOf("file:") === -1) {
  172. noIndexedDB();
  173. }
  174. else {
  175. try {
  176. var textureName = url.substring(5);
  177. var blobURL;
  178. try {
  179. blobURL = URL.createObjectURL(BABYLON.FilesTextures[textureName], { oneTimeOnly: true });
  180. }
  181. catch (ex) {
  182. // Chrome doesn't support oneTimeOnly parameter
  183. blobURL = URL.createObjectURL(BABYLON.FilesTextures[textureName]);
  184. }
  185. img.src = blobURL;
  186. }
  187. catch (e) {
  188. console.log("Error while trying to load texture: " + textureName);
  189. img.src = null;
  190. }
  191. }
  192. }
  193. return img;
  194. };
  195. BABYLON.Tools.LoadFile = function (url, callback, progressCallBack, database, useArrayBuffer) {
  196. var noIndexedDB = function () {
  197. var request = new XMLHttpRequest();
  198. var loadUrl = BABYLON.Tools.BaseUrl + url;
  199. request.open('GET', loadUrl, true);
  200. if (useArrayBuffer) {
  201. request.responseType = "arraybuffer";
  202. }
  203. request.onprogress = progressCallBack;
  204. request.onreadystatechange = function () {
  205. if (request.readyState == 4) {
  206. if (request.status == 200) {
  207. callback(!useArrayBuffer ? request.responseText : request.response);
  208. } else { // Failed
  209. throw new Error(request.status, "Unable to load " + loadUrl);
  210. }
  211. }
  212. };
  213. request.send(null);
  214. };
  215. var loadFromIndexedDB = function () {
  216. database.loadSceneFromDB(url, callback, progressCallBack, noIndexedDB);
  217. };
  218. // Caching only scenes files
  219. if (database && url.indexOf(".babylon") !== -1 && (database.enableSceneOffline)) {
  220. database.openAsync(loadFromIndexedDB, noIndexedDB);
  221. }
  222. else {
  223. noIndexedDB();
  224. }
  225. };
  226. BABYLON.Tools.ReadFile = function (fileToLoad, callback, progressCallBack) {
  227. var reader = new FileReader();
  228. reader.onload = function (e) {
  229. callback(e.target.result);
  230. };
  231. reader.onprogress = progressCallBack;
  232. // Asynchronous read
  233. reader.readAsText(fileToLoad);
  234. };
  235. // Misc.
  236. BABYLON.Tools.WithinEpsilon = function (a, b) {
  237. var num = a - b;
  238. return -1.401298E-45 <= num && num <= 1.401298E-45;
  239. };
  240. var cloneValue = function (source, destinationObject) {
  241. if (!source)
  242. return null;
  243. if (source instanceof BABYLON.Mesh) {
  244. return null;
  245. }
  246. if (source instanceof BABYLON.SubMesh) {
  247. return source.clone(destinationObject);
  248. } else if (source.clone) {
  249. return source.clone();
  250. }
  251. return null;
  252. };
  253. BABYLON.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) { // Test if auto inject was not done
  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. BABYLON.Tools.IsEmpty = function (obj) {
  290. for (var i in obj) {
  291. return false;
  292. }
  293. return true;
  294. };
  295. // FPS
  296. var fpsRange = 60;
  297. var previousFramesDuration = [];
  298. var fps = 60;
  299. var deltaTime = 0;
  300. BABYLON.Tools.GetFps = function () {
  301. return fps;
  302. };
  303. BABYLON.Tools.GetDeltaTime = function () {
  304. return deltaTime;
  305. };
  306. BABYLON.Tools._MeasureFps = function () {
  307. previousFramesDuration.push((new Date).getTime());
  308. var length = previousFramesDuration.length;
  309. if (length >= 2) {
  310. deltaTime = previousFramesDuration[length - 1] - previousFramesDuration[length - 2];
  311. }
  312. if (length >= fpsRange) {
  313. if (length > fpsRange) {
  314. previousFramesDuration.splice(0, 1);
  315. length = previousFramesDuration.length;
  316. }
  317. var sum = 0;
  318. for (var id = 0; id < length - 1; id++) {
  319. sum += previousFramesDuration[id + 1] - previousFramesDuration[id];
  320. }
  321. fps = 1000.0 / (sum / (length - 1));
  322. }
  323. };
  324. })();