Skip to content
1        10        20        30        40        50        60        70        80        90        100       110       120       130       140       150       160       170       1801<!doctype html>2<html lang="en">3  <head>4    <meta charset="utf-8" />5    <meta name="viewport" content="width=device-width, initial-scale=1" />6    <title>Torment — Model gallery</title>7    <style>8      :root {9        color-scheme: dark;10        --bg: #120f18;11        --panel: #1c1723;12        --line: #2d2536;13        --ink: #e9e1d6;14        --muted: #9a8fa5;15        --gold: #e0bd76;16      }17      * { box-sizing: border-box; }18      html, body { margin: 0; }19      body {20        background: var(--bg);21        color: var(--ink);22        font: 14px/1.45 ui-sans-serif, system-ui, sans-serif;23      }24      header {25        position: sticky;26        top: 0;27        z-index: 2;28        display: flex;29        flex-wrap: wrap;30        align-items: baseline;31        gap: 12px 20px;32        padding: 14px 20px;33        background: rgba(18, 15, 24, 0.94);34        border-bottom: 1px solid var(--line);35        backdrop-filter: blur(8px);36      }37      h1 { margin: 0; font: 600 18px Georgia, serif; color: var(--gold); }38      input {39        min-width: 260px;40        padding: 7px 10px;41        border: 1px solid var(--line);42        border-radius: 6px;43        background: var(--panel);44        color: var(--ink);45        font: inherit;46      }47      #status { color: var(--muted); font-size: 13px; }48      main {49        display: grid;50        grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));51        gap: 14px;52        padding: 18px 20px 40px;53      }54      figure {55        margin: 0;56        overflow: hidden;57        background: var(--panel);58        border: 1px solid var(--line);59        border-radius: 8px;60      }61      figure canvas {62        display: block;63        width: 100%;64        aspect-ratio: 4 / 3;65        background: #171320;66      }67      figcaption { padding: 9px 11px 11px; }68      .name {69        display: block;70        overflow: hidden;71        font-weight: 600;72        white-space: nowrap;73        text-overflow: ellipsis;74      }75      .meta { display: block; margin-top: 2px; color: var(--muted); font-size: 11.5px; }76      .path {77        display: block;78        overflow: hidden;79        margin-top: 2px;80        color: #6f6479;81        font-size: 10.5px;82        white-space: nowrap;83        text-overflow: ellipsis;84      }85      .error { color: #ff8d7e; }86      .hidden { display: none; }87    </style>88    <script type="importmap">89      {90        "imports": {91          "three": "../vendor/three.module.js",92          "three/addons/": "../vendor/addons/"93        }94      }95    </script>96  </head>97  <body>98    <header>99      <h1>Model gallery</h1>100      <input id="filter" type="search" placeholder="Filter by name or folder…" autocomplete="off" />101      <span id="status">Loading models…</span>102    </header>103    <main id="grid"></main>104105    <script type="module">106      import * as THREE from 'three';107      import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';108109      const grid = document.querySelector('#grid');110      const status = document.querySelector('#status');111      const filter = document.querySelector('#filter');112113      const width = 320;114      const height = 240;115116      const renderer = new THREE.WebGLRenderer({ antialias: true, alpha: false });117      renderer.setPixelRatio(1);118      renderer.setSize(width, height);119      renderer.toneMapping = THREE.ACESFilmicToneMapping;120      renderer.toneMappingExposure = 1.15;121122      const camera = new THREE.PerspectiveCamera(35, width / height, 0.01, 100);123      camera.position.set(2.05, 1.5, 2.35);124      camera.lookAt(0, 0, 0);125126      // `?q=` lets a screenshot filter the sheet without touching the input.127      const params = new URLSearchParams(location.search);128      if (params.get('q')) filter.value = params.get('q');129130      const loader = new GLTFLoader();131      const gallery = { ready: false, total: 0, done: 0, errors: [] };132      window.__gallery = gallery;133134      const cards = [];135136      function countTriangles(root) {137        let triangles = 0;138        let meshes = 0;139        root.traverse((object) => {140          if (!object.isMesh || !object.geometry?.attributes?.position) return;141          meshes += 1;142          const geometry = object.geometry;143          triangles += geometry.index144            ? geometry.index.count / 3145            : geometry.attributes.position.count / 3;146        });147        return { triangles: Math.round(triangles), meshes };148      }149150      function renderThumbnail(scene) {151        renderer.render(scene, camera);152        const canvas = document.createElement('canvas');153        canvas.width = width;154        canvas.height = height;155        canvas.getContext('2d').drawImage(renderer.domElement, 0, 0);156        return canvas;157      }158159      function makeScene(model) {160        model.updateMatrixWorld(true);161        const box = new THREE.Box3().setFromObject(model);162        const size = box.getSize(new THREE.Vector3());163        const center = box.getCenter(new THREE.Vector3());164        const maxDimension = Math.max(size.x, size.y, size.z);165166        model.position.sub(center);167168        const scene = new THREE.Scene();169        scene.background = new THREE.Color(0x171320);170        scene.add(new THREE.HemisphereLight(0xc4d4ff, 0x2a1f33, 1.7));171172        const key = new THREE.DirectionalLight(0xfff0d0, 2.6);173        key.position.set(3, 5, 4);174        scene.add(key);175176        const fill = new THREE.DirectionalLight(0x8899ff, 1.0);177        fill.position.set(-4, 2, -3);178        scene.add(fill);179180        const group = new THREE.Group();181        group.add(model);182        if (maxDimension > 0) group.scale.setScalar(1.8 / maxDimension);183        scene.add(group);184        return { scene, size };185      }186187      function addCard(path) {188        const figure = document.createElement('figure');189        const caption = document.createElement('figcaption');190        const name = document.createElement('span');191        name.className = 'name';192        name.textContent = path.split('/').pop().replace(/\.glb$/i, '');193        name.title = path;194        const meta = document.createElement('span');195        meta.className = 'meta';196        meta.textContent = 'Loading…';197        const folder = document.createElement('span');198        folder.className = 'path';199        folder.textContent = path.split('/').slice(0, -1).join(' / ');200        caption.append(name, meta, folder);201        figure.append(caption);202        grid.append(figure);203        const card = { path, figure, meta, canvas: null, search: path.toLowerCase() };204        cards.push(card);205        return card;206      }207208      function applyFilter() {209        const query = filter.value.trim().toLowerCase();210        let shown = 0;211        let hidden = 0;212        for (const card of cards) {213          const match = !query || card.search.includes(query);214          card.figure.classList.toggle('hidden', !match);215          if (match) shown += 1;216          else hidden += 1;217        }218        status.textContent = `${shown} shown${hidden ? ` · ${hidden} hidden` : ''} · ${gallery.total} models`;219        gallery.shown = shown;220      }221222      filter.addEventListener('input', applyFilter);223224      async function loadModel(card, index) {225        try {226          const gltf = await loader.loadAsync(encodeURI('/' + card.path));227          const { scene, size } = makeScene(gltf.scene);228          const canvas = renderThumbnail(scene);229          card.figure.insertBefore(canvas, card.figure.firstChild);230          card.canvas = canvas;231          const { triangles, meshes } = countTriangles(gltf.scene);232          card.meta.textContent = `${triangles.toLocaleString()} tris · ${meshes} mesh${meshes === 1 ? '' : 'es'} · ${size.x.toFixed(2)} × ${size.y.toFixed(2)} × ${size.z.toFixed(2)}`;233        } catch (error) {234          card.meta.className = 'meta error';235          card.meta.textContent = `Failed: ${error.message}`;236          gallery.errors.push({ path: card.path, message: String(error.message ?? error) });237        }238        gallery.done = index + 1;239        status.textContent = `Rendering ${gallery.done} / ${gallery.total}…`;240      }241242      async function start() {243        const response = await fetch('/__models');244        const { models } = await response.json();245        gallery.total = models.length;246247        for (const path of models) addCard(path);248        applyFilter();249250        for (const [index, path] of models.entries()) {251          const card = cards.find((candidate) => candidate.path === path);252          await loadModel(card, index);253        }254255        gallery.ready = true;256        applyFilter();257        status.textContent = `${cards.length} models${gallery.errors.length ? ` · ${gallery.errors.length} failed` : ''}`;258      }259260      start().catch((error) => {261        status.textContent = `Gallery failed: ${error.message}`;262        gallery.ready = true;263        gallery.errors.push({ path: '', message: String(error.message ?? error) });264      });265    </script>266  </body>267</html>