{
 "id": "moller-trumbore-ray-triangle",
 "title": "Möller–Trumbore ray-triangle intersection",
 "aliases": [
  "Moller-Trumbore",
  "MT intersection"
 ],
 "domain": "geometry",
 "subdomain": "ray-primitive intersection",
 "stage": "geometry",
 "realtime": "realtime",
 "platforms": [
  "cpu",
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "cuda"
 ],
 "year_introduced": 1997,
 "inputs": [
  "mesh"
 ],
 "outputs": [
  "depth"
 ],
 "style": "both",
 "dynamic": "both",
 "summary": "Tests a ray against a triangle by solving for the hit distance and two barycentric coordinates in one go, with no precomputed plane and no extra per-triangle storage beyond the three vertices.",
 "how_it_works": "A point on the triangle is `A + u(B-A) + v(C-A)`, and a point on the ray is `O + tD`. Setting them equal gives a 3x3 linear system in (t, u, v). Cramer's rule solves it with two cross products and a handful of dot products: one cross of the ray direction with an edge yields the determinant, and a second cross yields the numerators for v and t. The determinant also tells you whether the ray is parallel to the triangle. You reject early when u falls outside [0,1] or u+v exceeds 1, so most misses cost one cross product. The result is the distance t plus barycentrics, which you can reuse to interpolate normals, UVs or vertex colours. Because it stores nothing but the vertices it fits mesh storage as-is, which is why BVH leaves in CPU and GPU tracers use it as the primitive test.",
 "when_to_use": "Choose it as the default leaf test for ray tracing, picking and CPU collision queries over triangle soups. Prefer `three-mesh-bvh` when you want it already wired into a BVH for three.js picking, and `sah-bvh-construction` for the acceleration structure around it. Prefer watertight intersection variants when thin-edge leaks between adjacent triangles are unacceptable. Prefer `sphere-tracing` when the surface is an SDF instead of a mesh.",
 "cost": {
  "class": "cheap",
  "notes": "A few dozen flops per test; in practice traversal and memory access dominate, not this test."
 },
 "failure_modes": [
  "Rays passing exactly through a shared edge or vertex can slip through both neighbouring triangles because of rounding, leaving pinholes",
  "Grazing rays with a tiny determinant give noisy t and barycentrics; the epsilon threshold also discards legitimate near-parallel hits",
  "Self-intersection acne when secondary rays start on a surface without an offset or t-min",
  "Single-precision error grows far from the origin, so large world coordinates make hits jitter unless vertices are rebased"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "doi:10.1080/10867651.1997.10487468",
   "type": "paper",
   "title": "Fast, Minimum Storage Ray-Triangle Intersection",
   "authors": [
    "Tomas Möller",
    "Ben Trumbore"
   ],
   "year": 1997,
   "venue": "Journal of Graphics Tools",
   "url": "https://doi.org/10.1080/10867651.1997.10487468",
   "ids": {
    "doi": "10.1080/10867651.1997.10487468"
   },
   "license": null,
   "extra": {
    "csl_type": "journal-article"
   },
   "provenance": {
    "title": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1080/10867651.1997.10487468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1080/10867651.1997.10487468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1080/10867651.1997.10487468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1080/10867651.1997.10487468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "crossref",
     "endpoint": "https://api.crossref.org/works/10.1080/10867651.1997.10487468",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": 1,
   "__bib": "mller1997fast"
  },
  {
   "role": "implementation",
   "key": "gh:gkjohnson/three-mesh-bvh",
   "type": "code",
   "title": "gkjohnson/three-mesh-bvh",
   "description": "A BVH implementation to speed up raycasting and enable spatial queries against three.js meshes.",
   "authors": [
    "gkjohnson"
   ],
   "year": 2018,
   "venue": "GitHub",
   "url": "https://github.com/gkjohnson/three-mesh-bvh",
   "ids": {
    "repo": "gkjohnson/three-mesh-bvh"
   },
   "license": "MIT",
   "extra": {
    "stars": 3601,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-28T18:11:52Z",
    "language": "JavaScript",
    "homepage": "https://gkjohnson.github.io/three-mesh-bvh/raycast.html",
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/gkjohnson/three-mesh-bvh",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "gkjohnson2018gkjohnson"
  }
 ],
 "presets": [
  {
   "id": "moller-trumbore-tetrahedron",
   "title": "Möller–Trumbore picking a spinning tetrahedron",
   "runtime": "glsl-fragment",
   "params": {
    "eps": 0.000001,
    "spin": 0.6
   },
   "code": "// GLSL ES 3.0, Shadertoy-style. Moller-Trumbore ray/triangle test on a spinning tetrahedron.\nconst float EPS = 1e-6;\nconst float SPIN = 0.6;\n\nbool mollerTrumbore(vec3 o, vec3 d, vec3 a, vec3 b, vec3 c, out float t, out vec2 uv) {\n  vec3 e1 = b - a, e2 = c - a;\n  vec3 p = cross(d, e2);\n  float det = dot(e1, p);\n  if (abs(det) < EPS) return false;      // ray parallel to triangle plane\n  float inv = 1.0 / det;\n  vec3 s = o - a;\n  float u = dot(s, p) * inv;\n  if (u < 0.0 || u > 1.0) return false;\n  vec3 q = cross(s, e1);\n  float v = dot(d, q) * inv;\n  if (v < 0.0 || u + v > 1.0) return false;\n  t = dot(e2, q) * inv;\n  uv = vec2(u, v);\n  return t > EPS;\n}\n\nmat3 rotY(float a) { float c = cos(a), s = sin(a); return mat3(c, 0., -s, 0., 1., 0., s, 0., c); }\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 p = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  vec3 ro = vec3(0.0, 0.6, 3.2);\n  vec3 rd = normalize(vec3(p, -1.6));\n  mat3 R = rotY(iTime * SPIN);\n  vec3 V[4];\n  V[0] = R * vec3( 1.0, -0.7,  0.8);\n  V[1] = R * vec3(-1.0, -0.7,  0.8);\n  V[2] = R * vec3( 0.0, -0.7, -1.0);\n  V[3] = R * vec3( 0.0,  1.0,  0.0);\n  int I[12] = int[12](0,2,1, 0,1,3, 1,2,3, 2,0,3);\n  float best = 1e9; vec3 col = mix(vec3(0.05, 0.07, 0.12), vec3(0.35, 0.45, 0.6), p.y + 0.5);\n  for (int f = 0; f < 4; f++) {\n    vec3 a = V[I[3*f]], b = V[I[3*f+1]], c = V[I[3*f+2]];\n    float t; vec2 uv;\n    if (mollerTrumbore(ro, rd, a, b, c, t, uv) && t < best) {\n      best = t;\n      vec3 n = normalize(cross(b - a, c - a));\n      float lit = 0.25 + 0.75 * max(dot(n, normalize(vec3(0.5, 0.8, 0.6))), 0.0);\n      vec3 base = 0.5 + 0.5 * cos(6.2831 * (float(f) / 4.0 + vec3(0.0, 0.33, 0.67)));\n      float w = 1.0 - uv.x - uv.y;\n      float edge = smoothstep(0.0, 0.04, min(min(uv.x, uv.y), w));\n      col = base * lit * mix(0.35, 1.0, edge);\n    }\n  }\n  fragColor = vec4(pow(col, vec3(1.0 / 2.2)), 1.0);\n}\n",
   "basis": "original",
   "license_note": "written for this index",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-29",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "moller-trumbore-ray-triangle",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-29T15:51:50.471Z",
     "renderer": "Chromium 145.0.7632.6 headless, WebGL2 via ANGLE/SwiftShader, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.4797,
      "std": 0.1576,
      "unique_colors": 54
     },
     "image": "/specimens/moller-trumbore-tetrahedron.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "three-mesh-bvh",
  "sphere-tracing",
  "sah-bvh-construction"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "sah-bvh-construction",
   "why": "The BVH culls most triangles and hands the surviving leaves to the Möller–Trumbore test."
  },
  {
   "id": "three-mesh-bvh",
   "why": "Provides ray, closest-point and shape casts over indexed meshes using this test at the leaves."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/geometry/moller-trumbore-ray-triangle.json"
 },
 "solves": [
  "need to know which triangle a mouse ray hits",
  "ray casting against a triangle mesh for picking",
  "getting barycentric coordinates of a ray hit",
  "writing my own ray tracer leaf test",
  "cpu raycast misses thin triangle edges"
 ],
 "authority": "Möller & Trumbore, 1997",
 "url": "https://texelatlas.vercel.app/t/moller-trumbore-ray-triangle/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 5.73,
  "tier": "light",
  "p50_ms": 0.1885,
  "p95_ms": 0.1913,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 40,
  "preset_id": "moller-trumbore-tetrahedron",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 4.62,
   "tier": "light",
   "p50_ms": 0.0734,
   "p95_ms": 0.0748,
   "speedup_x": 2.57,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 3.56,
   "tier": "light",
   "p50_ms": 0.1024,
   "p95_ms": 0.1044,
   "speedup_x": 1.84,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}