ShaderNode.js 8.5 KB

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