123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408 |
- // core
- import PropertyNode from './core/PropertyNode.js';
- import VarNode from './core/VarNode.js';
- // inputs
- import ColorNode from './inputs/ColorNode.js';
- import FloatNode from './inputs/FloatNode.js';
- import IntNode from './inputs/IntNode.js';
- import Vector2Node from './inputs/Vector2Node.js';
- import Vector3Node from './inputs/Vector3Node.js';
- import Vector4Node from './inputs/Vector4Node.js';
- // accessors
- import PositionNode from './accessors/PositionNode.js';
- import NormalNode from './accessors/NormalNode.js';
- // math
- import OperatorNode from './math/OperatorNode.js';
- import CondNode from './math/CondNode.js';
- import MathNode from './math/MathNode.js';
- // utils
- import ArrayElementNode from './utils/ArrayElementNode.js';
- import ConvertNode from './utils/ConvertNode.js';
- import JoinNode from './utils/JoinNode.js';
- import SplitNode from './utils/SplitNode.js';
- // core
- import { Vector2, Vector3, Vector4, Color } from 'three';
- const NodeHandler = {
- construct( NodeClosure, params ) {
- const inputs = params.shift();
- return NodeClosure( ShaderNodeObjects( inputs ), ...params );
- },
- get: function ( node, prop ) {
- if ( typeof prop === 'string' && node[ prop ] === undefined ) {
- if ( /^[xyzwrgbastpq]{1,4}$/.test( prop ) === true ) {
- // accessing properties ( swizzle )
- prop = prop
- .replace( /r|s/g, 'x' )
- .replace( /g|t/g, 'y' )
- .replace( /b|p/g, 'z' )
- .replace( /a|q/g, 'w' );
- return ShaderNodeObject( new SplitNode( node, prop ) );
- } else if ( /^\d+$/.test( prop ) === true ) {
- // accessing array
- return ShaderNodeObject( new ArrayElementNode( node, new FloatNode( Number( prop ) ).setConst( true ) ) );
- }
- }
- return node[ prop ];
- }
- };
- const nodeObjects = new WeakMap();
- const ShaderNodeObject = ( obj ) => {
- const type = typeof obj;
- if ( type === 'number' ) {
- return ShaderNodeObject( new FloatNode( obj ).setConst( true ) );
- } else if ( type === 'object' ) {
- if ( obj.isNode === true ) {
- let nodeObject = nodeObjects.get( obj );
- if ( nodeObject === undefined ) {
- nodeObject = new Proxy( obj, NodeHandler );
- nodeObjects.set( obj, nodeObject );
- }
- return nodeObject;
- }
- }
- return obj;
- };
- const ShaderNodeObjects = ( objects ) => {
- for ( const name in objects ) {
- objects[ name ] = ShaderNodeObject( objects[ name ] );
- }
- return objects;
- };
- const ShaderNodeArray = ( array ) => {
- const len = array.length;
- for ( let i = 0; i < len; i ++ ) {
- array[ i ] = ShaderNodeObject( array[ i ] );
- }
- return array;
- };
- const ShaderNodeProxy = ( NodeClass, scope = null, factor = null ) => {
- if ( scope === null ) {
- return ( ...params ) => {
- return ShaderNodeObject( new NodeClass( ...ShaderNodeArray( params ) ) );
- };
- } else if ( factor === null ) {
- return ( ...params ) => {
- return ShaderNodeObject( new NodeClass( scope, ...ShaderNodeArray( params ) ) );
- };
- } else {
- factor = ShaderNodeObject( factor );
- return ( ...params ) => {
- return ShaderNodeObject( new NodeClass( scope, ...ShaderNodeArray( params ), factor ) );
- };
- }
- };
- const ShaderNodeScript = function ( jsFunc ) {
- return ( inputs, builder ) => {
- ShaderNodeObjects( inputs );
- return ShaderNodeObject( jsFunc( inputs, builder ) );
- };
- };
- export const ShaderNode = new Proxy( ShaderNodeScript, NodeHandler );
- //
- // Node Material Shader Syntax
- //
- export const uniform = new ShaderNode( ( inputNode ) => {
- inputNode.setConst( false );
- return inputNode;
- } );
- export const nodeObject = ( val ) => {
- return ShaderNodeObject( val );
- };
- export const float = ( val ) => {
- if ( val?.isNode === true ) {
- return nodeObject( new ConvertNode( val, 'float' ) );
- }
- return nodeObject( new FloatNode( val ).setConst( true ) );
- };
- export const int = ( val ) => {
- if ( val?.isNode === true ) {
- return nodeObject( new ConvertNode( val, 'int' ) );
- }
- return nodeObject( new IntNode( val ).setConst( true ) );
- };
- export const color = ( ...params ) => {
- if ( params[ 0 ]?.isNode === true ) {
- return nodeObject( new ConvertNode( params[0], 'color' ) );
- }
- return nodeObject( new ColorNode( new Color( ...params ) ).setConst( true ) );
- };
- export const join = ( ...params ) => {
- return nodeObject( new JoinNode( ShaderNodeArray( params ) ) );
- };
- export const cond = ( ...params ) => {
- return nodeObject( new CondNode( ...ShaderNodeArray( params ) ) );
- };
- export const vec2 = ( ...params ) => {
- if ( params[ 0 ]?.isNode === true ) {
- return nodeObject( new ConvertNode( params[ 0 ], 'vec2' ) );
- } else {
- // Providing one scalar value: This value is used for all components
- if ( params.length === 1 ) {
- params[ 1 ] = params[ 0 ];
- }
- return nodeObject( new Vector2Node( new Vector2( ...params ) ).setConst( true ) );
- }
- };
- export const vec3 = ( ...params ) => {
- if ( params[ 0 ]?.isNode === true ) {
- return nodeObject( new ConvertNode( params[ 0 ], 'vec3' ) );
- } else {
- // Providing one scalar value: This value is used for all components
- if ( params.length === 1 ) {
- params[ 1 ] = params[ 2 ] = params[ 0 ];
- }
- return nodeObject( new Vector3Node( new Vector3( ...params ) ).setConst( true ) );
- }
- };
- export const vec4 = ( ...params ) => {
- if ( params[ 0 ]?.isNode === true ) {
- return nodeObject( new ConvertNode( params[ 0 ], 'vec4' ) );
- } else {
- // Providing one scalar value: This value is used for all components
- if ( params.length === 1 ) {
- params[ 1 ] = params[ 2 ] = params[ 3 ] = params[ 0 ];
- }
- return nodeObject( new Vector4Node( new Vector4( ...params ) ).setConst( true ) );
- }
- };
- export const addTo = ( varNode, ...params ) => {
- varNode.node = add( varNode.node, ...ShaderNodeArray( params ) );
- return nodeObject( varNode );
- };
- export const add = ShaderNodeProxy( OperatorNode, '+' );
- export const sub = ShaderNodeProxy( OperatorNode, '-' );
- export const mul = ShaderNodeProxy( OperatorNode, '*' );
- export const div = ShaderNodeProxy( OperatorNode, '/' );
- export const remainder = ShaderNodeProxy( OperatorNode, '%' );
- export const equal = ShaderNodeProxy( OperatorNode, '==' );
- export const assign = ShaderNodeProxy( OperatorNode, '=' );
- export const lessThan = ShaderNodeProxy( OperatorNode, '<' );
- export const greaterThan = ShaderNodeProxy( OperatorNode, '>' );
- export const lessThanEqual = ShaderNodeProxy( OperatorNode, '<=' );
- export const greaterThanEqual = ShaderNodeProxy( OperatorNode, '>=' );
- export const and = ShaderNodeProxy( OperatorNode, '&&' );
- export const or = ShaderNodeProxy( OperatorNode, '||' );
- export const xor = ShaderNodeProxy( OperatorNode, '^^' );
- export const bitAnd = ShaderNodeProxy( OperatorNode, '&' );
- export const bitOr = ShaderNodeProxy( OperatorNode, '|' );
- export const bitXor = ShaderNodeProxy( OperatorNode, '^' );
- export const shiftLeft = ShaderNodeProxy( OperatorNode, '<<' );
- export const shiftRight = ShaderNodeProxy( OperatorNode, '>>' );
- export const element = ShaderNodeProxy( ArrayElementNode );
- export const normalGeometry = ShaderNodeObject( new NormalNode( NormalNode.GEOMETRY ) );
- export const normalLocal = ShaderNodeObject( new NormalNode( NormalNode.LOCAL ) );
- export const normalWorld = ShaderNodeObject( new NormalNode( NormalNode.WORLD ) );
- export const normalView = ShaderNodeObject( new NormalNode( NormalNode.VIEW ) );
- export const transformedNormalView = ShaderNodeObject( new VarNode( new NormalNode( NormalNode.VIEW ), 'TransformedNormalView', 'vec3' ) );
- export const positionLocal = ShaderNodeObject( new PositionNode( PositionNode.LOCAL ) );
- export const positionWorld = ShaderNodeObject( new PositionNode( PositionNode.WORLD ) );
- export const positionView = ShaderNodeObject( new PositionNode( PositionNode.VIEW ) );
- export const positionViewDirection = ShaderNodeObject( new PositionNode( PositionNode.VIEW_DIRECTION ) );
- export const PI = float( 3.141592653589793 );
- export const PI2 = float( 6.283185307179586 );
- export const PI_HALF = float( 1.5707963267948966 );
- export const RECIPROCAL_PI = float( 0.3183098861837907 );
- export const RECIPROCAL_PI2 = float( 0.15915494309189535 );
- export const EPSILON = float( 1e-6 );
- export const diffuseColor = ShaderNodeObject( new PropertyNode( 'DiffuseColor', 'vec4' ) );
- export const roughness = ShaderNodeObject( new PropertyNode( 'Roughness', 'float' ) );
- export const metalness = ShaderNodeObject( new PropertyNode( 'Metalness', 'float' ) );
- export const alphaTest = ShaderNodeObject( new PropertyNode( 'AlphaTest', 'float' ) );
- export const specularColor = ShaderNodeObject( new PropertyNode( 'SpecularColor', 'color' ) );
- export const abs = ShaderNodeProxy( MathNode, 'abs' );
- export const acos = ShaderNodeProxy( MathNode, 'acos' );
- export const asin = ShaderNodeProxy( MathNode, 'asin' );
- export const atan = ShaderNodeProxy( MathNode, 'atan' );
- export const ceil = ShaderNodeProxy( MathNode, 'ceil' );
- export const clamp = ShaderNodeProxy( MathNode, 'clamp' );
- export const cos = ShaderNodeProxy( MathNode, 'cos' );
- export const cross = ShaderNodeProxy( MathNode, 'cross' );
- export const degrees = ShaderNodeProxy( MathNode, 'degrees' );
- export const dFdx = ShaderNodeProxy( MathNode, 'dFdx' );
- export const dFdy = ShaderNodeProxy( MathNode, 'dFdy' );
- export const distance = ShaderNodeProxy( MathNode, 'distance' );
- export const dot = ShaderNodeProxy( MathNode, 'dot' );
- export const exp = ShaderNodeProxy( MathNode, 'exp' );
- export const exp2 = ShaderNodeProxy( MathNode, 'exp2' );
- export const faceforward = ShaderNodeProxy( MathNode, 'faceforward' );
- export const floor = ShaderNodeProxy( MathNode, 'floor' );
- export const fract = ShaderNodeProxy( MathNode, 'fract' );
- export const invert = ShaderNodeProxy( MathNode, 'invert' );
- export const inversesqrt = ShaderNodeProxy( MathNode, 'inversesqrt' );
- export const length = ShaderNodeProxy( MathNode, 'length' );
- export const log = ShaderNodeProxy( MathNode, 'log' );
- export const log2 = ShaderNodeProxy( MathNode, 'log2' );
- export const max = ShaderNodeProxy( MathNode, 'max' );
- export const min = ShaderNodeProxy( MathNode, 'min' );
- export const mix = ShaderNodeProxy( MathNode, 'mix' );
- export const mod = ShaderNodeProxy( MathNode, 'mod' );
- export const negate = ShaderNodeProxy( MathNode, 'negate' );
- export const normalize = ShaderNodeProxy( MathNode, 'normalize' );
- export const pow = ShaderNodeProxy( MathNode, 'pow' );
- export const pow2 = ShaderNodeProxy( MathNode, 'pow', 2 );
- export const pow3 = ShaderNodeProxy( MathNode, 'pow', 3 );
- export const pow4 = ShaderNodeProxy( MathNode, 'pow', 4 );
- export const radians = ShaderNodeProxy( MathNode, 'radians' );
- export const reflect = ShaderNodeProxy( MathNode, 'reflect' );
- export const refract = ShaderNodeProxy( MathNode, 'refract' );
- export const round = ShaderNodeProxy( MathNode, 'round' );
- export const saturate = ShaderNodeProxy( MathNode, 'saturate' );
- export const sign = ShaderNodeProxy( MathNode, 'sign' );
- export const sin = ShaderNodeProxy( MathNode, 'sin' );
- export const smoothstep = ShaderNodeProxy( MathNode, 'smoothstep' );
- export const sqrt = ShaderNodeProxy( MathNode, 'sqrt' );
- export const step = ShaderNodeProxy( MathNode, 'step' );
- export const tan = ShaderNodeProxy( MathNode, 'tan' );
- export const transformDirection = ShaderNodeProxy( MathNode, 'transformDirection' );
|