ShaderNode.js 13 KB

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