| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100 |
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>模型测试</title>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
- <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/GLTFLoader.js"></script>
- <style>
- body {
- margin: 0;
- overflow: hidden;
- }
- #info {
- position: absolute;
- top: 10px;
- left: 10px;
- background: rgba(255, 255, 255, 0.8);
- padding: 10px;
- border-radius: 5px;
- font-family: Arial, sans-serif;
- }
- </style>
- </head>
- <body>
- <div id="info">
- <h3>模型测试</h3>
- <p>模型路径: /models/启闭机.glb</p>
- <div id="status">加载中...</div>
- </div>
- <script>
- // 创建场景
- const scene = new THREE.Scene();
- scene.background = new THREE.Color(0xf0f0f0);
-
- // 创建相机
- const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
- camera.position.z = 5;
-
- // 创建渲染器
- const renderer = new THREE.WebGLRenderer({ antialias: true });
- renderer.setSize(window.innerWidth, window.innerHeight);
- document.body.appendChild(renderer.domElement);
-
- // 添加光源
- const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
- scene.add(ambientLight);
-
- const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
- directionalLight.position.set(1, 1, 1);
- scene.add(directionalLight);
-
- // 加载模型
- const loader = new THREE.GLTFLoader();
- const statusElement = document.getElementById('status');
-
- loader.load(
- '/models/启闭机.glb',
- function (gltf) {
- const model = gltf.scene;
- model.scale.set(0.5, 0.5, 0.5);
- scene.add(model);
- statusElement.innerHTML = '模型加载成功!';
- console.log('模型加载成功:', gltf);
- },
- function (xhr) {
- const percent = (xhr.loaded / xhr.total * 100).toFixed(0);
- statusElement.innerHTML = `加载中: ${percent}%`;
- },
- function (error) {
- statusElement.innerHTML = '模型加载失败: ' + error.message;
- console.error('模型加载失败:', error);
- }
- );
-
- // 动画循环
- function animate() {
- requestAnimationFrame(animate);
-
- // 旋转模型
- scene.traverse(function (object) {
- if (object.isMesh) {
- object.rotation.y += 0.01;
- }
- });
-
- renderer.render(scene, camera);
- }
-
- animate();
-
- // 响应窗口大小变化
- window.addEventListener('resize', function () {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize(window.innerWidth, window.innerHeight);
- });
- </script>
- </body>
- </html>
|