在Qmlqt3d中将二维坐标转换为三维坐标



我有一个窗口大小宽度:1500高度:780,我想用二维坐标渲染三维对象,给定z=1作为伪值。以下是qml代码。(如果我必须在x=0,y=0的情况下渲染,则3d对象应该在qml中矩形渲染的位置(即窗口坐标(完全渲染

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"
}
}

该代码与线框示例完全相同,包括相机设置。我试图在qml中使用qt3dunprojectapi,但最终没有成功。你能帮我找到线索吗?

请阅读本文档,了解如何将世界坐标转换为屏幕坐标,反之亦然。这有助于理解一些逻辑。还有这个。

在qt3d中,我使用RayCaster和ScreenRayCaster获得三维坐标它提供了本地位置和世界位置以及屏幕x,y请参阅kdab及其示例中的此站点

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)
}
}

最新更新