ScriptableEditor.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. import { BaseNodeEditor } from '../BaseNodeEditor.js';
  2. import { CodeEditorElement } from '../elements/CodeEditorElement.js';
  3. import { disposeScene, resetScene, createElementFromJSON, isGPUNode, onValidType } from '../NodeEditorUtils.js';
  4. import { ScriptableNodeResources, scriptable, js, scriptableValue } from 'three/tsl';
  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 = ScriptableNodeResources.get( 'scene' );
  107. const composer = ScriptableNodeResources.get( 'composer' );
  108. if ( editor ) {
  109. if ( editorOutputAdded && editorOutputAdded.isObject3D === true ) {
  110. editorOutputAdded.removeFromParent();
  111. disposeScene( editorOutputAdded );
  112. resetScene( scene );
  113. } else if ( composer && editorOutputAdded && editorOutputAdded.isPass === true ) {
  114. composer.removePass( editorOutputAdded );
  115. }
  116. if ( editorOutput && editorOutput.isObject3D === true ) {
  117. scene.add( editorOutput );
  118. } else if ( composer && editorOutput && editorOutput.isPass === true ) {
  119. composer.addPass( editorOutput );
  120. }
  121. this.editorOutputAdded = editorOutput;
  122. } else {
  123. if ( editorOutputAdded && editorOutputAdded.isObject3D === true ) {
  124. editorOutputAdded.removeFromParent();
  125. disposeScene( editorOutputAdded );
  126. resetScene( scene );
  127. } else if ( composer && editorOutputAdded && editorOutputAdded.isPass === true ) {
  128. composer.removePass( editorOutputAdded );
  129. }
  130. this.editorOutputAdded = null;
  131. }
  132. }
  133. setEditor( editor ) {
  134. super.setEditor( editor );
  135. this.updateOutputInEditor();
  136. }
  137. clearParameters() {
  138. this.layoutJSON = '';
  139. this.scriptableNode.clearParameters();
  140. for ( const element of this.elements.concat() ) {
  141. if ( element !== this.editorElement && element !== this.title ) {
  142. this.remove( element );
  143. }
  144. }
  145. }
  146. addElementFromJSON( json ) {
  147. const { id, element, inputNode, outputType } = createElementFromJSON( json );
  148. this.add( element );
  149. this.scriptableNode.setParameter( id, inputNode );
  150. if ( outputType ) {
  151. element.setObjectCallback( () => {
  152. return this.scriptableNode.getOutput( id );
  153. } );
  154. }
  155. //
  156. const onUpdate = () => {
  157. const value = element.value;
  158. const paramValue = value && value.isScriptableValueNode ? value : scriptableValue( value );
  159. this.scriptableNode.setParameter( id, paramValue );
  160. this.update();
  161. };
  162. element.addEventListener( 'changeInput', onUpdate );
  163. element.onConnect( onUpdate, true );
  164. //element.onConnect( () => this.getScriptable().call( 'onDeepChange' ), true );
  165. return element;
  166. }
  167. updateLayout( layout = null, force = false ) {
  168. const needsUpdateWidth = this.hasExternalEditor || this.editorElement === null;
  169. if ( this.waitToLayoutJSON !== null ) {
  170. if ( this.waitToLayoutJSON === JSON.stringify( layout || '{}' ) ) {
  171. this.waitToLayoutJSON = null;
  172. if ( needsUpdateWidth ) this.setWidth( layout.width );
  173. } else {
  174. return;
  175. }
  176. }
  177. if ( layout ) {
  178. const layoutCacheKey = JSON.stringify( layout );
  179. if ( this.layoutJSON !== layoutCacheKey || force === true ) {
  180. this.clearParameters();
  181. if ( layout.name ) {
  182. this.setName( layout.name );
  183. }
  184. if ( layout.icon ) {
  185. this.setIcon( layout.icon );
  186. }
  187. if ( needsUpdateWidth ) {
  188. if ( layout.width !== undefined ) {
  189. this.setWidth( layout.width );
  190. } else {
  191. this.setWidth( defaultWidth );
  192. }
  193. }
  194. if ( layout.elements ) {
  195. for ( const element of layout.elements ) {
  196. this.addElementFromJSON( element );
  197. }
  198. if ( this.editorElement ) {
  199. this.remove( this.editorElement );
  200. this.add( this.editorElement );
  201. }
  202. }
  203. this.layoutJSON = layoutCacheKey;
  204. }
  205. } else {
  206. this.setName( defaultTitle );
  207. this.setIcon( null );
  208. this.setWidth( defaultWidth );
  209. this.clearParameters();
  210. }
  211. this.updateOutputConnection();
  212. }
  213. get hasExternalEditor() {
  214. return this.title.getLinkedObject() !== null;
  215. }
  216. get codeNode() {
  217. return this.hasExternalEditor ? this.title.getLinkedObject() : this.editorCodeNode;
  218. }
  219. _initExternalConnection() {
  220. setInputAestheticsFromType( this.title, 'CodeNode' ).onValid( onValidType( 'CodeNode' ) ).onConnect( () => {
  221. this.hasExternalEditor ? this._toExternal() : this._toInternal();
  222. this.update();
  223. }, true );
  224. }
  225. _toInternal() {
  226. if ( this.hasInternalEditor === true ) return;
  227. if ( this.editorElement === null ) {
  228. this.editorElement = new CodeEditorElement( this.editorCodeNode.code );
  229. this.editorElement.addEventListener( 'change', () => {
  230. this.setSource( this.editorElement.source );
  231. this.editorElement.focus();
  232. } );
  233. this.add( this.editorElement );
  234. }
  235. this.setResizable( true );
  236. this.editorElement.setVisible( true );
  237. this.hasInternalEditor = true;
  238. this.update( /*true*/ );
  239. }
  240. _toExternal() {
  241. if ( this.hasInternalEditor === false ) return;
  242. this.editorElement.setVisible( false );
  243. this.setResizable( false );
  244. this.hasInternalEditor = false;
  245. this.update( /*true*/ );
  246. }
  247. serialize( data ) {
  248. super.serialize( data );
  249. data.layoutJSON = this.layoutJSON;
  250. }
  251. deserialize( data ) {
  252. this.updateLayout( JSON.parse( data.layoutJSON || '{}' ), true );
  253. this.waitToLayoutJSON = data.layoutJSON;
  254. super.deserialize( data );
  255. }
  256. }
粤ICP备19079148号