babylon.tools.js 12 KB

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