Browse Source

3D模型查看器

master
修宁 7 months ago
parent
commit
252ab8adf0
  1. 102
      src/designer/Model3DView.vue
  2. 2
      src/designer/menus/Model3DView.ts

102
src/designer/Model3DView.vue

@ -28,6 +28,11 @@
<el-radio-button label="0.001" :value="0.001" />
</el-radio-group>
</div>
<div class="demo-color-block">
<span class="demonstration">物体数:<el-text type="danger">{{ restate.objects }}</el-text></span>
<span class="demonstration"> 顶点数:<el-text type="danger">{{ restate.vertices }}</el-text></span>
<span class="demonstration"> 三角形数:<el-text type="danger">{{ restate.faces }}</el-text></span>
</div>
</el-space>
<div class="main-content">
<Split class="model3d-content" :direction="'horizontal'">
@ -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()
}

2
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,

Loading…
Cancel
Save