maath
maath copied to clipboard
add(buffer): Normalize
normalize(buffer, size)
const myRandoms = pipe(
new Float32Array(10_000 * 3),
b => inSphere(b),
b => normalize(b, 3),
b => map(b,
)
impl:
const normalize = (buffer: TypedArray, stride: number) => {
for (let i = 0; i < buffer.length; i += stride) {
const x = buffer[i + 0];
const y = buffer[i + 1];
const z = buffer[i + 2];
const l = Math.sqrt(x * x + y * y + z * z);
buffer[i + 0] = x / l;
buffer[i + 1] = y / l;
buffer[i + 2] = z / l;
}
return buffer;
};