babylon.tools.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. var BABYLON = BABYLON || {};
  2. (function () {
  3. BABYLON.Tools = {};
  4. // Smart array
  5. BABYLON.Tools.SmartArray = function(capacity) {
  6. this.data = new Array(capacity);
  7. this.length = 0;
  8. };
  9. BABYLON.Tools.SmartArray.prototype.push = function(value) {
  10. this.data[this.length++] = value;
  11. if (this.length > this.data.length) {
  12. this.data.length *= 2;
  13. }
  14. };
  15. BABYLON.Tools.SmartArray.prototype.pushNoDuplicate = function(value) {
  16. if (this.indexOf(value) > -1) {
  17. return;
  18. }
  19. this.push(value);
  20. };
  21. BABYLON.Tools.SmartArray.prototype.reset = function() {
  22. this.length = 0;
  23. };
  24. BABYLON.Tools.SmartArray.prototype.concat = function (array) {
  25. if (array.length === 0) {
  26. return;
  27. }
  28. if (this.length + array.length > this.data.length) {
  29. this.data.length = (this.length + array.length) * 2;
  30. }
  31. for (var index = 0; index < array.length; index++) {
  32. this.data[this.length++] = (array.data || array)[index];
  33. }
  34. };
  35. BABYLON.Tools.SmartArray.prototype.concatWithNoDuplicate = function (array) {
  36. if (array.length === 0) {
  37. return;
  38. }
  39. if (this.length + array.length > this.data.length) {
  40. this.data.length = (this.length + array.length) * 2;
  41. }
  42. for (var index = 0; index < array.length; index++) {
  43. var item = (array.data || array)[index];
  44. var pos = this.data.indexOf(item);
  45. if (pos === -1 ||pos >= this.length) {
  46. this.data[this.length++] = item;
  47. }
  48. }
  49. };
  50. BABYLON.Tools.SmartArray.prototype.indexOf = function (value) {
  51. var position = this.data.indexOf(value);
  52. if (position >= this.length) {
  53. return -1;
  54. }
  55. return position;
  56. };
  57. // Misc.
  58. BABYLON.Tools.GetPointerPrefix = function() {
  59. var eventPrefix = "pointer";
  60. // Check if hand.js is referenced or if the browser natively supports pointer events
  61. if (!navigator.pointerEnabled) {
  62. eventPrefix = "mouse";
  63. }
  64. return eventPrefix;
  65. };
  66. BABYLON.Tools.QueueNewFrame = function (func) {
  67. if (window.requestAnimationFrame)
  68. window.requestAnimationFrame(func);
  69. else if (window.msRequestAnimationFrame)
  70. window.msRequestAnimationFrame(func);
  71. else if (window.webkitRequestAnimationFrame)
  72. window.webkitRequestAnimationFrame(func);
  73. else if (window.mozRequestAnimationFrame)
  74. window.mozRequestAnimationFrame(func);
  75. else if (window.oRequestAnimationFrame)
  76. window.oRequestAnimationFrame(func);
  77. else {
  78. window.setTimeout(func, 16);
  79. }
  80. };
  81. BABYLON.Tools.RequestFullscreen = function (element) {
  82. if (element.requestFullscreen)
  83. element.requestFullscreen();
  84. else if (element.msRequestFullscreen)
  85. element.msRequestFullscreen();
  86. else if (element.webkitRequestFullscreen)
  87. element.webkitRequestFullscreen();
  88. else if (element.mozRequestFullScreen)
  89. element.mozRequestFullScreen();
  90. };
  91. BABYLON.Tools.ExitFullscreen = function () {
  92. if (document.exitFullscreen) {
  93. document.exitFullscreen();
  94. }
  95. else if (document.mozCancelFullScreen) {
  96. document.mozCancelFullScreen();
  97. }
  98. else if (document.webkitCancelFullScreen) {
  99. document.webkitCancelFullScreen();
  100. }
  101. else if (document.msCancelFullScreen) {
  102. document.msCancelFullScreen();
  103. }
  104. };
  105. // External files
  106. BABYLON.Tools.BaseUrl = "";
  107. BABYLON.Tools.LoadImage = function (url, onload, onerror) {
  108. var img = new Image();
  109. img.onload = function () {
  110. onload(img);
  111. };
  112. img.onerror = function (err) {
  113. onerror(img, err);
  114. };
  115. var noIndexedDB = function () {
  116. img.src = url;
  117. };
  118. var loadFromIndexedDB = function () {
  119. BABYLON.Database.LoadImageFromDB(url, img);
  120. };
  121. if (BABYLON.Database.enableTexturesOffline && BABYLON.Database.isUASupportingBlobStorage) {
  122. BABYLON.Database.OpenAsync(loadFromIndexedDB, noIndexedDB);
  123. }
  124. else {
  125. noIndexedDB();
  126. }
  127. return img;
  128. };
  129. BABYLON.Tools.LoadFile = function (url, callback, progressCallBack) {
  130. var noIndexedDB = function () {
  131. var request = new XMLHttpRequest();
  132. var loadUrl = BABYLON.Tools.BaseUrl + url;
  133. request.open('GET', loadUrl, true);
  134. request.onprogress = progressCallBack;
  135. request.onreadystatechange = function () {
  136. if (request.readyState == 4) {
  137. if (request.status == 200) {
  138. callback(request.responseText);
  139. } else { // Failed
  140. throw new Error(request.status, "Unable to load " + loadUrl);
  141. }
  142. }
  143. };
  144. request.send(null);
  145. };
  146. var loadFromIndexedDB = function () {
  147. BABYLON.Database.LoadSceneFromDB(callback, progressCallBack);
  148. };
  149. // Caching only scenes files
  150. if (url.indexOf(".babylon") !== -1 && (BABYLON.Database.enableSceneOffline)) {
  151. BABYLON.Database.OpenAsync(loadFromIndexedDB, noIndexedDB);
  152. }
  153. else {
  154. noIndexedDB();
  155. }
  156. };
  157. // Misc.
  158. BABYLON.Tools.isIE = function () {
  159. return window.ActiveXObject !== undefined;
  160. };
  161. BABYLON.Tools.WithinEpsilon = function (a, b) {
  162. var num = a - b;
  163. return -1.401298E-45 <= num && num <= 1.401298E-45;
  164. };
  165. var cloneValue = function (source, destinationObject) {
  166. if (!source)
  167. return null;
  168. if (source instanceof BABYLON.Mesh) {
  169. return null;
  170. }
  171. if (source instanceof BABYLON.SubMesh) {
  172. return source.clone(destinationObject);
  173. } else if (source.clone) {
  174. return source.clone();
  175. }
  176. return null;
  177. };
  178. BABYLON.Tools.DeepCopy = function (source, destination, doNotCopyList, mustCopyList) {
  179. for (var prop in source) {
  180. if (prop[0] === "_" && (!mustCopyList || mustCopyList.indexOf(prop) === -1)) {
  181. continue;
  182. }
  183. if (doNotCopyList && doNotCopyList.indexOf(prop) !== -1) {
  184. continue;
  185. }
  186. var sourceValue = source[prop];
  187. var typeOfSourceValue = typeof sourceValue;
  188. if (typeOfSourceValue == "function") {
  189. continue;
  190. }
  191. if (typeOfSourceValue == "object") {
  192. if (sourceValue instanceof Array) {
  193. destination[prop] = [];
  194. if (sourceValue.length > 0) {
  195. if (typeof sourceValue[0] == "object") {
  196. for (var index = 0; index < sourceValue.length; index++) {
  197. var clonedValue = cloneValue(sourceValue[index], destination);
  198. if (destination[prop].indexOf(clonedValue) === -1) { // Test if auto inject was not done
  199. destination[prop].push(clonedValue);
  200. }
  201. }
  202. } else {
  203. destination[prop] = sourceValue.slice(0);
  204. }
  205. }
  206. } else {
  207. destination[prop] = cloneValue(sourceValue, destination);
  208. }
  209. } else {
  210. destination[prop] = sourceValue;
  211. }
  212. }
  213. };
  214. // FPS
  215. var fpsRange = 60;
  216. var previousFramesDuration = [];
  217. var fps = 60;
  218. var deltaTime = 0;
  219. BABYLON.Tools.GetFps = function () {
  220. return fps;
  221. };
  222. BABYLON.Tools.GetDeltaTime = function () {
  223. return deltaTime;
  224. };
  225. BABYLON.Tools._MeasureFps = function () {
  226. previousFramesDuration.push((new Date).getTime());
  227. var length = previousFramesDuration.length;
  228. if (length >= 2) {
  229. deltaTime = previousFramesDuration[length - 1] - previousFramesDuration[length - 2];
  230. }
  231. if (length >= fpsRange) {
  232. if (length > fpsRange) {
  233. previousFramesDuration.splice(0, 1);
  234. length = previousFramesDuration.length;
  235. }
  236. var sum = 0;
  237. for (var id = 0; id < length - 1; id++) {
  238. sum += previousFramesDuration[id + 1] - previousFramesDuration[id];
  239. }
  240. fps = 1000.0 / (sum / (length - 1));
  241. }
  242. };
  243. })();