最近要做一個(gè)3d儀表,所以了解了一下3d相關(guān)方面的知識(shí)。這里暫時(shí)不做一一贅述,只記錄下當(dāng)前的需求。
需求:
????????由于****.mesh文件比較多,qt轉(zhuǎn)換后的名字大多都能顧名思義,但是為了更加準(zhǔn)確的找到某個(gè)部件,于是需要一個(gè)工具可以打開(kāi)并查看****.mesh文件。自己在網(wǎng)上搜了很多工具,但是都打不開(kāi),要么是打開(kāi)出錯(cuò)。
分析:
? ? ? ? 既然Qt可以加載,何不自己寫(xiě)一個(gè)簡(jiǎn)單的工具。
開(kāi)干:
代碼如下,很簡(jiǎn)單:
import QtQuick
import QtQuick.Window
import QtQuick3D
import QtQuick.Controls
import Qt.labs.platform
import QtQuick.Layouts
import QtQml
ApplicationWindow {
width: 1024
height: 768
visible: true
title: qsTr("讀取mesh文件");
property int currentFileIndex: 0;
property int filesCount: 0;
property bool isLoad: false;
property string current3dFile: ""
header: ToolBar {
RowLayout {
anchors.fill: parent
ToolButton {
Layout.preferredWidth: 150;
Layout.fillHeight: true;
text: "Open mesh Model"
onPressed: {
isLoad = false;
fileDialog.open()
}
}
ToolButton {
Layout.preferredWidth: 100;
Layout.fillHeight: true;
text: "Previous"
onPressed: {
previous();
}
}
ToolButton {
Layout.preferredWidth: 100;
Layout.fillHeight: true;
text: "Next"
onPressed: {
next();
}
}
Item {
Layout.fillHeight: true;
Layout.fillWidth: true;
}
}
}
FileDialog {
id: fileDialog;
fileMode: FileDialog.OpenFiles;
nameFilters: ["3d mesh files (*.mesh)"]
onAccepted: {
current3dFile = fileDialog.files[currentFileIndex];
filesCount = fileDialog.files.length;
console.log("current file = " , current3dFile , " count = " , filesCount )
isLoad = true;
}
}
footer: ToolBar {
TextEdit {
id: name
anchors.centerIn: parent;
readOnly: true;
font.pixelSize: 18;
verticalAlignment: Text.AlignVCenter
horizontalAlignment: Text.AlignHCenter;
text: isLoad ? ( " "+ Number(currentFileIndex+1) + "/" + filesCount + " name : "+ getFileName(current3dFile) ) : "The 3D file is not loaded" ;
}
}
function getFileName(url) {
if ( url.length ===0)
return "is null";
var pos1 = url.lastIndexOf('/');
var pos2 = url.lastIndexOf('\\');
var pos = Math.max(pos1, pos2);
if (pos < 0) {
return url;
}
else {
return url.substring(pos + 1);
}
}
function previous() {
if ( currentFileIndex > 0) {
currentFileIndex --;
}
current3dFile = fileDialog.files[currentFileIndex];
}
function next() {
if ( currentFileIndex < filesCount-1) {
currentFileIndex ++;
}
current3dFile = fileDialog.files[currentFileIndex];
}
View3D {
id: view3D
anchors.fill: parent
environment: sceneEnvironment
SceneEnvironment {
id: sceneEnvironment
antialiasingQuality: SceneEnvironment.High
antialiasingMode: SceneEnvironment.MSAA
}
MouseArea{
id:mouse
anchors.fill: parent
property int cx: 0
property int cy: 0
onWheel: function(wheel){
if(wheel.angleDelta.y>0)
camera.z = camera.z+5
else
camera.z = camera.z-5
}
onPressed:function(mouse) {
cx = mouse.x
cy = mouse.y
}
onPositionChanged: function(mouse){
var intervalX = mouse.x-cx
var intervalY = mouse.y-cy
cameraNode.eulerRotation.y = intervalX+cameraNode.eulerRotation.y
cameraNode.eulerRotation.x = cameraNode.eulerRotation.x-intervalY
cx = mouse.x
cy = mouse.y
}
}
Node {
id: node
DirectionalLight {
id: directionalLight
}
Model {
id: cubeModel
source:current3dFile;
DefaultMaterial {
id: cubeMaterial
diffuseColor: "#b5bcd7"
}
materials: cubeMaterial
}
}
Node{
id:cameraNode
PerspectiveCamera {
id: camera
z: 15
}
}
}
}
所有代碼都在,復(fù)制下來(lái)新建一個(gè)qml的3d工程即可運(yùn)行。
已經(jīng)實(shí)現(xiàn)功能:
1.鼠標(biāo)拖動(dòng)旋轉(zhuǎn)
2.滾輪放大縮小
3.上一個(gè)mesh文件
4.下一個(gè)mesh文件
5.mesh文件名字支持ctrl+c
如果有時(shí)間還可以添加如下功能:
1.多添加幾個(gè)相機(jī)機(jī)位。
2.增加材質(zhì)渲染選項(xiàng),可以選擇其他顏色。
3.其他個(gè)性化需求。
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-522721.html
?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-522721.html
到了這里,關(guān)于讀取3D文件mesh格式工具的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!