test-model-load.html 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. <style>
  8. body {
  9. margin: 0;
  10. font-family: Arial, sans-serif;
  11. padding: 20px;
  12. }
  13. #info {
  14. background: #f0f0f0;
  15. padding: 20px;
  16. border-radius: 5px;
  17. margin-bottom: 20px;
  18. }
  19. #status {
  20. font-weight: bold;
  21. margin: 10px 0;
  22. }
  23. button {
  24. padding: 10px 20px;
  25. margin: 5px;
  26. cursor: pointer;
  27. }
  28. </style>
  29. </head>
  30. <body>
  31. <div id="info">
  32. <h1>模型加载测试</h1>
  33. <div id="status">准备测试...</div>
  34. <button onclick="testDirectLoad()">直接加载模型</button>
  35. <button onclick="testThreeJS()">Three.js加载模型</button>
  36. <button onclick="testCesium()">检查Cesium</button>
  37. <div id="details"></div>
  38. </div>
  39. <script>
  40. const statusElement = document.getElementById('status');
  41. const detailsElement = document.getElementById('details');
  42. function updateStatus(message, isError = false) {
  43. statusElement.innerHTML = message;
  44. statusElement.style.color = isError ? 'red' : 'green';
  45. }
  46. function addDetails(message) {
  47. const p = document.createElement('p');
  48. p.textContent = message;
  49. detailsElement.appendChild(p);
  50. }
  51. function testDirectLoad() {
  52. updateStatus('正在直接加载模型...');
  53. addDetails('开始直接加载模型测试');
  54. fetch('/models/启闭机.glb')
  55. .then(response => {
  56. if (!response.ok) {
  57. throw new Error(`HTTP error! status: ${response.status}`);
  58. }
  59. return response.blob();
  60. })
  61. .then(blob => {
  62. addDetails(`模型文件大小: ${blob.size} bytes`);
  63. addDetails(`模型文件类型: ${blob.type}`);
  64. updateStatus('模型文件加载成功!');
  65. })
  66. .catch(error => {
  67. addDetails(`错误: ${error.message}`);
  68. updateStatus('模型文件加载失败!', true);
  69. });
  70. }
  71. function testThreeJS() {
  72. updateStatus('正在测试Three.js...');
  73. addDetails('开始Three.js加载模型测试');
  74. // 检查Three.js是否加载
  75. if (typeof THREE === 'undefined') {
  76. addDetails('Three.js未加载');
  77. updateStatus('Three.js未加载!', true);
  78. return;
  79. }
  80. addDetails(`Three.js版本: ${THREE.REVISION}`);
  81. // 检查GLTFLoader是否加载
  82. if (typeof GLTFLoader === 'undefined') {
  83. addDetails('GLTFLoader未加载');
  84. updateStatus('GLTFLoader未加载!', true);
  85. return;
  86. }
  87. addDetails('GLTFLoader已加载');
  88. // 创建简单的场景
  89. const scene = new THREE.Scene();
  90. const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  91. camera.position.z = 5;
  92. const renderer = new THREE.WebGLRenderer();
  93. renderer.setSize(window.innerWidth, window.innerHeight);
  94. document.body.appendChild(renderer.domElement);
  95. // 添加光源
  96. const ambientLight = new THREE.AmbientLight(0xffffff, 0.5);
  97. scene.add(ambientLight);
  98. const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
  99. directionalLight.position.set(1, 1, 1);
  100. scene.add(directionalLight);
  101. // 加载模型
  102. const loader = new GLTFLoader();
  103. addDetails('开始加载模型...');
  104. loader.load(
  105. '/models/启闭机.glb',
  106. function (gltf) {
  107. const model = gltf.scene;
  108. model.scale.set(0.5, 0.5, 0.5);
  109. scene.add(model);
  110. addDetails(`模型加载成功!部件数量: ${model.children.length}`);
  111. updateStatus('模型加载成功!');
  112. // 动画循环
  113. function animate() {
  114. requestAnimationFrame(animate);
  115. model.rotation.y += 0.01;
  116. renderer.render(scene, camera);
  117. }
  118. animate();
  119. },
  120. function (xhr) {
  121. const percent = (xhr.loaded / xhr.total * 100).toFixed(0);
  122. addDetails(`加载进度: ${percent}%`);
  123. },
  124. function (error) {
  125. addDetails(`错误: ${error.message}`);
  126. updateStatus('模型加载失败!', true);
  127. }
  128. );
  129. }
  130. function testCesium() {
  131. updateStatus('正在检查Cesium...');
  132. addDetails('开始Cesium检查测试');
  133. if (typeof Cesium === 'undefined') {
  134. addDetails('Cesium未加载');
  135. updateStatus('Cesium未加载!', true);
  136. return;
  137. }
  138. addDetails('Cesium已加载');
  139. if (typeof window.viewer === 'undefined') {
  140. addDetails('window.viewer未定义');
  141. updateStatus('Cesium viewer未初始化!', true);
  142. return;
  143. }
  144. addDetails('Cesium viewer已初始化');
  145. const canvas = document.querySelector('.cesium-viewer canvas');
  146. if (!canvas) {
  147. addDetails('Cesium canvas未找到');
  148. updateStatus('Cesium canvas未找到!', true);
  149. return;
  150. }
  151. addDetails('Cesium canvas已找到');
  152. addDetails(`Canvas尺寸: ${canvas.width}x${canvas.height}`);
  153. updateStatus('Cesium检查完成!');
  154. }
  155. // 页面加载完成
  156. window.addEventListener('load', function() {
  157. addDetails('页面加载完成');
  158. addDetails('当前URL: ' + window.location.href);
  159. });
  160. </script>
  161. </body>
  162. </html>