PySide6 和 Qt3D 的光线投射问题

Ray casting issues with PySide6 and Qt3D

提问人:Victor Alekseev 提问时间:10/26/2023 最后编辑:Victor Alekseev 更新时间:10/26/2023 访问量:27

问:

我正在尝试在 pyside6 中执行连续光线投射。我主要遵循这个问题的修复示例,但将其翻译成 python。

我的环境参数: Windows 10、Python 3.11、PySide6 6.6.0。

对于光线投射,我尝试了 和 . 当连接到信号PySide6.Qt3DRender.Qt3DRender.QAbstractRayCaster.hitsChanged时,我收到PySide本身重复打印到标准输出的消息:PySide6.Qt3DRender.Qt3DRender.QScreenRayCasterPySide6.Qt3DRender.Qt3DRender.QRayCaster

TypeError:无法调用元函数,因为我不知道如何处理Qt3DRender::QAbstractRayCaster::Hits

如果我不连接到信号,则不会打印任何内容。

问题是如何使信号按预期工作。PySide6.Qt3DRender.Qt3DRender.QAbstractRayCaster.hitsChanged

最小可重现示例如下。相机直接对准球体,光线正好从窗口中心投射:

import sys
from PySide6.QtCore import Slot, QPoint
from PySide6.QtGui import QVector3D
from PySide6.Qt3DCore import (Qt3DCore)
from PySide6.Qt3DExtras import (Qt3DExtras)
from PySide6.Qt3DRender import Qt3DRender
from PySide6.QtWidgets import QWidget, QLabel, QVBoxLayout, QApplication, QSizePolicy


class Window(Qt3DExtras.Qt3DWindow):
    def __init__(self):
        super().__init__()

        # Camera
        self.camera().lens().setPerspectiveProjection(60, 16 / 9, 0.1, 1000)
        self.camera().setPosition(QVector3D(0, 0, 1.5))
        self.camera().setViewCenter(QVector3D(0, 0, 0))

        # root
        self.rootEntity = Qt3DCore.QEntity()
        self.setRootEntity(self.rootEntity)

        # Sphere
        self.material = Qt3DExtras.QPhongMaterial(self.rootEntity)

        self.sphereEntity = Qt3DCore.QEntity(self.rootEntity)
        self.sphereMesh = Qt3DExtras.QSphereMesh()
        self.sphereMesh.setRadius(1)

        self.sphereTransform = Qt3DCore.QTransform()

        self.sphereEntity.addComponent(self.sphereMesh)
        self.sphereEntity.addComponent(self.sphereTransform)
        self.sphereEntity.addComponent(self.material)

        # camera controls
        self.camController = Qt3DExtras.QFirstPersonCameraController(self.rootEntity)
        self.camController.setLinearSpeed(5)
        self.camController.setLookSpeed(120)
        self.camController.setCamera(self.camera())


        # ray casting
        self.rays_caster = Qt3DRender.QScreenRayCaster()
        Qt3DRender.QRayCaster
        self.rays_caster.setPosition(QPoint(150, 150))
        self.rays_caster.setEnabled(True)
        self.rays_caster.setRunMode(Qt3DRender.QAbstractRayCaster.RunMode.Continuous)

        self.rootEntity.addComponent(self.rays_caster)
        self.rays_caster.hitsChanged.connect(self.hits_changed)
        
        # configure picking
        self.pickable_layer = Qt3DRender.QLayer(self.rootEntity)
        self.pickable_layer.setRecursive(True)
        self.rootEntity.addComponent(self.pickable_layer)
        self.rays_caster.addLayer(self.pickable_layer)
    
    def hits_changed(self, hits):
        print(hits)


class MainWidget(QWidget):
    def __init__(self) -> None:
        super().__init__()
        vbox = QVBoxLayout()
        label = QLabel('Это окно для рендера')
        vbox.addWidget(label)
        self.view = Window()
        widg = QWidget()
        widg.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.MinimumExpanding)
        
        self.view_wind = QWidget.createWindowContainer(self.view, parent=widg)
        self.view_wind.setMinimumSize(300, 300)
        vbox2 = QVBoxLayout()
        vbox2.addWidget(self.view_wind)
        widg.setLayout(vbox2)
        vbox.addWidget(widg)
        self.setLayout(vbox)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    widg = MainWidget()
    widg.show()
    sys.exit(app.exec())
运行时错误 raycasting pyside6 qt3d

评论


答: 暂无答案