ShaderNode.js 12 KB

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