NodeEditor.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { Canvas, CircleMenu, ButtonInput, ContextMenu, Loader } from '../libs/flow.module.js';
  2. import { StandardMaterialEditor } from './materials/StandardMaterialEditor.js';
  3. import { OperatorEditor } from './math/OperatorEditor.js';
  4. import { NormalizeEditor } from './math/NormalizeEditor.js';
  5. import { InvertEditor } from './math/InvertEditor.js';
  6. import { LimiterEditor } from './math/LimiterEditor.js';
  7. import { DotEditor } from './math/DotEditor.js';
  8. import { PowerEditor } from './math/PowerEditor.js';
  9. import { TrigonometryEditor } from './math/TrigonometryEditor.js';
  10. import { FloatEditor } from './inputs/FloatEditor.js';
  11. import { Vector2Editor } from './inputs/Vector2Editor.js';
  12. import { Vector3Editor } from './inputs/Vector3Editor.js';
  13. import { Vector4Editor } from './inputs/Vector4Editor.js';
  14. import { SliderEditor } from './inputs/SliderEditor.js';
  15. import { ColorEditor } from './inputs/ColorEditor.js';
  16. import { BlendEditor } from './display/BlendEditor.js';
  17. import { UVEditor } from './accessors/UVEditor.js';
  18. import { PositionEditor } from './accessors/PositionEditor.js';
  19. import { NormalEditor } from './accessors/NormalEditor.js';
  20. import { TimerEditor } from './utils/TimerEditor.js';
  21. import { OscillatorEditor } from './utils/OscillatorEditor.js';
  22. import { CheckerEditor } from './procedural/CheckerEditor.js';
  23. import { EventDispatcher } from 'three';
  24. export const ClassLib = {
  25. 'StandardMaterialEditor': StandardMaterialEditor,
  26. 'OperatorEditor': OperatorEditor,
  27. 'NormalizeEditor': NormalizeEditor,
  28. 'InvertEditor': InvertEditor,
  29. 'LimiterEditor': LimiterEditor,
  30. 'DotEditor': DotEditor,
  31. 'PowerEditor': PowerEditor,
  32. 'TrigonometryEditor': TrigonometryEditor,
  33. 'FloatEditor': FloatEditor,
  34. 'Vector2Editor': Vector2Editor,
  35. 'Vector3Editor': Vector3Editor,
  36. 'Vector4Editor': Vector4Editor,
  37. 'SliderEditor': SliderEditor,
  38. 'ColorEditor': ColorEditor,
  39. 'BlendEditor': BlendEditor,
  40. 'UVEditor': UVEditor,
  41. 'PositionEditor': PositionEditor,
  42. 'NormalEditor': NormalEditor,
  43. 'TimerEditor': TimerEditor,
  44. 'OscillatorEditor': OscillatorEditor,
  45. 'CheckerEditor': CheckerEditor
  46. };
  47. export class NodeEditor extends EventDispatcher {
  48. constructor() {
  49. super();
  50. const domElement = document.createElement( 'flow' );
  51. const canvas = new Canvas();
  52. domElement.appendChild( canvas.dom );
  53. this.canvas = canvas;
  54. this.domElement = domElement;
  55. this.nodesContext = null;
  56. this.examplesContext = null;
  57. this._initMenu();
  58. this._initNodesContext();
  59. this._initExamplesContext();
  60. }
  61. add( node ) {
  62. this.canvas.add( node );
  63. return this;
  64. }
  65. get nodes() {
  66. return this.canvas.nodes;
  67. }
  68. newProject() {
  69. this.canvas.clear();
  70. this.dispatchEvent( { type: 'new' } );
  71. }
  72. loadJSON( json ) {
  73. this.canvas.clear();
  74. this.canvas.deserialize( json );
  75. this.dispatchEvent( { type: 'load' } );
  76. }
  77. _initMenu() {
  78. const menu = new CircleMenu();
  79. const menuButton = new ButtonInput().setIcon( 'ti ti-menu-2' );
  80. const examplesButton = new ButtonInput().setIcon( 'ti ti-file-symlink' ).setToolTip( 'Examples' );
  81. const newButton = new ButtonInput().setIcon( 'ti ti-file' ).setToolTip( 'New' );
  82. const openButton = new ButtonInput().setIcon( 'ti ti-upload' ).setToolTip( 'Open' );
  83. const saveButton = new ButtonInput().setIcon( 'ti ti-download' ).setToolTip( 'Save' );
  84. const hideContext = () => {
  85. this.examplesContext.hide();
  86. this.nodesContext.hide();
  87. };
  88. menuButton.onClick( () => {
  89. this.nodesContext.show( 60, 50 );
  90. } );
  91. examplesButton.onClick( () => {
  92. this.examplesContext.show( 60, 175 );
  93. } );
  94. newButton.onClick( () => {
  95. hideContext();
  96. this.newProject();
  97. } );
  98. openButton.onClick( () => {
  99. hideContext();
  100. const input = document.createElement( 'input' );
  101. input.type = 'file';
  102. input.onchange = e => {
  103. const file = e.target.files[ 0 ];
  104. const reader = new FileReader();
  105. reader.readAsText( file, 'UTF-8' );
  106. reader.onload = readerEvent => {
  107. const loader = new Loader( Loader.OBJECTS );
  108. const json = loader.parse( JSON.parse( readerEvent.target.result ), ClassLib );
  109. this.loadJSON( json );
  110. };
  111. };
  112. input.click();
  113. } );
  114. saveButton.onClick( () => {
  115. hideContext();
  116. const json = JSON.stringify( this.canvas.toJSON() );
  117. const a = document.createElement( 'a' );
  118. const file = new Blob( [ json ], { type: 'text/plain' } );
  119. a.href = URL.createObjectURL( file );
  120. a.download = 'node_editor.json';
  121. a.click();
  122. } );
  123. menu.add( menuButton )
  124. .add( newButton )
  125. .add( examplesButton )
  126. .add( openButton )
  127. .add( saveButton );
  128. this.domElement.appendChild( menu.dom );
  129. this.menu = menu;
  130. }
  131. _initExamplesContext() {
  132. const context = new ContextMenu();
  133. //**************//
  134. // MAIN
  135. //**************//
  136. const onClickExample = async ( button ) => {
  137. this.examplesContext.hide();
  138. const filename = button.getExtra();
  139. const loader = new Loader( Loader.OBJECTS );
  140. const json = await loader.load( `./jsm/node-editor/examples/${filename}.json`, ClassLib );
  141. this.loadJSON( json );
  142. };
  143. const addExample = ( context, name, filename = null ) => {
  144. filename = filename || name.replaceAll( ' ', '-' ).toLowerCase();
  145. context.add( new ButtonInput( name )
  146. .setIcon( 'ti ti-file-symlink' )
  147. .onClick( onClickExample )
  148. .setExtra( filename )
  149. );
  150. };
  151. //**************//
  152. // EXAMPLES
  153. //**************//
  154. const basicContext = new ContextMenu();
  155. const advancedContext = new ContextMenu();
  156. addExample( basicContext, 'Animate UV' );
  157. addExample( basicContext, 'Fake top light' );
  158. addExample( basicContext, 'Oscillator color' );
  159. addExample( advancedContext, 'Rim' );
  160. //**************//
  161. // MAIN
  162. //**************//
  163. context.add( new ButtonInput( 'Basic' ), basicContext );
  164. context.add( new ButtonInput( 'Advanced' ), advancedContext );
  165. this.domElement.appendChild( context.dom );
  166. this.examplesContext = context;
  167. }
  168. _initNodesContext() {
  169. const context = new ContextMenu( this.domElement );
  170. let isContext = false;
  171. let contextPosition = {};
  172. const add = ( node ) => {
  173. const canvas = this.canvas;
  174. const canvasRect = canvas.rect;
  175. if ( isContext ) {
  176. node.setPosition(
  177. contextPosition.x,
  178. contextPosition.y
  179. );
  180. } else {
  181. const defaultOffsetX = 350 / 2;
  182. const defaultOffsetY = 20;
  183. node.setPosition(
  184. ( canvas.relativeX + ( canvasRect.width / 2 ) ) - defaultOffsetX,
  185. ( canvas.relativeY + ( canvasRect.height / 2 ) ) - defaultOffsetY
  186. );
  187. }
  188. context.hide();
  189. this.add( node );
  190. this.canvas.select( node );
  191. isContext = false;
  192. };
  193. context.onContext( () => {
  194. isContext = true;
  195. const { relativeClientX, relativeClientY } = this.canvas;
  196. contextPosition.x = relativeClientX;
  197. contextPosition.y = relativeClientY;
  198. } );
  199. //**************//
  200. // INPUTS
  201. //**************//
  202. const inputsContext = new ContextMenu();
  203. const sliderInput = new ButtonInput( 'Slider' ).setIcon( 'ti ti-adjustments-horizontal' )
  204. .onClick( () => add( new SliderEditor() ) );
  205. const floatInput = new ButtonInput( 'Float' ).setIcon( 'ti ti-box-multiple-1' )
  206. .onClick( () => add( new FloatEditor() ) );
  207. const vector2Input = new ButtonInput( 'Vector 2' ).setIcon( 'ti ti-box-multiple-2' )
  208. .onClick( () => add( new Vector2Editor() ) );
  209. const vector3Input = new ButtonInput( 'Vector 3' ).setIcon( 'ti ti-box-multiple-3' )
  210. .onClick( () => add( new Vector3Editor() ) );
  211. const vector4Input = new ButtonInput( 'Vector 4' ).setIcon( 'ti ti-box-multiple-4' )
  212. .onClick( () => add( new Vector4Editor() ) );
  213. const colorInput = new ButtonInput( 'Color' ).setIcon( 'ti ti-palette' )
  214. .onClick( () => add( new ColorEditor() ) );
  215. //const mapInput = new ButtonInput( 'Map' ).setIcon( 'ti ti-photo' );
  216. //const cubeMapInput = new ButtonInput( 'Cube Map' ).setIcon( 'ti ti-box' );
  217. //const integerInput = new ButtonInput( 'Integer' ).setIcon( 'ti ti-list-numbers' );
  218. inputsContext
  219. .add( sliderInput )
  220. .add( floatInput )
  221. .add( vector2Input )
  222. .add( vector3Input )
  223. .add( vector4Input )
  224. .add( colorInput );
  225. //**************//
  226. // MATH
  227. //**************//
  228. const mathContext = new ContextMenu();
  229. const operatorsNode = new ButtonInput( 'Operator' ).setIcon( 'ti ti-math-symbols' )
  230. .onClick( () => add( new OperatorEditor() ) );
  231. const normalizeNode = new ButtonInput( 'Normalize' ).setIcon( 'ti ti-fold' )
  232. .onClick( () => add( new NormalizeEditor() ) );
  233. const invertNode = new ButtonInput( 'Invert' ).setToolTip( 'Negate' ).setIcon( 'ti ti-flip-vertical' )
  234. .onClick( () => add( new InvertEditor() ) );
  235. const limiterNode = new ButtonInput( 'Limiter' ).setToolTip( 'Min / Max' ).setIcon( 'ti ti-arrow-bar-to-up' )
  236. .onClick( () => add( new LimiterEditor() ) );
  237. const dotNode = new ButtonInput( 'Dot Product' ).setIcon( 'ti ti-arrows-up-left' )
  238. .onClick( () => add( new DotEditor() ) );
  239. const powNode = new ButtonInput( 'Power' ).setIcon( 'ti ti-arrow-up-right' )
  240. .onClick( () => add( new PowerEditor() ) );
  241. const triNode = new ButtonInput( 'Trigonometry' ).setToolTip( 'Sin / Cos / Tan' ).setIcon( 'ti ti-wave-sine' )
  242. .onClick( () => add( new TrigonometryEditor() ) );
  243. mathContext
  244. .add( operatorsNode )
  245. .add( invertNode )
  246. .add( limiterNode )
  247. .add( dotNode )
  248. .add( powNode )
  249. .add( triNode )
  250. .add( normalizeNode );
  251. //**************//
  252. // ACCESSORS
  253. //**************//
  254. const accessorsContext = new ContextMenu();
  255. const uvNode = new ButtonInput( 'UV' ).setIcon( 'ti ti-details' )
  256. .onClick( () => add( new UVEditor() ) );
  257. const positionNode = new ButtonInput( 'Position' ).setIcon( 'ti ti-hierarchy' )
  258. .onClick( () => add( new PositionEditor() ) );
  259. const normalNode = new ButtonInput( 'Normal' ).setIcon( 'ti ti-fold-up' )
  260. .onClick( () => add( new NormalEditor() ) );
  261. accessorsContext
  262. .add( uvNode )
  263. .add( positionNode )
  264. .add( normalNode );
  265. //**************//
  266. // PROCEDURAL
  267. //**************//
  268. const proceduralContext = new ContextMenu();
  269. const checkerNode = new ButtonInput( 'Checker' ).setIcon( 'ti ti-border-outer' )
  270. .onClick( () => add( new CheckerEditor() ) );
  271. proceduralContext
  272. .add( checkerNode );
  273. //**************//
  274. // DISPLAY
  275. //**************//
  276. const displayContext = new ContextMenu();
  277. const blendNode = new ButtonInput( 'Blend' ).setIcon( 'ti ti-layers-subtract' )
  278. .onClick( () => add( new BlendEditor() ) );
  279. displayContext
  280. .add( blendNode );
  281. //**************//
  282. // UTILS
  283. //**************//
  284. const utilsContext = new ContextMenu();
  285. const timerNode = new ButtonInput( 'Timer' ).setIcon( 'ti ti-clock' )
  286. .onClick( () => add( new TimerEditor() ) );
  287. const oscNode = new ButtonInput( 'Oscillator' ).setIcon( 'ti ti-wave-sine' )
  288. .onClick( () => add( new OscillatorEditor() ) );
  289. utilsContext
  290. .add( timerNode )
  291. .add( oscNode );
  292. //**************//
  293. // MAIN
  294. //**************//
  295. context.add( new ButtonInput( 'Inputs' ).setIcon( 'ti ti-forms' ), inputsContext );
  296. context.add( new ButtonInput( 'Accessors' ).setIcon( 'ti ti-vector-triangle' ), accessorsContext );
  297. context.add( new ButtonInput( 'Display' ).setIcon( 'ti ti-brightness' ), displayContext );
  298. context.add( new ButtonInput( 'Math' ).setIcon( 'ti ti-calculator' ), mathContext );
  299. context.add( new ButtonInput( 'Procedural' ).setIcon( 'ti ti-infinity' ), proceduralContext );
  300. context.add( new ButtonInput( 'Utils' ).setIcon( 'ti ti-apps' ), utilsContext );
  301. this.nodesContext = context;
  302. }
  303. }