Three.js glTF / GLB Model Viewer (Drag & Drop Inspector)

Drag-and-drop glTF/GLB viewer in Three.js with scene-graph, mesh, triangle, material and animation stats, wireframe, bounding-box and normals helpers, room-environment lighting, and an exported-then-reimported sample model.

Key topics: three.js gltf viewer, glb viewer, gltfloader, model inspector, drag and drop, gltfexporter

What this code does

- Loading: dropped or chosen files are read as ArrayBuffers and parsed with `GLTFLoader.parse`, entirely in the browser.
- Round-Trip Sample: a procedural robot with a keyframed wave is exported to GLB by `GLTFExporter` and loaded back like any file.
- Normalisation: models are centred on the grid and scaled to about 3 units from their bounding box.
- Inspector: counts meshes, triangles, vertices, materials, textures and clips, and prints the top of the node tree.
- Animation: every clip plays through an `AnimationMixer`.
- Lighting: `RoomEnvironment` through PMREM gives neutral studio reflections for PBR materials.
- Controls: open file, play animations, wireframe, bounding box, vertex normals, grid, environment, turntable and reload sample.

Continue Learning

JavaScript Implementation

ES6+
import { GLTFLoader } from 'three/examples/jsm/loaders/GLTFLoader.js'

const loader = new GLTFLoader()
renderer.domElement.addEventListener('dragover', (e) => e.preventDefault())
renderer.domElement.addEventListener('drop', async (e) => {
  e.preventDefault()
  const buffer = await e.dataTransfer.files[0].arrayBuffer()
  loader.parse(buffer, '', (gltf) => {
    scene.add(gltf.scene)
    const mixer = new THREE.AnimationMixer(gltf.scene)
    gltf.animations.forEach((clip) => mixer.clipAction(clip).play())
    // call mixer.update(delta) every frame
  })
})