如何在ApplicationWindow中运行Qt QML 3D示例

2024-06-26 14:32:04 发布

您现在位置:Python中文网/ 问答频道 /正文

是否可以在Qt for Python应用程序中运行3D QML示例(即"Qt 3D: Shadow Map QML Example")?我已将所有.qml文件和文件夹“着色器”复制到新的Qt for Python项目中,并更改了main.qml(添加了ApplicationWinow并将纵横比更改为1920/1080),还更改了adsfeffect.qml中的路径,但这会给我错误:

QFSFileEngine::open: No file name specified
Could not read shader source file: ""
QFSFileEngine::open: No file name specified
Could not read shader source file: ""
QFSFileEngine::open: No file name specified
Could not read shader source file: ""
QFSFileEngine::open: No file name specified
Could not read shader source file: ""
QFSFileEngine::open: No file name specified
Could not read shader source file: ""
QFSFileEngine::open: No file name specified
Could not read shader source file: ""
QFSFileEngine::open: No file name specified
Could not read shader source file: ""
QFSFileEngine::open: No file name specified
Could not read shader source file: ""

如果这对您来说更容易,请给我一些提示或您自己的示例,如何用QML编写3D内容并用Python运行它,以及通常如何与Python合作开始使用Qt3D。关于它的信息太少了:/我想在我的一个应用程序页面中添加一些3D查看器

main.py:

import sys
import os

from PySide2.QtGui import QGuiApplication
from PySide2.QtQml import QQmlApplicationEngine


if __name__ == "__main__":
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine()
    engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))

    if not engine.rootObjects():
        sys.exit(-1)
    sys.exit(app.exec_())

main.qml:

import QtQuick.Window 2.15
import QtQuick 2.15
import QtQuick.Controls  2.15

import QtQuick 2.1 as QQ2
import Qt3D.Core 2.0
import Qt3D.Render 2.0
import Qt3D.Input 2.0
import Qt3D.Extras 2.0

ApplicationWindow {
    id: applicationWindow
    title: qsTr("Hello World")
    visible: true
    width: 1280
    height: 800

    Entity {
        id: sceneRoot

        Camera {
            id: camera
            projectionType: CameraLens.PerspectiveProjection
            fieldOfView: 45
            //aspectRatio: _window.width / _window.height
            aspectRatio: 1920 / 1080
            nearPlane: 0.1
            farPlane: 1000.0
            position: Qt.vector3d(0.0, 10.0, 20.0)
            viewCenter: Qt.vector3d(0.0, 0.0, 0.0)
            upVector: Qt.vector3d(0.0, 1.0, 0.0)
        }

        FirstPersonCameraController { camera: camera }

        ShadowMapLight {
            id: light
        }

        components: [
            ShadowMapFrameGraph {
                id: framegraph
                viewCamera: camera
                lightCamera: light.lightCamera
            },
            // Event Source will be set by the Qt3DQuickWindow
            InputSettings { }
        ]


        AdsEffect {
            id: shadowMapEffect

            shadowTexture: framegraph.shadowTexture
            light: light
        }


        // Trefoil knot entity
        Trefoil {
            material: AdsMaterial {
                effect: shadowMapEffect
                specularColor: Qt.rgba(0.5, 0.5, 0.5, 1.0)
            }
        }

        // Toyplane entity
        Toyplane {
            material: AdsMaterial {
                effect: shadowMapEffect
                diffuseColor: Qt.rgba(0.9, 0.5, 0.3, 1.0)
                shininess: 75
            }
        }

        // Plane entity
        GroundPlane {
            material: AdsMaterial {
                effect: shadowMapEffect
                diffuseColor: Qt.rgba(0.2, 0.5, 0.3, 1.0)
                specularColor: Qt.rgba(0, 0, 0, 1.0)
            }
        }
    }
}

adsfect.qml:

import Qt3D.Core 2.0
import Qt3D.Render 2.0

Effect {
    id: root

    property Texture2D shadowTexture
    property ShadowMapLight light

    // These parameters act as default values for the effect. They take
    // priority over any parameters specified in the RenderPasses below
    // (none provided in this example). In turn these parameters can be
    // overwritten by specifying them in a Material that references this
    // effect.
    // The priority order is:
    //
    // Material -> Effect -> Technique -> RenderPass -> GLSL default values
    parameters: [
        Parameter { name: "lightViewProjection"; value: root.light.lightViewProjection },
        Parameter { name: "lightPosition";  value: root.light.lightPosition },
        Parameter { name: "lightIntensity"; value: root.light.lightIntensity },
        Parameter { name: "shadowMapTexture"; value: root.shadowTexture }
    ]

    techniques: [
        Technique {
            graphicsApiFilter {
                api: GraphicsApiFilter.OpenGL
                profile: GraphicsApiFilter.CoreProfile
                majorVersion: 3
                minorVersion: 2
            }

            renderPasses: [
                RenderPass {
                    filterKeys: [ FilterKey { name: "pass"; value: "shadowmap" } ]

                    shaderProgram: ShaderProgram {
                        vertexShaderCode:   loadSource("shaders/shadowmap.vert")
                        fragmentShaderCode: loadSource("shaders/shadowmap.frag")
                    }

                    renderStates: [
                        PolygonOffset { scaleFactor: 4; depthSteps: 4 },
                        DepthTest { depthFunction: DepthTest.Less }
                    ]
                },

                RenderPass {
                    filterKeys: [ FilterKey { name : "pass"; value : "forward" } ]

                    shaderProgram: ShaderProgram {
                        vertexShaderCode:   loadSource("shaders/ads.vert")
                        fragmentShaderCode: loadSource("shaders/ads.frag")
                    }

                    // no special render state set => use the default set of states
                }
            ]
        },
        Technique {
            graphicsApiFilter {
                api: GraphicsApiFilter.OpenGLES
                majorVersion: 3
                minorVersion: 0
            }

            renderPasses: [
                RenderPass {
                    filterKeys: [ FilterKey { name: "pass"; value: "shadowmap" } ]

                    shaderProgram: ShaderProgram {
                        vertexShaderCode:   loadSource("shaders/es3/shadowmap.vert")
                        fragmentShaderCode: loadSource("shaders/es3/shadowmap.frag")
                    }

                    renderStates: [
                        PolygonOffset { scaleFactor: 4; depthSteps: 4 },
                        DepthTest { depthFunction: DepthTest.Less }
                    ]
                },

                RenderPass {
                    filterKeys: [ FilterKey { name : "pass"; value : "forward" } ]

                    shaderProgram: ShaderProgram {
                        vertexShaderCode:   loadSource("shaders/es3/ads.vert")
                        fragmentShaderCode: loadSource("shaders/es3/ads.frag")
                    }
                }
            ]
        }
    ]
}

项目中的文件(从原始示例复制):

enter image description here


Tags: nonameimportsourcereadvaluenotopen