@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
53 lines (38 loc) • 1.48 kB
JavaScript
import { assert } from "../../../core/assert.js";
import { compute_triangle_area_3d } from "../../../core/geom/3d/triangle/compute_triangle_area_3d.js";
/**
*
* @param {Float32Array} result Area of individual polygons will be written here
* @param {number[]|Float32Array} points
* @param {number[]|Uint8Array|Uint16Array|Uint32Array} indices
* @param {number} [polygon_count]
* @returns {number} total surface area
*/
export function computeMeshSurfaceArea(result, points, indices, polygon_count = indices.length / 3) {
assert.lessThanOrEqual(polygon_count, indices.length / 3, "index underflow");
let total = 0;
for (let i = 0; i < polygon_count; i++) {
const index3 = i * 3;
const a = indices[index3];
const b = indices[index3 + 1];
const c = indices[index3 + 2];
//read points
const a3 = a * 3;
const x0 = points[a3];
const y0 = points[a3 + 1];
const z0 = points[a3 + 2];
const b3 = b * 3;
const x1 = points[b3];
const y1 = points[b3 + 1];
const z1 = points[b3 + 2];
const c3 = c * 3;
const x2 = points[c3];
const y2 = points[c3 + 2];
const z2 = points[c3 + 2];
const area = compute_triangle_area_3d(x0, y0, z0, x1, y1, z1, x2, y2, z2);
assert.notNaN(area, "area");
result[i] = area;
total += area;
}
return total;
}