babylon.tools.js 7.0 KB

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