ShaderNode.js 6.0 KB

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