# utils

Typed array merge helpers for Three.js buffer geometry construction.

---

## `mergeIndexArrays(arrays, offsets) → Uint32Array`

Merges multiple `Uint32Array` index buffers into one, applying a per-array vertex offset to each index value. Use this when concatenating geometries: each geometry's indices need to be shifted by the cumulative vertex count of all preceding geometries.

### Parameters

| Name | Type | Description |
|------|------|-------------|
| `arrays` | `Uint32Array[]` | Index arrays to merge |
| `offsets` | `number[]` | Per-array vertex offset; `offsets[i]` is added to every index in `arrays[i]` |

### Returns

`Uint32Array` — merged index array with offsets applied.

### Example

```typescript
import { mergeIndexArrays } from '@yaoc1996/js-tools/three-utils'

const idx0 = new Uint32Array([0, 1, 2])          // triangle in mesh 0
const idx1 = new Uint32Array([0, 1, 2])          // triangle in mesh 1
const merged = mergeIndexArrays([idx0, idx1], [3, 3])
// → [0, 1, 2, 3, 4, 5]
```

### Notes

- Throws if `arrays.length !== offsets.length`.

---

## `mergeTypedArrays(arrays) → T`

Concatenates typed arrays of the same type into a single new array. The return type matches the element type.

### Parameters

| Name | Type | Description |
|------|------|-------------|
| `arrays` | `T[]` | Non-empty array of typed arrays to merge; all must be the same concrete type |

### Returns

`T` — merged typed array of the same type as the inputs.

### Example

```typescript
import { mergeTypedArrays } from '@yaoc1996/js-tools/three-utils'

const a = new Float32Array([1, 2, 3])
const b = new Float32Array([4, 5, 6])
const merged = mergeTypedArrays([a, b])
// → Float32Array [1, 2, 3, 4, 5, 6]
```

### Notes

- Throws `"Must have at least one array."` when called with an empty array.
- Supported types: `Float32Array`, `Float64Array`, `Int8Array`, `Int16Array`, `Int32Array`, `Uint8Array`, `Uint8ClampedArray`, `Uint16Array`, `Uint32Array`.

---

## Types

```typescript
type TypedArray =
    | Int8Array
    | Uint8Array
    | Uint8ClampedArray
    | Int16Array
    | Uint16Array
    | Int32Array
    | Uint32Array
    | Float32Array
    | Float64Array
```
