ShaderNode.js 11 KB

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