在现代前端开发中,HTML5 的画布(<canvas>
)元素为我们提供了强大的绘图能力,不仅可以进行 2D 绘图,借助 WebGL 还能实现 3D 绘图。而加载 3D 模型是 3D 绘图中的一个重要应用场景,比如在电商网站展示商品的 3D 模型、在线游戏场景搭建等。本文将详细介绍如何使用 HTML5 画布和 Three.js 库来加载 3D 模型。
在开始之前,我们需要引入 Three.js 库。可以从官方网站(https://threejs.org/)下载,也可以使用 CDN 引入:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Model Loading</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
</head>
<body>
</body>
</html>
在 Three.js 中,我们需要创建一个场景(Scene
)来容纳所有的 3D 对象,一个相机(Camera
)来决定我们的视角,以及一个渲染器(Renderer
)来将场景渲染到画布上。
// 创建场景
const scene = new THREE.Scene();
// 创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
Three.js 支持多种 3D 模型格式,如 GLTF、OBJ 等。这里我们以 GLTF 格式为例,使用 GLTFLoader
来加载模型。
// 引入 GLTFLoader
const loader = new THREE.GLTFLoader();
// 加载模型
loader.load(
// 模型文件的路径
'path/to/your/model.gltf',
// 加载成功的回调函数
function (gltf) {
// 将模型添加到场景中
scene.add(gltf.scene);
},
// 加载过程中的回调函数
function (xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
},
// 加载失败的回调函数
function (error) {
console.error('Error loading model:', error);
}
);
最后,我们需要在每一帧中更新场景并进行渲染。
function animate() {
requestAnimationFrame(animate);
// 可以在这里添加动画效果,比如旋转模型
if (scene.children.length > 0) {
scene.children[0].rotation.y += 0.01;
}
renderer.render(scene, camera);
}
animate();
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>3D Model Loading</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/three-gltf-loader@1.11.3/dist/GLTFLoader.min.js"></script>
</head>
<body>
<script>
// 创建场景
const scene = new THREE.Scene();
// 创建相机
const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
camera.position.z = 5;
// 创建渲染器
const renderer = new THREE.WebGLRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);
// 引入 GLTFLoader
const loader = new THREE.GLTFLoader();
// 加载模型
loader.load(
// 模型文件的路径
'path/to/your/model.gltf',
// 加载成功的回调函数
function (gltf) {
// 将模型添加到场景中
scene.add(gltf.scene);
},
// 加载过程中的回调函数
function (xhr) {
console.log((xhr.loaded / xhr.total * 100) + '% loaded');
},
// 加载失败的回调函数
function (error) {
console.error('Error loading model:', error);
}
);
function animate() {
requestAnimationFrame(animate);
// 可以在这里添加动画效果,比如旋转模型
if (scene.children.length > 0) {
scene.children[0].rotation.y += 0.01;
}
renderer.render(scene, camera);
}
animate();
</script>
</body>
</html>
OBJLoader
。步骤 | 描述 |
---|---|
创建场景、相机和渲染器 | 为 3D 绘图搭建基础环境 |
加载 3D 模型 | 使用相应的加载器加载指定格式的模型 |
渲染场景 | 在每一帧中更新场景并进行渲染 |
通过以上步骤,我们可以在 HTML5 画布中轻松加载和展示 3D 模型,为用户带来更加丰富的交互体验。