babylon.tools.js 11 KB

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