babylon.tools.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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.onload = function () {
  127. onload(img);
  128. };
  129. img.onerror = function (err) {
  130. onerror(img, err);
  131. };
  132. var noIndexedDB = function () {
  133. img.src = url;
  134. };
  135. var loadFromIndexedDB = function () {
  136. database.loadImageFromDB(url, img);
  137. };
  138. if (database && database.enableTexturesOffline && BABYLON.Database.isUASupportingBlobStorage) {
  139. database.openAsync(loadFromIndexedDB, noIndexedDB);
  140. }
  141. else {
  142. if (url.indexOf("file:") === -1) {
  143. noIndexedDB();
  144. }
  145. else {
  146. try {
  147. var textureName = url.substring(5);
  148. var blobURL;
  149. try {
  150. blobURL = URL.createObjectURL(BABYLON.FilesTextures[textureName], { oneTimeOnly: true });
  151. }
  152. catch (ex) {
  153. // Chrome doesn't support oneTimeOnly parameter
  154. blobURL = URL.createObjectURL(BABYLON.FilesTextures[textureName]);
  155. }
  156. img.src = blobURL;
  157. }
  158. catch (e) {
  159. console.log("Error while trying to load texture: " + textureName);
  160. img.src = null;
  161. }
  162. }
  163. }
  164. return img;
  165. };
  166. BABYLON.Tools.LoadFile = function (url, callback, progressCallBack, database) {
  167. var noIndexedDB = function () {
  168. var request = new XMLHttpRequest();
  169. var loadUrl = BABYLON.Tools.BaseUrl + url;
  170. request.open('GET', loadUrl, true);
  171. request.onprogress = progressCallBack;
  172. request.onreadystatechange = function () {
  173. if (request.readyState == 4) {
  174. if (request.status == 200) {
  175. callback(request.responseText);
  176. } else { // Failed
  177. throw new Error(request.status, "Unable to load " + loadUrl);
  178. }
  179. }
  180. };
  181. request.send(null);
  182. };
  183. var loadFromIndexedDB = function () {
  184. database.loadSceneFromDB(url, callback, progressCallBack, noIndexedDB);
  185. };
  186. // Caching only scenes files
  187. if (database && url.indexOf(".babylon") !== -1 && (database.enableSceneOffline)) {
  188. database.openAsync(loadFromIndexedDB, noIndexedDB);
  189. }
  190. else {
  191. noIndexedDB();
  192. }
  193. };
  194. BABYLON.Tools.ReadFile = function (fileToLoad, callback, progressCallBack) {
  195. var reader = new FileReader();
  196. reader.onload = function (e) {
  197. callback(e.target.result);
  198. };
  199. reader.onprogress = progressCallBack;
  200. // Asynchronous read
  201. reader.readAsText(fileToLoad);
  202. };
  203. // Misc.
  204. BABYLON.Tools.isIE = function () {
  205. return window.ActiveXObject !== undefined;
  206. };
  207. BABYLON.Tools.WithinEpsilon = function (a, b) {
  208. var num = a - b;
  209. return -1.401298E-45 <= num && num <= 1.401298E-45;
  210. };
  211. var cloneValue = function (source, destinationObject) {
  212. if (!source)
  213. return null;
  214. if (source instanceof BABYLON.Mesh) {
  215. return null;
  216. }
  217. if (source instanceof BABYLON.SubMesh) {
  218. return source.clone(destinationObject);
  219. } else if (source.clone) {
  220. return source.clone();
  221. }
  222. return null;
  223. };
  224. BABYLON.Tools.DeepCopy = function (source, destination, doNotCopyList, mustCopyList) {
  225. for (var prop in source) {
  226. if (prop[0] === "_" && (!mustCopyList || mustCopyList.indexOf(prop) === -1)) {
  227. continue;
  228. }
  229. if (doNotCopyList && doNotCopyList.indexOf(prop) !== -1) {
  230. continue;
  231. }
  232. var sourceValue = source[prop];
  233. var typeOfSourceValue = typeof sourceValue;
  234. if (typeOfSourceValue == "function") {
  235. continue;
  236. }
  237. if (typeOfSourceValue == "object") {
  238. if (sourceValue instanceof Array) {
  239. destination[prop] = [];
  240. if (sourceValue.length > 0) {
  241. if (typeof sourceValue[0] == "object") {
  242. for (var index = 0; index < sourceValue.length; index++) {
  243. var clonedValue = cloneValue(sourceValue[index], destination);
  244. if (destination[prop].indexOf(clonedValue) === -1) { // Test if auto inject was not done
  245. destination[prop].push(clonedValue);
  246. }
  247. }
  248. } else {
  249. destination[prop] = sourceValue.slice(0);
  250. }
  251. }
  252. } else {
  253. destination[prop] = cloneValue(sourceValue, destination);
  254. }
  255. } else {
  256. destination[prop] = sourceValue;
  257. }
  258. }
  259. };
  260. // FPS
  261. var fpsRange = 60;
  262. var previousFramesDuration = [];
  263. var fps = 60;
  264. var deltaTime = 0;
  265. BABYLON.Tools.GetFps = function () {
  266. return fps;
  267. };
  268. BABYLON.Tools.GetDeltaTime = function () {
  269. return deltaTime;
  270. };
  271. BABYLON.Tools._MeasureFps = function () {
  272. previousFramesDuration.push((new Date).getTime());
  273. var length = previousFramesDuration.length;
  274. if (length >= 2) {
  275. deltaTime = previousFramesDuration[length - 1] - previousFramesDuration[length - 2];
  276. }
  277. if (length >= fpsRange) {
  278. if (length > fpsRange) {
  279. previousFramesDuration.splice(0, 1);
  280. length = previousFramesDuration.length;
  281. }
  282. var sum = 0;
  283. for (var id = 0; id < length - 1; id++) {
  284. sum += previousFramesDuration[id + 1] - previousFramesDuration[id];
  285. }
  286. fps = 1000.0 / (sum / (length - 1));
  287. }
  288. };
  289. })();