ShaderNode.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. import Node 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 } from '../core/NodeUtils.js';
  8. const shaderNodeHandler = {
  9. construct( NodeClosure, params ) {
  10. const inputs = params.shift();
  11. return NodeClosure( nodeObjects( inputs ), ...params );
  12. },
  13. get: function ( node, prop ) {
  14. if ( typeof prop === 'string' && node[ prop ] === undefined ) {
  15. if ( /^[xyzwrgbastpq]{1,4}$/.test( prop ) === true ) {
  16. // accessing properties ( swizzle )
  17. prop = prop
  18. .replace( /r|s/g, 'x' )
  19. .replace( /g|t/g, 'y' )
  20. .replace( /b|p/g, 'z' )
  21. .replace( /a|q/g, 'w' );
  22. return nodeObject( new SplitNode( node, prop ) );
  23. } else if ( /^\d+$/.test( prop ) === true ) {
  24. // accessing array
  25. return nodeObject( new ArrayElementNode( node, new ConstNode( Number( prop ), 'uint' ) ) );
  26. }
  27. }
  28. return node[ prop ];
  29. }
  30. };
  31. const nodeObjectsCacheMap = new WeakMap();
  32. const ShaderNodeObject = function ( obj ) {
  33. const type = typeof obj;
  34. if ( ( type === 'number' ) || ( type === 'boolean' ) ) {
  35. return nodeObject( getAutoTypedConstNode( obj ) );
  36. } else if ( type === 'object' ) {
  37. if ( obj?.isNode === true ) {
  38. let nodeObject = nodeObjectsCacheMap.get( obj );
  39. if ( nodeObject === undefined ) {
  40. nodeObject = new Proxy( obj, shaderNodeHandler );
  41. nodeObjectsCacheMap.set( obj, nodeObject );
  42. nodeObjectsCacheMap.set( nodeObject, nodeObject );
  43. }
  44. return nodeObject;
  45. }
  46. }
  47. return obj;
  48. };
  49. const ShaderNodeObjects = function ( objects ) {
  50. for ( const name in objects ) {
  51. objects[ name ] = nodeObject( objects[ name ] );
  52. }
  53. return objects;
  54. };
  55. const ShaderNodeArray = function ( array ) {
  56. const len = array.length;
  57. for ( let i = 0; i < len; i ++ ) {
  58. array[ i ] = nodeObject( array[ i ] );
  59. }
  60. return array;
  61. };
  62. const ShaderNodeProxy = function ( NodeClass, scope = null, factor = null, settings = null ) {
  63. const assignNode = ( node ) => nodeObject( settings !== null ? Object.assign( node, settings ) : node );
  64. if ( scope === null ) {
  65. return ( ...params ) => {
  66. return assignNode( new NodeClass( ...nodeArray( params ) ) );
  67. };
  68. } else if ( factor !== null ) {
  69. factor = nodeObject( factor );
  70. return ( ...params ) => {
  71. return assignNode( new NodeClass( scope, ...nodeArray( params ), factor ) );
  72. };
  73. } else {
  74. return ( ...params ) => {
  75. return assignNode( new NodeClass( scope, ...nodeArray( params ) ) );
  76. };
  77. }
  78. };
  79. const ShaderNodeImmutable = function ( NodeClass, ...params ) {
  80. return nodeObject( new NodeClass( ...nodeArray( params ) ) );
  81. };
  82. class ShaderNodeInternal extends Node {
  83. constructor( jsFunc ) {
  84. super();
  85. this._jsFunc = jsFunc;
  86. }
  87. call( inputs, builder ) {
  88. inputs = nodeObjects( inputs );
  89. return nodeObject( this._jsFunc( inputs, builder ) );
  90. }
  91. generate( builder, output ) {
  92. const nodeCall = this.call( {}, builder );
  93. if ( nodeCall === undefined ) {
  94. return '';
  95. }
  96. return builder.format( nodeCall.build( builder ), nodeCall.getNodeType( builder ), output );
  97. }
  98. }
  99. const ShaderNodeScript = function ( jsFunc ) {
  100. return new ShaderNodeInternal( jsFunc );
  101. };
  102. export const ShaderNode = new Proxy( ShaderNodeScript, shaderNodeHandler );
  103. export const nodeObject = ( val ) => /* new */ ShaderNodeObject( val );
  104. export const nodeObjects = ( val ) => new ShaderNodeObjects( val );
  105. export const nodeArray = ( val ) => new ShaderNodeArray( val );
  106. export const nodeProxy = ( ...val ) => new ShaderNodeProxy( ...val );
  107. export const nodeImmutable = ( ...val ) => new ShaderNodeImmutable( ...val );
  108. const bools = [ false, true ];
  109. const uints = [ 0, 1, 2, 3 ];
  110. const ints = [ - 1, - 2 ];
  111. 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 ];
  112. const boolsCacheMap = new Map();
  113. for ( const bool of bools ) boolsCacheMap.set( bool, new ConstNode( bool ) );
  114. const uintsCacheMap = new Map();
  115. for ( const uint of uints ) uintsCacheMap.set( uint, new ConstNode( uint, 'uint' ) );
  116. const intsCacheMap = new Map( [ ...uintsCacheMap ].map( el => new ConstNode( el.value, 'int' ) ) );
  117. for ( const int of ints ) intsCacheMap.set( int, new ConstNode( int, 'int' ) );
  118. const floatsCacheMap = new Map( [ ...intsCacheMap ].map( el => new ConstNode( el.value ) ) );
  119. for ( const float of floats ) floatsCacheMap.set( float, new ConstNode( float ) );
  120. for ( const float of floats ) floatsCacheMap.set( - float, new ConstNode( - float ) );
  121. export const cacheMaps = { bool: boolsCacheMap, uint: uintsCacheMap, ints: intsCacheMap, float: floatsCacheMap };
  122. const constNodesCacheMap = new Map( [ ...boolsCacheMap, ...floatsCacheMap ] );
  123. const getAutoTypedConstNode = ( value ) => {
  124. if ( constNodesCacheMap.has( value ) ) {
  125. return constNodesCacheMap.get( value );
  126. } else if ( value.isNode === true ) {
  127. return value;
  128. } else {
  129. return new ConstNode( value );
  130. }
  131. };
  132. export const ConvertType = function ( type, cacheMap = null ) {
  133. return ( ...params ) => {
  134. if ( params.length === 0 ) {
  135. return nodeObject( new ConstNode( getValueFromType( type ), type ) );
  136. } else {
  137. if ( type === 'color' && params[ 0 ].isNode !== true ) {
  138. params = [ getValueFromType( type, ...params ) ];
  139. }
  140. if ( params.length === 1 && cacheMap !== null && cacheMap.has( params[ 0 ] ) ) {
  141. return cacheMap.get( params[ 0 ] );
  142. }
  143. const nodes = params.map( getAutoTypedConstNode );
  144. if ( nodes.length === 1 ) {
  145. return nodeObject( nodes[ 0 ].nodeType === type ? nodes[ 0 ] : new ConvertNode( nodes[ 0 ], type ) );
  146. }
  147. return nodeObject( new JoinNode( nodes, type ) );
  148. }
  149. };
  150. };
  151. export const getConstNodeType = ( value ) => value.nodeType || value.convertTo || ( typeof value === 'string' ? value : null );