ScriptableNode.html 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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">CodeNode</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"><code>codeNode</code></td>
  55. <td class="description last"><p>The code node.<br/>Default is <code>null</code>.</p></td>
  56. </tr>
  57. <tr>
  58. <td class="name"><code>parameters</code></td>
  59. <td class="description last"><p>The parameters definition.<br/>Default is <code>{}</code>.</p></td>
  60. </tr>
  61. </tbody>
  62. </table>
  63. </div>
  64. </div>
  65. <h2 class="subsection-title">Properties</h2>
  66. <div class="member">
  67. <h3 class="name" id="codeNode" translate="no">.<a href="#codeNode">codeNode</a><span class="type-signature"> : <a href="CodeNode.html">CodeNode</a></span> </h3>
  68. <div class="description">
  69. <p>The code node.<br/>Default is <code>null</code>.</p>
  70. </div>
  71. </div>
  72. <div class="member">
  73. <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>
  74. <div class="description">
  75. <p>This flag can be used for type testing.<br/>Default is <code>true</code>.</p>
  76. </div>
  77. </div>
  78. <div class="member">
  79. <h3 class="name" id="parameters" translate="no">.<a href="#parameters">parameters</a><span class="type-signature"> : Object</span> </h3>
  80. <div class="description">
  81. <p>The parameters definition.<br/>Default is <code>{}</code>.</p>
  82. </div>
  83. </div>
  84. <div class="member">
  85. <h3 class="name" id="source" translate="no">.<a href="#source">source</a><span class="type-signature"> : string</span> </h3>
  86. <div class="description">
  87. <p>The source code of the scriptable node.</p>
  88. </div>
  89. </div>
  90. <h2 class="subsection-title">Methods</h2>
  91. <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">any</span> )</span><span class="type-signature"> : <a href="global.html#any">any</a></span> </h3>
  92. <div class="method">
  93. <div class="description">
  94. <p>Calls a function from the script.</p>
  95. </div>
  96. <table class="params">
  97. <tbody>
  98. <tr>
  99. <td class="name"><code>name</code></td>
  100. <td class="description last"><p>The function name.</p></td>
  101. </tr>
  102. <tr>
  103. <td class="name"><code>params</code></td>
  104. <td class="description last"><p>A list of parameters.</p></td>
  105. </tr>
  106. </tbody>
  107. </table>
  108. <dl class="details">
  109. <dt class="tag-returns"><strong>Returns:</strong> The result of the function call.</dt>
  110. </dl>
  111. </div>
  112. <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">any</span> )</span><span class="type-signature"> : Promise.&lt;<a href="global.html#any">any</a>></span> <span class="type-signature">(async) </span></h3>
  113. <div class="method">
  114. <div class="description">
  115. <p>Asynchronously calls a function from the script.</p>
  116. </div>
  117. <table class="params">
  118. <tbody>
  119. <tr>
  120. <td class="name"><code>name</code></td>
  121. <td class="description last"><p>The function name.</p></td>
  122. </tr>
  123. <tr>
  124. <td class="name"><code>params</code></td>
  125. <td class="description last"><p>A list of parameters.</p></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="clearParameters" translate="no">.<a href="#clearParameters">clearParameters</a><span class="signature">()</span><span class="type-signature"> : <a href="ScriptableNode.html">ScriptableNode</a></span> </h3>
  134. <div class="method">
  135. <div class="description">
  136. <p>Deletes all parameters from the script.</p>
  137. </div>
  138. <dl class="details">
  139. <dt class="tag-returns"><strong>Returns:</strong> A reference to this node.</dt>
  140. </dl>
  141. </div>
  142. <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>
  143. <div class="method">
  144. <div class="description">
  145. <p>Deletes a parameter from the script.</p>
  146. </div>
  147. <table class="params">
  148. <tbody>
  149. <tr>
  150. <td class="name"><code>name</code></td>
  151. <td class="description last"><p>The parameter to remove.</p></td>
  152. </tr>
  153. </tbody>
  154. </table>
  155. <dl class="details">
  156. <dt class="tag-returns"><strong>Returns:</strong> A reference to this node.</dt>
  157. </dl>
  158. </div>
  159. <h3 class="name name-method" id="dispose" translate="no">.<a href="#dispose">dispose</a><span class="signature">()</span> </h3>
  160. <div class="method">
  161. <div class="description">
  162. <p>Frees all internal resources.</p>
  163. </div>
  164. <dl class="details">
  165. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Node.html#dispose">Node#dispose</a></dt>
  166. </dl>
  167. </div>
  168. <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>
  169. <div class="method">
  170. <div class="description">
  171. <p>Returns default output of the script.</p>
  172. </div>
  173. <dl class="details">
  174. <dt class="tag-returns"><strong>Returns:</strong> The default output.</dt>
  175. </dl>
  176. </div>
  177. <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>
  178. <div class="method">
  179. <div class="description">
  180. <p>Returns default node output of the script.</p>
  181. </div>
  182. <dl class="details">
  183. <dt class="tag-returns"><strong>Returns:</strong> The default node output.</dt>
  184. </dl>
  185. </div>
  186. <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>
  187. <div class="method">
  188. <div class="description">
  189. <p>Returns an input from the layout with the given id/name.</p>
  190. </div>
  191. <table class="params">
  192. <tbody>
  193. <tr>
  194. <td class="name"><code>id</code></td>
  195. <td class="description last"><p>The id/name of the input.</p></td>
  196. </tr>
  197. </tbody>
  198. </table>
  199. <dl class="details">
  200. <dt class="tag-returns"><strong>Returns:</strong> The element entry.</dt>
  201. </dl>
  202. </div>
  203. <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>
  204. <div class="method">
  205. <div class="description">
  206. <p>Returns the layout of the script.</p>
  207. </div>
  208. <dl class="details">
  209. <dt class="tag-returns"><strong>Returns:</strong> The script's layout.</dt>
  210. </dl>
  211. </div>
  212. <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>
  213. <div class="method">
  214. <div class="description">
  215. <p>Gets the value of a local script variable.</p>
  216. </div>
  217. <table class="params">
  218. <tbody>
  219. <tr>
  220. <td class="name"><code>name</code></td>
  221. <td class="description last"><p>The variable name.</p></td>
  222. </tr>
  223. </tbody>
  224. </table>
  225. <dl class="details">
  226. <dt class="tag-returns"><strong>Returns:</strong> The value.</dt>
  227. </dl>
  228. </div>
  229. <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>
  230. <div class="method">
  231. <div class="description">
  232. <p>Returns a function created from the node's script.</p>
  233. </div>
  234. <dl class="details">
  235. <dt class="tag-returns"><strong>Returns:</strong> The function representing the node's code.</dt>
  236. </dl>
  237. </div>
  238. <h3 class="name name-method" id="getNodeType" translate="no">.<a href="#getNodeType">getNodeType</a><span class="signature">( builder : <span class="param-type">NodeBuilder</span> )</span><span class="type-signature"> : string</span> </h3>
  239. <div class="method">
  240. <div class="description">
  241. <p>Overwritten since the node types is inferred from the script's output.</p>
  242. </div>
  243. <table class="params">
  244. <tbody>
  245. <tr>
  246. <td class="name"><code>builder</code></td>
  247. <td class="description last"><p>The current node builder</p></td>
  248. </tr>
  249. </tbody>
  250. </table>
  251. <dl class="details">
  252. <dt class="tag-overrides"><strong>Overrides:</strong> <a href="Node.html#getNodeType">Node#getNodeType</a></dt>
  253. </dl>
  254. <dl class="details">
  255. <dt class="tag-returns"><strong>Returns:</strong> The node type.</dt>
  256. </dl>
  257. </div>
  258. <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>
  259. <div class="method">
  260. <div class="description">
  261. <p>Returns an object representation of the script.</p>
  262. </div>
  263. <dl class="details">
  264. <dt class="tag-returns"><strong>Returns:</strong> The result object.</dt>
  265. </dl>
  266. </div>
  267. <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>
  268. <div class="method">
  269. <div class="description">
  270. <p>Returns a script output for the given name.</p>
  271. </div>
  272. <table class="params">
  273. <tbody>
  274. <tr>
  275. <td class="name"><code>name</code></td>
  276. <td class="description last"><p>The name of the output.</p></td>
  277. </tr>
  278. </tbody>
  279. </table>
  280. <dl class="details">
  281. <dt class="tag-returns"><strong>Returns:</strong> The node value.</dt>
  282. </dl>
  283. </div>
  284. <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>
  285. <div class="method">
  286. <div class="description">
  287. <p>Returns an output from the layout with the given id/name.</p>
  288. </div>
  289. <table class="params">
  290. <tbody>
  291. <tr>
  292. <td class="name"><code>id</code></td>
  293. <td class="description last"><p>The id/name of the output.</p></td>
  294. </tr>
  295. </tbody>
  296. </table>
  297. <dl class="details">
  298. <dt class="tag-returns"><strong>Returns:</strong> The element entry.</dt>
  299. </dl>
  300. </div>
  301. <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>
  302. <div class="method">
  303. <div class="description">
  304. <p>Returns a parameter for the given name</p>
  305. </div>
  306. <table class="params">
  307. <tbody>
  308. <tr>
  309. <td class="name"><code>name</code></td>
  310. <td class="description last"><p>The name of the parameter.</p></td>
  311. </tr>
  312. </tbody>
  313. </table>
  314. <dl class="details">
  315. <dt class="tag-returns"><strong>Returns:</strong> The node value.</dt>
  316. </dl>
  317. </div>
  318. <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>
  319. <div class="method">
  320. <div class="description">
  321. <p>Returns the value of this node which is the value of
  322. the default output.</p>
  323. </div>
  324. <dl class="details">
  325. <dt class="tag-returns"><strong>Returns:</strong> The value.</dt>
  326. </dl>
  327. </div>
  328. <h3 class="name name-method" id="onRefresh" translate="no">.<a href="#onRefresh">onRefresh</a><span class="signature">()</span> </h3>
  329. <div class="method">
  330. <div class="description">
  331. <p>Event listener for the <code>refresh</code> event.</p>
  332. </div>
  333. </div>
  334. <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>
  335. <div class="method">
  336. <div class="description">
  337. <p>Refreshes the script node.</p>
  338. </div>
  339. <table class="params">
  340. <tbody>
  341. <tr>
  342. <td class="name"><code>output</code></td>
  343. <td class="description last"><p>An optional output.<br/>Default is <code>null</code>.</p></td>
  344. </tr>
  345. </tbody>
  346. </table>
  347. </div>
  348. <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>
  349. <div class="method">
  350. <div class="description">
  351. <p>Sets the reference of a local script variable.</p>
  352. </div>
  353. <table class="params">
  354. <tbody>
  355. <tr>
  356. <td class="name"><code>name</code></td>
  357. <td class="description last"><p>The variable name.</p></td>
  358. </tr>
  359. <tr>
  360. <td class="name"><code>value</code></td>
  361. <td class="description last"><p>The reference to set.</p></td>
  362. </tr>
  363. </tbody>
  364. </table>
  365. <dl class="details">
  366. <dt class="tag-returns"><strong>Returns:</strong> The resource map</dt>
  367. </dl>
  368. </div>
  369. <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">Node</span> )</span><span class="type-signature"> : <a href="ScriptableNode.html">ScriptableNode</a></span> </h3>
  370. <div class="method">
  371. <div class="description">
  372. <p>Defines a script output for the given name and value.</p>
  373. </div>
  374. <table class="params">
  375. <tbody>
  376. <tr>
  377. <td class="name"><code>name</code></td>
  378. <td class="description last"><p>The name of the output.</p></td>
  379. </tr>
  380. <tr>
  381. <td class="name"><code>value</code></td>
  382. <td class="description last"><p>The node value.</p></td>
  383. </tr>
  384. </tbody>
  385. </table>
  386. <dl class="details">
  387. <dt class="tag-returns"><strong>Returns:</strong> A reference to this node.</dt>
  388. </dl>
  389. </div>
  390. <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">any</span> )</span><span class="type-signature"> : <a href="ScriptableNode.html">ScriptableNode</a></span> </h3>
  391. <div class="method">
  392. <div class="description">
  393. <p>Sets a value for the given parameter name.</p>
  394. </div>
  395. <table class="params">
  396. <tbody>
  397. <tr>
  398. <td class="name"><code>name</code></td>
  399. <td class="description last"><p>The parameter name.</p></td>
  400. </tr>
  401. <tr>
  402. <td class="name"><code>value</code></td>
  403. <td class="description last"><p>The parameter value.</p></td>
  404. </tr>
  405. </tbody>
  406. </table>
  407. <dl class="details">
  408. <dt class="tag-returns"><strong>Returns:</strong> A reference to this node.</dt>
  409. </dl>
  410. </div>
  411. <h2 class="subsection-title">Source</h2>
  412. <p>
  413. <a href="https://github.com/mrdoob/three.js/blob/master/src/nodes/code/ScriptableNode.js" target="_blank" rel="noopener" translate="no">src/nodes/code/ScriptableNode.js</a>
  414. </p>
  415. </article>
  416. </section>
  417. <script src="../scripts/linenumber.js"></script>
  418. <script src="../scripts/page.js"></script>
  419. </body>
  420. </html>
粤ICP备19079148号