ShaderNodeBaseElements.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // core
  2. //import ArrayUniformNode from '../core/ArrayUniformNode.js';
  3. import AttributeNode from '../core/AttributeNode.js';
  4. import BypassNode from '../core/BypassNode.js';
  5. import CodeNode from '../core/CodeNode.js';
  6. import ContextNode from '../core/ContextNode.js';
  7. import ExpressionNode from '../core/ExpressionNode.js';
  8. import FunctionCallNode from '../core/FunctionCallNode.js';
  9. import FunctionNode from '../core/FunctionNode.js';
  10. import InstanceIndexNode from '../core/InstanceIndexNode.js';
  11. import PropertyNode from '../core/PropertyNode.js';
  12. import UniformNode from '../core/UniformNode.js';
  13. import VarNode from '../core/VarNode.js';
  14. import VaryNode from '../core/VaryNode.js';
  15. // accessors
  16. import BufferNode from '../accessors/BufferNode.js';
  17. import CameraNode from '../accessors/CameraNode.js';
  18. import MaterialNode from '../accessors/MaterialNode.js';
  19. import MaterialReferenceNode from '../accessors/MaterialReferenceNode.js';
  20. import ModelViewProjectionNode from '../accessors/ModelViewProjectionNode.js';
  21. import NormalNode from '../accessors/NormalNode.js';
  22. import Object3DNode from '../accessors/Object3DNode.js';
  23. import PointUVNode from '../accessors/PointUVNode.js';
  24. import PositionNode from '../accessors/PositionNode.js';
  25. import ReferenceNode from '../accessors/ReferenceNode.js';
  26. import StorageBufferNode from '../accessors/StorageBufferNode.js';
  27. import TextureNode from '../accessors/TextureNode.js';
  28. import UVNode from '../accessors/UVNode.js';
  29. // display
  30. import FrontFacingNode from '../display/FrontFacingNode.js';
  31. // gpgpu
  32. import ComputeNode from '../gpgpu/ComputeNode.js';
  33. // math
  34. import MathNode from '../math/MathNode.js';
  35. import OperatorNode from '../math/OperatorNode.js';
  36. import CondNode from '../math/CondNode.js';
  37. // lights
  38. import ReflectedLightNode from '../lights/ReflectedLightNode.js';
  39. // utils
  40. import ArrayElementNode from '../utils/ArrayElementNode.js';
  41. import ConvertNode from '../utils/ConvertNode.js';
  42. // shader node utils
  43. import { ShaderNode, nodeObject, nodeObjects, nodeArray, nodeProxy, nodeImmutable, ConvertType, getConstNodeType, cacheMaps } from './ShaderNode.js';
  44. // shader node utils
  45. export { ShaderNode, nodeObject, nodeObjects, nodeArray, nodeProxy, nodeImmutable };
  46. export const color = new ConvertType( 'color' );
  47. export const float = new ConvertType( 'float', cacheMaps.float );
  48. export const int = new ConvertType( 'int', cacheMaps.int );
  49. export const uint = new ConvertType( 'uint', cacheMaps.uint );
  50. export const bool = new ConvertType( 'bool', cacheMaps.bool );
  51. export const vec2 = new ConvertType( 'vec2' );
  52. export const ivec2 = new ConvertType( 'ivec2' );
  53. export const uvec2 = new ConvertType( 'uvec2' );
  54. export const bvec2 = new ConvertType( 'bvec2' );
  55. export const vec3 = new ConvertType( 'vec3' );
  56. export const ivec3 = new ConvertType( 'ivec3' );
  57. export const uvec3 = new ConvertType( 'uvec3' );
  58. export const bvec3 = new ConvertType( 'bvec3' );
  59. export const vec4 = new ConvertType( 'vec4' );
  60. export const ivec4 = new ConvertType( 'ivec4' );
  61. export const uvec4 = new ConvertType( 'uvec4' );
  62. export const bvec4 = new ConvertType( 'bvec4' );
  63. export const mat3 = new ConvertType( 'mat3' );
  64. export const imat3 = new ConvertType( 'imat3' );
  65. export const umat3 = new ConvertType( 'umat3' );
  66. export const bmat3 = new ConvertType( 'bmat3' );
  67. export const mat4 = new ConvertType( 'mat4' );
  68. export const imat4 = new ConvertType( 'imat4' );
  69. export const umat4 = new ConvertType( 'umat4' );
  70. export const bmat4 = new ConvertType( 'bmat4' );
  71. // core
  72. // @TODO: ArrayUniformNode
  73. export const func = ( code ) => {
  74. const node = nodeObject( new FunctionNode( code ) );
  75. const call = node.call.bind( node );
  76. node.call = ( params ) => nodeObject( call( params ) );
  77. return node;
  78. };
  79. export const uniform = ( nodeOrType ) => {
  80. const nodeType = getConstNodeType( nodeOrType );
  81. // TODO: get ConstNode from .traverse() in the future
  82. const value = nodeOrType.isNode === true ? nodeOrType.node?.value || nodeOrType.value : nodeOrType;
  83. return nodeObject( new UniformNode( value, nodeType ) );
  84. };
  85. export const attribute = ( name, nodeOrType ) => nodeObject( new AttributeNode( name, getConstNodeType( nodeOrType ) ) );
  86. export const property = ( name, nodeOrType ) => nodeObject( new PropertyNode( name, getConstNodeType( nodeOrType ) ) );
  87. export const bypass = nodeProxy( BypassNode );
  88. export const code = nodeProxy( CodeNode );
  89. export const context = nodeProxy( ContextNode );
  90. export const expression = nodeProxy( ExpressionNode );
  91. export const call = nodeProxy( FunctionCallNode );
  92. export const instanceIndex = nodeImmutable( InstanceIndexNode );
  93. export const label = nodeProxy( VarNode );
  94. export const temp = label;
  95. export const vary = nodeProxy( VaryNode );
  96. // accesors
  97. export const buffer = ( value, nodeOrType, count ) => nodeObject( new BufferNode( value, getConstNodeType( nodeOrType ), count ) );
  98. export const storage = ( value, nodeOrType, count ) => nodeObject( new StorageBufferNode( value, getConstNodeType( nodeOrType ), count ) );
  99. export const cameraProjectionMatrix = nodeImmutable( CameraNode, CameraNode.PROJECTION_MATRIX );
  100. export const cameraViewMatrix = nodeImmutable( CameraNode, CameraNode.VIEW_MATRIX );
  101. export const cameraNormalMatrix = nodeImmutable( CameraNode, CameraNode.NORMAL_MATRIX );
  102. export const cameraWorldMatrix = nodeImmutable( CameraNode, CameraNode.WORLD_MATRIX );
  103. export const cameraPosition = nodeImmutable( CameraNode, CameraNode.POSITION );
  104. export const materialAlphaTest = nodeImmutable( MaterialNode, MaterialNode.ALPHA_TEST );
  105. export const materialColor = nodeImmutable( MaterialNode, MaterialNode.COLOR );
  106. export const materialOpacity = nodeImmutable( MaterialNode, MaterialNode.OPACITY );
  107. export const materialSpecular = nodeImmutable( MaterialNode, MaterialNode.SPECULAR );
  108. export const materialRoughness = nodeImmutable( MaterialNode, MaterialNode.ROUGHNESS );
  109. export const materialMetalness = nodeImmutable( MaterialNode, MaterialNode.METALNESS );
  110. export const diffuseColor = nodeImmutable( PropertyNode, 'DiffuseColor', 'vec4' );
  111. export const roughness = nodeImmutable( PropertyNode, 'Roughness', 'float' );
  112. export const metalness = nodeImmutable( PropertyNode, 'Metalness', 'float' );
  113. export const alphaTest = nodeImmutable( PropertyNode, 'AlphaTest', 'float' );
  114. export const specularColor = nodeImmutable( PropertyNode, 'SpecularColor', 'color' );
  115. export const reference = ( name, nodeOrType, object ) => nodeObject( new ReferenceNode( name, getConstNodeType( nodeOrType ), object ) );
  116. export const materialReference = ( name, nodeOrType, material ) => nodeObject( new MaterialReferenceNode( name, getConstNodeType( nodeOrType ), material ) );
  117. export const modelViewProjection = nodeProxy( ModelViewProjectionNode );
  118. export const normalGeometry = nodeImmutable( NormalNode, NormalNode.GEOMETRY );
  119. export const normalLocal = nodeImmutable( NormalNode, NormalNode.LOCAL );
  120. export const normalWorld = nodeImmutable( NormalNode, NormalNode.WORLD );
  121. export const normalView = nodeImmutable( NormalNode, NormalNode.VIEW );
  122. export const transformedNormalView = nodeImmutable( VarNode, normalView, 'TransformedNormalView' );
  123. export const viewMatrix = nodeProxy( Object3DNode, Object3DNode.VIEW_MATRIX );
  124. export const normalMatrix = nodeProxy( Object3DNode, Object3DNode.NORMAL_MATRIX );
  125. export const worldMatrix = nodeProxy( Object3DNode, Object3DNode.WORLD_MATRIX );
  126. export const position = nodeProxy( Object3DNode, Object3DNode.POSITION );
  127. export const viewPosition = nodeProxy( Object3DNode, Object3DNode.VIEW_POSITION );
  128. export const positionGeometry = nodeImmutable( PositionNode, PositionNode.GEOMETRY );
  129. export const positionLocal = nodeImmutable( PositionNode, PositionNode.LOCAL );
  130. export const positionWorld = nodeImmutable( PositionNode, PositionNode.WORLD );
  131. export const positionView = nodeImmutable( PositionNode, PositionNode.VIEW );
  132. export const positionViewDirection = nodeImmutable( PositionNode, PositionNode.VIEW_DIRECTION );
  133. export const texture = nodeProxy( TextureNode );
  134. export const sampler = ( texture ) => nodeObject( new ConvertNode( texture.isNode === true ? texture : new TextureNode( texture ), 'sampler' ) );
  135. export const uv = ( ...params ) => nodeObject( new UVNode( ...params ) );
  136. export const pointUV = nodeImmutable( PointUVNode );
  137. // gpgpu
  138. export const compute = ( node, count, workgroupSize ) => nodeObject( new ComputeNode( nodeObject( node ), count, workgroupSize ) );
  139. // math
  140. export const EPSILON = float( 1e-6 );
  141. export const INFINITY = float( 1e6 );
  142. export const cond = nodeProxy( CondNode );
  143. export const add = nodeProxy( OperatorNode, '+' );
  144. export const sub = nodeProxy( OperatorNode, '-' );
  145. export const mul = nodeProxy( OperatorNode, '*' );
  146. export const div = nodeProxy( OperatorNode, '/' );
  147. export const remainder = nodeProxy( OperatorNode, '%' );
  148. export const equal = nodeProxy( OperatorNode, '==' );
  149. export const assign = nodeProxy( OperatorNode, '=' );
  150. export const lessThan = nodeProxy( OperatorNode, '<' );
  151. export const greaterThan = nodeProxy( OperatorNode, '>' );
  152. export const lessThanEqual = nodeProxy( OperatorNode, '<=' );
  153. export const greaterThanEqual = nodeProxy( OperatorNode, '>=' );
  154. export const and = nodeProxy( OperatorNode, '&&' );
  155. export const or = nodeProxy( OperatorNode, '||' );
  156. export const xor = nodeProxy( OperatorNode, '^^' );
  157. export const bitAnd = nodeProxy( OperatorNode, '&' );
  158. export const bitOr = nodeProxy( OperatorNode, '|' );
  159. export const bitXor = nodeProxy( OperatorNode, '^' );
  160. export const shiftLeft = nodeProxy( OperatorNode, '<<' );
  161. export const shiftRight = nodeProxy( OperatorNode, '>>' );
  162. export const radians = nodeProxy( MathNode, MathNode.RADIANS );
  163. export const degrees = nodeProxy( MathNode, MathNode.DEGREES );
  164. export const exp = nodeProxy( MathNode, MathNode.EXP );
  165. export const exp2 = nodeProxy( MathNode, MathNode.EXP2 );
  166. export const log = nodeProxy( MathNode, MathNode.LOG );
  167. export const log2 = nodeProxy( MathNode, MathNode.LOG2 );
  168. export const sqrt = nodeProxy( MathNode, MathNode.SQRT );
  169. export const inversesqrt = nodeProxy( MathNode, MathNode.INVERSE_SQRT );
  170. export const floor = nodeProxy( MathNode, MathNode.FLOOR );
  171. export const ceil = nodeProxy( MathNode, MathNode.CEIL );
  172. export const normalize = nodeProxy( MathNode, MathNode.NORMALIZE );
  173. export const fract = nodeProxy( MathNode, MathNode.FRACT );
  174. export const sin = nodeProxy( MathNode, MathNode.SIN );
  175. export const cos = nodeProxy( MathNode, MathNode.COS );
  176. export const tan = nodeProxy( MathNode, MathNode.TAN );
  177. export const asin = nodeProxy( MathNode, MathNode.ASIN );
  178. export const acos = nodeProxy( MathNode, MathNode.ACOS );
  179. export const atan = nodeProxy( MathNode, MathNode.ATAN );
  180. export const abs = nodeProxy( MathNode, MathNode.ABS );
  181. export const sign = nodeProxy( MathNode, MathNode.SIGN );
  182. export const length = nodeProxy( MathNode, MathNode.LENGTH );
  183. export const negate = nodeProxy( MathNode, MathNode.NEGATE );
  184. export const invert = nodeProxy( MathNode, MathNode.INVERT );
  185. export const dFdx = nodeProxy( MathNode, MathNode.DFDX );
  186. export const dFdy = nodeProxy( MathNode, MathNode.DFDY );
  187. export const saturate = nodeProxy( MathNode, MathNode.SATURATE );
  188. export const round = nodeProxy( MathNode, MathNode.ROUND );
  189. export const min = nodeProxy( MathNode, MathNode.MIN );
  190. export const max = nodeProxy( MathNode, MathNode.MAX );
  191. export const mod = nodeProxy( MathNode, MathNode.MOD );
  192. export const step = nodeProxy( MathNode, MathNode.STEP );
  193. export const reflect = nodeProxy( MathNode, MathNode.REFLECT );
  194. export const distance = nodeProxy( MathNode, MathNode.DISTANCE );
  195. export const dot = nodeProxy( MathNode, MathNode.DOT );
  196. export const cross = nodeProxy( MathNode, MathNode.CROSS );
  197. export const pow = nodeProxy( MathNode, MathNode.POW );
  198. export const pow2 = nodeProxy( MathNode, MathNode.POW, 2 );
  199. export const pow3 = nodeProxy( MathNode, MathNode.POW, 3 );
  200. export const pow4 = nodeProxy( MathNode, MathNode.POW, 4 );
  201. export const transformDirection = nodeProxy( MathNode, MathNode.TRANSFORM_DIRECTION );
  202. export const mix = nodeProxy( MathNode, MathNode.MIX );
  203. export const clamp = nodeProxy( MathNode, MathNode.CLAMP );
  204. export const refract = nodeProxy( MathNode, MathNode.REFRACT );
  205. export const smoothstep = nodeProxy( MathNode, MathNode.SMOOTHSTEP );
  206. export const faceforward = nodeProxy( MathNode, MathNode.FACEFORWARD );
  207. // display
  208. export const frontFacing = nodeImmutable( FrontFacingNode );
  209. export const faceDirection = sub( mul( float( frontFacing ), 2 ), 1 );
  210. // lights
  211. export const reflectedLight = nodeProxy( ReflectedLightNode );
  212. // utils
  213. export const element = nodeProxy( ArrayElementNode );
  214. // miscellaneous
  215. export const dotNV = saturate( dot( transformedNormalView, positionViewDirection ) );