index.js 4.3 KB

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