GLB模型在Three.js场景中显示上下颠倒。
在 src/scenes/Map3DScene.vue 的 loadModel() 函数中添加:
// 在 model.position.sub(center) 之后添加:
model.rotation.x = -Math.PI / 2; // 绕X轴旋转 -90度
async function loadModel() {
const loader = new GLTFLoader()
const gltf = await loader.loadAsync(props.modelUrl)
const model = gltf.scene
// 计算包围盒并居中
const box = new THREE.Box3().setFromObject(model)
const center = box.getCenter(new THREE.Vector3())
model.position.sub(center)
// 修复上下颠倒问题:绕X轴旋转 -90度
model.rotation.x = -Math.PI / 2
const group = new THREE.Group()
group.add(model)
group.position.set(0, 0, 0)
group.rotation.y = THREE.MathUtils.degToRad(5)
map.add(group)
modelGroup = group
}
如果 -Math.PI / 2 不正确,请尝试以下替代方案:
model.rotation.x = Math.PI / 2;
model.rotation.x = Math.PI;
import { convertCoordinateSystem } from '../utils/coordinateConverter'
// 在模型加载后
const convertedModel = convertCoordinateSystem(model, 'z-up', 'y-up', 'flipped')
打开浏览器开发者工具,查看控制台中的模型信息:
[Map3D] === 模型调试信息 ===
[Map3D] 包围盒中心 (局部): (x, y, z)
[Map3D] 包围盒尺寸 (m): (x, y, z)
取消注释代码中的坐标系参考轴:
// const axes = createCoordinateAxes(100)
// group.add(axes)
项目中的变换面板可以实时调整模型位置、旋转和缩放。
创建了以下测试页面帮助调试:
test-rotation.html - 交互式旋转调试test-simple-rotation.html - 简单旋转测试指南A: 因为不同3D软件使用不同的坐标系标准,需要转换到Three.js的坐标系。
A: 不会,坐标系转换只影响静态模型的初始方向。
A: 查看模型导出软件的设置,或根据模型尺寸判断(Z轴尺寸最大可能是Z-up)。
A: 确保在居中(model.position.sub(center))之后再进行旋转。
src/scenes/Map3DScene.vue: 添加模型旋转代码src/utils/coordinateConverter.ts: 创建坐标系转换工具src/utils/coordinateDebug.ts: 创建坐标系调试工具test-rotation.html: 交互式旋转调试页面test-simple-rotation.html: 简单旋转测试指南