frame.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 && !(scene.activeCamera &&
  99. scene.activeCamera.getClassName &&
  100. scene.activeCamera.getClassName() === 'WebXRCamera')) {
  101. fpsLabel.innerHTML = engine.getFps().toFixed() + " fps";
  102. }
  103. }.bind(this));
  104. } catch (e) {
  105. // showError(e.message);
  106. }
  107. };
  108. window.addEventListener("resize", function () {
  109. if (engine) {
  110. engine.resize();
  111. }
  112. });
  113. // UI
  114. var cleanHash = function () {
  115. var splits = decodeURIComponent(location.hash.substr(1)).split("#");
  116. if (splits.length > 2) {
  117. splits.splice(2, splits.length - 2);
  118. }
  119. location.hash = splits.join("#");
  120. };
  121. var checkHash = function () {
  122. if (location.hash) {
  123. cleanHash();
  124. try {
  125. var xmlHttp = new XMLHttpRequest();
  126. xmlHttp.onreadystatechange = function () {
  127. if (xmlHttp.readyState === 4) {
  128. if (xmlHttp.status === 200) {
  129. var snippet = JSON.parse(xmlHttp.responseText);
  130. var snippetCode = JSON.parse(snippet.jsonPayload).code;
  131. compileAndRun(snippetCode);
  132. var refresh = document.getElementById("refresh");
  133. if (snippet.name != null && snippet.name != "") {
  134. this.currentSnippetTitle = snippet.name;
  135. } else this.currentSnippetTitle = null;
  136. if (snippet.description != null && snippet.description != "") {
  137. this.currentSnippetDescription = snippet.description;
  138. } else this.currentSnippetDescription = null;
  139. if (snippet.tags != null && snippet.tags != "") {
  140. this.currentSnippetTags = snippet.tags;
  141. } else this.currentSnippetTags = null;
  142. updateMetadata.call(this);
  143. if (refresh) {
  144. refresh.addEventListener("click", function () {
  145. compileAndRun(snippetCode);
  146. });
  147. }
  148. }
  149. }
  150. };
  151. var hash = location.hash.substr(1);
  152. currentSnippetToken = hash.split("#")[0];
  153. if (!hash.split("#")[1]) hash += "#0";
  154. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  155. xmlHttp.send();
  156. var edit = document.getElementById("edit");
  157. if (edit) {
  158. edit.href = "//www.babylonjs-playground.com/#" + hash;
  159. }
  160. } catch (e) {
  161. }
  162. }
  163. };
  164. var updateMetadata = function() {
  165. var selection;
  166. if (this.currentSnippetTitle) {
  167. selection = document.querySelector('title');
  168. if (selection) {
  169. selection.innerText = (this.currentSnippetTitle + " | Babylon.js Playground");
  170. }
  171. }
  172. if (this.currentSnippetDescription) {
  173. selection = document.querySelector('meta[name="description"]');
  174. if (selection) {
  175. selection.setAttribute("content", this.currentSnippetDescription + " - Babylon.js Playground");
  176. }
  177. }
  178. if (this.currentSnippetTags) {
  179. selection = document.querySelector('meta[name="keywords"]');
  180. if (selection) {
  181. selection.setAttribute("content", "babylon.js, game engine, webgl, 3d," + this.currentSnippetTags);
  182. }
  183. }
  184. }
  185. checkHash();
  186. }
  187. run();