@woosh/meep-engine
Version:
Pure JavaScript game engine. Fully featured and production ready.
108 lines (88 loc) • 3.59 kB
JavaScript
import { assert } from "../../../../../core/assert.js";
import { bvh32_query_user_data_ray } from "../../../../../core/bvh2/binary/2/bvh32_query_user_data_ray.js";
import { SurfacePoint3 } from "../../../../../core/geom/3d/SurfacePoint3.js";
import { computeTriangleRayIntersection } from "../../../../../core/geom/3d/triangle/computeTriangleRayIntersection.js";
const scratch_array = []
const scratch_hit = new SurfacePoint3()
/**
*
* @param {SurfacePoint3} result
* @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} originX
* @param {number} originY
* @param {number} originZ
* @param {number} directionX
* @param {number} directionY
* @param {number} directionZ
* @returns {boolean}
*/
export function bvh32_geometry_raycast(
result,
bvh,
vertices, vertex_offset, vertex_stride,
indices,
originX, originY, originZ,
directionX, directionY, directionZ
) {
let hit_found = false;
const hit_count = bvh32_query_user_data_ray(
scratch_array, 0,
bvh,
originX, originY, originZ,
directionX, directionY, directionZ
);
let best_distance = Infinity;
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];
const triangle_hit_found = computeTriangleRayIntersection(
scratch_hit,
originX, originY, originZ,
directionX, directionY, directionZ,
ax, ay, az,
bx, by, bz,
cx, cy, cz
)
if (!triangle_hit_found) {
continue;
}
hit_found = true;
const distance_sqr = scratch_hit.position._distanceSqrTo(originX, originY, originZ);
if (distance_sqr < best_distance) {
best_distance = distance_sqr;
result.copy(scratch_hit);
}
}
return hit_found;
}