ScriptableEditor.js 8.3 KB

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