NodeEditorLib.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { NodePrototypeEditor } from './editors/NodePrototypeEditor.js';
  2. import { ScriptableEditor } from './editors/ScriptableEditor.js';
  3. import { BasicMaterialEditor } from './editors/BasicMaterialEditor.js';
  4. import { StandardMaterialEditor } from './editors/StandardMaterialEditor.js';
  5. import { PointsMaterialEditor } from './editors/PointsMaterialEditor.js';
  6. import { FloatEditor } from './editors/FloatEditor.js';
  7. import { Vector2Editor } from './editors/Vector2Editor.js';
  8. import { Vector3Editor } from './editors/Vector3Editor.js';
  9. import { Vector4Editor } from './editors/Vector4Editor.js';
  10. import { SliderEditor } from './editors/SliderEditor.js';
  11. import { ColorEditor } from './editors/ColorEditor.js';
  12. import { TextureEditor } from './editors/TextureEditor.js';
  13. import { UVEditor } from './editors/UVEditor.js';
  14. import { PreviewEditor } from './editors/PreviewEditor.js';
  15. import { TimerEditor } from './editors/TimerEditor.js';
  16. import { SplitEditor } from './editors/SplitEditor.js';
  17. import { SwizzleEditor } from './editors/SwizzleEditor.js';
  18. import { JoinEditor } from './editors/JoinEditor.js';
  19. import { StringEditor } from './editors/StringEditor.js';
  20. import { FileEditor } from './editors/FileEditor.js';
  21. import { CustomNodeEditor } from './editors/CustomNodeEditor.js';
  22. export const ClassLib = {
  23. BasicMaterialEditor,
  24. StandardMaterialEditor,
  25. PointsMaterialEditor,
  26. FloatEditor,
  27. Vector2Editor,
  28. Vector3Editor,
  29. Vector4Editor,
  30. SliderEditor,
  31. ColorEditor,
  32. TextureEditor,
  33. UVEditor,
  34. TimerEditor,
  35. SplitEditor,
  36. SwizzleEditor,
  37. JoinEditor,
  38. StringEditor,
  39. FileEditor,
  40. ScriptableEditor,
  41. PreviewEditor,
  42. NodePrototypeEditor
  43. };
  44. let nodeList = null;
  45. let nodeListLoading = false;
  46. export const getNodeList = async () => {
  47. if ( nodeList === null ) {
  48. if ( nodeListLoading === false ) {
  49. nodeListLoading = true;
  50. const response = await fetch( './Nodes.json' );
  51. nodeList = await response.json();
  52. } else {
  53. await new Promise( res => {
  54. const verifyNodeList = () => {
  55. if ( nodeList !== null ) {
  56. res();
  57. } else {
  58. window.requestAnimationFrame( verifyNodeList );
  59. }
  60. };
  61. verifyNodeList();
  62. } );
  63. }
  64. }
  65. return nodeList;
  66. };
  67. export const init = async () => {
  68. const nodeList = await getNodeList();
  69. const traverseNodeEditors = ( list ) => {
  70. for ( const node of list ) {
  71. getNodeEditorClass( node );
  72. if ( Array.isArray( node.children ) ) {
  73. traverseNodeEditors( node.children );
  74. }
  75. }
  76. };
  77. traverseNodeEditors( nodeList.nodes );
  78. };
  79. export const getNodeEditorClass = async ( nodeData ) => {
  80. const editorClass = nodeData.editorClass || nodeData.name.replace( / /g, '' );
  81. //
  82. let nodeClass = nodeData.nodeClass || ClassLib[ editorClass ];
  83. if ( nodeClass !== undefined ) {
  84. if ( nodeData.editorClass !== undefined ) {
  85. nodeClass.prototype.icon = nodeData.icon;
  86. }
  87. return nodeClass;
  88. }
  89. //
  90. if ( nodeData.editorURL ) {
  91. const moduleEditor = await import( nodeData.editorURL );
  92. const moduleName = nodeData.editorClass || Object.keys( moduleEditor )[ 0 ];
  93. nodeClass = moduleEditor[ moduleName ];
  94. } else if ( nodeData.shaderNode ) {
  95. const createNodeEditorClass = ( nodeData ) => {
  96. return class extends CustomNodeEditor {
  97. constructor() {
  98. super( nodeData );
  99. }
  100. get className() {
  101. return editorClass;
  102. }
  103. };
  104. };
  105. nodeClass = createNodeEditorClass( nodeData );
  106. }
  107. if ( nodeClass !== null ) {
  108. ClassLib[ editorClass ] = nodeClass;
  109. }
  110. return nodeClass;
  111. };