index.js 3.5 KB

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