frame.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. (function () {
  2. var snippetUrl = "https://babylonjs-api2.azurewebsites.net/snippets";
  3. var currentSnippetToken;
  4. var engine;
  5. var fpsLabel = document.getElementById("fpsLabel");
  6. var scripts;
  7. var zipCode;
  8. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  9. var loadScript = function (scriptURL, title) {
  10. var xhr = new XMLHttpRequest();
  11. xhr.open('GET', scriptURL, true);
  12. xhr.onreadystatechange = function () {
  13. if (xhr.readyState === 4) {
  14. if (xhr.status === 200) {
  15. blockEditorChange = true;
  16. console.log(xhr.responseText);
  17. jsEditor.setValue(xhr.responseText);
  18. jsEditor.setPosition({ lineNumber: 0, column: 0 });
  19. blockEditorChange = false;
  20. compileAndRun();
  21. document.getElementById("currentScript").innerHTML = title;
  22. currentSnippetToken = null;
  23. }
  24. }
  25. };
  26. xhr.send(null);
  27. };
  28. var showError = function(error) {
  29. console.warn(error);
  30. };
  31. compileAndRun = function (code) {
  32. try {
  33. if (!BABYLON.Engine.isSupported()) {
  34. showError("Your browser does not support WebGL");
  35. return;
  36. }
  37. if (engine) {
  38. engine.dispose();
  39. engine = null;
  40. }
  41. var canvas = document.getElementById("renderCanvas");
  42. engine = new BABYLON.Engine(canvas, true, {stencil: true});
  43. engine.renderEvenInBackground = false;
  44. BABYLON.Camera.ForceAttachControlToAlwaysPreventDefault = true;
  45. engine.runRenderLoop(function () {
  46. if (engine.scenes.length === 0) {
  47. return;
  48. }
  49. if (canvas.width !== canvas.clientWidth) {
  50. engine.resize();
  51. }
  52. var scene = engine.scenes[0];
  53. if (scene.activeCamera || scene.activeCameras.length > 0) {
  54. scene.render();
  55. }
  56. fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
  57. });
  58. var scene;
  59. if (code.indexOf("createScene") !== -1) { // createScene
  60. eval(code);
  61. scene = createScene();
  62. if (!scene) {
  63. showError("createScene function must return a scene.");
  64. return;
  65. }
  66. zipCode = code + "\r\n\r\nvar scene = createScene();";
  67. } else if (code.indexOf("CreateScene") !== -1) { // CreateScene
  68. eval(code);
  69. scene = CreateScene();
  70. if (!scene) {
  71. showError("CreateScene function must return a scene.");
  72. return;
  73. }
  74. zipCode = code + "\r\n\r\nvar scene = CreateScene();";
  75. } else if (code.indexOf("createscene") !== -1) { // createscene
  76. eval(code);
  77. scene = createscene();
  78. if (!scene) {
  79. showError("createscene function must return a scene.");
  80. return;
  81. }
  82. zipCode = code + "\r\n\r\nvar scene = createscene();";
  83. } else { // Direct code
  84. scene = new BABYLON.Scene(engine);
  85. eval("runScript = function(scene, canvas) {" + code + "}");
  86. runScript(scene, canvas);
  87. zipCode = "var scene = new BABYLON.Scene(engine);\r\n\r\n" + code;
  88. }
  89. } catch (e) {
  90. // showError(e.message);
  91. }
  92. };
  93. window.addEventListener("resize", function () {
  94. if (engine) {
  95. engine.resize();
  96. }
  97. });
  98. // UI
  99. var cleanHash = function () {
  100. var splits = decodeURIComponent(location.hash.substr(1)).split("#");
  101. if (splits.length > 2) {
  102. splits.splice(2, splits.length - 2);
  103. }
  104. location.hash = splits.join("#");
  105. };
  106. var checkHash = function () {
  107. if (location.hash) {
  108. cleanHash();
  109. try {
  110. var xmlHttp = new XMLHttpRequest();
  111. xmlHttp.onreadystatechange = function () {
  112. if (xmlHttp.readyState === 4) {
  113. if (xmlHttp.status === 200) {
  114. var snippetCode = JSON.parse(JSON.parse(xmlHttp.responseText)[0].jsonPayload).code;
  115. compileAndRun(snippetCode);
  116. document.getElementById("refresh").addEventListener("click", function () {
  117. compileAndRun(snippetCode);
  118. });
  119. }
  120. }
  121. };
  122. var hash = location.hash.substr(1);
  123. currentSnippetToken = hash.split("#")[0];
  124. if(!hash.split("#")[1]) hash += "#0";
  125. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  126. xmlHttp.send();
  127. document.getElementById("link").href = "//www.babylonjs-playground.com/#" + hash;
  128. } catch (e) {
  129. }
  130. }
  131. };
  132. checkHash();
  133. })();