html加载threejs gltf模型
时间: 2023-12-24 09:03:31 浏览: 144
以下是使用HTML加载Three.js GLTF模型的示例代码:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Load GLTF Model with Three.js</title>
<script src="https://cdn.bootcdn.net/ajax/libs/three.js/r128/three.min.js"></script>
</head>
<body>
<script>
const scene = new THREE.Scene();
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
const loader = new THREE.GLTFLoader();
loader.load('model/model.gltf', function(gltf) {
scene.add(gltf.scene);
}, undefined, function(error) {
console.error(error);
});
camera.position.z = 5;
const animate = function() {
requestAnimationFrame(animate);
renderer.render(scene, camera);
};
animate();
</script>
</body>
</html>
```
在这个示例中,我们使用了Three.js的GLTFLoader来加载GLTF模型。我们首先创建了一个场景、相机和渲染器,然后使用GLTFLoader加载模型并将其添加到场景中。最后,我们设置了相机的位置并使用requestAnimationFrame函数来渲染场景。
阅读全文