@@ -92,7 +97,10 @@ let gui, tcontrols, modelGroup const restate = reactive({ targetColor: '#ff0000', loadScale: 1, - mode: 'translate' + mode: 'translate', + objects: 0, + vertices: 0, + faces: 0 }) // 状态变量 @@ -449,14 +457,8 @@ function handleMtlUpload(file) { const objLoader = new OBJLoader() objLoader.setMaterials(materials) - modelGroup = objLoader.parse(lastObjfile) - if (restate.loadScale) { - modelGroup.scale.set(restate.loadScale, restate.loadScale, restate.loadScale) - } - - scene.add(modelGroup) - tcontrols.attach(modelGroup) - transformEditCtl.value?.attachObject3D(modelGroup) + const content = objLoader.parse(lastObjfile) + addGroupToScene(content) }) } reader.readAsText(file) @@ -465,6 +467,60 @@ function handleMtlUpload(file) { } } +function addGroupToScene(group) { + modelGroup = group + if (restate.loadScale) { + // 设置默认模型缩放 + modelGroup.scale.set(restate.loadScale, restate.loadScale, restate.loadScale) + } + scene.add(modelGroup) + tcontrols.attach(modelGroup) + transformEditCtl.value?.attachObject3D(modelGroup) + + controls.target.copy(modelGroup.position) + controls.update() + + // 遍历场景中的所有对象 + let totalObjects = 0 + let totalVertices = 0 + let totalFaces = 0 + modelGroup.traverse(function(child) { + if (child.isMesh) { + totalObjects++ + + // 获取几何体 + const geometry = child.geometry + + // 如果几何体是 BufferGeometry 类型 + if (geometry.isBufferGeometry) { + // 计算顶点数 + if (geometry.attributes.position) { + totalVertices += geometry.attributes.position.count + } + + // 计算面数(假设每个面都是由三个顶点组成的三角形) + if (geometry.index) { + totalFaces += geometry.index.count / 3 + } else if (geometry.attributes.position) { + // 如果没有索引,计算非索引几何体的面数 + totalFaces += geometry.attributes.position.count / 3 + } + } + // 如果几何体是 Geometry 类型(较旧的版本使用) + else if (geometry.isGeometry) { + // 计算顶点数 + totalVertices += geometry.vertices.length + + // 计算面数 + totalFaces += geometry.faces.length + } + } + }) + restate.objects = totalObjects + restate.vertices = totalVertices + restate.faces = totalFaces +} + function handleFileChange(file) { console.log('file', file) if (!file) return @@ -491,16 +547,7 @@ function handleFileChange(file) { const loader = new FBXLoader() const arrayBuffer = reader.result const content = loader.parse(arrayBuffer, '') - modelGroup = content - if (restate.loadScale) { - modelGroup.scale.set(restate.loadScale, restate.loadScale, restate.loadScale) - } - scene.add(modelGroup) - tcontrols.attach(modelGroup) - transformEditCtl.value?.attachObject3D(modelGroup) - - controls.target.copy(modelGroup.position) - controls.update() + addGroupToScene(content) system.clearLoading() } @@ -512,13 +559,8 @@ function handleFileChange(file) { const loader = new OBJLoader() lastObjfile = reader.result //@ts-ignore - modelGroup = loader.parse(reader.result) - if (restate.loadScale) { - modelGroup.scale.set(restate.loadScale, restate.loadScale, restate.loadScale) - } - scene.add(modelGroup) - tcontrols.attach(modelGroup) - transformEditCtl.value?.attachObject3D(modelGroup) + const content = loader.parse(reader.result) + addGroupToScene(content) system.clearLoading() } @@ -529,13 +571,7 @@ function handleFileChange(file) { const arrayBuffer = reader.result //@ts-ignore const content = loader.parse(arrayBuffer, '') - modelGroup = content - if (restate.loadScale) { - modelGroup.scale.set(restate.loadScale, restate.loadScale, restate.loadScale) - } - scene.add(modelGroup) - tcontrols.attach(modelGroup) - transformEditCtl.value?.attachObject3D(modelGroup) + addGroupToScene(content) system.clearLoading() } diff --git a/src/designer/menus/Model3DView.ts b/src/designer/menus/Model3DView.ts index 846121f..8594f87 100644 --- a/src/designer/menus/Model3DView.ts +++ b/src/designer/menus/Model3DView.ts @@ -12,7 +12,7 @@ export default defineMenu((menus) => { click: () => { system.showDialog(Model3DView, { title: '模型查看器', - width: 800, + width: 950, height: 400, showClose: true, showMax: true,