MeshAttributeAccessor

An iterator over a mesh vertex attribute.

Usage:

 1const mesh = this.object.getComponent('mesh').mesh;
 2const positions = mesh.attribute(MeshAttribute.Position);
 3
 4// Equivalent to `new Float32Array(3)`.
 5const temp = positions.createArray();
 6
 7for(let i = 0; i < positions.length; ++i) {
 8    // `pos` will reference `temp` and thereby not allocate additional
 9    // memory, which would cause a perf spike when collected.
10    const pos = positions.get(i, temp);
11    // Scale position by 2 on x-axis only.
12    pos[0] *= 2.0;
13    positions.set(i, pos);
14}
15// We're done modifying, tell the engine to move vertex data to the GPU.
16mesh.update();

.length: number 

Max number of elements.

.engine: WonderlandEngine 

Hosting engine instance.

.createArray(count: number) ⇒ InstanceType 

Create a new TypedArray to hold this attribute’s values.

This method is useful to create a view to hold the data to pass to get and set

Example:

1const vertexCount = 4;
2const positionAttribute = mesh.attribute(MeshAttribute.Position);
3
4// A position has 3 floats per vertex. Thus, positions has length 3 * 4.
5const positions = positionAttribute.createArray(vertexCount);

Returns: A TypedArray with the appropriate format to access the data

ParamTypeDescription
countnumberThe number of vertices expected.

.get<T>(index: number, out: T) ⇒ T 

.get(index: number) ⇒ InstanceType

Get attribute element.

Returns: The out parameter

ParamTypeDescription
indexnumberIndex
outTPreallocated array to write into, to avoid garbage, otherwise will allocate a new TypedArray. out.length needs to be a multiple of the attributes component count, see MeshAttribute. If out.length is more than one multiple, it will be filled with the next n attribute elements, which can reduce overhead of this call.
Template ParamType Definition
Textends NumberArray

.set(i: number, v: Readonly<NumberArray>) ⇒ MeshAttributeAccessor 

Set attribute element.

Returns: Reference to self (for method chaining)

ParamTypeDescription
inumberIndex
vReadonly<NumberArray>Value to set the element to v.length needs to be a multiple of the attributes component count, see MeshAttribute. If v.length is more than one multiple, it will be filled with the next n attribute elements, which can reduce overhead of this call.