* Updates a single element in the buffer at a given index. Use this * when only a small number of elements need to change. If you need to * replace all the data at once, use * `update()` instead. * * ```js * let buf; * *
(index, value)
| 312 | * struct buffers. |
| 313 | */ |
| 314 | set(index, value) { |
| 315 | const device = this._renderer.device; |
| 316 | |
| 317 | if (this._schema !== null) { |
| 318 | // buffer was created with an array of structs |
| 319 | if (typeof value !== 'object' || value === null || Array.isArray(value)) { |
| 320 | throw new Error( |
| 321 | 'set() expects a plain object matching the original struct format for this buffer' |
| 322 | ); |
| 323 | } |
| 324 | |
| 325 | const { stride } = this._schema; |
| 326 | const byteOffset = index * stride; |
| 327 | |
| 328 | if (byteOffset + stride > this.size) { |
| 329 | throw new Error( |
| 330 | `set() index ${index} is out of bounds for this buffer ` + |
| 331 | `(buffer holds ${Math.floor(this.size / stride)} elements)` |
| 332 | ); |
| 333 | } |
| 334 | |
| 335 | // pack just this one element using the same logic as update() |
| 336 | const packed = this._renderer._packStructArray([value], this._schema); |
| 337 | // use packed.buffer (ArrayBuffer) so the size arg is always in bytes |
| 338 | device.queue.writeBuffer(this.buffer, byteOffset, packed.buffer, 0, stride); |
| 339 | } else { |
| 340 | // buffer was created with a float array |
| 341 | if (typeof value !== 'number') { |
| 342 | throw new Error( |
| 343 | 'set() expects a number for this float buffer' |
| 344 | ); |
| 345 | } |
| 346 | |
| 347 | const byteOffset = index * 4; |
| 348 | |
| 349 | if (byteOffset + 4 > this.size) { |
| 350 | throw new Error( |
| 351 | `set() index ${index} is out of bounds for this buffer ` + |
| 352 | `(buffer holds ${Math.floor(this.size / 4)} floats)` |
| 353 | ); |
| 354 | } |
| 355 | |
| 356 | device.queue.writeBuffer(this.buffer, byteOffset, new Float32Array([value])); |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
| 361 | /** |
no test coverage detected