A loader for loading glTF models into the Threejs scene.
glTF is a specification for the efficient transmission and loading of 3D scenes and models.
glTF minimizes both the size of 3D assets, and the runtime processing needed to unpack and use those assets.
A glTF file may contain one or more scenes, meshes, materials, textures, skins, skeletons, morph targets, animations, lights and cameras.
Assets can be provided in either JSON (.gltf) or binary (.glb) format.
Resources
The 3D models used in this lesson can be easily created using Blender. If you don't want to use blender to create the models, then you can download them from the zip file named models3.zip. Extract the models3.zip contents into the ./dist/client/models/ folder.
import*asTHREEfrom'three'import{OrbitControls}from'three/examples/jsm/controls/OrbitControls'import{GLTFLoader}from'three/examples/jsm/loaders/GLTFLoader'importStatsfrom'three/examples/jsm/libs/stats.module'constscene=newTHREE.Scene()scene.add(newTHREE.AxesHelper(5))constlight=newTHREE.SpotLight(0xffffff,Math.PI*20)light.position.set(5,5,5)scene.add(light)constcamera=newTHREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,0.1,1000)camera.position.z=2constrenderer=newTHREE.WebGLRenderer()// Since Three r150, lighting has changed significantly with every version up to three r158// keep the threejs defaults, and reduce light watts in blender if using punctual lights// if using Threejs lights, then you need to experiment with light intensity.// renderer.physicallyCorrectLights = true //deprecated// renderer.useLegacyLights = false //deprecatedrenderer.shadowMap.enabled=truerenderer.setSize(window.innerWidth,window.innerHeight)document.body.appendChild(renderer.domElement)constcontrols=newOrbitControls(camera,renderer.domElement)controls.enableDamping=trueconstloader=newGLTFLoader()loader.load('models/monkey.glb',function(gltf){// gltf.scene.traverse(function (child) {// if ((child as THREE.Mesh).isMesh) {// const m = (child as THREE.Mesh)// m.receiveShadow = true// m.castShadow = true// }// if (((child as THREE.Light)).isLight) {// const l = (child as THREE.SpotLight)// l.castShadow = true// l.shadow.bias = -.003// l.shadow.mapSize.width = 2048// l.shadow.mapSize.height = 2048// }// })scene.add(gltf.scene)},(xhr)=>{console.log((xhr.loaded/xhr.total)*100+'% loaded')},(error)=>{console.log(error)})window.addEventListener('resize',onWindowResize,false)functiononWindowResize(){camera.aspect=window.innerWidth/window.innerHeightcamera.updateProjectionMatrix()renderer.setSize(window.innerWidth,window.innerHeight)render()}conststats=newStats()document.body.appendChild(stats.dom)functionanimate(){requestAnimationFrame(animate)controls.update()render()stats.update()}functionrender(){renderer.render(scene,camera)}animate()
import*asTHREEfrom'three'import{OrbitControls}from'three/examples/jsm/controls/OrbitControls'import{GLTFLoader}from'three/examples/jsm/loaders/GLTFLoader'importStatsfrom'three/examples/jsm/libs/stats.module'constscene=newTHREE.Scene()scene.add(newTHREE.AxesHelper(5))// const light = new THREE.SpotLight(0xffffff, Math.PI * 20)// light.position.set(5, 5, 5)// scene.add(light);constcamera=newTHREE.PerspectiveCamera(75,window.innerWidth/window.innerHeight,0.1,1000)camera.position.z=2constrenderer=newTHREE.WebGLRenderer()// Since Three r150, lighting has changed significantly with every version up to three r158// keep the threejs defaults, and reduce light watts in blender if using punctual lights// if using Threejs lights, then you need to experiment with light intensity.// renderer.physicallyCorrectLights = true //deprecated// renderer.useLegacyLights = false //deprecatedrenderer.shadowMap.enabled=truerenderer.setSize(window.innerWidth,window.innerHeight)document.body.appendChild(renderer.domElement)constcontrols=newOrbitControls(camera,renderer.domElement)controls.enableDamping=trueconstloader=newGLTFLoader()loader.load('models/monkey.glb',function(gltf){gltf.scene.traverse(function(child){if((childasTHREE.Mesh).isMesh){constm=childasTHREE.Meshm.receiveShadow=truem.castShadow=true}if((childasTHREE.Light).isLight){constl=childasTHREE.SpotLightl.castShadow=truel.shadow.bias=-0.003l.shadow.mapSize.width=2048l.shadow.mapSize.height=2048}})scene.add(gltf.scene)},(xhr)=>{console.log((xhr.loaded/xhr.total)*100+'% loaded')},(error)=>{console.log(error)})window.addEventListener('resize',onWindowResize,false)functiononWindowResize(){camera.aspect=window.innerWidth/window.innerHeightcamera.updateProjectionMatrix()renderer.setSize(window.innerWidth,window.innerHeight)render()}conststats=newStats()document.body.appendChild(stats.dom)functionanimate(){requestAnimationFrame(animate)controls.update()render()stats.update()}functionrender(){renderer.render(scene,camera)}animate()