• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

读取3D文件mesh格式工具

武飞扬头像
Liu-Eleven
帮助1

最近要做一个3d仪表,所以了解了一下3d相关方面的知识。这里暂时不做一个一个地赘述,只记录下当前的需求。

需求:

        由于****.mesh文件比较多,qt转换后的名字大多都能顾名思义,但是为了更加准确的找到某个部件,于是需要一个工具可以打开并查看****.mesh文件。自己在网上搜了很多工具,但是都打不开,要么是打开出错。

分析:

        既然Qt可以加载,何不自己写一个简单的工具。

开干:

代码如下,很简单:

  1.  
    import QtQuick
  2.  
    import QtQuick.Window
  3.  
    import QtQuick3D
  4.  
    import QtQuick.Controls
  5.  
    import Qt.labs.platform
  6.  
    import QtQuick.Layouts
  7.  
    import QtQml
  8.  
    ApplicationWindow {
  9.  
    width: 1024
  10.  
    height: 768
  11.  
    visible: true
  12.  
    title: qsTr("读取mesh文件");
  13.  
     
  14.  
    property int currentFileIndex: 0;
  15.  
    property int filesCount: 0;
  16.  
     
  17.  
    property bool isLoad: false;
  18.  
    property string current3dFile: ""
  19.  
     
  20.  
     
  21.  
     
  22.  
     
  23.  
    header: ToolBar {
  24.  
    RowLayout {
  25.  
    anchors.fill: parent
  26.  
    ToolButton {
  27.  
    Layout.preferredWidth: 150;
  28.  
    Layout.fillHeight: true;
  29.  
    text: "Open mesh Model"
  30.  
    onPressed: {
  31.  
    isLoad = false;
  32.  
    fileDialog.open()
  33.  
    }
  34.  
    }
  35.  
     
  36.  
    ToolButton {
  37.  
    Layout.preferredWidth: 100;
  38.  
    Layout.fillHeight: true;
  39.  
    text: "Previous"
  40.  
    onPressed: {
  41.  
    previous();
  42.  
    }
  43.  
    }
  44.  
     
  45.  
    ToolButton {
  46.  
    Layout.preferredWidth: 100;
  47.  
    Layout.fillHeight: true;
  48.  
    text: "Next"
  49.  
    onPressed: {
  50.  
    next();
  51.  
    }
  52.  
    }
  53.  
    Item {
  54.  
    Layout.fillHeight: true;
  55.  
    Layout.fillWidth: true;
  56.  
    }
  57.  
    }
  58.  
    }
  59.  
     
  60.  
    FileDialog {
  61.  
    id: fileDialog;
  62.  
    fileMode: FileDialog.OpenFiles;
  63.  
    nameFilters: ["3d mesh files (*.mesh)"]
  64.  
    onAccepted: {
  65.  
    current3dFile = fileDialog.files[currentFileIndex];
  66.  
    filesCount = fileDialog.files.length;
  67.  
    console.log("current file = " , current3dFile , " count = " , filesCount )
  68.  
    isLoad = true;
  69.  
    }
  70.  
    }
  71.  
     
  72.  
    footer: ToolBar {
  73.  
     
  74.  
    TextEdit {
  75.  
    id: name
  76.  
    anchors.centerIn: parent;
  77.  
    readOnly: true;
  78.  
    font.pixelSize: 18;
  79.  
    verticalAlignment: Text.AlignVCenter
  80.  
    horizontalAlignment: Text.AlignHCenter;
  81.  
     
  82.  
    text: isLoad ? ( " " Number(currentFileIndex 1) "/" filesCount " name : " getFileName(current3dFile) ) : "The 3D file is not loaded" ;
  83.  
    }
  84.  
    }
  85.  
     
  86.  
     
  87.  
    function getFileName(url) {
  88.  
    if ( url.length ===0)
  89.  
    return "is null";
  90.  
     
  91.  
    var pos1 = url.lastIndexOf('/');
  92.  
    var pos2 = url.lastIndexOf('\\');
  93.  
    var pos = Math.max(pos1, pos2);
  94.  
    if (pos < 0) {
  95.  
    return url;
  96.  
    }
  97.  
    else {
  98.  
    return url.substring(pos 1);
  99.  
    }
  100.  
    }
  101.  
     
  102.  
     
  103.  
     
  104.  
    function previous() {
  105.  
    if ( currentFileIndex > 0) {
  106.  
    currentFileIndex --;
  107.  
    }
  108.  
     
  109.  
    current3dFile = fileDialog.files[currentFileIndex];
  110.  
    }
  111.  
     
  112.  
    function next() {
  113.  
    if ( currentFileIndex < filesCount-1) {
  114.  
    currentFileIndex ;
  115.  
    }
  116.  
    current3dFile = fileDialog.files[currentFileIndex];
  117.  
    }
  118.  
     
  119.  
     
  120.  
    View3D {
  121.  
    id: view3D
  122.  
    anchors.fill: parent
  123.  
    environment: sceneEnvironment
  124.  
    SceneEnvironment {
  125.  
    id: sceneEnvironment
  126.  
    antialiasingQuality: SceneEnvironment.High
  127.  
    antialiasingMode: SceneEnvironment.MSAA
  128.  
    }
  129.  
     
  130.  
    MouseArea{
  131.  
    id:mouse
  132.  
    anchors.fill: parent
  133.  
    property int cx: 0
  134.  
    property int cy: 0
  135.  
    onWheel: function(wheel){
  136.  
    if(wheel.angleDelta.y>0)
  137.  
    camera.z = camera.z 5
  138.  
    else
  139.  
    camera.z = camera.z-5
  140.  
    }
  141.  
    onPressed:function(mouse) {
  142.  
    cx = mouse.x
  143.  
    cy = mouse.y
  144.  
    }
  145.  
     
  146.  
    onPositionChanged: function(mouse){
  147.  
    var intervalX = mouse.x-cx
  148.  
    var intervalY = mouse.y-cy
  149.  
    cameraNode.eulerRotation.y = intervalX cameraNode.eulerRotation.y
  150.  
    cameraNode.eulerRotation.x = cameraNode.eulerRotation.x-intervalY
  151.  
    cx = mouse.x
  152.  
    cy = mouse.y
  153.  
     
  154.  
    }
  155.  
    }
  156.  
    Node {
  157.  
    id: node
  158.  
    DirectionalLight {
  159.  
    id: directionalLight
  160.  
    }
  161.  
     
  162.  
    Model {
  163.  
    id: cubeModel
  164.  
    source:current3dFile;
  165.  
    DefaultMaterial {
  166.  
    id: cubeMaterial
  167.  
    diffuseColor: "#b5bcd7"
  168.  
    }
  169.  
    materials: cubeMaterial
  170.  
    }
  171.  
    }
  172.  
     
  173.  
    Node{
  174.  
    id:cameraNode
  175.  
     
  176.  
    PerspectiveCamera {
  177.  
    id: camera
  178.  
    z: 15
  179.  
    }
  180.  
    }
  181.  
    }
  182.  
    }
学新通

所有代码都在,复制下来新建一个qml的3d工程即可运行。

已经实现功能:

1.鼠标拖动旋转

2.滚轮放大缩小

3.上一个mesh文件

4.下一个mesh文件

5.mesh文件名字支持ctrl c

如果有时间还可以添加如下功能:

1.多添加几个相机机位。

2.增加材质渲染选项,可以选择其他颜色。

3.其他个性化需求。

 学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiehee
系列文章
更多 icon
同类精品
更多 icon
继续加载