index.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. try {
  31. nodeMaterial.build(true);
  32. } catch (err) {
  33. // Swallow the error here
  34. }
  35. showEditor();
  36. }
  37. }
  38. }
  39. }
  40. var hash = location.hash.substr(1);
  41. currentSnippetToken = hash.split("#")[0];
  42. xmlHttp.open("GET", snippetUrl + "/" + hash.replace("#", "/"));
  43. xmlHttp.send();
  44. } catch (e) {
  45. }
  46. }
  47. }
  48. setTimeout(checkHash, 200);
  49. }
  50. var showEditor = function() {
  51. editorDisplayed = true;
  52. var hostElement = document.getElementById("host-element");
  53. BABYLON.NodeEditor.Show({
  54. nodeMaterial: nodeMaterial,
  55. hostElement: hostElement,
  56. customLoadObservable: customLoadObservable,
  57. customSave: {
  58. label: "Save as unique URL",
  59. action: (data) => {
  60. return new Promise((resolve, reject) => {
  61. var xmlHttp = new XMLHttpRequest();
  62. xmlHttp.onreadystatechange = function () {
  63. if (xmlHttp.readyState == 4) {
  64. if (xmlHttp.status == 200) {
  65. var baseUrl = location.href.replace(location.hash, "").replace(location.search, "");
  66. var snippet = JSON.parse(xmlHttp.responseText);
  67. var newUrl = baseUrl + "#" + snippet.id;
  68. currentSnippetToken = snippet.id;
  69. if (snippet.version && snippet.version != "0") {
  70. newUrl += "#" + snippet.version;
  71. }
  72. location.href = newUrl;
  73. resolve();
  74. }
  75. else {
  76. reject(`Unable to save your node material. It may be too large (${(dataToSend.payload.length / 1024).toFixed(2)} KB) because of embedded textures. Please reduce texture sizes or point to a specific url instead of embedding them and try again.`);
  77. }
  78. }
  79. }
  80. xmlHttp.open("POST", snippetUrl + (currentSnippetToken ? "/" + currentSnippetToken : ""), true);
  81. xmlHttp.setRequestHeader("Content-Type", "application/json");
  82. var dataToSend = {
  83. payload : JSON.stringify({
  84. nodeMaterial: data
  85. }),
  86. name: "",
  87. description: "",
  88. tags: ""
  89. };
  90. xmlHttp.send(JSON.stringify(dataToSend));
  91. });
  92. }
  93. }
  94. });
  95. }
  96. // Let's start
  97. if (BABYLON.Engine.isSupported()) {
  98. var canvas = document.createElement("canvas");
  99. var engine = new BABYLON.Engine(canvas, false, {disableWebGL2Support: true});
  100. var scene = new BABYLON.Scene(engine);
  101. var light0 = new BABYLON.HemisphericLight("light #0", new BABYLON.Vector3(0, 1, 0), scene);
  102. var light1 = new BABYLON.HemisphericLight("light #1", new BABYLON.Vector3(0, 1, 0), scene);
  103. var light2 = new BABYLON.HemisphericLight("light #2", new BABYLON.Vector3(0, 1, 0), scene);
  104. nodeMaterial = new BABYLON.NodeMaterial("node");
  105. // Set to default
  106. if (!location.hash) {
  107. const mode = BABYLON.DataStorage.ReadNumber("Mode", BABYLON.NodeMaterialModes.Material);
  108. switch (mode) {
  109. case BABYLON.NodeMaterialModes.Material:
  110. nodeMaterial.setToDefault();
  111. break;
  112. case BABYLON.NodeMaterialModes.PostProcess:
  113. nodeMaterial.setToDefaultPostProcess();
  114. break;
  115. case BABYLON.NodeMaterialModes.Particle:
  116. nodeMaterial.setToDefaultParticle();
  117. break;
  118. }
  119. nodeMaterial.build(true);
  120. showEditor();
  121. }
  122. }
  123. else {
  124. alert('Babylon.js is not supported.')
  125. }
  126. checkHash();