ScriptableEditor.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. import { BaseNodeEditor } from '../BaseNodeEditor.js';
  2. import { CodeEditorElement } from '../elements/CodeEditorElement.js';
  3. import { disposeScene, createElementFromJSON, isGPUNode, onValidType } from '../NodeEditorUtils.js';
  4. import { global, scriptable, js, scriptableValue } from 'three/nodes';
  5. import { getColorFromType, setInputAestheticsFromType, setOutputAestheticsFromType } from '../DataTypeLib.js';
  6. const defaultTitle = 'Scriptable';
  7. const defaultWidth = 500;
  8. export class ScriptableEditor extends BaseNodeEditor {
  9. constructor( source = null, enableEditor = true ) {
  10. let codeNode = null;
  11. let scriptableNode = null;
  12. if ( source && source.isCodeNode ) {
  13. codeNode = source;
  14. } else {
  15. codeNode = js( source || '' );
  16. }
  17. scriptableNode = scriptable( codeNode );
  18. super( defaultTitle, scriptableNode, defaultWidth );
  19. this.scriptableNode = scriptableNode;
  20. this.editorCodeNode = codeNode;
  21. this.editorOutput = null;
  22. this.editorOutputAdded = null;
  23. this.layout = null;
  24. this.editorElement = null;
  25. this.layoutJSON = '';
  26. this.initCacheKey = '';
  27. this.initId = 0;
  28. this.waitToLayoutJSON = null;
  29. this.hasInternalEditor = false;
  30. this._updating = false;
  31. this.onValidElement = () => {};
  32. if ( enableEditor ) {
  33. this.title.setSerializable( true );
  34. this._initExternalConnection();
  35. this._toInternal();
  36. }
  37. const defaultOutput = this.scriptableNode.getDefaultOutput();
  38. defaultOutput.events.addEventListener( 'refresh', () => {
  39. this.update();
  40. } );
  41. this.update();
  42. }
  43. getColor() {
  44. const color = getColorFromType( this.layout ? this.layout.outputType : null );
  45. return color ? color + 'BB' : null;
  46. }
  47. hasJSON() {
  48. return true;
  49. }
  50. exportJSON() {
  51. return this.scriptableNode.toJSON();
  52. }
  53. setSource( source ) {
  54. this.editorCodeNode.code = source;
  55. this.update();
  56. return this;
  57. }
  58. update( force = false ) {
  59. if ( this._updating === true ) return;
  60. this._updating = true;
  61. this.scriptableNode.codeNode = this.codeNode;
  62. this.scriptableNode.needsUpdate = true;
  63. let layout = null;
  64. let scriptableValueOutput = null;
  65. try {
  66. const object = this.scriptableNode.getObject();
  67. layout = this.scriptableNode.getLayout();
  68. this.updateLayout( layout, force );
  69. scriptableValueOutput = this.scriptableNode.getDefaultOutput();
  70. const initCacheKey = typeof object.init === 'function' ? object.init.toString() : '';
  71. if ( initCacheKey !== this.initCacheKey ) {
  72. this.initCacheKey = initCacheKey;
  73. const initId = ++ this.initId;
  74. this.scriptableNode.callAsync( 'init' ).then( () => {
  75. if ( initId === this.initId ) {
  76. this.update();
  77. if ( this.editor ) this.editor.tips.message( 'ScriptEditor: Initialized.' );
  78. }
  79. } );
  80. }
  81. } catch ( e ) {
  82. console.error( e );
  83. if ( this.editor ) this.editor.tips.error( e.message );
  84. }
  85. const editorOutput = scriptableValueOutput ? scriptableValueOutput.value : null;
  86. this.value = isGPUNode( editorOutput ) ? this.scriptableNode : scriptableValueOutput;
  87. this.layout = layout;
  88. this.editorOutput = editorOutput;
  89. this.updateOutputInEditor();
  90. this.updateOutputConnection();
  91. this.invalidate();
  92. this._updating = false;
  93. }
  94. updateOutputConnection() {
  95. const layout = this.layout;
  96. if ( layout ) {
  97. const outputType = layout.outputType;
  98. setOutputAestheticsFromType( this.title, outputType );
  99. } else {
  100. this.title.setOutput( 0 );
  101. }
  102. }
  103. updateOutputInEditor() {
  104. const { editor, editorOutput, editorOutputAdded } = this;
  105. if ( editor && editorOutput === editorOutputAdded ) return;
  106. const scene = global.get( 'scene' );
  107. const composer = global.get( 'composer' );
  108. if ( editor ) {
  109. if ( editorOutputAdded && editorOutputAdded.isObject3D === true ) {
  110. editorOutputAdded.removeFromParent();
  111. disposeScene( editorOutputAdded );
  112. } else if ( composer && editorOutputAdded && editorOutputAdded.isPass === true ) {
  113. composer.removePass( editorOutputAdded );
  114. }
  115. if ( editorOutput && editorOutput.isObject3D === true ) {
  116. scene.add( editorOutput );
  117. } else if ( composer && editorOutput && editorOutput.isPass === true ) {
  118. composer.addPass( editorOutput );
  119. }
  120. this.editorOutputAdded = editorOutput;
  121. } else {
  122. if ( editorOutputAdded && editorOutputAdded.isObject3D === true ) {
  123. editorOutputAdded.removeFromParent();
  124. disposeScene( editorOutputAdded );
  125. } else if ( composer && editorOutputAdded && editorOutputAdded.isPass === true ) {
  126. composer.removePass( editorOutputAdded );
  127. }
  128. this.editorOutputAdded = null;
  129. }
  130. }
  131. setEditor( editor ) {
  132. super.setEditor( editor );
  133. this.updateOutputInEditor();
  134. }
  135. clearParameters() {
  136. this.layoutJSON = '';
  137. this.scriptableNode.clearParameters();
  138. for ( const element of this.elements.concat() ) {
  139. if ( element !== this.editorElement && element !== this.title ) {
  140. this.remove( element );
  141. }
  142. }
  143. }
  144. addElementFromJSON( json ) {
  145. const { id, element, inputNode, outputType } = createElementFromJSON( json );
  146. this.add( element );
  147. this.scriptableNode.setParameter( id, inputNode );
  148. if ( outputType ) {
  149. element.setObjectCallback( () => {
  150. return this.scriptableNode.getOutput( id );
  151. } );
  152. }
  153. //
  154. const onUpdate = () => {
  155. const value = element.value;
  156. const paramValue = value && value.isScriptableValueNode ? value : scriptableValue( value );
  157. this.scriptableNode.setParameter( id, paramValue );
  158. this.update();
  159. };
  160. element.addEventListener( 'changeInput', onUpdate );
  161. element.onConnect( onUpdate, true );
  162. //element.onConnect( () => this.getScriptable().call( 'onDeepChange' ), true );
  163. return element;
  164. }
  165. updateLayout( layout = null, force = false ) {
  166. const needsUpdateWidth = this.hasExternalEditor || this.editorElement === null;
  167. if ( this.waitToLayoutJSON !== null ) {
  168. if ( this.waitToLayoutJSON === JSON.stringify( layout || '{}' ) ) {
  169. this.waitToLayoutJSON = null;
  170. if ( needsUpdateWidth ) this.setWidth( layout.width );
  171. } else {
  172. return;
  173. }
  174. }
  175. if ( layout ) {
  176. const layoutCacheKey = JSON.stringify( layout );
  177. if ( this.layoutJSON !== layoutCacheKey || force === true ) {
  178. this.clearParameters();
  179. if ( layout.name ) {
  180. this.setName( layout.name );
  181. }
  182. if ( layout.icon ) {
  183. this.setIcon( layout.icon );
  184. }
  185. if ( needsUpdateWidth ) {
  186. if ( layout.width !== undefined ) {
  187. this.setWidth( layout.width );
  188. } else {
  189. this.setWidth( defaultWidth );
  190. }
  191. }
  192. if ( layout.elements ) {
  193. for ( const element of layout.elements ) {
  194. this.addElementFromJSON( element );
  195. }
  196. if ( this.editorElement ) {
  197. this.remove( this.editorElement );
  198. this.add( this.editorElement );
  199. }
  200. }
  201. this.layoutJSON = layoutCacheKey;
  202. }
  203. } else {
  204. this.setName( defaultTitle );
  205. this.setIcon( null );
  206. this.setWidth( defaultWidth );
  207. this.clearParameters();
  208. }
  209. this.updateOutputConnection();
  210. }
  211. get hasExternalEditor() {
  212. return this.title.getLinkedObject() !== null;
  213. }
  214. get codeNode() {
  215. return this.hasExternalEditor ? this.title.getLinkedObject() : this.editorCodeNode;
  216. }
  217. _initExternalConnection() {
  218. setInputAestheticsFromType(this.title, 'CodeNode' ).onValid( onValidType( 'CodeNode' ) ).onConnect( () => {
  219. this.hasExternalEditor ? this._toExternal() : this._toInternal();
  220. this.update();
  221. }, true );
  222. }
  223. _toInternal() {
  224. if ( this.hasInternalEditor === true ) return;
  225. if ( this.editorElement === null ) {
  226. this.editorElement = new CodeEditorElement( this.editorCodeNode.code );
  227. this.editorElement.addEventListener( 'change', () => {
  228. this.setSource( this.editorElement.source );
  229. this.editorElement.focus();
  230. } );
  231. this.add( this.editorElement );
  232. }
  233. this.setResizable( true );
  234. this.editorElement.setVisible( true );
  235. this.hasInternalEditor = true;
  236. this.update( /*true*/ );
  237. }
  238. _toExternal() {
  239. if ( this.hasInternalEditor === false ) return;
  240. this.editorElement.setVisible( false );
  241. this.setResizable( false );
  242. this.hasInternalEditor = false;
  243. this.update( /*true*/ );
  244. }
  245. serialize( data ) {
  246. super.serialize( data );
  247. data.layoutJSON = this.layoutJSON;
  248. }
  249. deserialize( data ) {
  250. this.updateLayout( JSON.parse( data.layoutJSON || '{}' ), true );
  251. this.waitToLayoutJSON = data.layoutJSON;
  252. super.deserialize( data );
  253. }
  254. }