glTF models
Load models from Blender and other tools with GLTFLoader, lit by LitProgram.
glTF is the standard format for models on the web, and Blender and most 3D tools export it. GLTFLoader reads a .glb or .gltf file into wtc-gl objects: nodes become Objs, meshes become Meshes, cameras become Cameras, and each material becomes a LitProgram. It's made for the occasional hero model, not for a game's worth of assets.
import {
Renderer,
Camera,
Obj,
GLTFLoader,
Lights,
Shadow,
Vec2,
Vec3
} from 'wtc-gl'
// A small island made in Blender, loaded with GLTFLoader. Its materials
// become LitPrograms sharing one Lights, and it casts a Shadow onto itself.
export default function (container) {
const renderer = new Renderer({
dpr: Math.min(devicePixelRatio, 2),
alpha: true
})
const { gl } = renderer
container.appendChild(gl.canvas)
const camera = new Camera({ fov: 35 })
camera.position.reset(6, 4, 7)
camera.lookAt(new Vec3(0, -0.4, 0))
const scene = new Obj()
const lights = new Lights({ direction: [-0.4, 0.8, 0.5], ambient: 0.08 })
const shadow = new Shadow(gl, { lights, mapSize: 2048 })
let gltf = null
let disposed = false
let elapsed = 0
const draw = () => {
// Nothing to draw until it's loaded
if (!gltf) return
gltf.scene.rotation.y = elapsed * 0.15
gltf.scene.updateRotation()
shadow.render({ scene })
renderer.render({ scene, camera })
}
const abort = new AbortController()
// Through import.meta.url, so the bundler copies the file
const url = new URL('./assets/island.glb', import.meta.url).href
GLTFLoader.load(gl, url, { lights, shadow, signal: abort.signal })
.then((loaded) => {
if (disposed) return loaded.remove()
gltf = loaded
gltf.scene.setParent(scene)
// The loader doesn't add casters. Fit the map around the island,
// with a little room as it spins.
scene.updateMatrixWorld()
shadow.add(gltf.scene).fit(gltf.scene)
shadow.size *= 1.1
draw()
})
.catch((error) => {
if (!disposed) console.error(error)
})
return {
frame(time) {
elapsed = time
draw()
},
resize(width, height) {
renderer.dimensions = new Vec2(width, height)
camera.perspective({ aspect: width / height })
},
dispose() {
disposed = true
abort.abort()
gltf?.remove()
shadow.remove()
renderer.dispose()
gl.canvas.remove()
}
}
}Loading a file
GLTFLoader.load fetches a file and resolves with what's in it. Add gltf.scene to your own scene:
import { GLTFLoader, Lights } from 'wtc-gl'
const lights = new Lights()
const url = new URL('./models/hero.glb', import.meta.url).href
const gltf = await GLTFLoader.load(gl, url, { lights })
gltf.scene.setParent(scene)new URL(..., import.meta.url) has the bundler copy the file into the build and give you its final address. A file in your public folder can be loaded by its path instead.
The options are all optional:
lightsis theLightsevery program shares. Pass the scene's own, so the model is lit like everything else. A newLights()by default.shadowis aShadowevery program receives. The loader doesn't add casters: passgltf.scene, or the nodes that should cast, toshadow.add.baseURLis what relative URIs in the file, for its buffers and images, are resolved against. By default, the file's own URL.signalis anAbortSignalthat cancels the requests, if the page moves on before the model arrives.
GLTFLoader.parse(gl, data, options) reads a file you already have: a .glb or .gltf as an ArrayBuffer, or the parsed JSON of a .gltf.
Loading is asynchronous, so draw the rest of the scene, or nothing, until it resolves.
What you get back
| Property | What it is |
|---|---|
scene | The default scene's root, an Obj. Add it to your own scene |
scenes | Every scene's root |
nodes | Every node, by its index in the file |
meshes | Every Mesh made, including each primitive of a mesh with several |
cameras | Every Camera made, one per node with a camera |
programs | The LitPrograms made for the materials |
textures | The textures loaded for the materials |
lights | The Lights the programs share |
json | The file's parsed JSON |
remove() | Frees every geometry, program and texture made for the file |
Nodes keep their names from the file, and their transforms as position, quaternion and scale. A node with a mesh is the Mesh itself; when the mesh has several primitives, one per material, the node is an Obj with a Mesh for each, named Name.0, Name.1 and so on. Meshes used by several nodes share their geometry and program.
Find a node by name with traverse:
let sign = null
gltf.scene.traverse((node) => {
if (node.name === 'Sign') sign = node
return null
})A camera from the file starts with the file's aspect ratio, or the canvas's. Set it on resize, as for any camera: gltf.cameras[0].perspective({ aspect }).
Geometry
The POSITION, NORMAL, TEXCOORD_0 and COLOR_0 attributes are read into floats as position, normal, uv and colour; others are ignored. Because they're on the CPU, bounds, Raycast, the debug helpers and the geometry tools all work on them. Quantised attributes (KHR_mesh_quantization) are converted to floats as they're read.
A primitive without normals is shaded flat.
Materials
glTF materials are physically based; LitProgram is a simpler Blinn-Phong material. The loader approximates each one:
- The base colour factor and texture become
colour,opacityandmap. alphaModeOPAQUE draws fully opaque, MASK discards belowalphaCutoff(0.5 by default) withalphaTest, and BLEND drawstransparent.doubleSidedmaps straight across.- The emissive factor, times
KHR_materials_emissive_strength, and the emissive texture becomeemissiveandemissiveMap. - Roughness sets the highlight: none when fully rough, small and sharp when smooth. Metalness isn't modelled.
- A primitive with vertex colours gets
vertexColours.
Normal, occlusion and metallic-roughness textures are ignored. Textures keep their sampler's wrapping and filtering.
glTF stores colour factors and vertex colours as linear values, while LitProgram shades in sRGB, the space textures and the page are in. The loader converts them to sRGB, so a colour picked in Blender looks as it did there rather than darker and more saturated.
The programs are ordinary LitPrograms, so you can change them after loading:
for (const program of gltf.programs) {
program.specular = 0
}materialProgram is the function that makes them, if you need the same mapping for a material of your own.
Instancing
A node exported with EXT_mesh_gpu_instancing becomes an InstancedMesh with a matrix per instance, drawn in one call. Its program is made with instanced: true. Picking an instance with Raycast gives its instanceId.
Extras
Custom properties set in Blender can be exported as extras on each node. They aren't copied onto the objects, but gltf.json.nodes[i] and gltf.nodes[i] share an index:
gltf.json.nodes.forEach((node, i) => {
if (node.extras?.clickable) clickable.push(gltf.nodes[i])
})This is handy for tagging which meshes are colliders, which can be clicked, or which should cast shadows.
Freeing a model
gltf.remove() frees every geometry, program and texture the loader made. Take the scene out of yours first. It doesn't touch the Lights or Shadow you passed in.
gltf.scene.setParent(null)
gltf.remove()What's out of scope
- Skinning, morph targets and animation. Nodes are placed as the file has them, and move only as you move them.
- PBR materials and image-based lighting: materials are approximated as above.
- Texture coordinates other than the first set.
- Compressed files, with Draco or meshopt. Export uncompressed.
A file that requires an extension the loader doesn't support is rejected with an error naming it. The supported ones are EXT_mesh_gpu_instancing, KHR_materials_emissive_strength and KHR_mesh_quantization.
See also
- Lights and LitProgram, for the light the model is shaded with.
- Shadows, to cast shadows from a model.
- Raycast, to pick nodes and instances under the pointer.
- The glTF demo, with the file's camera and hover picking.
- API: GLTFLoader, GLTFLoaderOptions, GLTF, materialProgram.