test-model.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>模型测试</title>
  7. <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
  8. <script src="https://cdn.jsdelivr.net/npm/three@0.128.0/examples/js/loaders/GLTFLoader.js"></script>
  9. <style>
  10. body {
  11. margin: 0;
  12. overflow: hidden;
  13. }
  14. #info {
  15. position: absolute;
  16. top: 10px;
  17. left: 10px;
  18. background: rgba(255, 255, 255, 0.8);
  19. padding: 10px;
  20. border-radius: 5px;
  21. font-family: Arial, sans-serif;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="info">
  27. <h3>模型测试</h3>
  28. <p>模型路径: /models/启闭机.glb</p>
  29. <div id="status">加载中...</div>
  30. </div>
  31. <script>
  32. // 创建场景
  33. const scene = new THREE.Scene();
  34. scene.background = new THREE.Color(0xf0f0f0);
  35. // 创建相机
  36. const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  37. camera.position.z = 5;
  38. // 创建渲染器
  39. const renderer = new THREE.WebGLRenderer({ antialias: true });
  40. renderer.setSize(window.innerWidth, window.innerHeight);
  41. document.body.appendChild(renderer.domElement);
  42. // 添加光源
  43. const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
  44. scene.add(ambientLight);
  45. const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
  46. directionalLight.position.set(1, 1, 1);
  47. scene.add(directionalLight);
  48. // 加载模型
  49. const loader = new THREE.GLTFLoader();
  50. const statusElement = document.getElementById('status');
  51. loader.load(
  52. '/models/启闭机.glb',
  53. function (gltf) {
  54. const model = gltf.scene;
  55. model.scale.set(0.5, 0.5, 0.5);
  56. scene.add(model);
  57. statusElement.innerHTML = '模型加载成功!';
  58. console.log('模型加载成功:', gltf);
  59. },
  60. function (xhr) {
  61. const percent = (xhr.loaded / xhr.total * 100).toFixed(0);
  62. statusElement.innerHTML = `加载中: ${percent}%`;
  63. },
  64. function (error) {
  65. statusElement.innerHTML = '模型加载失败: ' + error.message;
  66. console.error('模型加载失败:', error);
  67. }
  68. );
  69. // 动画循环
  70. function animate() {
  71. requestAnimationFrame(animate);
  72. // 旋转模型
  73. scene.traverse(function (object) {
  74. if (object.isMesh) {
  75. object.rotation.y += 0.01;
  76. }
  77. });
  78. renderer.render(scene, camera);
  79. }
  80. animate();
  81. // 响应窗口大小变化
  82. window.addEventListener('resize', function () {
  83. camera.aspect = window.innerWidth / window.innerHeight;
  84. camera.updateProjectionMatrix();
  85. renderer.setSize(window.innerWidth, window.innerHeight);
  86. });
  87. </script>
  88. </body>
  89. </html>