ScriptableNode.html 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>ScriptableNode - Three.js Docs</title>
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <script src="../scripts/highlight.min.js"></script>
  8. <link type="text/css" rel="stylesheet" href="../styles/highlight-three.css">
  9. <link type="text/css" rel="stylesheet" href="../styles/page.css">
  10. </head>
  11. <body>
  12. <p class="inheritance" translate="no"><a href="EventDispatcher.html">EventDispatcher</a> → <a href="Node.html">Node</a> → </p>
  13. <h1 translate="no">ScriptableNode</h1>
  14. <section>
  15. <header>
  16. <div class="class-description"><p>This type of node allows to implement nodes with custom scripts. The script
  17. section is represented as an instance of <code>CodeNode</code> written with JavaScript.
  18. The script itself must adhere to a specific structure.</p>
  19. <ul>
  20. <li>main(): Executed once by default and every time <code>node.needsUpdate</code> is set.</li>
  21. <li>layout: The layout object defines the script's interface (inputs and outputs).</li>
  22. </ul></div>
  23. <h2>Code Example</h2>
  24. <div translate="no"><pre><code class="language-js">ScriptableNodeResources.set( 'TSL', TSL );
  25. const scriptableNode = scriptable( js( `
  26. layout = {
  27. outputType: 'node',
  28. elements: [
  29. { name: 'source', inputType: 'node' },
  30. ]
  31. };
  32. const { mul, oscSine } = TSL;
  33. function main() {
  34. const source = parameters.get( 'source' ) || float();
  35. return mul( source, oscSine() ) );
  36. }
  37. ` ) );
  38. scriptableNode.setParameter( 'source', color( 1, 0, 0 ) );
  39. const material = new THREE.MeshBasicNodeMaterial();
  40. material.colorNode = scriptableNode;
  41. </code></pre></div>
  42. </header>
  43. <article>
  44. <div class="container-overview">
  45. <h2>Constructor</h2>
  46. <h3 class="name name-method" id="ScriptableNode" translate="no">new <a href="#ScriptableNode">ScriptableNode</a><span class="signature">( codeNode : <span class="param-type"><a href="CodeNode.html">CodeNode</a></span>, parameters : <span class="param-type">Object</span> )</span> </h3>
  47. <div class="method">
  48. <div class="description">
  49. <p>Constructs a new scriptable node.</p>
  50. </div>
  51. <table class="params">
  52. <tbody>
  53. <tr>
  54. <td class="name">
  55. <strong>codeNode</strong>
  56. </td>
  57. <td class="description last">
  58. <p>The code node.</p>
  59. <p>Default is <code>null</code>.</p>
  60. </td>
  61. </tr>
  62. <tr>
  63. <td class="name">
  64. <strong>parameters</strong>
  65. </td>
  66. <td class="description last">
  67. <p>The parameters definition.</p>
  68. <p>Default is <code>{}</code>.</p>
  69. </td>
  70. </tr>
  71. </tbody>
  72. </table>
  73. </div>
  74. </div>
  75. <h2 class="subsection-title">Properties</h2>
  76. <div class="member">
  77. <h3 class="name" id="codeNode" translate="no">.<a href="#codeNode">codeNode</a><span class="type-signature"> : <a href="CodeNode.html">CodeNode</a></span> </h3>
  78. <div class="description">
  79. <p>The code node.</p>
  80. <p>Default is <code>null</code>.</p>
  81. </div>
  82. </div>
  83. <div class="member">
  84. <h3 class="name" id="isScriptableNode" translate="no">.<a href="#isScriptableNode">isScriptableNode</a><span class="type-signature"> : boolean</span> <span class="type-signature">(readonly) </span></h3>
  85. <div class="description">
  86. <p>This flag can be used for type testing.</p>
  87. <p>Default is <code>true</code>.</p>
  88. </div>
  89. </div>
  90. <div class="member">
  91. <h3 class="name" id="parameters" translate="no">.<a href="#parameters">parameters</a><span class="type-signature"> : Object</span> </h3>
  92. <div class="description">
  93. <p>The parameters definition.</p>
  94. <p>Default is <code>{}</code>.</p>
  95. </div>
  96. </div>
  97. <div class="member">
  98. <h3 class="name" id="source" translate="no">.<a href="#source">source</a><span class="type-signature"> : string</span> </h3>
  99. <div class="description">
  100. <p>The source code of the scriptable node.</p>
  101. </div>
  102. </div>
  103. <h2 class="subsection-title">Methods</h2>
  104. <h3 class="name name-method" id="call" translate="no">.<a href="#call">call</a><span class="signature">( name : <span class="param-type">string</span>, &hellip;params : <span class="param-type"><a href="global.html#any">any</a></span> )</span><span class="type-signature"> : <a href="global.html#any">any</a></span> </h3>
  105. <div class="method">
  106. <div class="description">
  107. <p>Calls a function from the script.</p>
  108. </div>
  109. <table class="params">
  110. <tbody>
  111. <tr>
  112. <td class="name">
  113. <strong>name</strong>
  114. </td>
  115. <td class="description last">
  116. <p>The function name.</p>
  117. </td>
  118. </tr>
  119. <tr>
  120. <td class="name">
  121. <strong>params</strong>
  122. </td>
  123. <td class="description last">
  124. <p>A list of parameters.</p>
  125. </td>
  126. </tr>
  127. </tbody>
  128. </table>
  129. <dl class="details">
  130. <dt class="tag-returns"><strong>Returns:</strong> The result of the function call.</dt>
  131. </dl>
  132. </div>
  133. <h3 class="name name-method" id="callAsync" translate="no">.<a href="#callAsync">callAsync</a><span class="signature">( name : <span class="param-type">string</span>, &hellip;params : <span class="param-type"><a href="global.html#any">any</a></span> )</span><span class="type-signature"> : Promise.&lt;<a href="global.html#any">any</a>></span> <span class="type-signature">(async) </span></h3>
  134. <div class="method">
  135. <div class="description">
  136. <p>Asynchronously calls a function from the script.</p>
  137. </div>
  138. <table class="params">
  139. <tbody>
  140. <tr>
  141. <td class="name">
  142. <strong>name</strong>
  143. </td>
  144. <td class="description last">
  145. <p>The function name.</p>
  146. </td>
  147. </tr>
  148. <tr>
  149. <td class="name">
  150. <strong>params</strong>
  151. </td>
  152. <td class="description last">
  153. <p>A list of parameters.</p>
  154. </td>
  155. </tr>
  156. </tbody>
  157. </table>
  158. <dl class="details">
  159. <dt class="tag-returns"><strong>Returns:</strong> The result of the function call.</dt>
  160. </dl>
  161. </div>
  162. <h3 class="name name-method" id="clearParameters" translate="no">.<a href="#clearParameters">clearParameters</a><span class="signature">()</span><span class="type-signature"> : <a href="ScriptableNode.html">ScriptableNode</a></span> </h3>
  163. <div class="method">
  164. <div class="description">
  165. <p>Deletes all parameters from the script.</p>
  166. </div>
  167. <dl class="details">
  168. <dt class="tag-returns"><strong>Returns:</strong> A reference to this node.</dt>
  169. </dl>
  170. </div>
  171. <h3 class="name name-method" id="deleteParameter" translate="no">.<a href="#deleteParameter">deleteParameter</a><span class="signature">( name : <span class="param-type">string</span> )</span><span class="type-signature"> : <a href="ScriptableNode.html">ScriptableNode</a></span> </h3>
  172. <div class="method">
  173. <div class="description">
  174. <p>Deletes a parameter from the script.</p>
  175. </div>
  176. <table class="params">
  177. <tbody>
  178. <tr>
  179. <td class="name">
  180. <strong>name</strong>
  181. </td>
  182. <td class="description last">
  183. <p>The parameter to remove.</p>
  184. </td>
  185. </tr>
  186. </tbody>
  187. </table>
  188. <dl class="details">
  189. <dt class="tag-returns"><strong>Returns:</strong> A reference to this node.</dt>
  190. </dl>
  191. </div>
  192. <h3 class="name name-method" id="dispose" translate="no">.<a href="#dispose">dispose</a><span class="signature">()</span> </h3>
  193. <div class="method">
  194. <div class="description">
  195. <p>Frees all internal resources.</p>
  196. </div>
  197. <dl class="details">
  198. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Node.html#dispose">Node#dispose</a></dt>
  199. </dl>
  200. </div>
  201. <h3 class="name name-method" id="getDefaultOutput" translate="no">.<a href="#getDefaultOutput">getDefaultOutput</a><span class="signature">()</span><span class="type-signature"> : <a href="ScriptableValueNode.html">ScriptableValueNode</a></span> </h3>
  202. <div class="method">
  203. <div class="description">
  204. <p>Returns default output of the script.</p>
  205. </div>
  206. <dl class="details">
  207. <dt class="tag-returns"><strong>Returns:</strong> The default output.</dt>
  208. </dl>
  209. </div>
  210. <h3 class="name name-method" id="getDefaultOutputNode" translate="no">.<a href="#getDefaultOutputNode">getDefaultOutputNode</a><span class="signature">()</span><span class="type-signature"> : <a href="Node.html">Node</a></span> </h3>
  211. <div class="method">
  212. <div class="description">
  213. <p>Returns default node output of the script.</p>
  214. </div>
  215. <dl class="details">
  216. <dt class="tag-returns"><strong>Returns:</strong> The default node output.</dt>
  217. </dl>
  218. </div>
  219. <h3 class="name name-method" id="getInputLayout" translate="no">.<a href="#getInputLayout">getInputLayout</a><span class="signature">( id : <span class="param-type">string</span> )</span><span class="type-signature"> : Object</span> </h3>
  220. <div class="method">
  221. <div class="description">
  222. <p>Returns an input from the layout with the given id/name.</p>
  223. </div>
  224. <table class="params">
  225. <tbody>
  226. <tr>
  227. <td class="name">
  228. <strong>id</strong>
  229. </td>
  230. <td class="description last">
  231. <p>The id/name of the input.</p>
  232. </td>
  233. </tr>
  234. </tbody>
  235. </table>
  236. <dl class="details">
  237. <dt class="tag-returns"><strong>Returns:</strong> The element entry.</dt>
  238. </dl>
  239. </div>
  240. <h3 class="name name-method" id="getLayout" translate="no">.<a href="#getLayout">getLayout</a><span class="signature">()</span><span class="type-signature"> : Object</span> </h3>
  241. <div class="method">
  242. <div class="description">
  243. <p>Returns the layout of the script.</p>
  244. </div>
  245. <dl class="details">
  246. <dt class="tag-returns"><strong>Returns:</strong> The script's layout.</dt>
  247. </dl>
  248. </div>
  249. <h3 class="name name-method" id="getLocal" translate="no">.<a href="#getLocal">getLocal</a><span class="signature">( name : <span class="param-type">string</span> )</span><span class="type-signature"> : Object</span> </h3>
  250. <div class="method">
  251. <div class="description">
  252. <p>Gets the value of a local script variable.</p>
  253. </div>
  254. <table class="params">
  255. <tbody>
  256. <tr>
  257. <td class="name">
  258. <strong>name</strong>
  259. </td>
  260. <td class="description last">
  261. <p>The variable name.</p>
  262. </td>
  263. </tr>
  264. </tbody>
  265. </table>
  266. <dl class="details">
  267. <dt class="tag-returns"><strong>Returns:</strong> The value.</dt>
  268. </dl>
  269. </div>
  270. <h3 class="name name-method" id="getMethod" translate="no">.<a href="#getMethod">getMethod</a><span class="signature">()</span><span class="type-signature"> : function</span> </h3>
  271. <div class="method">
  272. <div class="description">
  273. <p>Returns a function created from the node's script.</p>
  274. </div>
  275. <dl class="details">
  276. <dt class="tag-returns"><strong>Returns:</strong> The function representing the node's code.</dt>
  277. </dl>
  278. </div>
  279. <h3 class="name name-method" id="getNodeType" translate="no">.<a href="#getNodeType">getNodeType</a><span class="signature">( builder : <span class="param-type"><a href="NodeBuilder.html">NodeBuilder</a></span> )</span><span class="type-signature"> : string</span> </h3>
  280. <div class="method">
  281. <div class="description">
  282. <p>Overwritten since the node types is inferred from the script's output.</p>
  283. </div>
  284. <table class="params">
  285. <tbody>
  286. <tr>
  287. <td class="name">
  288. <strong>builder</strong>
  289. </td>
  290. <td class="description last">
  291. <p>The current node builder</p>
  292. </td>
  293. </tr>
  294. </tbody>
  295. </table>
  296. <dl class="details">
  297. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Node.html#getNodeType">Node#getNodeType</a></dt>
  298. </dl>
  299. <dl class="details">
  300. <dt class="tag-returns"><strong>Returns:</strong> The node type.</dt>
  301. </dl>
  302. </div>
  303. <h3 class="name name-method" id="getObject" translate="no">.<a href="#getObject">getObject</a><span class="signature">()</span><span class="type-signature"> : Object</span> </h3>
  304. <div class="method">
  305. <div class="description">
  306. <p>Returns an object representation of the script.</p>
  307. </div>
  308. <dl class="details">
  309. <dt class="tag-returns"><strong>Returns:</strong> The result object.</dt>
  310. </dl>
  311. </div>
  312. <h3 class="name name-method" id="getOutput" translate="no">.<a href="#getOutput">getOutput</a><span class="signature">( name : <span class="param-type">string</span> )</span><span class="type-signature"> : <a href="ScriptableValueNode.html">ScriptableValueNode</a></span> </h3>
  313. <div class="method">
  314. <div class="description">
  315. <p>Returns a script output for the given name.</p>
  316. </div>
  317. <table class="params">
  318. <tbody>
  319. <tr>
  320. <td class="name">
  321. <strong>name</strong>
  322. </td>
  323. <td class="description last">
  324. <p>The name of the output.</p>
  325. </td>
  326. </tr>
  327. </tbody>
  328. </table>
  329. <dl class="details">
  330. <dt class="tag-returns"><strong>Returns:</strong> The node value.</dt>
  331. </dl>
  332. </div>
  333. <h3 class="name name-method" id="getOutputLayout" translate="no">.<a href="#getOutputLayout">getOutputLayout</a><span class="signature">( id : <span class="param-type">string</span> )</span><span class="type-signature"> : Object</span> </h3>
  334. <div class="method">
  335. <div class="description">
  336. <p>Returns an output from the layout with the given id/name.</p>
  337. </div>
  338. <table class="params">
  339. <tbody>
  340. <tr>
  341. <td class="name">
  342. <strong>id</strong>
  343. </td>
  344. <td class="description last">
  345. <p>The id/name of the output.</p>
  346. </td>
  347. </tr>
  348. </tbody>
  349. </table>
  350. <dl class="details">
  351. <dt class="tag-returns"><strong>Returns:</strong> The element entry.</dt>
  352. </dl>
  353. </div>
  354. <h3 class="name name-method" id="getParameter" translate="no">.<a href="#getParameter">getParameter</a><span class="signature">( name : <span class="param-type">string</span> )</span><span class="type-signature"> : <a href="ScriptableValueNode.html">ScriptableValueNode</a></span> </h3>
  355. <div class="method">
  356. <div class="description">
  357. <p>Returns a parameter for the given name</p>
  358. </div>
  359. <table class="params">
  360. <tbody>
  361. <tr>
  362. <td class="name">
  363. <strong>name</strong>
  364. </td>
  365. <td class="description last">
  366. <p>The name of the parameter.</p>
  367. </td>
  368. </tr>
  369. </tbody>
  370. </table>
  371. <dl class="details">
  372. <dt class="tag-returns"><strong>Returns:</strong> The node value.</dt>
  373. </dl>
  374. </div>
  375. <h3 class="name name-method" id="getValue" translate="no">.<a href="#getValue">getValue</a><span class="signature">()</span><span class="type-signature"> : <a href="Node.html">Node</a></span> </h3>
  376. <div class="method">
  377. <div class="description">
  378. <p>Returns the value of this node which is the value of
  379. the default output.</p>
  380. </div>
  381. <dl class="details">
  382. <dt class="tag-returns"><strong>Returns:</strong> The value.</dt>
  383. </dl>
  384. </div>
  385. <h3 class="name name-method" id="onRefresh" translate="no">.<a href="#onRefresh">onRefresh</a><span class="signature">()</span> </h3>
  386. <div class="method">
  387. <div class="description">
  388. <p>Event listener for the <code>refresh</code> event.</p>
  389. </div>
  390. </div>
  391. <h3 class="name name-method" id="refresh" translate="no">.<a href="#refresh">refresh</a><span class="signature">( output : <span class="param-type">string</span> )</span> </h3>
  392. <div class="method">
  393. <div class="description">
  394. <p>Refreshes the script node.</p>
  395. </div>
  396. <table class="params">
  397. <tbody>
  398. <tr>
  399. <td class="name">
  400. <strong>output</strong>
  401. </td>
  402. <td class="description last">
  403. <p>An optional output.</p>
  404. <p>Default is <code>null</code>.</p>
  405. </td>
  406. </tr>
  407. </tbody>
  408. </table>
  409. </div>
  410. <h3 class="name name-method" id="setLocal" translate="no">.<a href="#setLocal">setLocal</a><span class="signature">( name : <span class="param-type">string</span>, value : <span class="param-type">Object</span> )</span><span class="type-signature"> : <a href="Resources.html">Resources</a></span> </h3>
  411. <div class="method">
  412. <div class="description">
  413. <p>Sets the reference of a local script variable.</p>
  414. </div>
  415. <table class="params">
  416. <tbody>
  417. <tr>
  418. <td class="name">
  419. <strong>name</strong>
  420. </td>
  421. <td class="description last">
  422. <p>The variable name.</p>
  423. </td>
  424. </tr>
  425. <tr>
  426. <td class="name">
  427. <strong>value</strong>
  428. </td>
  429. <td class="description last">
  430. <p>The reference to set.</p>
  431. </td>
  432. </tr>
  433. </tbody>
  434. </table>
  435. <dl class="details">
  436. <dt class="tag-returns"><strong>Returns:</strong> The resource map</dt>
  437. </dl>
  438. </div>
  439. <h3 class="name name-method" id="setOutput" translate="no">.<a href="#setOutput">setOutput</a><span class="signature">( name : <span class="param-type">string</span>, value : <span class="param-type"><a href="Node.html">Node</a></span> )</span><span class="type-signature"> : <a href="ScriptableNode.html">ScriptableNode</a></span> </h3>
  440. <div class="method">
  441. <div class="description">
  442. <p>Defines a script output for the given name and value.</p>
  443. </div>
  444. <table class="params">
  445. <tbody>
  446. <tr>
  447. <td class="name">
  448. <strong>name</strong>
  449. </td>
  450. <td class="description last">
  451. <p>The name of the output.</p>
  452. </td>
  453. </tr>
  454. <tr>
  455. <td class="name">
  456. <strong>value</strong>
  457. </td>
  458. <td class="description last">
  459. <p>The node value.</p>
  460. </td>
  461. </tr>
  462. </tbody>
  463. </table>
  464. <dl class="details">
  465. <dt class="tag-returns"><strong>Returns:</strong> A reference to this node.</dt>
  466. </dl>
  467. </div>
  468. <h3 class="name name-method" id="setParameter" translate="no">.<a href="#setParameter">setParameter</a><span class="signature">( name : <span class="param-type">string</span>, value : <span class="param-type"><a href="global.html#any">any</a></span> )</span><span class="type-signature"> : <a href="ScriptableNode.html">ScriptableNode</a></span> </h3>
  469. <div class="method">
  470. <div class="description">
  471. <p>Sets a value for the given parameter name.</p>
  472. </div>
  473. <table class="params">
  474. <tbody>
  475. <tr>
  476. <td class="name">
  477. <strong>name</strong>
  478. </td>
  479. <td class="description last">
  480. <p>The parameter name.</p>
  481. </td>
  482. </tr>
  483. <tr>
  484. <td class="name">
  485. <strong>value</strong>
  486. </td>
  487. <td class="description last">
  488. <p>The parameter value.</p>
  489. </td>
  490. </tr>
  491. </tbody>
  492. </table>
  493. <dl class="details">
  494. <dt class="tag-returns"><strong>Returns:</strong> A reference to this node.</dt>
  495. </dl>
  496. </div>
  497. <h2 class="subsection-title">Source</h2>
  498. <p>
  499. <a href="https://github.com/mrdoob/three.js/blob/master/src/nodes/code/ScriptableNode.js" translate="no" target="_blank" rel="noopener">src/nodes/code/ScriptableNode.js</a>
  500. </p>
  501. </article>
  502. </section>
  503. <script src="../scripts/linenumber.js"></script>
  504. <script src="../scripts/page.js"></script>
  505. </body>
  506. </html>
粤ICP备19079148号