ShaderNode.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 ) {
  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 ( ( type === 'float' ) || ( type === 'boolean' ) ) {
  58. return nodeObject( getAutoTypedConstNode( obj ) );
  59. } else if ( type && type !== 'string' ) {
  60. return nodeObject( new ConstNode( obj ) );
  61. }
  62. return obj;
  63. };
  64. const ShaderNodeObjects = function ( objects ) {
  65. for ( const name in objects ) {
  66. objects[ name ] = nodeObject( objects[ name ] );
  67. }
  68. return objects;
  69. };
  70. const ShaderNodeArray = function ( array ) {
  71. const len = array.length;
  72. for ( let i = 0; i < len; i ++ ) {
  73. array[ i ] = nodeObject( array[ i ] );
  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 getAutoTypedConstNode = ( value ) => {
  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 );
  138. }
  139. };
  140. const ConvertType = function ( type, cacheMap = null ) {
  141. return ( ...params ) => {
  142. if ( params.length === 0 ) {
  143. return nodeObject( new ConstNode( getValueFromType( type ), type ) );
  144. } else {
  145. if ( type === 'color' && params[ 0 ].isNode !== true ) {
  146. params = [ getValueFromType( type, ...params ) ];
  147. }
  148. if ( params.length === 1 && cacheMap !== null && cacheMap.has( params[ 0 ] ) ) {
  149. return cacheMap.get( params[ 0 ] );
  150. }
  151. const nodes = params.map( getAutoTypedConstNode );
  152. if ( nodes.length === 1 ) {
  153. return nodeObject( nodes[ 0 ].nodeType === type || getValueType( nodes[ 0 ].value ) === type ? nodes[ 0 ] : new ConvertNode( nodes[ 0 ], type ) );
  154. }
  155. return nodeObject( new JoinNode( nodes, type ) );
  156. }
  157. };
  158. };
  159. // exports
  160. // utils
  161. export const getConstNodeType = ( value ) => ( value !== undefined && value !== null ) ? ( value.nodeType || value.convertTo || ( typeof value === 'string' ? value : null ) ) : null;
  162. // shader node base
  163. export function ShaderNode( jsFunc ) {
  164. return new Proxy( new ShaderNodeInternal( jsFunc ), shaderNodeHandler );
  165. }
  166. export const nodeObject = ( val ) => /* new */ ShaderNodeObject( val );
  167. export const nodeObjects = ( val ) => new ShaderNodeObjects( val );
  168. export const nodeArray = ( val ) => new ShaderNodeArray( val );
  169. export const nodeProxy = ( ...val ) => new ShaderNodeProxy( ...val );
  170. export const nodeImmutable = ( ...val ) => new ShaderNodeImmutable( ...val );
  171. export const shader = ( ...val ) => new ShaderNode( ...val );
  172. addNodeClass( ShaderNode );
  173. // types
  174. // @TODO: Maybe export from ConstNode.js?
  175. export const color = new ConvertType( 'color' );
  176. export const float = new ConvertType( 'float', cacheMaps.float );
  177. export const int = new ConvertType( 'int', cacheMaps.int );
  178. export const uint = new ConvertType( 'uint', cacheMaps.uint );
  179. export const bool = new ConvertType( 'bool', cacheMaps.bool );
  180. export const vec2 = new ConvertType( 'vec2' );
  181. export const ivec2 = new ConvertType( 'ivec2' );
  182. export const uvec2 = new ConvertType( 'uvec2' );
  183. export const bvec2 = new ConvertType( 'bvec2' );
  184. export const vec3 = new ConvertType( 'vec3' );
  185. export const ivec3 = new ConvertType( 'ivec3' );
  186. export const uvec3 = new ConvertType( 'uvec3' );
  187. export const bvec3 = new ConvertType( 'bvec3' );
  188. export const vec4 = new ConvertType( 'vec4' );
  189. export const ivec4 = new ConvertType( 'ivec4' );
  190. export const uvec4 = new ConvertType( 'uvec4' );
  191. export const bvec4 = new ConvertType( 'bvec4' );
  192. export const mat3 = new ConvertType( 'mat3' );
  193. export const imat3 = new ConvertType( 'imat3' );
  194. export const umat3 = new ConvertType( 'umat3' );
  195. export const bmat3 = new ConvertType( 'bmat3' );
  196. export const mat4 = new ConvertType( 'mat4' );
  197. export const imat4 = new ConvertType( 'imat4' );
  198. export const umat4 = new ConvertType( 'umat4' );
  199. export const bmat4 = new ConvertType( 'bmat4' );
  200. export const string = ( value = '' ) => nodeObject( new ConstNode( value, 'string' ) );
  201. export const arrayBuffer = ( value ) => nodeObject( new ConstNode( value, 'ArrayBuffer' ) );
  202. addNodeElement( 'color', color );
  203. addNodeElement( 'float', float );
  204. addNodeElement( 'int', int );
  205. addNodeElement( 'uint', uint );
  206. addNodeElement( 'bool', bool );
  207. addNodeElement( 'vec2', vec2 );
  208. addNodeElement( 'ivec2', ivec2 );
  209. addNodeElement( 'uvec2', uvec2 );
  210. addNodeElement( 'bvec2', bvec2 );
  211. addNodeElement( 'vec3', vec3 );
  212. addNodeElement( 'ivec3', ivec3 );
  213. addNodeElement( 'uvec3', uvec3 );
  214. addNodeElement( 'bvec3', bvec3 );
  215. addNodeElement( 'vec4', vec4 );
  216. addNodeElement( 'ivec4', ivec4 );
  217. addNodeElement( 'uvec4', uvec4 );
  218. addNodeElement( 'bvec4', bvec4 );
  219. addNodeElement( 'mat3', mat3 );
  220. addNodeElement( 'imat3', imat3 );
  221. addNodeElement( 'umat3', umat3 );
  222. addNodeElement( 'bmat3', bmat3 );
  223. addNodeElement( 'mat4', mat4 );
  224. addNodeElement( 'imat4', imat4 );
  225. addNodeElement( 'umat4', umat4 );
  226. addNodeElement( 'bmat4', bmat4 );
  227. addNodeElement( 'string', string );
  228. addNodeElement( 'arrayBuffer', arrayBuffer );
  229. // basic nodes
  230. // HACK - we cannot export them from the corresponding files because of the cyclic dependency
  231. export const element = nodeProxy( ArrayElementNode );
  232. export const convert = ( node, types ) => nodeObject( new ConvertNode( nodeObject( node ), types ) );
  233. export const split = ( node, channels ) => nodeObject( new SplitNode( nodeObject( node ), channels ) );
  234. addNodeElement( 'element', element );
  235. addNodeElement( 'convert', convert );