@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
88 lines (72 loc) • 2.94 kB
JavaScript
import { assert } from "../../../../../core/assert.js";
import {
bvh32_query_user_data_overlaps_clipping_volume
} from "../../../../../core/bvh2/binary/2/bvh32_query_user_data_overlaps_clipping_volume.js";
import {
triangle_intersects_clipping_volume
} from "../../../../../core/geom/3d/triangle/triangle_intersects_clipping_volume.js";
const scratch_array = []
/**
*
* @param {BinaryUint32BVH} bvh
* @param {number[]|ArrayLike<number>} vertices
* @param {number} vertex_offset Unless you're using an interleaved buffer of some kind, this will be 0
* @param {number} vertex_stride Unless you're using an interleaved buffer, this should be 3
* @param {number[]|ArrayLike<number>|undefined} indices if this is set to undefined - implicit indexing will be used
* @param {number[]|Float32Array} planes
* @param {number} planes_offset
* @param {number} plane_count
* @returns {boolean}
*/
export function bvh32_geometry_overlap_clipping_volume(
bvh,
vertices, vertex_offset, vertex_stride,
indices,
planes, planes_offset, plane_count
) {
const hit_count = bvh32_query_user_data_overlaps_clipping_volume(
scratch_array, 0,
bvh,
planes, planes_offset, plane_count
);
let a, b, c;
for (let i = 0; i < hit_count; i++) {
const triangle_index = scratch_array[i];
const index3 = triangle_index * 3;
if (indices !== undefined) {
assert.lessThan(index3 + 2, indices.length, 'triangle index overflow, possibly geometry changed but tree was not rebuilt?');
a = indices[index3];
b = indices[index3 + 1];
c = indices[index3 + 2];
} else {
// implicit indices
a = index3;
b = index3 + 1;
c = index3 + 2;
}
const a_address = a * vertex_stride + vertex_offset;
const b_address = b * vertex_stride + vertex_offset;
const c_address = c * vertex_stride + vertex_offset;
assert.lessThan(a_address + 2, vertices.length, 'a-vertex overflow');
assert.lessThan(b_address + 2, vertices.length, 'b-vertex overflow');
assert.lessThan(c_address + 2, vertices.length, 'c-vertex overflow');
const ax = vertices[a_address];
const ay = vertices[a_address + 1];
const az = vertices[a_address + 2];
const bx = vertices[b_address];
const by = vertices[b_address + 1];
const bz = vertices[b_address + 2];
const cx = vertices[c_address];
const cy = vertices[c_address + 1];
const cz = vertices[c_address + 2];
if (triangle_intersects_clipping_volume(
planes, planes_offset, plane_count,
ax, ay, az,
bx, by, bz,
cx, cy, cz
)) {
return true;
}
}
return false;
}