Langsung ke konten
Skillpedia
Website & UI (frontend)6.6k Β· #13861

threejs-gltf-loading

Fungsinya

Skill ini menjelaskan cara memasukkan model 3D ke scene web three.js dan membuat animasinya berjalan. Skill ini untuk pengembang yang membangun pengalaman 3D di web.

Kapan dipakai

  • βœ“Saat Anda perlu mengimpor model 3D glTF atau GLB ke scene three.js.
  • βœ“Ketika animasi model harus diputar, misalnya gerakan berjalan atau diam.
  • βœ“Ketika file model besar dan perlu geometri atau tekstur yang dikompresi.

Link skill

https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/web-engines/threejs-gltf-loading/SKILL.md

Salin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.

Cara pakai

Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.

Pesan siap tempel

Gunakan skill ini untuk membantu saya: https://github.com/gamedev-skills/awesome-gamedev-agent-skills/blob/main/skills/web-engines/threejs-gltf-loading/SKILL.md Baca file SKILL.md di link tersebut dan ikuti instruksinya untuk tugas saya.
Mau AI Anda menemukan skill seperti ini sendiri?Pasang MCP Skillpedia gratis: cukup satu baris, dan AI mencari di antara 15.000+ skill sebelum tiap tugas.Pasang dalam 1 menit β†’

Contoh permintaan ke AI

β€œMuat model karakter saya ke halaman three.js dan putar animasi berjalannya.”

5 skill baru setiap minggu. 1 menit baca.

Cara kerjanya

Skill ini menjelaskan cara memuat file dengan glTF loader, menambahkan model ke scene, dan memakai animation mixer untuk memutar klip yang disertakan model.

Konten teknis (SKILL.md asli)

Teks asli dari pembuat, dalam bahasa Inggris, langsung dari GitHub.

three.js glTF Loading

Load .gltf/.glb models and play their animations in three.js, including compressed geometry (DRACO/Meshopt) and textures (KTX2). Patterns target r186; preserve an existing project's pinned release unless migration is requested.

When to use

  • Use to import a 3D model, add it to the scene, inspect its node hierarchy, and play baked/skinned animation clips with an AnimationMixer.
  • Use when files are .gltf/.glb, or code imports GLTFLoader / DRACOLoader / KTX2Loader from three/addons/loaders/....

When not to use: creating the renderer/camera/loop β†’ threejs-scene-setup. Tuning surface look, lights, or shadows on the loaded model β†’ threejs-materials-lighting. Authoring/exporting the model itself (Blender) is out of scope; prefer glTF over OBJ/FBX for runtime.

Core workflow

  1. Why glTF. It's a transmission format: binary vertex data, PBR materials, and animations are ready to render with minimal parsing. Prefer it over OBJ (no scene graph, no animation) and FBX (heavy) for the web.
  2. Load with GLTFLoader. loader.load(url, onLoad, onProgress, onError). The result gltf has gltf.scene (the Object3D root), gltf.animations (AnimationClip[]), gltf.cameras, and gltf.asset.
  3. Add gltf.scene to your scene and frame it. Inspect the hierarchy with traverse / getObjectByName to find the parts you'll control.
  4. Play animations with an AnimationMixer. One mixer per animated root; mixer.clipAction(clip).play(); advance with mixer.update(delta) every frame.
  5. Decode compressed assets. Attach a DRACOLoader, a KTX2Loader, and/or the Meshopt decoder so DRACO meshes, KTX2 textures, and meshopt-compressed buffers load. Since r185 the DRACO and KTX2 loaders find their bundled decoder files on their own.
  6. Validate changed glTF files when the project already has the tool. Run gltf-transform validate path/to/model.glb for each changed .gltf or .glb; fix reported errors before loading.
  7. Verify what loaded β€” log the scene graph and gltf.animations, and confirm the model is visible (right scale, lit) and the clip actually plays.

Patterns

1. Load a model and frame it

import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

const loader = new GLTFLoader();
loader.load(
  'assets/robot.glb',
  (gltf) => {
    const root = gltf.scene;
    scene.add(root);
    // Inspect: gltf.animations is an array of AnimationClip.
    console.log('clips:', gltf.animations.map((c) => c.name));
  },
  (event) => {
    // total is 0 when the server sends no Content-Length (e.g. compressed responses)
    if (event.total) console.log(`${(event.loaded / event.total) * 100}% loaded`);
  },
  (error) => console.error('glTF load failed:', error)
);

2. Play a skinned animation with AnimationMixer

import * as THREE from 'three';
import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';

let mixer;                                  // declare outside so the loop can see it
const timer = new THREE.Timer();            // THREE.Clock is deprecated since r183

new GLTFLoader().load('assets/character.glb', (gltf) => {
  scene.add(gltf.scene);
  mixer = new THREE.AnimationMixer(gltf.scene);          // one mixer per animated root
  const clip = THREE.AnimationClip.findByName(gltf.animations, 'Run')
            ?? gltf.animations[0];
  mixer.clipAction(clip).play();
});

renderer.setAnimationLoop((time) => {
  timer.update(time);
  const dt = timer.getDelta();
  if (mixer) mixer.update(dt);              // advance the animation by real seconds
  renderer.render(scene, camera);
});

3. Cross-fade between two clips

const actions = {};
mixer = new THREE.AnimationMixer(gltf.scene);
for (const clip of gltf.animations) {
  actions[clip.name] = mixer.clipAction(clip);
}
let current = actions['Idle'];
current.play();

function transitionTo(name, duration = 0.3) {
  const next = actions[name];
  if (!next || next === current) return;
  next.reset().play();                      // rewind and clear any old fade
  current.crossFadeTo(next, duration, false); // fade the old one out, new one in
  current = next;
}

4. DRACO-compressed geometry

import { GLTFLoader } from 'three/addons/loaders/GLTFLoader.js';
import { DRACOLoader, DRACO_GLTF_CONFIG } from 'three/addons/loaders/DRACOLoader.js';

// r185+: DRACOLoader finds the decoder that ships n
Pertanyaan umum

Mengapa memakai glTF dan bukan OBJ atau FBX?

Menurut skill, glTF dibuat untuk dijalankan di web, OBJ tidak memiliki animasi, dan FBX berat.

Apakah ini membahas cara membuat model 3D?

Tidak. Pembuatan atau ekspor model di alat lain berada di luar skill ini.

Apakah ini membahas pencahayaan model?

Tidak. Material dan pencahayaan dibahas dalam skill three.js terpisah.

Detail

Deskripsi asli dari pembuat

Load glTF/GLB models in three.js with GLTFLoader and play their skinned animations with AnimationMixer, including DRACO/Meshopt-compressed meshes and KTX2 textures. Use when importing 3D models into three.js β€” when the user mentions glTF, GLB, GLTFLoader, AnimationMixer, animation clips, DRACOLoader, or "load a 3D model". For scene/camera/renderer setup use threejs-scene-setup; for materials and lights use threejs-materials-lighting.

repo: gamedev-skills/awesome-gamedev-agent-skills

Popularitas

Bintang GitHub
6.6k
Peringkat
#13861
Lisensi
Apache-2.0
Diperbarui
28/10/2026

Apakah skill ini berguna?

0

Masuk untuk memberi suara.

Komentar

  • …

Skill lain dari repositori ini

Skill serupa