ShaderNode.js 12 KB

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