问题描述
我有一个窗口大小宽度: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 中,我使用 RayCaster 和 ScreenRayCaster 获得 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)
}
}