Skip to content

NEW FEATURES ‐ [PRE TEST]

Nikola edited this page Feb 28, 2025 · 1 revision

- Raycast func for test:

// Function to perform the raycast
function raycast(origin: vec3, direction: vec3, vertices: Float32Array, indices: Uint16Array): boolean {
    for (let i = 0; i < indices.length; i += 3) {
        const v0 = vec3.fromValues(vertices[indices[i] * 3], vertices[indices[i] * 3 + 1], vertices[indices[i] * 3 + 2]);
        const v1 = vec3.fromValues(vertices[indices[i + 1] * 3], vertices[indices[i + 1] * 3 + 1], vertices[indices[i + 1] * 3 + 2]);
        const v2 = vec3.fromValues(vertices[indices[i + 2] * 3], vertices[indices[i + 2] * 3 + 1], vertices[indices[i + 2] * 3 + 2]);

        const edge1 = vec3.create();
        const edge2 = vec3.create();
        vec3.subtract(edge1, v1, v0);
        vec3.subtract(edge2, v2, v0);

        const h = vec3.create();
        vec3.cross(h, direction, edge2);
        const a = vec3.dot(edge1, h);

        if (a > -0.00001 && a < 0.00001) continue;

        const f = 1 / a;
        const s = vec3.create();
        vec3.subtract(s, origin, v0);
        const u = f * vec3.dot(s, h);

        if (u < 0 || u > 1) continue;

        const q = vec3.create();
        vec3.cross(q, s, edge1);
        const v = f * vec3.dot(direction, q);

        if (v < 0 || u + v > 1) continue;

        const t = f * vec3.dot(edge2, q);
        if (t > 0.00001) {
            return true;
        }
    }
    return false;
}