MeshViewer.qml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. // Copyright (C) 2025 The Qt Company Ltd.
  2. // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
  3. import QtQuick
  4. import QtQuick.Controls
  5. import QtQuick.Layouts
  6. import QtQuick.Window
  7. import QtQuick.Dialogs
  8. import QtQuick3D
  9. import QtQuick3D.Helpers
  10. import QtQuick3D.lightmapviewer
  11. import LightmapFile 1.0
  12. ColumnLayout {
  13. id: root
  14. anchors.fill: parent
  15. property string selectedModelCandidate: ""
  16. onSelectedModelCandidateChanged: {
  17. if (!selectedModelCandidate || selectedModelCandidate === "") {
  18. view.lmTextureCandidates = []
  19. lview.mSelectedTextureCandidate = -1
  20. return
  21. }
  22. view.lmTextureCandidates = LightmapFile.texturesAvailableFor(
  23. selectedModelCandidate)
  24. view.lmSelectedTextureCandidate
  25. = view.lmTextureCandidates.length ? view.lmTextureCandidates[0].value : -1
  26. }
  27. Pane {
  28. Layout.fillWidth: true
  29. padding: 8
  30. ColumnLayout {
  31. width: parent.width
  32. spacing: 6
  33. ColumnLayout {
  34. RowLayout {
  35. Label {
  36. text: "Key:"
  37. }
  38. ComboBox {
  39. id: comboLmModelCandidate
  40. Layout.preferredWidth: 220
  41. model: view.lmModelCandidates
  42. onActivated: root.selectedModelCandidate = currentText
  43. currentIndex: {
  44. const i = view.lmModelCandidates.indexOf(
  45. root.selectedModelCandidate)
  46. return (i >= 0 ? i : -1)
  47. }
  48. enabled: !!selectedEntry
  49. && selectedEntry.kind === "mesh"
  50. }
  51. Label {
  52. text: "Texture:"
  53. }
  54. ComboBox {
  55. id: comboLmTextureCandidate
  56. Layout.preferredWidth: 220
  57. model: view.lmTextureCandidates
  58. textRole: "name"
  59. valueRole: "value"
  60. function indexForValue(val) {
  61. for (var i = 0; i < view.lmTextureCandidates.length; ++i)
  62. if (view.lmTextureCandidates[i].value === val)
  63. return i
  64. return -1
  65. }
  66. onActivated: view.lmSelectedTextureCandidate = Number(
  67. currentValue)
  68. currentIndex: indexForValue(
  69. view.lmSelectedTextureCandidate)
  70. enabled: !!selectedEntry
  71. && selectedEntry.kind === "mesh"
  72. }
  73. }
  74. RowLayout {
  75. CheckBox {
  76. id: checkboxBfCull
  77. text: "Backface Culling"
  78. checked: true
  79. }
  80. CheckBox {
  81. id: checkboxDebugUV
  82. text: "Debug UV"
  83. checked: false
  84. }
  85. }
  86. }
  87. }
  88. }
  89. View3D {
  90. id: view
  91. Layout.fillWidth: true
  92. Layout.fillHeight: true
  93. enabled: meshLoader.visible
  94. visible: meshLoader.visible
  95. property var lmModelCandidates: []
  96. property var lmTextureCandidates: []
  97. property int lmSelectedTextureCandidate: -1
  98. property real boundsDiameter: 0
  99. property vector3d boundsCenter
  100. property vector3d boundsSize
  101. function updateBounds(bounds) {
  102. boundsSize = Qt.vector3d(bounds.maximum.x - bounds.minimum.x,
  103. bounds.maximum.y - bounds.minimum.y,
  104. bounds.maximum.z - bounds.minimum.z)
  105. boundsDiameter = Math.max(boundsSize.x, boundsSize.y, boundsSize.z)
  106. boundsCenter = Qt.vector3d(
  107. (bounds.maximum.x + bounds.minimum.x) / 2,
  108. (bounds.maximum.y + bounds.minimum.y) / 2,
  109. (bounds.maximum.z + bounds.minimum.z) / 2)
  110. model.position = Qt.vector3d(-boundsCenter.x, -boundsCenter.y,
  111. -boundsCenter.z)
  112. cameraNode.clipNear = boundsDiameter / 100
  113. cameraNode.clipFar = boundsDiameter * 10
  114. resetCamera()
  115. }
  116. function resetCamera() {
  117. cameraNode.position = boundsCenter
  118. cameraNode.position = Qt.vector3d(0, 0, 2 * boundsDiameter)
  119. cameraNode.eulerRotation = Qt.vector3d(0, 0, 0)
  120. }
  121. function refreshLightmapSelection() {
  122. if (!selectedEntry) {
  123. lmModelCandidates = []
  124. root.selectedModelCandidate = ""
  125. lmTextureCandidates = []
  126. lmSelectedTextureCandidate = -1
  127. return
  128. }
  129. if (selectedEntry.kind === "image") {
  130. lmModelCandidates = [selectedEntry.key]
  131. root.selectedModelCandidate = selectedEntry.key
  132. } else if (selectedEntry.kind === "mesh") {
  133. lmModelCandidates = LightmapFile.keysReferencingMesh(
  134. selectedEntry.key)
  135. root.selectedModelCandidate = lmModelCandidates.length ? lmModelCandidates[0] : ""
  136. } else {
  137. lmModelCandidates = []
  138. root.selectedModelCandidate = ""
  139. lmTextureCandidates = []
  140. lmSelectedTextureCandidate = -1
  141. }
  142. }
  143. Component.onCompleted: refreshLightmapSelection()
  144. Connections {
  145. target: window
  146. function onSelectedEntryChanged() {
  147. view.refreshLightmapSelection()
  148. }
  149. }
  150. environment: SceneEnvironment {
  151. backgroundMode: SceneEnvironment.Color
  152. clearColor: "black"
  153. }
  154. PerspectiveCamera {
  155. id: cameraNode
  156. z: 300
  157. }
  158. Node {
  159. id: modelNode
  160. Model {
  161. id: model
  162. geometry: LightmapMesh {
  163. source: LightmapFile.source
  164. key: selectedEntry.key
  165. onBoundsChanged: view.updateBounds(bounds)
  166. }
  167. materials: CustomMaterial {
  168. shadingMode: CustomMaterial.Unshaded
  169. cullMode: checkboxBfCull.checked ? Material.BackFaceCulling : Material.NoCulling
  170. property TextureInput baseMap: TextureInput {
  171. texture: Texture {
  172. id: lmTexture
  173. minFilter: Texture.Linear
  174. magFilter: Texture.Linear
  175. mipFilter: Texture.None
  176. tilingModeHorizontal: Texture.ClampToEdge
  177. tilingModeVertical: Texture.ClampToEdge
  178. textureData: LightmapFile.textureDataFor(
  179. root.selectedModelCandidate,
  180. view.lmSelectedTextureCandidate)
  181. }
  182. }
  183. property bool debugUV: checkboxDebugUV.checked
  184. vertexShader: "mesh.vert"
  185. fragmentShader: "mesh.frag"
  186. }
  187. }
  188. }
  189. ArcballController {
  190. id: arcballController
  191. controlledObject: modelNode
  192. function jumpToAxis(axis) {
  193. cameraRotation.from = arcballController.controlledObject.rotation
  194. cameraRotation.to = originGizmo.quaternionForAxis(
  195. axis, arcballController.controlledObject.rotation)
  196. cameraRotation.duration = 200
  197. cameraRotation.start()
  198. }
  199. function jumpToRotation(qRotation) {
  200. cameraRotation.from = arcballController.controlledObject.rotation
  201. cameraRotation.to = qRotation
  202. cameraRotation.duration = 100
  203. cameraRotation.start()
  204. }
  205. QuaternionAnimation {
  206. id: cameraRotation
  207. target: arcballController.controlledObject
  208. property: "rotation"
  209. type: QuaternionAnimation.Slerp
  210. running: false
  211. loops: 1
  212. }
  213. }
  214. OriginGizmo {
  215. id: originGizmo
  216. anchors.top: parent.top
  217. anchors.right: parent.right
  218. anchors.margins: 10
  219. width: 120
  220. height: 120
  221. targetNode: modelNode
  222. onAxisClicked: axis => {
  223. arcballController.jumpToAxis(axis)
  224. }
  225. }
  226. DragHandler {
  227. id: dragHandler
  228. target: null
  229. acceptedModifiers: Qt.NoModifier
  230. onCentroidChanged: {
  231. arcballController.mouseMoved(toNDC(centroid.position.x,
  232. centroid.position.y))
  233. }
  234. onActiveChanged: {
  235. if (active) {
  236. view.forceActiveFocus()
  237. arcballController.mousePressed(toNDC(centroid.position.x,
  238. centroid.position.y))
  239. } else
  240. arcballController.mouseReleased(toNDC(centroid.position.x,
  241. centroid.position.y))
  242. }
  243. function toNDC(x, y) {
  244. return Qt.vector2d((2.0 * x / width) - 1.0,
  245. 1.0 - (2.0 * y / height))
  246. }
  247. }
  248. WheelHandler {
  249. id: wheelHandler
  250. orientation: Qt.Vertical
  251. target: null
  252. acceptedDevices: PointerDevice.Mouse | PointerDevice.TouchPad
  253. onWheel: event => {
  254. let delta = -event.angleDelta.y * 0.01
  255. cameraNode.z += cameraNode.z * 0.1 * delta
  256. }
  257. }
  258. Keys.onPressed: event => {
  259. if (event.key === Qt.Key_Space) {
  260. let rotation = originGizmo.quaternionAlign(
  261. arcballController.controlledObject.rotation)
  262. arcballController.jumpToRotation(rotation)
  263. } else if (event.key === Qt.Key_S) {
  264. settingsPane.toggleHide()
  265. } else if (event.key === Qt.Key_Left
  266. || event.key === Qt.Key_A) {
  267. let rotation = originGizmo.quaternionRotateLeft(
  268. arcballController.controlledObject.rotation)
  269. arcballController.jumpToRotation(rotation)
  270. } else if (event.key === Qt.Key_Right
  271. || event.key === Qt.Key_D) {
  272. let rotation = originGizmo.quaternionRotateRight(
  273. arcballController.controlledObject.rotation)
  274. arcballController.jumpToRotation(rotation)
  275. }
  276. }
  277. }
  278. }