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();
- MeshAttributeAccessor
- .length: number
- .engine
- .createArray(count) ⇒ InstanceType
- .get<T>(index, out) ⇒ T
- .set(i, v) ⇒ MeshAttributeAccessor
.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:
Returns: A TypedArray with the appropriate format to access the data
Param | Type | Description |
---|---|---|
count | number | The number of vertices expected. |
.get<T>(index: number, out: T) ⇒ T
.get(index: number) ⇒ InstanceType
Get attribute element.
Returns: The out
parameter
Param | Type | Description |
---|---|---|
index | number | Index |
out | T | Preallocated 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 Param | Type Definition |
---|---|
T | extends NumberArray |
.set(i: number, v: Readonly<NumberArray>) ⇒ MeshAttributeAccessor
Set attribute element.
Returns: Reference to self (for method chaining)
Param | Type | Description |
---|---|---|
i | number | Index |
v | Readonly<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. |