index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. var snippetUrl = "https://snippet.babylonjs.com";
  2. var currentSnippetToken;
  3. var previousHash = "";
  4. var nodeMaterial;
  5. var customLoadObservable = new BABYLON.Observable();
  6. var editorDisplayed = false;
  7. var cleanHash = function () {
  8. var splits = decodeURIComponent(location.hash.substr(1)).split("#");
  9. if (splits.length > 2) {
  10. splits.splice(2, splits.length - 2);
  11. }
  12. location.hash = splits.join("#");
  13. }
  14. var checkHash = function () {
  15. if (location.hash) {
  16. if (previousHash != location.hash) {
  17. cleanHash();
  18. previousHash = location.hash;
  19. try {
  20. var xmlHttp = new XMLHttpRequest();
  21. xmlHttp.onreadystatechange = function () {
  22. if (xmlHttp.readyState == 4) {
  23. if (xmlHttp.status == 200) {
  24. var snippet = JSON.parse(JSON.parse(xmlHttp.responseText).jsonPayload);
  25. if (editorDisplayed) {
  26. customLoadObservable.notifyObservers(JSON.parse(snippet.nodeMaterial));
  27. } else {
  28. nodeMaterial.loadFromSerialization(snippet.nodeMaterial);
  29. nodeMaterial.build(true);
  30. showEditor();
  31. }
  32. }
  33. }
  34. }
  35. var hash = location.hash.substr(1);
  36. currentSnippetToken = hash.split("#")[0];
  37. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  38. xmlHttp.send();
  39. } catch (e) {
  40. }
  41. }
  42. }
  43. setTimeout(checkHash, 200);
  44. }
  45. var showEditor = function() {
  46. editorDisplayed = true;
  47. var hostElement = document.getElementById("host-element");
  48. BABYLON.NodeEditor.Show({
  49. nodeMaterial: nodeMaterial,
  50. hostElement: hostElement,
  51. customLoadObservable: customLoadObservable,
  52. customSave: {
  53. label: "Save as unique URL",
  54. action: (data) => {
  55. var xmlHttp = new XMLHttpRequest();
  56. xmlHttp.onreadystatechange = function () {
  57. if (xmlHttp.readyState == 4) {
  58. if (xmlHttp.status == 200) {
  59. var baseUrl = location.href.replace(location.hash, "").replace(location.search, "");
  60. var snippet = JSON.parse(xmlHttp.responseText);
  61. var newUrl = baseUrl + "#" + snippet.id;
  62. currentSnippetToken = snippet.id;
  63. if (snippet.version && snippet.version != "0") {
  64. newUrl += "#" + snippet.version;
  65. }
  66. location.href = newUrl;
  67. }
  68. else {
  69. console.log("Unable to save your code. Please retry.", null);
  70. }
  71. }
  72. }
  73. xmlHttp.open("POST", snippetUrl + (currentSnippetToken ? "/" + currentSnippetToken : ""), true);
  74. xmlHttp.setRequestHeader("Content-Type", "application/json");
  75. var dataToSend = {
  76. payload : JSON.stringify({
  77. nodeMaterial: data
  78. }),
  79. name: "",
  80. description: "",
  81. tags: ""
  82. };
  83. xmlHttp.send(JSON.stringify(dataToSend));
  84. }
  85. }
  86. });
  87. }
  88. // Let's start
  89. if (BABYLON.Engine.isSupported()) {
  90. var canvas = document.createElement("canvas");
  91. var engine = new BABYLON.Engine(canvas, false, {disableWebGL2Support: true});
  92. var scene = new BABYLON.Scene(engine);
  93. nodeMaterial = new BABYLON.NodeMaterial("node");
  94. // Set to default
  95. if (!location.hash) {
  96. nodeMaterial.setToDefault();
  97. nodeMaterial.build(true);
  98. showEditor();
  99. }
  100. }
  101. else {
  102. alert('Babylon.js is not supported.')
  103. }
  104. checkHash();