Qt5.12 C++ 如何在 QML 3D 场景中动态加载 QPixmap 作为纹理

huangapple go评论172阅读模式
英文:

Qt5.12 C++ How to dynamically load QPIxmap as texture in QML 3DScene

问题

以下是您要翻译的内容:

有一个应用程序在2D(QGraphicScene)和3D(QML 3D场景)中显示地图。我们需要在3D场景上使用2D场景的QML代码动态显示地图。加载使用QQuickImageProvider进行,该方法在QML中的图像上成功运行,但TextureLoader无法识别。是否有更简单的方法,如果没有,如何解决这个问题?

  1. id: scene3d
  2. Entity {
  3. Entity {
  4. id: floor
  5. components: [
  6. PlaneMesh {
  7. width: 10
  8. height: 10
  9. mirrored: true
  10. },
  11. TexturedMetalRoughMaterial {
  12. id: material
  13. baseColor: TextureLoader {
  14. id: textureLoader
  15. source: "image://colors/yellow.png"
  16. format: Texture.SRGB8_Alpha8
  17. generateMipMaps: true

在此处输入图像描述

请提供一个解决方案。

英文:

There is an application that displays a map in 2D (QGraphicScene) and 3D (QML 3D Scene) We need a map on 3DScene to dynamically display a map with a 2D Scene QML code, Loading occurs using QQuickImageProvider, which successfully works with Image in QML, but TextureLoader does not perceives.() Is there any simpler way, if not, how to solve this problem?

  1. id: scene3d
  2. Entity {
  3. Entity {
  4. id: floor
  5. components: [
  6. PlaneMesh {
  7. width: 10
  8. height: 10
  9. mirrored: true
  10. },
  11. TexturedMetalRoughMaterial {
  12. id: material
  13. baseColor: TextureLoader {
  14. id: textureLoader
  15. source: "image://colors/yellow.png"
  16. format: Texture.SRGB8_Alpha8
  17. generateMipMaps: true

enter image description here

Please suggest a solution

答案1

得分: 1

如果您使用QtQuick3D,可以通过将其设置为Texture.sourceItem来使用QtQuick 2D组件,例如:

  1. Model {
  2. source: "#Cube"
  3. materials: [
  4. DefaultMaterial {
  5. diffuseMap: Texture {
  6. sourceItem: Rectangle {
  7. width: 256
  8. height: 256
  9. color: "#78a"
  10. Image {
  11. anchors.centerIn: parent
  12. width: 224
  13. height: 224
  14. source: "https://stephenquan.github.io/images/qt/madewithqt.png"
  15. }
  16. }
  17. }
  18. }
  19. ]
  20. }

您可以通过访问 https://stephenquan.github.io/qmlonline/ 查看此示例。

英文:

If you use QtQuick3D you can use QtQuick 2D components such as Image by setting it to Texture.sourceItem, for instance:

  1. Model {
  2. source: "#Cube"
  3. materials: [
  4. DefaultMaterial {
  5. diffuseMap: Texture {
  6. sourceItem: Rectangle {
  7. width: 256
  8. height: 256
  9. color: "#78a"
  10. Image {
  11. anchors.centerIn: parent
  12. width: 224
  13. height: 224
  14. source: "https://stephenquan.github.io/images/qt/madewithqt.png"
  15. }
  16. }
  17. }
  18. }
  19. ]
  20. }

You can see a sample of this by visiting https://stephenquan.github.io/qmlonline/

huangapple
  • 本文由 发表于 2023年5月30日 02:47:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76359709.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定