PHP 7.4.33
Preview: FunctionOverloadingNode.js Size: 2.91 KB
/var/www/uibuilder.cmshelp.dk/httpdocs/node_modules/three/src/nodes/utils/FunctionOverloadingNode.js
import Node from '../core/Node.js';
import { nodeProxy } from '../tsl/TSLCore.js';

/**
 * This class allows to define multiple overloaded versions
 * of the same function. Depending on the parameters of the function
 * call, the node picks the best-fit overloaded version.
 *
 * @augments Node
 */
class FunctionOverloadingNode extends Node {

	static get type() {

		return 'FunctionOverloadingNode';

	}

	/**
	 * Constructs a new function overloading node.
	 *
	 * @param {Array<Function>} functionNodes - Array of `Fn` function definitions.
	 * @param {...Node} parametersNodes - A list of parameter nodes.
	 */
	constructor( functionNodes = [], ...parametersNodes ) {

		super();

		/**
		 * Array of `Fn` function definitions.
		 *
		 * @type {Array<Function>}
		 */
		this.functionNodes = functionNodes;

		/**
		 * A list of parameter nodes.
		 *
		 * @type {Array<Node>}
		 */
		this.parametersNodes = parametersNodes;

		/**
		 * The selected overloaded function call.
		 *
		 * @private
		 * @type {ShaderCallNodeInternal}
		 */
		this._candidateFnCall = null;

		/**
		 * This node is marked as global.
		 *
		 * @type {boolean}
		 * @default true
		 */
		this.global = true;

	}

	/**
	 * This method is overwritten since the node type is inferred from
	 * the function's return type.
	 *
	 * @param {NodeBuilder} builder - The current node builder.
	 * @return {string} The node type.
	 */
	getNodeType() {

		return this.functionNodes[ 0 ].shaderNode.layout.type;

	}

	setup( builder ) {

		const params = this.parametersNodes;

		let candidateFnCall = this._candidateFnCall;

		if ( candidateFnCall === null ) {

			let candidateFn = null;
			let candidateScore = - 1;

			for ( const functionNode of this.functionNodes ) {

				const shaderNode = functionNode.shaderNode;
				const layout = shaderNode.layout;

				if ( layout === null ) {

					throw new Error( 'FunctionOverloadingNode: FunctionNode must be a layout.' );

				}

				const inputs = layout.inputs;

				if ( params.length === inputs.length ) {

					let score = 0;

					for ( let i = 0; i < params.length; i ++ ) {

						const param = params[ i ];
						const input = inputs[ i ];

						if ( param.getNodeType( builder ) === input.type ) {

							score ++;

						} else {

							score = 0;

						}

					}

					if ( score > candidateScore ) {

						candidateFn = functionNode;
						candidateScore = score;

					}

				}

			}

			this._candidateFnCall = candidateFnCall = candidateFn( ...params );

		}

		return candidateFnCall;

	}

}

export default FunctionOverloadingNode;

const overloadingBaseFn = /*@__PURE__*/ nodeProxy( FunctionOverloadingNode );

/**
 * TSL function for creating a function overloading node.
 *
 * @tsl
 * @function
 * @param {Array<Function>} functionNodes - Array of `Fn` function definitions.
 * @returns {FunctionOverloadingNode}
 */
export const overloadingFn = ( functionNodes ) => ( ...params ) => overloadingBaseFn( functionNodes, ...params );

Directory Contents

Dirs: 0 × Files: 28
Name Size Perms Modified Actions
1.37 KB lrw-r--r-- 2025-03-28 11:04:37
Edit Download
1.92 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
5.23 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
814 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
1.47 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
2.32 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
2.91 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
1.92 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
6.33 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
1.08 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
1.95 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
999 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
1.15 KB lrw-r--r-- 2025-03-28 11:04:38
Edit Download
560 B lrw-r--r-- 2025-03-28 11:04:38
Edit Download
4.31 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
13.47 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
3.23 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.91 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
6.16 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.31 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
3.25 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.14 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.81 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
2.62 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.78 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
4.46 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1.14 KB lrw-r--r-- 2025-03-28 11:04:39
Edit Download
1008 B lrw-r--r-- 2025-03-28 11:04:39
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).