{
 "id": "meshoptimizer",
 "title": "meshoptimizer mesh optimization",
 "aliases": [
  "meshopt",
  "EXT_meshopt_compression"
 ],
 "domain": "performance",
 "subdomain": "mesh optimization",
 "stage": "performance",
 "realtime": "realtime",
 "platforms": [
  "webgl2",
  "webgpu",
  "desktop-gpu",
  "mobile-gpu",
  "cpu"
 ],
 "year_introduced": 2016,
 "inputs": [
  "mesh"
 ],
 "outputs": [
  "mesh"
 ],
 "style": "both",
 "dynamic": "both",
 "summary": "An MIT library that reorders for vertex cache and fetch efficiency, simplifies meshes for LOD, builds meshlets, and encodes vertex and index buffers for fast decoding.",
 "how_it_works": "Index buffers are reordered for post-transform cache hits, then vertices are reordered for fetch locality, and overdraw can be reduced further. Simplification collapses edges with a quadric error metric, returning a measured error that picks LOD by screen size. Buffer codecs byte-transposes and delta-codes streams so a general-purpose compressor works well, and the decoder runs at gigabytes per second using SIMD or WebAssembly.\n\nThe same library builds meshlets with bounds and cones for cluster pipelines.",
 "when_to_use": "Choose it as the default post-process for any mesh shipped to a GPU, and for LOD chains and meshlets. Prefer `draco-mesh-compression` when size is the only goal and decode speed is irrelevant. Prefer `nanite-virtualized-geometry` when the engine builds cluster hierarchies itself.",
 "cost": {
  "class": "cheap",
  "notes": "Offline processing; runtime cost is a fast decode."
 },
 "failure_modes": [
  "Aggressive simplification collapses thin features and breaks UV seams",
  "Attribute errors are not captured unless weighted",
  "Decoding requires matching filter and mode settings per stream",
  "Lossy quantization visible at large object scales"
 ],
 "sources": [
  {
   "role": "primary",
   "key": "gh:zeux/meshoptimizer",
   "type": "code",
   "title": "zeux/meshoptimizer",
   "description": "Mesh optimization library that makes meshes smaller and faster to render",
   "authors": [
    "zeux"
   ],
   "year": 2016,
   "venue": "GitHub",
   "url": "https://github.com/zeux/meshoptimizer",
   "ids": {
    "repo": "zeux/meshoptimizer"
   },
   "license": "MIT",
   "extra": {
    "stars": 8972,
    "stars_checked": "2026-11-29",
    "archived": false,
    "fork": false,
    "pushed_at": "2026-11-29T00:55:16Z",
    "language": "C++",
    "homepage": "https://meshoptimizer.org/",
    "license_name": "MIT License"
   },
   "provenance": {
    "title": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/meshoptimizer",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "authors": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/meshoptimizer",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "year": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/meshoptimizer",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "venue": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/meshoptimizer",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "url": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/meshoptimizer",
     "checked_at": "2026-11-29T15:51:27.588Z"
    },
    "license": {
     "authority": "github",
     "endpoint": "https://api.github.com/repos/zeux/meshoptimizer",
     "checked_at": "2026-11-29T15:51:27.588Z"
    }
   },
   "status": "verified",
   "checked_at": "2026-11-29T15:51:27.588Z",
   "title_sim": null,
   "__bib": "zeux2016zeux"
  }
 ],
 "presets": [
  {
   "id": "meshoptimizer-meshlet-default",
   "title": "meshoptimizer meshlet limits",
   "runtime": "json-params",
   "params": {
    "max_vertices": 64,
    "max_triangles": 126,
    "cone_weight": 0.25
   },
   "basis": "0",
   "license_note": "Defaults from the meshoptimizer README (MIT license).",
   "execution_fidelity": "parameter-only",
   "proof": {
    "kind": "parameter",
    "label": "parameter preset",
    "ok": true,
    "proof": null
   }
  },
  {
   "id": "meshoptimizer-live",
   "title": "meshoptimizer — 64-triangle meshlet clusters & QEM LOD wireframe (left) vs vertex-cache-optimized smooth shading (right)",
   "runtime": "glsl-fragment",
   "params": {
    "P_MESHLET_RES": 7,
    "P_LOD_ERROR": 0.55,
    "P_WIREFRAME": 0.45,
    "P_SPLIT": 0.48
   },
   "basis": "original",
   "license_note": "MIT — written for this index.",
   "code": "const float P_MESHLET_RES = 7.00; // P Meshlet Density [4.0, 12.0, 1.0]\nconst float P_LOD_ERROR = 0.55; // P QEM LOD Step [0.0, 1.0, 0.05]\nconst float P_WIREFRAME = 0.45; // P Triangle Wire [0.0, 1.0, 0.05]\nconst float P_SPLIT = 0.48; // P Meshlets / Shaded [0.0, 1.0, 0.02]\n\nfloat hash31(vec3 p) {\n  p = fract(p * 0.1031);\n  p += dot(p, p.yzx + 33.33);\n  return fract((p.x + p.y) * p.z);\n}\n\nvec2 rot2(vec2 p, float a) {\n  float c = cos(a);\n  float s = sin(a);\n  return vec2(c * p.x - s * p.y, s * p.x + c * p.y);\n}\n\n// Sculpted twisted torus-shell geometry ideal for showcasing spatial meshlet partitioning & LOD\nfloat mapMesh(vec3 p, float t) {\n  p.xz = rot2(p.xz, t * 0.48);\n  p.xy = rot2(p.xy, 0.30);\n  vec2 q = vec2(length(p.xz) - 0.38, p.y);\n  float ang = atan(p.z, p.x);\n  q = rot2(q, ang * 1.5);\n  float dBox = length(max(abs(q) - vec2(0.11, 0.16), 0.0)) - 0.055;\n  return dBox * 0.82;\n}\n\nvec3 calcNormal(vec3 p, float t) {\n  vec2 e = vec2(0.0025, 0.0);\n  return normalize(vec3(\n    mapMesh(p + e.xyy, t) - mapMesh(p - e.xyy, t),\n    mapMesh(p + e.yxy, t) - mapMesh(p - e.yxy, t),\n    mapMesh(p + e.yyx, t) - mapMesh(p - e.yyx, t)\n  ));\n}\n\nvec3 aces(vec3 x) {\n  x = max(x, 0.0);\n  return (x * (2.51 * x + 0.03)) / (x * (2.43 * x + 0.59) + 0.14);\n}\n\nvoid mainImage(out vec4 fragColor, in vec2 fragCoord) {\n  vec2 uv = (fragCoord - 0.5 * iResolution.xy) / iResolution.y;\n  float screenX = fragCoord.x / max(iResolution.x, 1.0);\n  float split = clamp(P_SPLIT, 0.0, 1.0);\n  float mRes = clamp(P_MESHLET_RES, 3.0, 14.0);\n  float lodErr = clamp(P_LOD_ERROR, 0.0, 1.0);\n  float wireAmt = clamp(P_WIREFRAME, 0.0, 1.0);\n\n  vec3 ro = vec3(0.0, 0.28, 2.08);\n  vec3 rd = normalize(vec3(uv.x, uv.y - 0.07, -1.50));\n  vec3 lDir = normalize(vec3(0.58, 0.78, 0.52));\n  vec3 col = mix(vec3(0.05, 0.07, 0.12), vec3(0.13, 0.18, 0.28), smoothstep(-0.4, 0.5, uv.y));\n\n  float tRay = 0.05;\n  bool hit = false;\n  for (int i = 0; i < 64; i++) {\n    float h = mapMesh(ro + rd * tRay, iTime);\n    if (h < 0.002) { hit = true; break; }\n    tRay += h;\n    if (tRay > 6.0) break;\n  }\n\n  if (hit) {\n    vec3 p = ro + rd * tRay;\n    vec3 n = calcNormal(p, iTime);\n    // Transform hit to rigid object space for stable meshlet IDs\n    vec3 pObj = p;\n    pObj.xz = rot2(pObj.xz, iTime * 0.48);\n    pObj.xy = rot2(pObj.xy, 0.30);\n\n    float ndl = max(dot(n, lDir), 0.0);\n    float spec = pow(max(dot(reflect(-lDir, n), -rd), 0.0), 42.0);\n    float fres = pow(1.0 - max(dot(n, -rd), 0.0), 4.0);\n\n    if (screenX < split) {\n      // Left: Voronoi-like 3D spatial meshlet clusters (each cluster ~64 tris) + QEM triangle density grid\n      vec3 scaled = pObj * mRes;\n      vec3 cid = floor(scaled);\n      float bestD = 9.0;\n      float secondD = 9.0;\n      vec3 bestId = cid;\n      for (int dz = -1; dz <= 1; dz++) {\n        for (int dy = -1; dy <= 1; dy++) {\n          for (int dx = -1; dx <= 1; dx++) {\n            vec3 nb = cid + vec3(float(dx), float(dy), float(dz));\n            vec3 jit = vec3(hash31(nb), hash31(nb + 13.7), hash31(nb + 29.1));\n            float dd = length((nb + jit * 0.75 + 0.125) - scaled);\n            if (dd < bestD) {\n              secondD = bestD;\n              bestD = dd;\n              bestId = nb;\n            } else if (dd < secondD) {\n              secondD = dd;\n            }\n          }\n        }\n      }\n      float hId = hash31(bestId + 5.3);\n      vec3 meshletCol = 0.50 + 0.44 * cos(vec3(0.0, 2.05, 4.10) + hId * 6.2831853);\n      float clusterBorder = smoothstep(0.08, 0.015, secondD - bestD);\n\n      // Sub-cluster QEM simplified triangle wireframe\n      float triFreq = mix(28.0, 10.0, lodErr);\n      vec3 tf = fract(pObj * triFreq);\n      float triEdge = min(min(min(tf.x, 1.0 - tf.x), min(tf.y, 1.0 - tf.y)), abs(tf.x + tf.y - 1.0));\n      float wire = smoothstep(0.08, 0.015, triEdge);\n\n      col = meshletCol * (0.28 + 0.88 * ndl) + vec3(0.95) * spec * 0.35;\n      col = mix(col, vec3(0.06, 0.08, 0.12), wire * wireAmt * 0.55);\n      col = mix(col, vec3(0.98, 0.98, 1.0), clusterBorder * 0.65);\n    } else {\n      // Right: Vertex-cache & overdraw-optimized clean shaded metallic ceramic surface\n      vec3 alb = mix(vec3(0.76, 0.79, 0.84), vec3(0.92, 0.66, 0.34), 0.5 + 0.5 * n.x);\n      col = alb * (0.22 + 0.94 * ndl) + vec3(1.15, 1.08, 0.98) * spec * 0.90 + vec3(0.32, 0.66, 0.98) * fres * 0.55;\n    }\n  } else if (rd.y < -0.005) {\n    float tf = (-0.54 - ro.y) / rd.y;\n    if (tf > 0.05 && tf < 14.0) {\n      vec3 fp = ro + rd * tf;\n      float sh = smoothstep(0.28, 0.72, length(fp.xz));\n      vec2 g = abs(fract(fp.xz * 2.5) - 0.5);\n      float grid = smoothstep(0.04, 0.01, min(g.x, g.y));\n      col = mix(col, mix(vec3(0.10, 0.12, 0.17), vec3(0.24, 0.30, 0.42), grid) * (0.35 + 0.65 * sh), exp(-tf * 0.16));\n    }\n  }\n\n  float sep = smoothstep(0.0035, 0.0008, abs(screenX - split));\n  col = mix(col, vec3(0.95, 0.98, 1.0), sep);\n  fragColor = vec4(pow(clamp(aces(col), 0.0, 1.0), vec3(1.0 / 2.2)), 1.0);\n}",
   "execution_fidelity": "conceptual-diagram",
   "proof": {
    "kind": "rendered",
    "label": "rendered 2026-11-30",
    "ok": true,
    "proof": {
     "ok": true,
     "technique": "meshoptimizer",
     "runtime": "glsl-fragment",
     "checked_at": "2026-11-30T21:07:15.000Z",
     "renderer": "Headless EGL Tesla T4/PCIe/SSE2, OpenGL 3.30 Core, 512×512, iTime=1.5",
     "stats": {
      "mean": 0.5501,
      "std": 0.1713,
      "min": 0.241,
      "max": 0.9498
     },
     "image": "/specimens/meshoptimizer-live.webp",
     "reason": null
    }
   }
  }
 ],
 "related": [
  "meshlets",
  "draco-mesh-compression"
 ],
 "extends": [],
 "supersedes": [],
 "combines_with": [
  {
   "id": "meshlets",
   "why": "meshopt builds clusters and orders their indices for mesh shaders."
  }
 ],
 "provenance": {
  "prose": "original text written for this index (research draft, editorially reviewed)",
  "sources_verified_at": "2026-11-29T15:51:27.588Z",
  "draft": "data/drafts/performance/meshoptimizer.json"
 },
 "solves": [
  "scene has too many triangles for the gpu",
  "mesh loading and vertex fetch is slow",
  "reduce model size for web delivery",
  "need lod versions of meshes automatically"
 ],
 "authority": "zeux, 2016",
 "url": "https://texelatlas.vercel.app/t/meshoptimizer/",
 "execution_fidelity": "conceptual-diagram",
 "fidelity_label": "Visual Diagram",
 "benchmark_eligible": false,
 "gpu_weight": {
  "weight_x": 20.01,
  "tier": "heavy",
  "p50_ms": 0.6582,
  "p95_ms": 0.6826,
  "baseline_p50_ms": 0.0329,
  "gpu": "Tesla T4/PCIe/SSE2",
  "resolution": "1920x1080",
  "method": "opengl-timer-query",
  "frames": 32,
  "preset_id": "meshoptimizer-live",
  "execution_fidelity": "conceptual-diagram",
  "fidelity_label": "Visual Diagram",
  "benchmark_eligible": false,
  "generated_at": "2026-11-30T21:07:47Z",
  "l4": {
   "weight_x": 18.17,
   "tier": "moderate",
   "p50_ms": 0.2889,
   "p95_ms": 0.2924,
   "speedup_x": 2.28,
   "gpu": "NVIDIA L4/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0159
  },
  "a100": {
   "weight_x": 13.7,
   "tier": "moderate",
   "p50_ms": 0.3947,
   "p95_ms": 0.4003,
   "speedup_x": 1.67,
   "gpu": "NVIDIA A100-SXM4-40GB/PCIe/SSE2",
   "resolution": "1920x1080",
   "baseline_p50_ms": 0.0288
  }
 }
}