AttributeAccessor

An iterator over a mesh vertex attribute.

Usage:

const mesh = this.object.getComponent('mesh').mesh;
const positions = mesh.attribute(MeshAttribute.Position);

// Equivalent to `new Float32Array(3)`.
const temp = positions.createArray();

for(let i = 0; i < positions.length; ++i) {
    // `pos` will reference `temp` and thereby not allocate additional
    // memory, which would cause a perf spike when collected.
    const pos = positions.get(i, temp);
    // Scale position by 2 on x-axis only.
    pos[0] *= 2.0;
    positions.set(i, pos);
}
// We're done modifying, tell the engine to move vertex data to the GPU.
mesh.update();

Properties

Methods


Properties

.length: number

Max number of elements.


.engine: WonderlandEngine

Hosting engine instance.



Methods

.createArray(count: number) ⇒ InstanceType<T>

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:

const vertexCount = 4;
const positionAttribute = mesh.attribute(MeshAttribute.Position);

// A position has 3 floats per vertex. Thus, positions has length 3 * 4.
const 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<T>

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>) ⇒ AttributeAccessor<T>

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.