• QML 3D入门知识路线


    目前使用的版本

            v5.14.0

    模块导入

            使用QML 3D时需要 import Qt3D.Core 2.14

    核心模块类

            V6以上的版本已经发布,所以有很多module会发生变化,主要有核心module、输入、逻辑、渲染、动画和扩展module,以及2D/3D场景模块       

    类名        能力

    View3D

    为绘制3D数据提供了在2D场景中显示的窗口

    OrbitCameraController

    按照轨道路线来控制场景相机

    QAbstractAnimation

    3D动画的顶层root类,派生的动画类提供了动画效果能力

          

    学习路线

            3d效果中离不开动画,所以要想学好3d部分,需要将动画部分也掌握。然后再从最基础的3d理论知识入门

    Demo

            官方给出了很多例子,可以根据官方demo进行由浅入深的学习。地址在source code中:SourceCode Root Path/qt3d/

            此外,网络上还提供了不少入门的demo,从较小的纬度(基础的加载3d资源、鼠标处理等操作)提供了演示操作,下面是收集来的各种操作集合。

            1、加载3D模型资源+将.obj文件转换成.mesh类型文件       

    1. import QtQuick 2.15
    2. import QtQuick.Window 2.15
    3. import QtQuick3D 1.15
    4. Window {
    5. width: 640
    6. height: 480
    7. visible: true
    8. title: qsTr("Hello World")
    9. View3D {
    10. id: view3D
    11. anchors.fill: parent
    12. environment: sceneEnvironment
    13. SceneEnvironment {
    14. id: sceneEnvironment
    15. antialiasingQuality: SceneEnvironment.High
    16. antialiasingMode: SceneEnvironment.MSAA
    17. }
    18. Node {
    19. id: node
    20. DirectionalLight {
    21. id: directionalLight
    22. }
    23. PerspectiveCamera {
    24. id: camera
    25. z: 15
    26. }
    27. Model {
    28. id: cubeModel
    29. source: "test.mesh"
    30. DefaultMaterial {
    31. id: cubeMaterial
    32. diffuseColor: "#4aee45"
    33. }
    34. materials: cubeMaterial
    35. }
    36. }
    37. }
    38. }

          2、鼠标控制场景缩放和旋转

    1. import QtQuick 2.15
    2. import QtQuick.Window 2.15
    3. import QtQuick3D 1.15
    4. Window {
    5. width: 640
    6. height: 480
    7. visible: true
    8. title: qsTr("Hello World")
    9. View3D {
    10. id: view3D
    11. anchors.fill: parent
    12. environment: sceneEnvironment
    13. SceneEnvironment {
    14. id: sceneEnvironment
    15. antialiasingQuality: SceneEnvironment.High
    16. antialiasingMode: SceneEnvironment.MSAA
    17. }
    18. MouseArea{
    19. id:mouse
    20. anchors.fill: parent
    21. property int cx: 0
    22. property int cy: 0
    23. onWheel: {
    24. if(wheel.angleDelta.y>0)
    25. camera.z = camera.z+5
    26. else
    27. camera.z = camera.z-5
    28. }
    29. onPressed: {
    30. cx = mouse.x
    31. cy = mouse.y
    32. }
    33. onPositionChanged: {
    34. var intervalX = mouse.x-cx
    35. var intervalY = mouse.y-cy
    36. cameraNode.eulerRotation.y = intervalX+cameraNode.eulerRotation.y
    37. cameraNode.eulerRotation.x = cameraNode.eulerRotation.x-intervalY
    38. cx = mouse.x
    39. cy = mouse.y
    40. }
    41. }
    42. Node {
    43. id: node
    44. DirectionalLight {
    45. id: directionalLight
    46. }
    47. Model {
    48. id: cubeModel
    49. source: "test.mesh"
    50. DefaultMaterial {
    51. id: cubeMaterial
    52. diffuseColor: "#4aee45"
    53. }
    54. materials: cubeMaterial
    55. }
    56. }
    57. Node{
    58. id:cameraNode
    59. PerspectiveCamera {
    60. id: camera
    61. z: 15
    62. }
    63. }
    64. }
    65. }

         

            3、设置模型的金属光泽材质

    1. import QtQuick 2.15
    2. import QtQuick.Window 2.15
    3. import QtQuick3D 1.15
    4. Window {
    5. width: 640
    6. height: 480
    7. visible: true
    8. title: qsTr("Hello World")
    9. View3D {
    10. id: view3D
    11. anchors.fill: parent
    12. environment: sceneEnvironment
    13. SceneEnvironment {
    14. id: sceneEnvironment
    15. antialiasingQuality: SceneEnvironment.High
    16. antialiasingMode: SceneEnvironment.MSAA
    17. }
    18. MouseArea{
    19. id:mouse
    20. anchors.fill: parent
    21. property int cx: 0
    22. property int cy: 0
    23. onWheel: {
    24. if(wheel.angleDelta.y>0)
    25. camera.z = camera.z+5
    26. else
    27. camera.z = camera.z-5
    28. }
    29. onPressed: {
    30. cx = mouse.x
    31. cy = mouse.y
    32. }
    33. onPositionChanged: {
    34. var intervalX = mouse.x-cx
    35. var intervalY = mouse.y-cy
    36. cameraNode.eulerRotation.y = intervalX+cameraNode.eulerRotation.y
    37. cameraNode.eulerRotation.x = cameraNode.eulerRotation.x-intervalY
    38. cx = mouse.x
    39. cy = mouse.y
    40. }
    41. }
    42. Node {
    43. id: node
    44. DirectionalLight {
    45. id: directionalLight
    46. }
    47. Model {
    48. id: cubeModel
    49. source: "test.mesh"
    50. materials: PrincipledMaterial {
    51. id: cubeMaterial
    52. baseColor: "#e9d805"
    53. roughness: 0.4
    54. metalness: 0.8
    55. }
    56. }
    57. }
    58. Node{
    59. id:cameraNode
    60. PerspectiveCamera {
    61. id: camera
    62. z: 15
    63. }
    64. }
    65. }
    66. }

            4、使用2d的动画类来操作3d模型

    1. import QtQuick 2.15
    2. import QtQuick.Window 2.15
    3. import QtQuick3D 1.15
    4. Window {
    5. width: 640
    6. height: 480
    7. visible: true
    8. title: qsTr("Hello Qt Quick 3D")
    9. View3D {
    10. id: view3D
    11. anchors.fill: parent
    12. environment: sceneEnvironment
    13. SceneEnvironment {
    14. id: sceneEnvironment
    15. antialiasingQuality: SceneEnvironment.High
    16. antialiasingMode: SceneEnvironment.MSAA
    17. }
    18. Node {
    19. id: node
    20. DirectionalLight {
    21. id: directionalLight
    22. }
    23. Model {
    24. id: cubeModel
    25. source: "test.mesh"
    26. DefaultMaterial {
    27. id: cubeMaterial
    28. diffuseColor: "#4aee45"
    29. }
    30. materials: cubeMaterial
    31. }
    32. }
    33. Node{
    34. id:cameraNode
    35. PerspectiveCamera {
    36. id: camera
    37. z: 15
    38. }
    39. NumberAnimation {
    40. id:camerAnimation
    41. target: cameraNode
    42. property: "eulerRotation.y"
    43. duration: 5000
    44. from: 0
    45. to: -360
    46. loops: Animation.Infinite
    47. running: true
    48. }
    49. }
    50. }
    51. }

     

            5、综合性的显示3D模型(材质颜色、动画以及鼠标缩放等)

             如果需要自定义背景图片时,需要设置View3D的背景色为透明,方法在上面链接中

    1. import QtQuick 2.15
    2. import QtQuick.Window 2.15
    3. import QtQuick3D 1.15
    4. Window {
    5. width: 480
    6. height: 450
    7. visible: true
    8. title: qsTr("Hello Qt Quick 3D")
    9. color: "#00192e"
    10. Image{
    11. opacity: 0.3
    12. anchors.fill: parent
    13. source: "qrc:/bg1.png"
    14. fillMode: Image.PreserveAspectCrop
    15. }
    16. View3D {
    17. id: view3D
    18. anchors.fill: parent
    19. environment: SceneEnvironment {
    20. id: sceneEnvironment
    21. //需要设置3D视图背景透明
    22. backgroundMode: SceneEnvironment.Transparent
    23. clearColor: "transparent"
    24. antialiasingQuality: SceneEnvironment.High
    25. antialiasingMode: SceneEnvironment.MSAA
    26. }
    27. MouseArea{
    28. id:mouse
    29. anchors.fill: parent
    30. property int cx: 0
    31. property int cy: 0
    32. onWheel: {
    33. if(wheel.angleDelta.y>0)
    34. cameraPerspective.z = cameraPerspective.z+5
    35. else
    36. cameraPerspective.z = cameraPerspective.z-5
    37. }
    38. onPressed: {
    39. camerAnimation.pause()
    40. cx = mouse.x
    41. cy = mouse.y
    42. }
    43. onReleased: {
    44. camerAnimation.resume()
    45. }
    46. onPositionChanged: {
    47. var intervalX = mouse.x-cx
    48. var intervalY = mouse.y-cy
    49. cameraNode.eulerRotation.y = intervalX+cameraNode.eulerRotation.y
    50. cameraNode.eulerRotation.x = cameraNode.eulerRotation.x-intervalY
    51. cx = mouse.x
    52. cy = mouse.y
    53. }
    54. }
    55. Node {
    56. id: scene
    57. DirectionalLight {
    58. x: 56
    59. eulerRotation.y: 90
    60. ambientColor: "#1c1a17"
    61. brightness: 163
    62. }
    63. Node {
    64. id: rootNode
    65. scale:Qt.vector3d(0.1,0.1,0.1)
    66. Node {
    67. x: 12.4775
    68. y: 36.2141
    69. z: 10.5153
    70. eulerRotation.x: 180
    71. eulerRotation.y: 32.0597
    72. eulerRotation.z: -180
    73. Model {
    74. x: 121.16
    75. y: -17.21
    76. z: 86.29
    77. eulerRotation.x: 180
    78. eulerRotation.y: -32.0597
    79. eulerRotation.z: -180
    80. source: "meshes/Plane.mesh"
    81. materials: PrincipledMaterial {
    82. baseColor: "#a0a2a3"
    83. roughness: 0.6
    84. metalness: 0.5
    85. }
    86. }
    87. }
    88. PointLight {
    89. x: 80.1709
    90. y: 382.888
    91. z: -150.021
    92. eulerRotation.x: -69.997
    93. eulerRotation.y: 59.9021
    94. eulerRotation.z: -180
    95. color: "#fffff5e1"
    96. }
    97. PointLight {
    98. x: -305.432
    99. y: 199.762
    100. z: 163.037
    101. eulerRotation.x: 173
    102. eulerRotation.y: -59.9035
    103. color: "#ffecf9ff"
    104. }
    105. PointLight {
    106. x: 238.189
    107. y: 380.379
    108. z: 252.482
    109. eulerRotation.x: 138.592
    110. eulerRotation.y: 36.109
    111. color: "#ff3b5966"
    112. }
    113. }
    114. Node{
    115. id:cameraNode
    116. eulerRotation.x: -20
    117. eulerRotation.y: 120
    118. PerspectiveCamera {
    119. id: cameraPerspective
    120. y: 5
    121. clipNear: 0.1
    122. fieldOfView: 50
    123. z:90
    124. clipFar: 800
    125. }
    126. NumberAnimation {
    127. id:camerAnimation
    128. target: cameraNode
    129. property: "eulerRotation.y"
    130. duration: 5000
    131. from: 0
    132. to: 360
    133. loops: Animation.Infinite
    134. running: true
    135. }
    136. }
    137. }
    138. Text {
    139. text: qsTr("鼠标左键:旋转;鼠标滚轮:缩放")
    140. anchors.right: parent.right
    141. anchors.bottom: parent.bottom
    142. font.pointSize: 12
    143. font.bold: true
    144. anchors.rightMargin: 10
    145. anchors.bottomMargin: 10
    146. font.family: "微软雅黑"
    147. color:"ghostwhite"
    148. }
    149. }
    150. }

     

    基本元素

            三维坐标

            两个坐标,连接起来就是北面墙。什么意思?想象一下,其中一个坐标是东北方的下墙角,以墙角为原点,往南是X轴;往上是Y轴;往西是Z轴。另一个坐标是西北方的下墙角,还是以墙角为原点,往南是X轴;往上是Y轴;往东是Z轴。两个坐标系相互延伸、连接起来就是一面北墙。

            坐标旋转方向

            半握右手,大拇指朝上,从手背到四指的延伸方向就是坐标轴旋转方向

            

  • 相关阅读:
    拷贝对象时编译器的一些优化
    golang_iota
    【OpenCV 例程 300篇】247. 特征检测之最大稳定极值区域(MSER)
    解决方案:读取两个文件夹里不同名的文件,处理映射不对应的文件
    【Maven】SpringBoot多模块项目利用reversion占位符,进行版本管理.打包时版本号不能识别问题
    Git获取本地仓库及基本操作指令
    数据结构之:数组
    拼多多快捷回复怎么设置
    怒冲GitHub榜首,京东T8幕后打造高并发面试手册,狂虐阿里面试官
    一致性哈希的简单认识
  • 原文地址:https://blog.csdn.net/weixin_43935710/article/details/136456214