读取3D文件mesh格式工具
最近要做一个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.其他个性化需求。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfiehee
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24