ShaderNode.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. // core
  2. import PropertyNode from './core/PropertyNode.js';
  3. import VarNode from './core/VarNode.js';
  4. // inputs
  5. import ColorNode from './inputs/ColorNode.js';
  6. import FloatNode from './inputs/FloatNode.js';
  7. import Vector2Node from './inputs/Vector2Node.js';
  8. import Vector3Node from './inputs/Vector3Node.js';
  9. import Vector4Node from './inputs/Vector4Node.js';
  10. // accessors
  11. import PositionNode from './accessors/PositionNode.js';
  12. import NormalNode from './accessors/NormalNode.js';
  13. // math
  14. import OperatorNode from './math/OperatorNode.js';
  15. import CondNode from './math/CondNode.js';
  16. import MathNode from './math/MathNode.js';
  17. // utils
  18. import ArrayElementNode from './utils/ArrayElementNode.js';
  19. import JoinNode from './utils/JoinNode.js';
  20. import SplitNode from './utils/SplitNode.js';
  21. // core
  22. import { Vector2, Vector3, Vector4, Color } from 'three';
  23. const NodeHandler = {
  24. construct( NodeClosure, params ) {
  25. const inputs = params.shift();
  26. return NodeClosure( ShaderNodeObjects( inputs ), ...params );
  27. },
  28. get: function ( node, prop ) {
  29. if ( typeof prop === 'string' && node[ prop ] === undefined ) {
  30. if ( /^[xyzwrgbastpq]{1,4}$/.test( prop ) === true ) {
  31. // accessing properties ( swizzle )
  32. prop = prop
  33. .replace( /r|s/g, 'x' )
  34. .replace( /g|t/g, 'y' )
  35. .replace( /b|p/g, 'z' )
  36. .replace( /a|q/g, 'w' );
  37. return ShaderNodeObject( new SplitNode( node, prop ) );
  38. } else if ( /^\d+$/.test( prop ) === true ) {
  39. // accessing array
  40. return ShaderNodeObject( new ArrayElementNode( node, new FloatNode( Number( prop ) ).setConst( true ) ) );
  41. }
  42. }
  43. return node[ prop ];
  44. }
  45. };
  46. const ShaderNodeObject = ( obj ) => {
  47. const type = typeof obj;
  48. if ( type === 'number' ) {
  49. return ShaderNodeObject( new FloatNode( obj ).setConst( true ) );
  50. } else if ( type === 'object' ) {
  51. if ( obj.isNode === true ) {
  52. const node = obj;
  53. if ( node.isProxyNode !== true ) {
  54. node.isProxyNode = true;
  55. return new Proxy( node, NodeHandler );
  56. }
  57. }
  58. }
  59. return obj;
  60. };
  61. const ShaderNodeObjects = ( objects ) => {
  62. for ( const name in objects ) {
  63. objects[ name ] = ShaderNodeObject( objects[ name ] );
  64. }
  65. return objects;
  66. };
  67. const ShaderNodeArray = ( array ) => {
  68. const len = array.length;
  69. for ( let i = 0; i < len; i ++ ) {
  70. array[ i ] = ShaderNodeObject( array[ i ] );
  71. }
  72. return array;
  73. };
  74. const ShaderNodeProxy = ( NodeClass, scope = null, factor = null ) => {
  75. if ( scope === null ) {
  76. return ( ...params ) => {
  77. return ShaderNodeObject( new NodeClass( ...ShaderNodeArray( params ) ) );
  78. };
  79. } else if ( factor === null ) {
  80. return ( ...params ) => {
  81. return ShaderNodeObject( new NodeClass( scope, ...ShaderNodeArray( params ) ) );
  82. };
  83. } else {
  84. factor = ShaderNodeObject( factor );
  85. return ( ...params ) => {
  86. return ShaderNodeObject( new NodeClass( scope, ...ShaderNodeArray( params ), factor ) );
  87. };
  88. }
  89. };
  90. const ShaderNodeScript = function ( jsFunc ) {
  91. return ( inputs, builder ) => {
  92. ShaderNodeObjects( inputs );
  93. return ShaderNodeObject( jsFunc( inputs, builder ) );
  94. };
  95. };
  96. export const ShaderNode = new Proxy( ShaderNodeScript, NodeHandler );
  97. //
  98. // Node Material Shader Syntax
  99. //
  100. export const uniform = new ShaderNode( ( inputNode ) => {
  101. inputNode.setConst( false );
  102. return inputNode;
  103. } );
  104. export const float = ( val ) => {
  105. return ShaderNodeObject( new FloatNode( val ).setConst( true ) );
  106. };
  107. export const color = ( ...params ) => {
  108. return ShaderNodeObject( new ColorNode( new Color( ...params ) ).setConst( true ) );
  109. };
  110. export const join = ( ...params ) => {
  111. return ShaderNodeObject( new JoinNode( ShaderNodeArray( params ) ) );
  112. };
  113. export const cond = ( ...params ) => {
  114. return ShaderNodeObject( new CondNode( ...ShaderNodeArray( params ) ) );
  115. };
  116. export const vec2 = ( ...params ) => {
  117. // Providing one scalar value: This value is used for all components
  118. if ( params.length === 1 ) {
  119. params[ 1 ] = params[ 0 ];
  120. }
  121. return ShaderNodeObject( new Vector2Node( new Vector2( ...params ) ).setConst( true ) );
  122. };
  123. export const vec3 = ( ...params ) => {
  124. // Providing one scalar value: This value is used for all components
  125. if ( params.length === 1 ) {
  126. params[ 1 ] = params[ 2 ] = params[ 0 ];
  127. }
  128. return ShaderNodeObject( new Vector3Node( new Vector3( ...params ) ).setConst( true ) );
  129. };
  130. export const vec4 = ( ...params ) => {
  131. // Providing one scalar value: This value is used for all components
  132. if ( params.length === 1 ) {
  133. params[ 1 ] = params[ 2 ] = params[ 3 ] = params[ 0 ];
  134. }
  135. return ShaderNodeObject( new Vector4Node( new Vector4( ...params ) ).setConst( true ) );
  136. };
  137. export const addTo = ( varNode, ...params ) => {
  138. varNode.node = add( varNode.node, ...ShaderNodeArray( params ) );
  139. return ShaderNodeObject( varNode );
  140. };
  141. export const add = ShaderNodeProxy( OperatorNode, '+' );
  142. export const sub = ShaderNodeProxy( OperatorNode, '-' );
  143. export const mul = ShaderNodeProxy( OperatorNode, '*' );
  144. export const div = ShaderNodeProxy( OperatorNode, '/' );
  145. export const equal = ShaderNodeProxy( OperatorNode, '==' );
  146. export const assign = ShaderNodeProxy( OperatorNode, '=' );
  147. export const greaterThan = ShaderNodeProxy( OperatorNode, '>' );
  148. export const and = ShaderNodeProxy( OperatorNode, '&&' );
  149. export const element = ShaderNodeProxy( ArrayElementNode );
  150. export const normalLocal = new NormalNode( NormalNode.LOCAL );
  151. export const normalWorld = new NormalNode( NormalNode.WORLD );
  152. export const normalView = new NormalNode( NormalNode.VIEW );
  153. export const transformedNormalView = new VarNode( new NormalNode( NormalNode.VIEW ), 'TransformedNormalView', 'vec3' );
  154. export const positionLocal = new PositionNode( PositionNode.LOCAL );
  155. export const positionWorld = new PositionNode( PositionNode.WORLD );
  156. export const positionView = new PositionNode( PositionNode.VIEW );
  157. export const positionViewDirection = new PositionNode( PositionNode.VIEW_DIRECTION );
  158. export const PI = float( 3.141592653589793 );
  159. export const PI2 = float( 6.283185307179586 );
  160. export const PI_HALF = float( 1.5707963267948966 );
  161. export const RECIPROCAL_PI = float( 0.3183098861837907 );
  162. export const RECIPROCAL_PI2 = float( 0.15915494309189535 );
  163. export const EPSILON = float( 1e-6 );
  164. export const diffuseColor = new PropertyNode( 'DiffuseColor', 'vec4' );
  165. export const roughness = new PropertyNode( 'Roughness', 'float' );
  166. export const metalness = new PropertyNode( 'Metalness', 'float' );
  167. export const alphaTest = new PropertyNode( 'AlphaTest', 'float' );
  168. export const specularColor = new PropertyNode( 'SpecularColor', 'color' );
  169. export const abs = ShaderNodeProxy( MathNode, 'abs' );
  170. export const negate = ShaderNodeProxy( MathNode, 'negate' );
  171. export const floor = ShaderNodeProxy( MathNode, 'floor' );
  172. export const mod = ShaderNodeProxy( MathNode, 'mod' );
  173. export const cross = ShaderNodeProxy( MathNode, 'cross' );
  174. export const fract = ShaderNodeProxy( MathNode, 'fract' );
  175. export const round = ShaderNodeProxy( MathNode, 'round' );
  176. export const max = ShaderNodeProxy( MathNode, 'max' );
  177. export const min = ShaderNodeProxy( MathNode, 'min' );
  178. export const sin = ShaderNodeProxy( MathNode, 'sin' );
  179. export const cos = ShaderNodeProxy( MathNode, 'cos' );
  180. export const dot = ShaderNodeProxy( MathNode, 'dot' );
  181. export const normalize = ShaderNodeProxy( MathNode, 'normalize' );
  182. export const sqrt = ShaderNodeProxy( MathNode, 'sqrt' );
  183. export const inversesqrt = ShaderNodeProxy( MathNode, 'inversesqrt' );
  184. export const sign = ShaderNodeProxy( MathNode, 'sign' );
  185. export const dFdx = ShaderNodeProxy( MathNode, 'dFdx' );
  186. export const dFdy = ShaderNodeProxy( MathNode, 'dFdy' );
  187. export const pow = ShaderNodeProxy( MathNode, 'pow' );
  188. export const pow2 = ShaderNodeProxy( MathNode, 'pow', 2 );
  189. export const pow3 = ShaderNodeProxy( MathNode, 'pow', 3 );
  190. export const pow4 = ShaderNodeProxy( MathNode, 'pow', 4 );
  191. export const exp = ShaderNodeProxy( MathNode, 'exp' );
  192. export const exp2 = ShaderNodeProxy( MathNode, 'exp2' );
  193. export const saturate = ShaderNodeProxy( MathNode, 'saturate' );
  194. export const transformDirection = ShaderNodeProxy( MathNode, 'transformDirection' );