ShaderNode.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. construct( 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 ShaderNodeInternal extends Node {
  107. constructor( jsFunc ) {
  108. super();
  109. this._jsFunc = jsFunc;
  110. }
  111. call( inputs, stack, builder ) {
  112. inputs = nodeObjects( inputs );
  113. return nodeObject( this._jsFunc( inputs, stack, builder ) );
  114. }
  115. getNodeType( builder ) {
  116. const { outputNode } = builder.getNodeProperties( this );
  117. return outputNode ? outputNode.getNodeType( builder ) : super.getNodeType( builder );
  118. }
  119. construct( builder ) {
  120. builder.addStack();
  121. builder.stack.outputNode = nodeObject( this._jsFunc( builder.stack, builder ) );
  122. return builder.removeStack();
  123. }
  124. }
  125. const bools = [ false, true ];
  126. const uints = [ 0, 1, 2, 3 ];
  127. const ints = [ - 1, - 2 ];
  128. 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 ];
  129. const boolsCacheMap = new Map();
  130. for ( const bool of bools ) boolsCacheMap.set( bool, new ConstNode( bool ) );
  131. const uintsCacheMap = new Map();
  132. for ( const uint of uints ) uintsCacheMap.set( uint, new ConstNode( uint, 'uint' ) );
  133. const intsCacheMap = new Map( [ ...uintsCacheMap ].map( el => new ConstNode( el.value, 'int' ) ) );
  134. for ( const int of ints ) intsCacheMap.set( int, new ConstNode( int, 'int' ) );
  135. const floatsCacheMap = new Map( [ ...intsCacheMap ].map( el => new ConstNode( el.value ) ) );
  136. for ( const float of floats ) floatsCacheMap.set( float, new ConstNode( float ) );
  137. for ( const float of floats ) floatsCacheMap.set( - float, new ConstNode( - float ) );
  138. const cacheMaps = { bool: boolsCacheMap, uint: uintsCacheMap, ints: intsCacheMap, float: floatsCacheMap };
  139. const constNodesCacheMap = new Map( [ ...boolsCacheMap, ...floatsCacheMap ] );
  140. const getConstNode = ( value, type ) => {
  141. if ( constNodesCacheMap.has( value ) ) {
  142. return constNodesCacheMap.get( value );
  143. } else if ( value.isNode === true ) {
  144. return value;
  145. } else {
  146. return new ConstNode( value, type );
  147. }
  148. };
  149. const safeGetNodeType = ( node ) => {
  150. try {
  151. return node.getNodeType();
  152. } catch ( _ ) {
  153. return undefined;
  154. }
  155. };
  156. const ConvertType = function ( type, cacheMap = null ) {
  157. return ( ...params ) => {
  158. if ( params.length === 0 || ( ! [ 'bool', 'float', 'int', 'uint' ].includes( type ) && params.every( param => typeof param !== 'object' ) ) ) {
  159. params = [ getValueFromType( type, ...params ) ];
  160. }
  161. if ( params.length === 1 && cacheMap !== null && cacheMap.has( params[ 0 ] ) ) {
  162. return nodeObject( cacheMap.get( params[ 0 ] ) );
  163. }
  164. if ( params.length === 1 ) {
  165. const node = getConstNode( params[ 0 ], type );
  166. if ( safeGetNodeType( node ) === type ) return nodeObject( node );
  167. return nodeObject( new ConvertNode( node, type ) );
  168. }
  169. const nodes = params.map( param => getConstNode( param ) );
  170. return nodeObject( new JoinNode( nodes, type ) );
  171. };
  172. };
  173. // exports
  174. // utils
  175. export const getConstNodeType = ( value ) => ( value !== undefined && value !== null ) ? ( value.nodeType || value.convertTo || ( typeof value === 'string' ? value : null ) ) : null;
  176. // shader node base
  177. export function ShaderNode( jsFunc ) {
  178. return new Proxy( new ShaderNodeInternal( jsFunc ), shaderNodeHandler );
  179. }
  180. export const nodeObject = ( val, altType = null ) => /* new */ ShaderNodeObject( val, altType );
  181. export const nodeObjects = ( val, altType = null ) => new ShaderNodeObjects( val, altType );
  182. export const nodeArray = ( val, altType = null ) => new ShaderNodeArray( val, altType );
  183. export const nodeProxy = ( ...params ) => new ShaderNodeProxy( ...params );
  184. export const nodeImmutable = ( ...params ) => new ShaderNodeImmutable( ...params );
  185. export const shader = ( jsFunc ) => { // @deprecated, r154
  186. console.warn( 'TSL: shader() is deprecated. Use tslFn() instead.' );
  187. return new ShaderNode( jsFunc );
  188. };
  189. export const tslFn = ( jsFunc ) => {
  190. let shaderNode = null;
  191. return ( ...params ) => {
  192. if ( shaderNode === null ) shaderNode = new ShaderNode( jsFunc );
  193. return shaderNode.call( ...params );
  194. };
  195. };
  196. addNodeClass( ShaderNode );
  197. // types
  198. // @TODO: Maybe export from ConstNode.js?
  199. export const color = new ConvertType( 'color' );
  200. export const float = new ConvertType( 'float', cacheMaps.float );
  201. export const int = new ConvertType( 'int', cacheMaps.int );
  202. export const uint = new ConvertType( 'uint', cacheMaps.uint );
  203. export const bool = new ConvertType( 'bool', cacheMaps.bool );
  204. export const vec2 = new ConvertType( 'vec2' );
  205. export const ivec2 = new ConvertType( 'ivec2' );
  206. export const uvec2 = new ConvertType( 'uvec2' );
  207. export const bvec2 = new ConvertType( 'bvec2' );
  208. export const vec3 = new ConvertType( 'vec3' );
  209. export const ivec3 = new ConvertType( 'ivec3' );
  210. export const uvec3 = new ConvertType( 'uvec3' );
  211. export const bvec3 = new ConvertType( 'bvec3' );
  212. export const vec4 = new ConvertType( 'vec4' );
  213. export const ivec4 = new ConvertType( 'ivec4' );
  214. export const uvec4 = new ConvertType( 'uvec4' );
  215. export const bvec4 = new ConvertType( 'bvec4' );
  216. export const mat3 = new ConvertType( 'mat3' );
  217. export const imat3 = new ConvertType( 'imat3' );
  218. export const umat3 = new ConvertType( 'umat3' );
  219. export const bmat3 = new ConvertType( 'bmat3' );
  220. export const mat4 = new ConvertType( 'mat4' );
  221. export const imat4 = new ConvertType( 'imat4' );
  222. export const umat4 = new ConvertType( 'umat4' );
  223. export const bmat4 = new ConvertType( 'bmat4' );
  224. export const string = ( value = '' ) => nodeObject( new ConstNode( value, 'string' ) );
  225. export const arrayBuffer = ( value ) => nodeObject( new ConstNode( value, 'ArrayBuffer' ) );
  226. addNodeElement( 'color', color );
  227. addNodeElement( 'float', float );
  228. addNodeElement( 'int', int );
  229. addNodeElement( 'uint', uint );
  230. addNodeElement( 'bool', bool );
  231. addNodeElement( 'vec2', vec2 );
  232. addNodeElement( 'ivec2', ivec2 );
  233. addNodeElement( 'uvec2', uvec2 );
  234. addNodeElement( 'bvec2', bvec2 );
  235. addNodeElement( 'vec3', vec3 );
  236. addNodeElement( 'ivec3', ivec3 );
  237. addNodeElement( 'uvec3', uvec3 );
  238. addNodeElement( 'bvec3', bvec3 );
  239. addNodeElement( 'vec4', vec4 );
  240. addNodeElement( 'ivec4', ivec4 );
  241. addNodeElement( 'uvec4', uvec4 );
  242. addNodeElement( 'bvec4', bvec4 );
  243. addNodeElement( 'mat3', mat3 );
  244. addNodeElement( 'imat3', imat3 );
  245. addNodeElement( 'umat3', umat3 );
  246. addNodeElement( 'bmat3', bmat3 );
  247. addNodeElement( 'mat4', mat4 );
  248. addNodeElement( 'imat4', imat4 );
  249. addNodeElement( 'umat4', umat4 );
  250. addNodeElement( 'bmat4', bmat4 );
  251. addNodeElement( 'string', string );
  252. addNodeElement( 'arrayBuffer', arrayBuffer );
  253. // basic nodes
  254. // HACK - we cannot export them from the corresponding files because of the cyclic dependency
  255. export const element = nodeProxy( ArrayElementNode );
  256. export const convert = ( node, types ) => nodeObject( new ConvertNode( nodeObject( node ), types ) );
  257. export const split = ( node, channels ) => nodeObject( new SplitNode( nodeObject( node ), channels ) );
  258. addNodeElement( 'element', element );
  259. addNodeElement( 'convert', convert );