Browse Source

3D模型查看器

master
修宁 7 months ago
parent
commit
a50ed2b523
  1. 79
      src/designer/Model3DView.vue

79
src/designer/Model3DView.vue

@ -22,6 +22,7 @@ import { OBJLoader } from 'three/examples/jsm/loaders/OBJLoader'
import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader' import { MTLLoader } from 'three/examples/jsm/loaders/MTLLoader'
import * as dat from 'three/examples/jsm/libs/lil-gui.module.min.js' import * as dat from 'three/examples/jsm/libs/lil-gui.module.min.js'
import Stats from 'three/examples/jsm/libs/stats.module' import Stats from 'three/examples/jsm/libs/stats.module'
import { TransformControls } from 'three/examples/jsm/controls/TransformControls.js'
// DOM refs // DOM refs
const canvasContainer = ref(null) const canvasContainer = ref(null)
@ -31,33 +32,26 @@ const guiPanel = ref(null)
let scene, camera, renderer, controls let scene, camera, renderer, controls
let statsControls, axesHelper, gridHelper let statsControls, axesHelper, gridHelper
let modelGroup = new THREE.Group() let modelGroup = new THREE.Group()
let gui let gui, transformControls
// //
const state = { const state = {
autoRotate: false,
showAxesHelper: true, showAxesHelper: true,
showGridHelper: true, showGridHelper: true,
cameraPosition: [0, 5, 10], obj: {
positionX: 0, position: { x: 0, y: 0, z: 0 },
positionY: 0, rotation: { x: 0, y: 0, z: 0 },
positionZ: 0, scale: { x: 1, y: 1, z: 1 }
rotationX: 0, },
rotationY: 0,
rotationZ: 0,
scaleX: 1,
scaleY: 1,
scaleZ: 1,
camera: { camera: {
position: { x: 0, y: 5, z: 10 }, position: { x: 0, y: 5, z: 10 },
rotation: { x: 0, y: 0, z: 0 } rotation: { x: 0, y: 0, z: 0 }
} },
mode: 'translate'
} }
onMounted(() => { onMounted(() => {
const viewerDom = canvasContainer.value
nextTick(() => { nextTick(() => {
console.log('viewerDom', viewerDom)
initThree() initThree()
initGUI() initGUI()
}) })
@ -109,12 +103,24 @@ function initThree() {
statsControls.dom.style.position = 'absolute' statsControls.dom.style.position = 'absolute'
viewerDom.appendChild(statsControls.dom) viewerDom.appendChild(statsControls.dom)
// TransformControls
transformControls = new TransformControls(camera, renderer.domElement)
transformControls.attach(modelGroup)
//
transformControls.addEventListener('change', () => {
//
renderer.render(scene, camera)
})
transformControls.addEventListener('dragging-changed', function(event) {
//
controls.enabled = !event.value
})
// //
function animate() { function animate() {
requestAnimationFrame(animate) requestAnimationFrame(animate)
if (state.autoRotate) {
modelGroup.rotation.y += 0.01
}
renderView() renderView()
} }
@ -133,9 +139,6 @@ function initGUI() {
gui = new dat.GUI({ autoPlace: false }) gui = new dat.GUI({ autoPlace: false })
guiDom.appendChild(gui.domElement) guiDom.appendChild(gui.domElement)
//
gui.add(state, 'autoRotate').name('自动旋转')
// 线 // 线
gui.add(state, 'showAxesHelper').name('显示坐标轴').onChange(val => { gui.add(state, 'showAxesHelper').name('显示坐标轴').onChange(val => {
axesHelper.visible = val axesHelper.visible = val
@ -146,7 +149,7 @@ function initGUI() {
}) })
// Position // Position
const cameraPosFolder = gui.addFolder('Camera Position') const cameraPosFolder = gui.addFolder('摄像机位置')
cameraPosFolder.add(state.camera.position, 'x', -100, 100).step(0.1).listen().onChange(val => { cameraPosFolder.add(state.camera.position, 'x', -100, 100).step(0.1).listen().onChange(val => {
camera.position.x = val camera.position.x = val
}) })
@ -158,7 +161,7 @@ function initGUI() {
}) })
// Rotation (in radians) // Rotation (in radians)
const cameraRotationFolder = gui.addFolder('Camera Rotation') const cameraRotationFolder = gui.addFolder('摄像机旋转角')
cameraRotationFolder.add(state.camera.rotation, 'x', -Math.PI, Math.PI).listen().onChange(val => { cameraRotationFolder.add(state.camera.rotation, 'x', -Math.PI, Math.PI).listen().onChange(val => {
camera.rotation.x = val camera.rotation.x = val
}) })
@ -172,40 +175,44 @@ function initGUI() {
// Position // Position
const positionFolder = gui.addFolder('Position') const positionFolder = gui.addFolder('Position')
positionFolder.add(state, 'positionX', -10, 10).onChange(val => { positionFolder.add(state.obj.position, 'x', -10, 10).onChange(val => {
modelGroup.position.x = val modelGroup.position.x = val
}) })
positionFolder.add(state, 'positionY', -10, 10).onChange(val => { positionFolder.add(state.obj.position, 'y', -10, 10).onChange(val => {
modelGroup.position.y = val modelGroup.position.y = val
}) })
positionFolder.add(state, 'positionZ', -10, 10).onChange(val => { positionFolder.add(state.obj.position, 'z', -10, 10).onChange(val => {
modelGroup.position.z = val modelGroup.position.z = val
}) })
// Rotation (in radians) // Rotation (in radians)
const rotationFolder = gui.addFolder('Rotation') const rotationFolder = gui.addFolder('Rotation')
rotationFolder.add(state, 'rotationX', -Math.PI, Math.PI).onChange(val => { rotationFolder.add(state.obj.rotation, 'x', -Math.PI, Math.PI).onChange(val => {
modelGroup.rotation.x = val modelGroup.rotation.x = val
}) })
rotationFolder.add(state, 'rotationY', -Math.PI, Math.PI).onChange(val => { rotationFolder.add(state.obj.rotation, 'y', -Math.PI, Math.PI).onChange(val => {
modelGroup.rotation.y = val modelGroup.rotation.y = val
}) })
rotationFolder.add(state, 'rotationZ', -Math.PI, Math.PI).onChange(val => { rotationFolder.add(state.obj.rotation, 'z', -Math.PI, Math.PI).onChange(val => {
modelGroup.rotation.z = val modelGroup.rotation.z = val
}) })
// Scale // Scale
const scaleFolder = gui.addFolder('Scale') const scaleFolder = gui.addFolder('Scale')
scaleFolder.add(state, 'scaleX', 0.001, 10).onChange(val => { scaleFolder.add(state.obj.scale, 'x', 0.001, 10).onChange(val => {
modelGroup.scale.x = val modelGroup.scale.x = val
}) })
scaleFolder.add(state, 'scaleY', 0.001, 10).onChange(val => { scaleFolder.add(state.obj.scale, 'y', 0.001, 10).onChange(val => {
modelGroup.scale.y = val modelGroup.scale.y = val
}) })
scaleFolder.add(state, 'scaleZ', 0.001, 10).onChange(val => { scaleFolder.add(state.obj.scale, 'z', 0.001, 10).onChange(val => {
modelGroup.scale.z = val modelGroup.scale.z = val
}) })
gui.add(state, 'mode', ['translate', 'rotate', 'scale']).onChange(function(value) {
transformControls.setMode(value)
})
// //
// const cameraFolder = gui.addFolder('') // const cameraFolder = gui.addFolder('')
// cameraFolder.add(state.cameraPosition, 0, -10, 10).name('X') // cameraFolder.add(state.cameraPosition, 0, -10, 10).name('X')
@ -269,9 +276,13 @@ function initMode3DCamera() {
const cameraNew = new THREE.PerspectiveCamera(25, viewerDom.clientWidth / viewerDom.clientHeight, 1, 2000) const cameraNew = new THREE.PerspectiveCamera(25, viewerDom.clientWidth / viewerDom.clientHeight, 1, 2000)
// //
// cameraNew.position.set(4, 2, -3); // cameraNew.position.set(4, 2, -3);
cameraNew.position.set(30, 30, 30) // cameraNew.position.set(30, 30, 30)
// 2.4,30,1.5;
cameraNew.position.set(2.4, 20, 1.5)
// //
cameraNew.lookAt(0, 0, 0) // cameraNew.lookAt(0, 0, 0)
// -1.57,0,1.57
cameraNew.rotation.set(-1.57, 0, 1.57)
camera = cameraNew camera = cameraNew
scene.add(camera) scene.add(camera)

Loading…
Cancel
Save