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

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

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

问题

以下是您要翻译的内容:

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

        id: scene3d
        Entity {
            Entity {
                id: floor

                components: [
                    PlaneMesh {
                        width: 10
                        height: 10
                        mirrored: true
                    },
                    TexturedMetalRoughMaterial {
                        id: material
                        baseColor:  TextureLoader {
                            id: textureLoader
                            source: "image://colors/yellow.png"
                            format: Texture.SRGB8_Alpha8
                            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?

        id: scene3d
        Entity {
            Entity {
                id: floor

                components: [
                    PlaneMesh {
                        width: 10
                        height: 10
                        mirrored: true
                    },
                    TexturedMetalRoughMaterial {
                        id: material
                        baseColor:  TextureLoader {
                            id: textureLoader
                            source: "image://colors/yellow.png"
                            format: Texture.SRGB8_Alpha8
                            generateMipMaps: true

enter image description here

Please suggest a solution

答案1

得分: 1

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

            Model {
                source: "#Cube"
                materials: [
                    DefaultMaterial {
                        diffuseMap: Texture {
                            sourceItem: Rectangle {
                                width: 256
                                height: 256
                                color: "#78a"
                                Image {
                                    anchors.centerIn: parent
                                    width: 224
                                    height: 224
                                    source: "https://stephenquan.github.io/images/qt/madewithqt.png"
                                }
                            }
                        }
                    }
                ]
            }

您可以通过访问 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:

            Model {
                source: "#Cube"
                materials: [
                    DefaultMaterial {
                        diffuseMap: Texture {
                            sourceItem: Rectangle {
                                width: 256
                                height: 256
                                color: "#78a"
                                Image {
                                    anchors.centerIn: parent
                                    width: 224
                                    height: 224
                                    source: "https://stephenquan.github.io/images/qt/madewithqt.png"
                                }
                            }
                        }
                    }
                ]
            }

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:

确定