INNER CODE UNIT · TypeScript
computeNormalsFromBuffers
voxelize/voxelize · packages/core/src/core/world/index.ts:80
function computeNormalsFromBuffers(
positions: ArrayLike<number>,
indices: ArrayLike<number>,
): Float32Array {
const normals = new Float32Array(positions.length);
for (let i = 0; i < indices.length; i += 3) {
const ia = indices[i] * 3;
const ib = indices[i + 1] * 3;
const ic = indices[i + 2] * 3;
const e1x = positions[ib] - positions[ia];
const e1y = positions[ib + 1] - positions[ia + 1];
const e1z = positions[ib + 2] - positions[ia + 2];
const e2x = positions[ic] - positions[ia];
const e2y = positions[ic + 1] - positions[ia + 1];
const e2z = positions[ic + 2] - positions[ia + 2];
let nx = e1y * e2z - e1z * e2y;
let ny = e1z * e2x - e1x * e2z;
let nz = e1x * e2y - e1y * e2x;