babylon.tools.js 10 KB

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