ShaderNode.js 11 KB

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