babylon.tools.js 7.6 KB

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