NodeEditor.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  1. import { Styles, Canvas, CircleMenu, ButtonInput, ContextMenu, Tips, Search, Loader } from '../libs/flow.module.js';
  2. import { BasicMaterialEditor } from './materials/BasicMaterialEditor.js';
  3. import { StandardMaterialEditor } from './materials/StandardMaterialEditor.js';
  4. import { PointsMaterialEditor } from './materials/PointsMaterialEditor.js';
  5. import { OperatorEditor } from './math/OperatorEditor.js';
  6. import { NormalizeEditor } from './math/NormalizeEditor.js';
  7. import { InvertEditor } from './math/InvertEditor.js';
  8. import { LimiterEditor } from './math/LimiterEditor.js';
  9. import { DotEditor } from './math/DotEditor.js';
  10. import { PowerEditor } from './math/PowerEditor.js';
  11. import { AngleEditor } from './math/AngleEditor.js';
  12. import { TrigonometryEditor } from './math/TrigonometryEditor.js';
  13. import { FloatEditor } from './inputs/FloatEditor.js';
  14. import { Vector2Editor } from './inputs/Vector2Editor.js';
  15. import { Vector3Editor } from './inputs/Vector3Editor.js';
  16. import { Vector4Editor } from './inputs/Vector4Editor.js';
  17. import { SliderEditor } from './inputs/SliderEditor.js';
  18. import { ColorEditor } from './inputs/ColorEditor.js';
  19. import { BlendEditor } from './display/BlendEditor.js';
  20. import { UVEditor } from './accessors/UVEditor.js';
  21. import { PositionEditor } from './accessors/PositionEditor.js';
  22. import { NormalEditor } from './accessors/NormalEditor.js';
  23. import { TimerEditor } from './utils/TimerEditor.js';
  24. import { OscillatorEditor } from './utils/OscillatorEditor.js';
  25. import { SplitEditor } from './utils/SplitEditor.js';
  26. import { JoinEditor } from './utils/JoinEditor.js';
  27. import { CheckerEditor } from './procedural/CheckerEditor.js';
  28. import { PointsEditor } from './scene/PointsEditor.js';
  29. import { MeshEditor } from './scene/MeshEditor.js';
  30. import { EventDispatcher } from 'three';
  31. Styles.icons.unlink = 'ti ti-unlink';
  32. export const NodeList = [
  33. {
  34. name: 'Inputs',
  35. icon: 'forms',
  36. children: [
  37. {
  38. name: 'Slider',
  39. icon: 'adjustments-horizontal',
  40. nodeClass: SliderEditor
  41. },
  42. {
  43. name: 'Float',
  44. icon: 'box-multiple-1',
  45. nodeClass: FloatEditor
  46. },
  47. {
  48. name: 'Vector 2',
  49. icon: 'box-multiple-2',
  50. nodeClass: Vector2Editor
  51. },
  52. {
  53. name: 'Vector 3',
  54. icon: 'box-multiple-3',
  55. nodeClass: Vector3Editor
  56. },
  57. {
  58. name: 'Vector 4',
  59. icon: 'box-multiple-4',
  60. nodeClass: Vector4Editor
  61. },
  62. {
  63. name: 'Color',
  64. icon: 'palette',
  65. nodeClass: ColorEditor
  66. }
  67. ]
  68. },
  69. {
  70. name: 'Accessors',
  71. icon: 'vector-triangle',
  72. children: [
  73. {
  74. name: 'UV',
  75. icon: 'details',
  76. nodeClass: UVEditor
  77. },
  78. {
  79. name: 'Position',
  80. icon: 'hierarchy',
  81. nodeClass: PositionEditor
  82. },
  83. {
  84. name: 'Normal',
  85. icon: 'fold-up',
  86. nodeClass: NormalEditor
  87. }
  88. ]
  89. },
  90. {
  91. name: 'Display',
  92. icon: 'brightness',
  93. children: [
  94. {
  95. name: 'Blend',
  96. icon: 'layers-subtract',
  97. nodeClass: BlendEditor
  98. }
  99. ]
  100. },
  101. {
  102. name: 'Math',
  103. icon: 'calculator',
  104. children: [
  105. {
  106. name: 'Operator',
  107. icon: 'math-symbols',
  108. nodeClass: OperatorEditor
  109. },
  110. {
  111. name: 'Invert',
  112. icon: 'flip-vertical',
  113. tip: 'Negate',
  114. nodeClass: OperatorEditor
  115. },
  116. {
  117. name: 'Limiter',
  118. icon: 'arrow-bar-to-up',
  119. tip: 'Min / Max',
  120. nodeClass: LimiterEditor
  121. },
  122. {
  123. name: 'Dot Product',
  124. icon: 'arrows-up-left',
  125. nodeClass: DotEditor
  126. },
  127. {
  128. name: 'Power',
  129. icon: 'arrow-up-right',
  130. nodeClass: PowerEditor
  131. },
  132. {
  133. name: 'Trigonometry',
  134. icon: 'wave-sine',
  135. tip: 'Sin / Cos / Tan / ...',
  136. nodeClass: TrigonometryEditor
  137. },
  138. {
  139. name: 'Angle',
  140. icon: 'angle',
  141. tip: 'Degress / Radians',
  142. nodeClass: AngleEditor
  143. },
  144. {
  145. name: 'Normalize',
  146. icon: 'fold',
  147. nodeClass: NormalizeEditor
  148. }
  149. ]
  150. },
  151. {
  152. name: 'Procedural',
  153. icon: 'infinity',
  154. children: [
  155. {
  156. name: 'Checker',
  157. icon: 'border-outer',
  158. nodeClass: CheckerEditor
  159. }
  160. ]
  161. },
  162. {
  163. name: 'Utils',
  164. icon: 'apps',
  165. children: [
  166. {
  167. name: 'Timer',
  168. icon: 'clock',
  169. nodeClass: TimerEditor
  170. },
  171. {
  172. name: 'Oscillator',
  173. icon: 'wave-sine',
  174. nodeClass: OscillatorEditor
  175. },
  176. {
  177. name: 'Split',
  178. icon: 'arrows-split-2',
  179. nodeClass: SplitEditor
  180. },
  181. {
  182. name: 'Join',
  183. icon: 'arrows-join-2',
  184. nodeClass: JoinEditor
  185. }
  186. ]
  187. },
  188. /*{
  189. name: 'Scene',
  190. icon: '3d-cube-sphere',
  191. children: [
  192. {
  193. name: 'Mesh',
  194. icon: '3d-cube-sphere',
  195. nodeClass: MeshEditor
  196. }
  197. ]
  198. },*/
  199. {
  200. name: 'Material',
  201. icon: 'circles',
  202. children: [
  203. {
  204. name: 'Basic Material',
  205. icon: 'circle',
  206. nodeClass: BasicMaterialEditor
  207. },
  208. {
  209. name: 'Standard Material',
  210. icon: 'circle',
  211. nodeClass: StandardMaterialEditor
  212. },
  213. {
  214. name: 'Points Material',
  215. icon: 'circle-dotted',
  216. nodeClass: PointsMaterialEditor
  217. }
  218. ]
  219. }
  220. ];
  221. export const ClassLib = {
  222. BasicMaterialEditor,
  223. StandardMaterialEditor,
  224. PointsMaterialEditor,
  225. PointsEditor,
  226. MeshEditor,
  227. OperatorEditor,
  228. NormalizeEditor,
  229. InvertEditor,
  230. LimiterEditor,
  231. DotEditor,
  232. PowerEditor,
  233. AngleEditor,
  234. TrigonometryEditor,
  235. FloatEditor,
  236. Vector2Editor,
  237. Vector3Editor,
  238. Vector4Editor,
  239. SliderEditor,
  240. ColorEditor,
  241. BlendEditor,
  242. UVEditor,
  243. PositionEditor,
  244. NormalEditor,
  245. TimerEditor,
  246. OscillatorEditor,
  247. SplitEditor,
  248. JoinEditor,
  249. CheckerEditor
  250. };
  251. export class NodeEditor extends EventDispatcher {
  252. constructor( scene = null ) {
  253. super();
  254. const domElement = document.createElement( 'flow' );
  255. const canvas = new Canvas();
  256. domElement.append( canvas.dom );
  257. this.scene = scene;
  258. this.canvas = canvas;
  259. this.domElement = domElement;
  260. this.nodesContext = null;
  261. this.examplesContext = null;
  262. this._initTips();
  263. this._initMenu();
  264. this._initSearch();
  265. this._initNodesContext();
  266. this._initExamplesContext();
  267. }
  268. centralizeNode( node ) {
  269. const canvas = this.canvas;
  270. const canvasRect = canvas.rect;
  271. const nodeRect = node.dom.getBoundingClientRect();
  272. const defaultOffsetX = nodeRect.width;
  273. const defaultOffsetY = nodeRect.height;
  274. node.setPosition(
  275. ( canvas.relativeX + ( canvasRect.width / 2 ) ) - defaultOffsetX,
  276. ( canvas.relativeY + ( canvasRect.height / 2 ) ) - defaultOffsetY
  277. );
  278. }
  279. add( node ) {
  280. const onRemove = () => {
  281. node.removeEventListener( 'remove', onRemove );
  282. node.setEditor( null );
  283. };
  284. node.setEditor( this );
  285. node.addEventListener( 'remove', onRemove );
  286. this.canvas.add( node );
  287. this.dispatchEvent( { type: 'add', node } );
  288. return this;
  289. }
  290. get nodes() {
  291. return this.canvas.nodes;
  292. }
  293. newProject() {
  294. this.canvas.clear();
  295. this.dispatchEvent( { type: 'new' } );
  296. }
  297. loadJSON( json ) {
  298. this.canvas.clear();
  299. this.canvas.deserialize( json );
  300. for ( const node of this.canvas.nodes ) {
  301. this.add( node );
  302. }
  303. this.dispatchEvent( { type: 'load' } );
  304. }
  305. _initTips() {
  306. this.tips = new Tips();
  307. this.domElement.append( this.tips.dom );
  308. }
  309. _initMenu() {
  310. const menu = new CircleMenu();
  311. const menuButton = new ButtonInput().setIcon( 'ti ti-apps' ).setToolTip( 'Add' );
  312. const examplesButton = new ButtonInput().setIcon( 'ti ti-file-symlink' ).setToolTip( 'Examples' );
  313. const newButton = new ButtonInput().setIcon( 'ti ti-file' ).setToolTip( 'New' );
  314. const openButton = new ButtonInput().setIcon( 'ti ti-upload' ).setToolTip( 'Open' );
  315. const saveButton = new ButtonInput().setIcon( 'ti ti-download' ).setToolTip( 'Save' );
  316. menuButton.onClick( () => this.nodesContext.open() );
  317. examplesButton.onClick( () => this.examplesContext.open() );
  318. newButton.onClick( () => {
  319. if ( confirm( 'Are you sure?' ) === true ) {
  320. this.newProject();
  321. }
  322. } );
  323. openButton.onClick( () => {
  324. const input = document.createElement( 'input' );
  325. input.type = 'file';
  326. input.onchange = e => {
  327. const file = e.target.files[ 0 ];
  328. const reader = new FileReader();
  329. reader.readAsText( file, 'UTF-8' );
  330. reader.onload = readerEvent => {
  331. const loader = new Loader( Loader.OBJECTS );
  332. const json = loader.parse( JSON.parse( readerEvent.target.result ), ClassLib );
  333. this.loadJSON( json );
  334. };
  335. };
  336. input.click();
  337. } );
  338. saveButton.onClick( () => {
  339. const json = JSON.stringify( this.canvas.toJSON() );
  340. const a = document.createElement( 'a' );
  341. const file = new Blob( [ json ], { type: 'text/plain' } );
  342. a.href = URL.createObjectURL( file );
  343. a.download = 'node_editor.json';
  344. a.click();
  345. } );
  346. menu.add( examplesButton )
  347. .add( menuButton )
  348. .add( newButton )
  349. .add( openButton )
  350. .add( saveButton );
  351. this.domElement.append( menu.dom );
  352. this.menu = menu;
  353. }
  354. _initExamplesContext() {
  355. const context = new ContextMenu();
  356. //**************//
  357. // MAIN
  358. //**************//
  359. const onClickExample = async ( button ) => {
  360. this.examplesContext.hide();
  361. const filename = button.getExtra();
  362. const loader = new Loader( Loader.OBJECTS );
  363. const json = await loader.load( `./jsm/node-editor/examples/${filename}.json`, ClassLib );
  364. this.loadJSON( json );
  365. };
  366. const addExample = ( context, name, filename = null ) => {
  367. filename = filename || name.replaceAll( ' ', '-' ).toLowerCase();
  368. context.add( new ButtonInput( name )
  369. .setIcon( 'ti ti-file-symlink' )
  370. .onClick( onClickExample )
  371. .setExtra( filename )
  372. );
  373. };
  374. //**************//
  375. // EXAMPLES
  376. //**************//
  377. const basicContext = new ContextMenu();
  378. const advancedContext = new ContextMenu();
  379. addExample( basicContext, 'Animate UV' );
  380. addExample( basicContext, 'Fake top light' );
  381. addExample( basicContext, 'Oscillator color' );
  382. addExample( advancedContext, 'Rim' );
  383. //**************//
  384. // MAIN
  385. //**************//
  386. context.add( new ButtonInput( 'Basic' ), basicContext );
  387. context.add( new ButtonInput( 'Advanced' ), advancedContext );
  388. this.examplesContext = context;
  389. }
  390. _initSearch() {
  391. const traverseNodeEditors = ( item ) => {
  392. if ( item.nodeClass ) {
  393. const button = new ButtonInput( item.name );
  394. button.setIcon( `ti ti-${item.icon}` );
  395. button.addEventListener( 'complete', () => {
  396. const node = new item.nodeClass();
  397. this.add( node );
  398. this.centralizeNode( node );
  399. } );
  400. search.add( button );
  401. }
  402. if ( item.children ) {
  403. for ( const subItem of item.children ) {
  404. traverseNodeEditors( subItem );
  405. }
  406. }
  407. };
  408. const search = new Search();
  409. search.forceAutoComplete = true;
  410. search.onFilter( () => {
  411. search.clear();
  412. for ( const item of NodeList ) {
  413. traverseNodeEditors( item );
  414. }
  415. const object3d = this.scene;
  416. if ( object3d !== null ) {
  417. object3d.traverse( ( obj3d ) => {
  418. if ( obj3d.isMesh === true || obj3d.isPoints === true ) {
  419. let prefix = null;
  420. let icon = null;
  421. let editorClass = null;
  422. if ( obj3d.isMesh === true ) {
  423. prefix = 'Mesh';
  424. icon = 'ti ti-3d-cube-sphere';
  425. editorClass = MeshEditor;
  426. } else if ( obj3d.isPoints === true ) {
  427. prefix = 'Points';
  428. icon = 'ti ti-border-none';
  429. editorClass = PointsEditor;
  430. }
  431. const button = new ButtonInput( `${prefix} - ${obj3d.name}` );
  432. button.setIcon( icon );
  433. button.addEventListener( 'complete', () => {
  434. for ( const node of this.canvas.nodes ) {
  435. if ( node.value === obj3d ) {
  436. // prevent duplicated node
  437. this.canvas.select( node );
  438. return;
  439. }
  440. }
  441. const node = new editorClass( obj3d );
  442. this.add( node );
  443. this.centralizeNode( node );
  444. } );
  445. search.add( button );
  446. }
  447. } );
  448. }
  449. } );
  450. search.onSubmit( () => {
  451. if ( search.currentFiltered !== null ) {
  452. search.currentFiltered.button.dispatchEvent( new Event( 'complete' ) );
  453. }
  454. } );
  455. this.domElement.append( search.dom );
  456. }
  457. _initNodesContext() {
  458. const context = new ContextMenu( this.domElement );
  459. let isContext = false;
  460. const contextPosition = {};
  461. const add = ( node ) => {
  462. if ( isContext ) {
  463. node.setPosition(
  464. contextPosition.x,
  465. contextPosition.y
  466. );
  467. } else {
  468. this.centralizeNode( node );
  469. }
  470. context.hide();
  471. this.add( node );
  472. this.canvas.select( node );
  473. isContext = false;
  474. };
  475. context.onContext( () => {
  476. isContext = true;
  477. const { relativeClientX, relativeClientY } = this.canvas;
  478. contextPosition.x = relativeClientX;
  479. contextPosition.y = relativeClientY;
  480. } );
  481. //**************//
  482. // INPUTS
  483. //**************//
  484. const createButtonMenu = ( item ) => {
  485. const button = new ButtonInput( item.name );
  486. button.setIcon( `ti ti-${item.icon}` );
  487. let context = null;
  488. if ( item.nodeClass ) {
  489. button.onClick( () => add( new item.nodeClass() ) );
  490. }
  491. if ( item.tip ) {
  492. button.setToolTip( item.tip );
  493. }
  494. if ( item.children ) {
  495. context = new ContextMenu();
  496. for ( const subItem of item.children ) {
  497. const buttonMenu = createButtonMenu( subItem );
  498. context.add( buttonMenu.button, buttonMenu.context );
  499. }
  500. }
  501. return { button, context };
  502. };
  503. for ( const item of NodeList ) {
  504. const buttonMenu = createButtonMenu( item );
  505. context.add( buttonMenu.button, buttonMenu.context );
  506. }
  507. this.nodesContext = context;
  508. }
  509. }