frame.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. var engine = null;
  2. var canvas = null;
  3. var scene = null;
  4. fastEval = function (code) {
  5. var head = document.getElementsByTagName('head')[0];
  6. var script = document.createElement('script');
  7. script.setAttribute('type', 'text/javascript');
  8. script.innerHTML = `try {${code};}
  9. catch(e) {
  10. handleException(e);
  11. }`;
  12. head.appendChild(script);
  13. }
  14. handleException = function (e) {
  15. console.error(e);
  16. }
  17. run = function () {
  18. var snippetUrl = "https://snippet.babylonjs.com";
  19. var fpsLabel = document.getElementById("fpsLabel");
  20. var refreshAnchor = document.getElementById("refresh");
  21. var editAnchor = document.getElementById("edit");
  22. var createEngineFunction = "createDefaultEngine";
  23. var createSceneFunction;
  24. if (location.href.toLowerCase().indexOf("noui") > -1) {
  25. fpsLabel.style.visibility = "hidden";
  26. fpsLabel.style.display = "none";
  27. refreshAnchor.style.visibility = "hidden";
  28. refreshAnchor.style.display = "none";
  29. editAnchor.style.visibility = "hidden";
  30. editAnchor.style.display = "none";
  31. }
  32. BABYLON.Engine.ShadersRepository = "/src/Shaders/";
  33. compileAndRun = function (code) {
  34. try {
  35. if (!BABYLON.Engine.isSupported()) {
  36. showError("Your browser does not support WebGL");
  37. return;
  38. }
  39. if (engine) {
  40. engine.dispose();
  41. engine = null;
  42. }
  43. canvas = document.getElementById("renderCanvas");
  44. createDefaultEngine = function () {
  45. return new BABYLON.Engine(canvas, true, {
  46. preserveDrawingBuffer: true,
  47. stencil: true
  48. });
  49. }
  50. if (code.indexOf("createEngine") !== -1) {
  51. createEngineFunction = "createEngine";
  52. }
  53. // Check for different typos
  54. if (code.indexOf("delayCreateScene") !== -1) { // delayCreateScene
  55. createSceneFunction = "delayCreateScene";
  56. checkCamera = false;
  57. } else if (code.indexOf("createScene") !== -1) { // createScene
  58. createSceneFunction = "createScene";
  59. } else if (code.indexOf("CreateScene") !== -1) { // CreateScene
  60. createSceneFunction = "CreateScene";
  61. } else if (code.indexOf("createscene") !== -1) { // createscene
  62. createSceneFunction = "createscene";
  63. }
  64. if (!createSceneFunction) {
  65. // Just pasted code.
  66. engine = createDefaultEngine();
  67. scene = new BABYLON.Scene(engine);
  68. var runScript = null;
  69. fastEval("runScript = function(scene, canvas) {" + code + "}");
  70. runScript(scene, canvas);
  71. } else {
  72. code += "\n engine = " + createEngineFunction + "();";
  73. code += "\n if (!engine) throw 'engine should not be null.';";
  74. code += "\n" + "scene = " + createSceneFunction + "();";
  75. // Execute the code
  76. fastEval(code);
  77. if (!engine) {
  78. console.error("createEngine function must return an engine.");
  79. return;
  80. }
  81. if (!scene) {
  82. console.error(createSceneFunction + " function must return a scene.");
  83. return;
  84. }
  85. }
  86. engine = engine;
  87. engine.runRenderLoop(function () {
  88. if (engine.scenes.length === 0) {
  89. return;
  90. }
  91. if (canvas.width !== canvas.clientWidth) {
  92. engine.resize();
  93. }
  94. var scene = engine.scenes[0];
  95. if (scene.activeCamera || scene.activeCameras.length > 0) {
  96. scene.render();
  97. }
  98. if (fpsLabel) {
  99. fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
  100. }
  101. }.bind(this));
  102. } catch (e) {
  103. // showError(e.message);
  104. }
  105. };
  106. window.addEventListener("resize", function () {
  107. if (engine) {
  108. engine.resize();
  109. }
  110. });
  111. // UI
  112. var cleanHash = function () {
  113. var splits = decodeURIComponent(location.hash.substr(1)).split("#");
  114. if (splits.length > 2) {
  115. splits.splice(2, splits.length - 2);
  116. }
  117. location.hash = splits.join("#");
  118. };
  119. var checkHash = function () {
  120. if (location.hash) {
  121. cleanHash();
  122. try {
  123. var xmlHttp = new XMLHttpRequest();
  124. xmlHttp.onreadystatechange = function () {
  125. if (xmlHttp.readyState === 4) {
  126. if (xmlHttp.status === 200) {
  127. var snippet = JSON.parse(xmlHttp.responseText);
  128. var snippetCode = JSON.parse(snippet.jsonPayload).code;
  129. compileAndRun(snippetCode);
  130. var refresh = document.getElementById("refresh");
  131. if (snippet.name != null && snippet.name != "") {
  132. this.currentSnippetTitle = snippet.name;
  133. } else this.currentSnippetTitle = null;
  134. if (snippet.description != null && snippet.description != "") {
  135. this.currentSnippetDescription = snippet.description;
  136. } else this.currentSnippetDescription = null;
  137. if (snippet.tags != null && snippet.tags != "") {
  138. this.currentSnippetTags = snippet.tags;
  139. } else this.currentSnippetTags = null;
  140. updateMetadata.call(this);
  141. if (refresh) {
  142. refresh.addEventListener("click", function () {
  143. compileAndRun(snippetCode);
  144. });
  145. }
  146. }
  147. }
  148. };
  149. var hash = location.hash.substr(1);
  150. currentSnippetToken = hash.split("#")[0];
  151. if (!hash.split("#")[1]) hash += "#0";
  152. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  153. xmlHttp.send();
  154. var edit = document.getElementById("edit");
  155. if (edit) {
  156. edit.href = "//www.babylonjs-playground.com/#" + hash;
  157. }
  158. } catch (e) {
  159. }
  160. }
  161. };
  162. var updateMetadata = function() {
  163. var selection;
  164. if (this.currentSnippetTitle) {
  165. selection = document.querySelector('title');
  166. if (selection) {
  167. selection.innerText = (this.currentSnippetTitle + " | Babylon.js Playground");
  168. }
  169. }
  170. if (this.currentSnippetDescription) {
  171. selection = document.querySelector('meta[name="description"]');
  172. if (selection) {
  173. selection.setAttribute("content", this.currentSnippetDescription + " - Babylon.js Playground");
  174. }
  175. }
  176. if (this.currentSnippetTags) {
  177. selection = document.querySelector('meta[name="keywords"]');
  178. if (selection) {
  179. selection.setAttribute("content", "babylon.js, game engine, webgl, 3d," + this.currentSnippetTags);
  180. }
  181. }
  182. }
  183. checkHash();
  184. }
  185. run();