Hello. I have written a class which creates a sparsely populated static spatial index with the geometry data parsed from a loaded glb… The values of this index are Triangles… The glb exported from blender has been validated visually, programmatically, and across multiple model viewers… The index is insanely fast and seems pretty accurate in terms of accessing only the pertinent references to the unique Triangles in any given spatial index.
The problem that I am facing is that by no means can I get a reliable ray triangle intersection function working, neither custom, nor using the utilities of THREE.js… The Ray.intersectTriangle method seemed very straightforward, but continued to give the error “cannot access property copy”… The Raycaster intersects function is meant to be used on a mesh, which turns out to be antithetical to my spatial index. This lead me to trying to make my own class method referencing the Moller-Trumbore algorithm, among others… Then, when this would not work with the THREE.js data structures, I made my own Vector3 class from the THREE.js Vector3 class and refactor the function to accept these Vector3s rather than Triangle or Line or Ray…
This is what I have now… Two slightly differing Moller-Trumbore algorithms packed into in one function… The first section very accurately ends the function if there is no intersection, but is useless for determining any sort of relevant Vector3 intersection. The second section is somewhat accurate for returning a point of intersection, but it seems as though something is way off with the scale or something. This seems like a trivial thing to do, but for whatever reason I just cannot overcome this one. I feel like I am just annoying everyone at this point. My resources have been academic articles, youtube videos, stack overflow, chatGPT, professors, and math majors.
mollerTrumboreIntersect(rayStart, rayEnd, v0, v1, v2) {
//Guard clause section returns if no collision.
const e1 = v1.sub(v0);
const e2 = v2.sub(v0);
const rayDirection = rayEnd.sub(rayStart);
const h = rayDirection.cross(e2);
const a = e1.dot(h);
if(a === 0) return(false);
const f = 1 / a;
const s = rayStart.sub(v0);
const u = f * s.dot(h);
if(u < 0 || u > 1) return(false);
const q = s.cross(e1);
const v = f * rayDirection.dot(q);
if(v < 0 || u + v > 1) return(false);
const t0 = f * e2.dot(q);
if(t0 <= 0) return(false);
//Section that return the point of intersection, which seems mostly correct until it to goes exponentially further off-scale as the subjects venture out farther from the origin of world space.
let edge1 = v1.sub(v0);
let edge2 = v2.sub(v0);
let pvec = rayEnd.cross(edge2);
let det = edge1.dot(pvec);
let tvec = rayStart.sub(v0);
let qvec = tvec.cross(edge1);
let t = (edge2.dot(qvec)) / det;
let out = new Vector3(0, 0, 0);
out.x = (rayStart.x + t * rayEnd.x);
out.y = (rayStart.y + t * rayEnd.y);
out.z = (rayStart.z + t * rayEnd.z);
return out;
}
At this point in my journey, it seems like nearly impossible to even get a semi-working ray-triangle intersect function in js, as the code and advice that everyone has provided so far has been non-working, trailed off into what is way over my head, or is just completely wrong… I am exhausted from just putting together the static spatial index part… It is very deadline-sensitive, and so makes me feel helpless. I do greatly appreciate anyone who might be able to help me. I wish to try any and all suggestions.