微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Qml qt3d

如何解决在 Qml qt3d

我有一个窗口大小宽度:1500 高度:780,我想用 2d 坐标渲染 3d 对象,将 z=1 作为虚拟值。以下是 qml 代码。(如果我必须在 x=0,y=0 处渲染 3d 对象应该渲染与 qml 中的 Rectangle 渲染即窗口坐标完全相同的位置)

Entity {
id: root

property real x: 2.0
property real y: 0.0
property real z: 0.0
property real scale: 1
property var mainCam
property var forwardRenderer
property Material material


components: [ trefoilMeshTransform,mesh,root.material ]

Transform {
    id: trefoilMeshTransform
     property real userAngle: 900.0
    translation:Qt.vector3d(0,1,1) 
    property real theta: 0.0
    property real phi:0.0
    property real roll: 0.0
    rotation: fromEulerAngles(theta,phi,roll)
    scale: root.scale
}


Mesh {
    id: mesh
    source: "assets/obj/cube.obj"
}

}

代码wireframe example完全相同,包括相机设置。我试图在 qml 中使用 qt3d unproject api 但最终没有成功。你能帮我提供线索吗?

解决方法

请阅读有关 How to convert world to screen coordinates and vice versa 的文档,这有助于理解一些逻辑。 还有this

在 qt3d 中,我使用 RayCasterScreenRayCaster 获得 3d 坐标 它为您提供本地位置和世界位置以及屏幕 x,y 参见 kdab 中的 this site 及其示例

RenderSettings{
    
    //all components that you need like viewport,InputSettings,...
    
    ScreenRayCaster {
        id: screenRayCaster
        
        onHitsChanged: printHits("Screen hits",hits)
    }
}

MouseHandler {
    id: mouseHandler
    sourceDevice:  MouseDevice {}
    onReleased: { screenRayCaster.trigger(Qt.point(mouse.x,mouse.y)) }
}

function printHits(desc,hits) {
    console.log(desc,hits.length)
    for (var i=0; i<hits.length; i++) {
        console.log("  " + hits[i].entity.objectName,hits[i].distance,hits[i].worldIntersection.x,hits[i].worldIntersection.y,hits[i].worldIntersection.z)
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。