babylon.tools.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. // Smart array
  42. BABYLON.Tools.SmartArray = function(capacity) {
  43. this.data = new Array(capacity);
  44. this.length = 0;
  45. };
  46. BABYLON.Tools.SmartArray.prototype.push = function(value) {
  47. this.data[this.length++] = value;
  48. if (this.length > this.data.length) {
  49. this.data.length *= 2;
  50. }
  51. };
  52. BABYLON.Tools.SmartArray.prototype.pushNoDuplicate = function(value) {
  53. if (this.indexOf(value) > -1) {
  54. return;
  55. }
  56. this.push(value);
  57. };
  58. BABYLON.Tools.SmartArray.prototype.sort = function (compareFn) {
  59. this.data.sort(compareFn);
  60. };
  61. BABYLON.Tools.SmartArray.prototype.reset = function() {
  62. this.length = 0;
  63. };
  64. BABYLON.Tools.SmartArray.prototype.concat = function (array) {
  65. if (array.length === 0) {
  66. return;
  67. }
  68. if (this.length + array.length > this.data.length) {
  69. this.data.length = (this.length + array.length) * 2;
  70. }
  71. for (var index = 0; index < array.length; index++) {
  72. this.data[this.length++] = (array.data || array)[index];
  73. }
  74. };
  75. BABYLON.Tools.SmartArray.prototype.concatWithNoDuplicate = function (array) {
  76. if (array.length === 0) {
  77. return;
  78. }
  79. if (this.length + array.length > this.data.length) {
  80. this.data.length = (this.length + array.length) * 2;
  81. }
  82. for (var index = 0; index < array.length; index++) {
  83. var item = (array.data || array)[index];
  84. var pos = this.data.indexOf(item);
  85. if (pos === -1 ||pos >= this.length) {
  86. this.data[this.length++] = item;
  87. }
  88. }
  89. };
  90. BABYLON.Tools.SmartArray.prototype.indexOf = function (value) {
  91. var position = this.data.indexOf(value);
  92. if (position >= this.length) {
  93. return -1;
  94. }
  95. return position;
  96. };
  97. // Misc.
  98. BABYLON.Tools.GetPointerPrefix = function() {
  99. var eventPrefix = "pointer";
  100. // Check if hand.js is referenced or if the browser natively supports pointer events
  101. if (!navigator.pointerEnabled) {
  102. eventPrefix = "mouse";
  103. }
  104. return eventPrefix;
  105. };
  106. BABYLON.Tools.QueueNewFrame = function (func) {
  107. if (window.requestAnimationFrame)
  108. window.requestAnimationFrame(func);
  109. else if (window.msRequestAnimationFrame)
  110. window.msRequestAnimationFrame(func);
  111. else if (window.webkitRequestAnimationFrame)
  112. window.webkitRequestAnimationFrame(func);
  113. else if (window.mozRequestAnimationFrame)
  114. window.mozRequestAnimationFrame(func);
  115. else if (window.oRequestAnimationFrame)
  116. window.oRequestAnimationFrame(func);
  117. else {
  118. window.setTimeout(func, 16);
  119. }
  120. };
  121. BABYLON.Tools.RequestFullscreen = function (element) {
  122. if (element.requestFullscreen)
  123. element.requestFullscreen();
  124. else if (element.msRequestFullscreen)
  125. element.msRequestFullscreen();
  126. else if (element.webkitRequestFullscreen)
  127. element.webkitRequestFullscreen();
  128. else if (element.mozRequestFullScreen)
  129. element.mozRequestFullScreen();
  130. };
  131. BABYLON.Tools.ExitFullscreen = function () {
  132. if (document.exitFullscreen) {
  133. document.exitFullscreen();
  134. }
  135. else if (document.mozCancelFullScreen) {
  136. document.mozCancelFullScreen();
  137. }
  138. else if (document.webkitCancelFullScreen) {
  139. document.webkitCancelFullScreen();
  140. }
  141. else if (document.msCancelFullScreen) {
  142. document.msCancelFullScreen();
  143. }
  144. };
  145. // External files
  146. BABYLON.Tools.BaseUrl = "";
  147. BABYLON.Tools.LoadImage = function (url, onload, onerror, database) {
  148. var img = new Image();
  149. img.crossOrigin = 'anonymous';
  150. img.onload = function () {
  151. onload(img);
  152. };
  153. img.onerror = function (err) {
  154. onerror(img, err);
  155. };
  156. var noIndexedDB = function () {
  157. img.src = url;
  158. };
  159. var loadFromIndexedDB = function () {
  160. database.loadImageFromDB(url, img);
  161. };
  162. if (database && database.enableTexturesOffline && BABYLON.Database.isUASupportingBlobStorage) {
  163. database.openAsync(loadFromIndexedDB, noIndexedDB);
  164. }
  165. else {
  166. if (url.indexOf("file:") === -1) {
  167. noIndexedDB();
  168. }
  169. else {
  170. try {
  171. var textureName = url.substring(5);
  172. var blobURL;
  173. try {
  174. blobURL = URL.createObjectURL(BABYLON.FilesTextures[textureName], { oneTimeOnly: true });
  175. }
  176. catch (ex) {
  177. // Chrome doesn't support oneTimeOnly parameter
  178. blobURL = URL.createObjectURL(BABYLON.FilesTextures[textureName]);
  179. }
  180. img.src = blobURL;
  181. }
  182. catch (e) {
  183. console.log("Error while trying to load texture: " + textureName);
  184. img.src = null;
  185. }
  186. }
  187. }
  188. return img;
  189. };
  190. BABYLON.Tools.LoadFile = function (url, callback, progressCallBack, database, useArrayBuffer) {
  191. var noIndexedDB = function () {
  192. var request = new XMLHttpRequest();
  193. var loadUrl = BABYLON.Tools.BaseUrl + url;
  194. request.open('GET', loadUrl, true);
  195. if (useArrayBuffer) {
  196. request.responseType = "arraybuffer";
  197. }
  198. request.onprogress = progressCallBack;
  199. request.onreadystatechange = function () {
  200. if (request.readyState == 4) {
  201. if (request.status == 200) {
  202. callback(!useArrayBuffer ? request.responseText : request.response);
  203. } else { // Failed
  204. throw new Error(request.status, "Unable to load " + loadUrl);
  205. }
  206. }
  207. };
  208. request.send(null);
  209. };
  210. var loadFromIndexedDB = function () {
  211. database.loadSceneFromDB(url, callback, progressCallBack, noIndexedDB);
  212. };
  213. // Caching only scenes files
  214. if (database && url.indexOf(".babylon") !== -1 && (database.enableSceneOffline)) {
  215. database.openAsync(loadFromIndexedDB, noIndexedDB);
  216. }
  217. else {
  218. noIndexedDB();
  219. }
  220. };
  221. BABYLON.Tools.ReadFile = function (fileToLoad, callback, progressCallBack) {
  222. var reader = new FileReader();
  223. reader.onload = function (e) {
  224. callback(e.target.result);
  225. };
  226. reader.onprogress = progressCallBack;
  227. // Asynchronous read
  228. reader.readAsText(fileToLoad);
  229. };
  230. // Misc.
  231. BABYLON.Tools.WithinEpsilon = function (a, b) {
  232. var num = a - b;
  233. return -1.401298E-45 <= num && num <= 1.401298E-45;
  234. };
  235. var cloneValue = function (source, destinationObject) {
  236. if (!source)
  237. return null;
  238. if (source instanceof BABYLON.Mesh) {
  239. return null;
  240. }
  241. if (source instanceof BABYLON.SubMesh) {
  242. return source.clone(destinationObject);
  243. } else if (source.clone) {
  244. return source.clone();
  245. }
  246. return null;
  247. };
  248. BABYLON.Tools.DeepCopy = function (source, destination, doNotCopyList, mustCopyList) {
  249. for (var prop in source) {
  250. if (prop[0] === "_" && (!mustCopyList || mustCopyList.indexOf(prop) === -1)) {
  251. continue;
  252. }
  253. if (doNotCopyList && doNotCopyList.indexOf(prop) !== -1) {
  254. continue;
  255. }
  256. var sourceValue = source[prop];
  257. var typeOfSourceValue = typeof sourceValue;
  258. if (typeOfSourceValue == "function") {
  259. continue;
  260. }
  261. if (typeOfSourceValue == "object") {
  262. if (sourceValue instanceof Array) {
  263. destination[prop] = [];
  264. if (sourceValue.length > 0) {
  265. if (typeof sourceValue[0] == "object") {
  266. for (var index = 0; index < sourceValue.length; index++) {
  267. var clonedValue = cloneValue(sourceValue[index], destination);
  268. if (destination[prop].indexOf(clonedValue) === -1) { // Test if auto inject was not done
  269. destination[prop].push(clonedValue);
  270. }
  271. }
  272. } else {
  273. destination[prop] = sourceValue.slice(0);
  274. }
  275. }
  276. } else {
  277. destination[prop] = cloneValue(sourceValue, destination);
  278. }
  279. } else {
  280. destination[prop] = sourceValue;
  281. }
  282. }
  283. };
  284. // FPS
  285. var fpsRange = 60;
  286. var previousFramesDuration = [];
  287. var fps = 60;
  288. var deltaTime = 0;
  289. BABYLON.Tools.GetFps = function () {
  290. return fps;
  291. };
  292. BABYLON.Tools.GetDeltaTime = function () {
  293. return deltaTime;
  294. };
  295. BABYLON.Tools._MeasureFps = function () {
  296. previousFramesDuration.push((new Date).getTime());
  297. var length = previousFramesDuration.length;
  298. if (length >= 2) {
  299. deltaTime = previousFramesDuration[length - 1] - previousFramesDuration[length - 2];
  300. }
  301. if (length >= fpsRange) {
  302. if (length > fpsRange) {
  303. previousFramesDuration.splice(0, 1);
  304. length = previousFramesDuration.length;
  305. }
  306. var sum = 0;
  307. for (var id = 0; id < length - 1; id++) {
  308. sum += previousFramesDuration[id + 1] - previousFramesDuration[id];
  309. }
  310. fps = 1000.0 / (sum / (length - 1));
  311. }
  312. };
  313. })();