babylon.tools.js 11 KB

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