Work with arrays
An array is an indexed list of values. For example, the data
of the point_position
property of a mesh is an array of float3
values, indexed by the vertex number.
You can get or set an individual value in an array by specifying its index. Arrays in Bifrost use zero-based indexing. This means that in an array of size 5, the index of the first value is 0, not 1, and the index of the last value is 4.
Although negative numbers do not represent valid array indices, the index type is a signed integer (long
).
Array ports
When a port's type is an array, its shape is wider where it touches the node, resembling a top hat on its side.
Some array ports allow fan-in connections. This means that you can connect multiple connections of either single elements or arrays. All the connected inputs are automatically concatenated into a single input array, similar to using a build_array
node.
- Drop a connection onto the main port icon to add a new input.
- Expand the port to view individual connections. This lets you disconnect specific inputs, for example.
To create fan-in ports in your own graphs and compounds, see Create fan-in ports for arrays
Nodes for working with arrays
There are many nodes for working with arrays in the Core::Array
namespace. In addition, the for-each
and iterate
compounds can be used to loop through the values of an array.
Auto-looping
Much of the time, you do not need to loop through an array to process its values. This is because most nodes will automatically loop on the elements of an array when needed.
For example:
- If you connect two single values as inputs to an
add
node, you get a single value as an output. No auto-looping is needed in this case. - If you connect a single value and an array to an
add
node, the single value is automatically added to every value in the array. The output is another array containing the sums. - If you connect two arrays to an
add
node, the corresponding values of the arrays are added together and the output is also an array. If the arrays have different sizes, the output array is truncated to the size of the smaller input.
When auto-looping occurs, it is indicated by a special icon on the affected ports: