UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

61 lines (44 loc) 1.51 kB
import { assert } from "../../../../core/assert.js"; /** * * @param {number[]|Float32Array|Uint8Array|Uint32Array} result * @param {number} result_offset * @param {Sampler2D} sampler * @param {number} [channel] * @return {number} */ export function sampler2d_channel_compute_min_indices( result, result_offset, sampler, channel = 0 ) { assert.defined(sampler, 'sampler'); assert.isNonNegativeInteger(result_offset, 'result_offset'); const itemSize = sampler.itemSize; assert.isNumber(channel, "channel"); assert.isNonNegativeInteger(channel, 'channel'); assert.ok(channel >= 0, `channel must be >= 0, was ${channel}`); assert.ok(channel < itemSize, `channel must be less than itemSize(=${itemSize}), was ${channel}`); assert.isArrayLike(result, "result"); const data = sampler.data; const l = data.length; if (l === 0) { //no data return 0; } let bestValue = data[channel]; result[result_offset] = 0; let result_count = 1; for (let i = channel + itemSize; i < l; i += itemSize) { const value = data[i]; if (bestValue > value) { bestValue = value; //drop result result_count = 1; result[result_offset] = i; } else if (value === bestValue) { result[result_offset + result_count] = i; result_count++; } } return result_count; }