ShaderNode.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import ArrayElementNode from '../utils/ArrayElementNode.js';
  3. import ConvertNode from '../utils/ConvertNode.js';
  4. import JoinNode from '../utils/JoinNode.js';
  5. import SplitNode from '../utils/SplitNode.js';
  6. import SetNode from '../utils/SetNode.js';
  7. import ConstNode from '../core/ConstNode.js';
  8. import { getValueFromType, getValueType } from '../core/NodeUtils.js';
  9. //
  10. let currentStack = null;
  11. const NodeElements = new Map(); // @TODO: Currently only a few nodes are added, probably also add others
  12. export function addNodeElement( name, nodeElement ) {
  13. if ( NodeElements.has( name ) ) {
  14. console.warn( `Redefinition of node element ${ name }` );
  15. return;
  16. }
  17. if ( typeof nodeElement !== 'function' ) throw new Error( `Node element ${ name } is not a function` );
  18. NodeElements.set( name, nodeElement );
  19. }
  20. const parseSwizzle = ( props ) => props.replace( /r|s/g, 'x' ).replace( /g|t/g, 'y' ).replace( /b|p/g, 'z' ).replace( /a|q/g, 'w' );
  21. const shaderNodeHandler = {
  22. setup( NodeClosure, params ) {
  23. const inputs = params.shift();
  24. return NodeClosure( nodeObjects( inputs ), ...params );
  25. },
  26. get( node, prop, nodeObj ) {
  27. if ( typeof prop === 'string' && node[ prop ] === undefined ) {
  28. if ( node.isStackNode !== true && prop === 'assign' ) {
  29. return ( ...params ) => {
  30. currentStack.assign( nodeObj, ...params );
  31. return nodeObj;
  32. };
  33. } else if ( NodeElements.has( prop ) ) {
  34. const nodeElement = NodeElements.get( prop );
  35. return node.isStackNode ? ( ...params ) => nodeObj.add( nodeElement( ...params ) ) : ( ...params ) => nodeElement( nodeObj, ...params );
  36. } else if ( prop === 'self' ) {
  37. return node;
  38. } else if ( prop.endsWith( 'Assign' ) && NodeElements.has( prop.slice( 0, prop.length - 'Assign'.length ) ) ) {
  39. const nodeElement = NodeElements.get( prop.slice( 0, prop.length - 'Assign'.length ) );
  40. return node.isStackNode ? ( ...params ) => nodeObj.assign( params[ 0 ], nodeElement( ...params ) ) : ( ...params ) => nodeObj.assign( nodeElement( nodeObj, ...params ) );
  41. } else if ( /^[xyzwrgbastpq]{1,4}$/.test( prop ) === true ) {
  42. // accessing properties ( swizzle )
  43. prop = parseSwizzle( prop );
  44. return nodeObject( new SplitNode( nodeObj, prop ) );
  45. } else if ( /^set[XYZWRGBASTPQ]{1,4}$/.test( prop ) === true ) {
  46. // set properties ( swizzle )
  47. prop = parseSwizzle( prop.slice( 3 ).toLowerCase() );
  48. // sort to xyzw sequence
  49. prop = prop.split( '' ).sort().join( '' );
  50. return ( value ) => nodeObject( new SetNode( node, prop, value ) );
  51. } else if ( prop === 'width' || prop === 'height' || prop === 'depth' ) {
  52. // accessing property
  53. if ( prop === 'width' ) prop = 'x';
  54. else if ( prop === 'height' ) prop = 'y';
  55. else if ( prop === 'depth' ) prop = 'z';
  56. return nodeObject( new SplitNode( node, prop ) );
  57. } else if ( /^\d+$/.test( prop ) === true ) {
  58. // accessing array
  59. return nodeObject( new ArrayElementNode( nodeObj, new ConstNode( Number( prop ), 'uint' ) ) );
  60. }
  61. }
  62. return Reflect.get( node, prop, nodeObj );
  63. },
  64. set( node, prop, value, nodeObj ) {
  65. if ( typeof prop === 'string' && node[ prop ] === undefined ) {
  66. // setting properties
  67. if ( /^[xyzwrgbastpq]{1,4}$/.test( prop ) === true || prop === 'width' || prop === 'height' || prop === 'depth' || /^\d+$/.test( prop ) === true ) {
  68. nodeObj[ prop ].assign( value );
  69. return true;
  70. }
  71. }
  72. return Reflect.set( node, prop, value, nodeObj );
  73. }
  74. };
  75. const nodeObjectsCacheMap = new WeakMap();
  76. const nodeBuilderFunctionsCacheMap = new WeakMap();
  77. const ShaderNodeObject = function ( obj, altType = null ) {
  78. const type = getValueType( obj );
  79. if ( type === 'node' ) {
  80. let nodeObject = nodeObjectsCacheMap.get( obj );
  81. if ( nodeObject === undefined ) {
  82. nodeObject = new Proxy( obj, shaderNodeHandler );
  83. nodeObjectsCacheMap.set( obj, nodeObject );
  84. nodeObjectsCacheMap.set( nodeObject, nodeObject );
  85. }
  86. return nodeObject;
  87. } else if ( ( altType === null && ( type === 'float' || type === 'boolean' ) ) || ( type && type !== 'shader' && type !== 'string' ) ) {
  88. return nodeObject( getConstNode( obj, altType ) );
  89. } else if ( type === 'shader' ) {
  90. return tslFn( obj );
  91. }
  92. return obj;
  93. };
  94. const ShaderNodeObjects = function ( objects, altType = null ) {
  95. for ( const name in objects ) {
  96. objects[ name ] = nodeObject( objects[ name ], altType );
  97. }
  98. return objects;
  99. };
  100. const ShaderNodeArray = function ( array, altType = null ) {
  101. const len = array.length;
  102. for ( let i = 0; i < len; i ++ ) {
  103. array[ i ] = nodeObject( array[ i ], altType );
  104. }
  105. return array;
  106. };
  107. const ShaderNodeProxy = function ( NodeClass, scope = null, factor = null, settings = null ) {
  108. const assignNode = ( node ) => nodeObject( settings !== null ? Object.assign( node, settings ) : node );
  109. if ( scope === null ) {
  110. return ( ...params ) => {
  111. return assignNode( new NodeClass( ...nodeArray( params ) ) );
  112. };
  113. } else if ( factor !== null ) {
  114. factor = nodeObject( factor );
  115. return ( ...params ) => {
  116. return assignNode( new NodeClass( scope, ...nodeArray( params ), factor ) );
  117. };
  118. } else {
  119. return ( ...params ) => {
  120. return assignNode( new NodeClass( scope, ...nodeArray( params ) ) );
  121. };
  122. }
  123. };
  124. const ShaderNodeImmutable = function ( NodeClass, ...params ) {
  125. return nodeObject( new NodeClass( ...nodeArray( params ) ) );
  126. };
  127. class ShaderCallNodeInternal extends Node {
  128. constructor( shaderNode, inputNodes ) {
  129. super();
  130. this.shaderNode = shaderNode;
  131. this.inputNodes = inputNodes;
  132. }
  133. getNodeType( builder ) {
  134. const { outputNode } = builder.getNodeProperties( this );
  135. return outputNode ? outputNode.getNodeType( builder ) : super.getNodeType( builder );
  136. }
  137. call( builder ) {
  138. const { shaderNode, inputNodes } = this;
  139. if ( shaderNode.layout ) {
  140. let functionNodesCacheMap = nodeBuilderFunctionsCacheMap.get( builder.constructor );
  141. if ( functionNodesCacheMap === undefined ) {
  142. functionNodesCacheMap = new WeakMap();
  143. nodeBuilderFunctionsCacheMap.set( builder.constructor, functionNodesCacheMap );
  144. }
  145. let functionNode = functionNodesCacheMap.get( shaderNode );
  146. if ( functionNode === undefined ) {
  147. functionNode = nodeObject( builder.buildFunctionNode( shaderNode ) );
  148. functionNodesCacheMap.set( shaderNode, functionNode );
  149. }
  150. if ( builder.currentFunctionNode !== null ) {
  151. builder.currentFunctionNode.includes.push( functionNode );
  152. }
  153. return nodeObject( functionNode.call( inputNodes ) );
  154. }
  155. const jsFunc = shaderNode.jsFunc;
  156. const outputNode = inputNodes !== null ? jsFunc( inputNodes, builder.stack, builder ) : jsFunc( builder.stack, builder );
  157. return nodeObject( outputNode );
  158. }
  159. setup( builder ) {
  160. builder.addStack();
  161. builder.stack.outputNode = this.call( builder );
  162. return builder.removeStack();
  163. }
  164. generate( builder, output ) {
  165. const { outputNode } = builder.getNodeProperties( this );
  166. if ( outputNode === null ) {
  167. // TSL: It's recommended to use `tslFn` in setup() pass.
  168. return this.call( builder ).build( builder, output );
  169. }
  170. return super.generate( builder, output );
  171. }
  172. }
  173. class ShaderNodeInternal extends Node {
  174. constructor( jsFunc ) {
  175. super();
  176. this.jsFunc = jsFunc;
  177. this.layout = null;
  178. }
  179. get isArrayInput() {
  180. return /^\((\s+)?\[/.test( this.jsFunc.toString() );
  181. }
  182. setLayout( layout ) {
  183. this.layout = layout;
  184. return this;
  185. }
  186. call( inputs = null ) {
  187. nodeObjects( inputs );
  188. return nodeObject( new ShaderCallNodeInternal( this, inputs ) );
  189. }
  190. setup() {
  191. return this.call();
  192. }
  193. }
  194. const bools = [ false, true ];
  195. const uints = [ 0, 1, 2, 3 ];
  196. const ints = [ - 1, - 2 ];
  197. const floats = [ 0.5, 1.5, 1 / 3, 1e-6, 1e6, Math.PI, Math.PI * 2, 1 / Math.PI, 2 / Math.PI, 1 / ( Math.PI * 2 ), Math.PI / 2 ];
  198. const boolsCacheMap = new Map();
  199. for ( const bool of bools ) boolsCacheMap.set( bool, new ConstNode( bool ) );
  200. const uintsCacheMap = new Map();
  201. for ( const uint of uints ) uintsCacheMap.set( uint, new ConstNode( uint, 'uint' ) );
  202. const intsCacheMap = new Map( [ ...uintsCacheMap ].map( el => new ConstNode( el.value, 'int' ) ) );
  203. for ( const int of ints ) intsCacheMap.set( int, new ConstNode( int, 'int' ) );
  204. const floatsCacheMap = new Map( [ ...intsCacheMap ].map( el => new ConstNode( el.value ) ) );
  205. for ( const float of floats ) floatsCacheMap.set( float, new ConstNode( float ) );
  206. for ( const float of floats ) floatsCacheMap.set( - float, new ConstNode( - float ) );
  207. const cacheMaps = { bool: boolsCacheMap, uint: uintsCacheMap, ints: intsCacheMap, float: floatsCacheMap };
  208. const constNodesCacheMap = new Map( [ ...boolsCacheMap, ...floatsCacheMap ] );
  209. const getConstNode = ( value, type ) => {
  210. if ( constNodesCacheMap.has( value ) ) {
  211. return constNodesCacheMap.get( value );
  212. } else if ( value.isNode === true ) {
  213. return value;
  214. } else {
  215. return new ConstNode( value, type );
  216. }
  217. };
  218. const safeGetNodeType = ( node ) => {
  219. try {
  220. return node.getNodeType();
  221. } catch ( _ ) {
  222. return undefined;
  223. }
  224. };
  225. const ConvertType = function ( type, cacheMap = null ) {
  226. return ( ...params ) => {
  227. if ( params.length === 0 || ( ! [ 'bool', 'float', 'int', 'uint' ].includes( type ) && params.every( param => typeof param !== 'object' ) ) ) {
  228. params = [ getValueFromType( type, ...params ) ];
  229. }
  230. if ( params.length === 1 && cacheMap !== null && cacheMap.has( params[ 0 ] ) ) {
  231. return nodeObject( cacheMap.get( params[ 0 ] ) );
  232. }
  233. if ( params.length === 1 ) {
  234. const node = getConstNode( params[ 0 ], type );
  235. if ( safeGetNodeType( node ) === type ) return nodeObject( node );
  236. return nodeObject( new ConvertNode( node, type ) );
  237. }
  238. const nodes = params.map( param => getConstNode( param ) );
  239. return nodeObject( new JoinNode( nodes, type ) );
  240. };
  241. };
  242. // exports
  243. export const defined = ( value ) => value && value.value;
  244. // utils
  245. export const getConstNodeType = ( value ) => ( value !== undefined && value !== null ) ? ( value.nodeType || value.convertTo || ( typeof value === 'string' ? value : null ) ) : null;
  246. // shader node base
  247. export function ShaderNode( jsFunc ) {
  248. return new Proxy( new ShaderNodeInternal( jsFunc ), shaderNodeHandler );
  249. }
  250. export const nodeObject = ( val, altType = null ) => /* new */ ShaderNodeObject( val, altType );
  251. export const nodeObjects = ( val, altType = null ) => new ShaderNodeObjects( val, altType );
  252. export const nodeArray = ( val, altType = null ) => new ShaderNodeArray( val, altType );
  253. export const nodeProxy = ( ...params ) => new ShaderNodeProxy( ...params );
  254. export const nodeImmutable = ( ...params ) => new ShaderNodeImmutable( ...params );
  255. export const tslFn = ( jsFunc ) => {
  256. const shaderNode = new ShaderNode( jsFunc );
  257. const fn = ( ...params ) => {
  258. let inputs;
  259. nodeObjects( params );
  260. if ( params[ 0 ] && params[ 0 ].isNode ) {
  261. inputs = [ ...params ];
  262. } else {
  263. inputs = params[ 0 ];
  264. }
  265. return shaderNode.call( inputs );
  266. };
  267. fn.shaderNode = shaderNode;
  268. fn.setLayout = ( layout ) => {
  269. shaderNode.setLayout( layout );
  270. return fn;
  271. };
  272. return fn;
  273. };
  274. addNodeClass( 'ShaderNode', ShaderNode );
  275. //
  276. export const setCurrentStack = ( stack ) => {
  277. if ( currentStack === stack ) {
  278. //throw new Error( 'Stack already defined.' );
  279. }
  280. currentStack = stack;
  281. };
  282. export const getCurrentStack = () => currentStack;
  283. export const If = ( ...params ) => currentStack.if( ...params );
  284. export function append( node ) {
  285. if ( currentStack ) currentStack.add( node );
  286. return node;
  287. }
  288. addNodeElement( 'append', append );
  289. // types
  290. // @TODO: Maybe export from ConstNode.js?
  291. export const color = new ConvertType( 'color' );
  292. export const float = new ConvertType( 'float', cacheMaps.float );
  293. export const int = new ConvertType( 'int', cacheMaps.ints );
  294. export const uint = new ConvertType( 'uint', cacheMaps.uint );
  295. export const bool = new ConvertType( 'bool', cacheMaps.bool );
  296. export const vec2 = new ConvertType( 'vec2' );
  297. export const ivec2 = new ConvertType( 'ivec2' );
  298. export const uvec2 = new ConvertType( 'uvec2' );
  299. export const bvec2 = new ConvertType( 'bvec2' );
  300. export const vec3 = new ConvertType( 'vec3' );
  301. export const ivec3 = new ConvertType( 'ivec3' );
  302. export const uvec3 = new ConvertType( 'uvec3' );
  303. export const bvec3 = new ConvertType( 'bvec3' );
  304. export const vec4 = new ConvertType( 'vec4' );
  305. export const ivec4 = new ConvertType( 'ivec4' );
  306. export const uvec4 = new ConvertType( 'uvec4' );
  307. export const bvec4 = new ConvertType( 'bvec4' );
  308. export const mat2 = new ConvertType( 'mat2' );
  309. export const imat2 = new ConvertType( 'imat2' );
  310. export const umat2 = new ConvertType( 'umat2' );
  311. export const bmat2 = new ConvertType( 'bmat2' );
  312. export const mat3 = new ConvertType( 'mat3' );
  313. export const imat3 = new ConvertType( 'imat3' );
  314. export const umat3 = new ConvertType( 'umat3' );
  315. export const bmat3 = new ConvertType( 'bmat3' );
  316. export const mat4 = new ConvertType( 'mat4' );
  317. export const imat4 = new ConvertType( 'imat4' );
  318. export const umat4 = new ConvertType( 'umat4' );
  319. export const bmat4 = new ConvertType( 'bmat4' );
  320. export const string = ( value = '' ) => nodeObject( new ConstNode( value, 'string' ) );
  321. export const arrayBuffer = ( value ) => nodeObject( new ConstNode( value, 'ArrayBuffer' ) );
  322. addNodeElement( 'color', color );
  323. addNodeElement( 'float', float );
  324. addNodeElement( 'int', int );
  325. addNodeElement( 'uint', uint );
  326. addNodeElement( 'bool', bool );
  327. addNodeElement( 'vec2', vec2 );
  328. addNodeElement( 'ivec2', ivec2 );
  329. addNodeElement( 'uvec2', uvec2 );
  330. addNodeElement( 'bvec2', bvec2 );
  331. addNodeElement( 'vec3', vec3 );
  332. addNodeElement( 'ivec3', ivec3 );
  333. addNodeElement( 'uvec3', uvec3 );
  334. addNodeElement( 'bvec3', bvec3 );
  335. addNodeElement( 'vec4', vec4 );
  336. addNodeElement( 'ivec4', ivec4 );
  337. addNodeElement( 'uvec4', uvec4 );
  338. addNodeElement( 'bvec4', bvec4 );
  339. addNodeElement( 'mat2', mat2 );
  340. addNodeElement( 'imat2', imat2 );
  341. addNodeElement( 'umat2', umat2 );
  342. addNodeElement( 'bmat2', bmat2 );
  343. addNodeElement( 'mat3', mat3 );
  344. addNodeElement( 'imat3', imat3 );
  345. addNodeElement( 'umat3', umat3 );
  346. addNodeElement( 'bmat3', bmat3 );
  347. addNodeElement( 'mat4', mat4 );
  348. addNodeElement( 'imat4', imat4 );
  349. addNodeElement( 'umat4', umat4 );
  350. addNodeElement( 'bmat4', bmat4 );
  351. addNodeElement( 'string', string );
  352. addNodeElement( 'arrayBuffer', arrayBuffer );
  353. // basic nodes
  354. // HACK - we cannot export them from the corresponding files because of the cyclic dependency
  355. export const element = nodeProxy( ArrayElementNode );
  356. export const convert = ( node, types ) => nodeObject( new ConvertNode( nodeObject( node ), types ) );
  357. export const split = ( node, channels ) => nodeObject( new SplitNode( nodeObject( node ), channels ) );
  358. addNodeElement( 'element', element );
  359. addNodeElement( 'convert', convert );