2
0

NodeEditor.js 14 KB

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