babylon.tools.js 5.8 KB

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