最近要做一个3d仪表,所以了解了一下3d相关方面的知识。这里暂时不做一一赘述,只记录下当前的需求。
需求:
由于****.mesh文件比较多,qt转换后的名字大多都能顾名思义,但是为了更加准确的找到某个部件,于是需要一个工具可以打开并查看****.mesh文件。自己在网上搜了很多工具,但是都打不开,要么是打开出错。
分析:
既然Qt可以加载,何不自己写一个简单的工具。
开干:
代码如下,很简单:
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
}
}
}
}
所有代码都在,复制下来新建一个qml的3d工程即可运行。
已经实现功能:
1.鼠标拖动旋转
2.滚轮放大缩小
3.上一个mesh文件
4.下一个mesh文件
5.mesh文件名字支持ctrl+c
如果有时间还可以添加如下功能:
1.多添加几个相机机位。
2.增加材质渲染选项,可以选择其他颜色。
3.其他个性化需求。
文章来源:https://www.toymoban.com/news/detail-522721.html
文章来源地址https://www.toymoban.com/news/detail-522721.html
到了这里,关于读取3D文件mesh格式工具的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!