-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* introduce array * updates * added `.toArray()`
- Loading branch information
Showing
11 changed files
with
271 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import TempNode from './TempNode.js'; | ||
import { addMethodChaining, nodeObject } from '../tsl/TSLCore.js'; | ||
|
||
/** @module ArrayNode **/ | ||
|
||
/** | ||
* ArrayNode represents a collection of nodes, typically created using the {@link module:TSL~array} function. | ||
* ```js | ||
* const colors = array( [ | ||
* vec3( 1, 0, 0 ), | ||
* vec3( 0, 1, 0 ), | ||
* vec3( 0, 0, 1 ) | ||
* ] ); | ||
* | ||
* const redColor = tintColors.element( 0 ); | ||
* | ||
* @augments Node | ||
*/ | ||
class ArrayNode extends TempNode { | ||
|
||
static get type() { | ||
|
||
return 'ArrayNode'; | ||
|
||
} | ||
|
||
/** | ||
* Constructs a new array node. | ||
* | ||
* @param {String} [nodeType] - The data type of the elements. | ||
* @param {Number} [count] - Size of the array. | ||
* @param {Array<Node>?} [values=null] - Array default values. | ||
*/ | ||
constructor( nodeType, count, values = null ) { | ||
|
||
super( nodeType ); | ||
|
||
/** | ||
* Array size. | ||
* | ||
* @type {Array<Node>} | ||
*/ | ||
this.count = count; | ||
|
||
/** | ||
* Array default values. | ||
* | ||
* @type {Array<Node>} | ||
*/ | ||
this.values = values; | ||
|
||
/** | ||
* This flag can be used for type testing. | ||
* | ||
* @type {Boolean} | ||
* @readonly | ||
* @default true | ||
*/ | ||
this.isArrayNode = true; | ||
|
||
} | ||
|
||
getNodeType( builder ) { | ||
|
||
if ( this.nodeType === null ) { | ||
|
||
this.nodeType = this.values[ 0 ].getNodeType( builder ); | ||
|
||
} | ||
|
||
return this.nodeType; | ||
|
||
} | ||
|
||
getElementType( builder ) { | ||
|
||
return this.getNodeType( builder ); | ||
|
||
} | ||
|
||
generate( builder ) { | ||
|
||
const type = this.getNodeType( builder ); | ||
|
||
return builder.generateArray( type, this.count, this.values ); | ||
|
||
} | ||
|
||
} | ||
|
||
export default ArrayNode; | ||
|
||
/** | ||
* TSL function for creating an array node. | ||
* | ||
* @function | ||
* @param {String|Array<Node>} nodeTypeOrValues - A string representing the element type (e.g., 'vec3') | ||
* or an array containing the default values (e.g., [ vec3() ]). | ||
* @param {Number?} [count] - Size of the array. | ||
* @returns {ArrayNode} | ||
*/ | ||
export const array = ( ...params ) => { | ||
|
||
let node; | ||
|
||
if ( params.length === 1 ) { | ||
|
||
const values = params[ 0 ]; | ||
|
||
node = new ArrayNode( null, values.length, values ); | ||
|
||
} else { | ||
|
||
const nodeType = params[ 0 ]; | ||
const count = params[ 1 ]; | ||
|
||
node = new ArrayNode( nodeType, count ); | ||
|
||
} | ||
|
||
return nodeObject( node ); | ||
|
||
}; | ||
|
||
addMethodChaining( 'toArray', ( node, count ) => array( Array( count ).fill( node ) ) ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.