@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
46 lines (33 loc) • 1.32 kB
JavaScript
import { v3_array_immediate_add } from "../../../../core/geom/vec3/v3_array_immediate_add.js";
import { geometry_construct_triangle_normal } from "../geometry_construct_triangle_normal.js";
import { v3_array_normalize_many } from "./v3_array_normalize_many.js";
const scratch_v3 = new Float64Array(3);
/**
* based on code from THREE.js
* Prior to calling this function, normals need to be set to 0
* @param {Array.<number>|Float32Array} positions
* @param {Array.<number>|Float32Array} normals
* @param {Array.<number>|Uint32Array|Uint16Array|Uint8Array} indices
*/
export function geometry_compute_vertex_normals_indexed(
positions,
normals,
indices
) {
const index_length = indices.length;
for (let i = 0; i < index_length; i += 3) {
const a = indices[i];
const b = indices[i + 1];
const c = indices[i + 2];
geometry_construct_triangle_normal(
scratch_v3, 0,
a, b, c, positions
);
// accumulate
v3_array_immediate_add(normals, a * 3, scratch_v3, 0);
v3_array_immediate_add(normals, b * 3, scratch_v3, 0);
v3_array_immediate_add(normals, c * 3, scratch_v3, 0);
}
// restore unit magnitude
v3_array_normalize_many(normals);
}