babylon.tools.js 5.5 KB

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