关于屏幕坐标转换成三维空间某一平面的点的问题

本人刚接触3D的内容,现在有一个问题解决不了,现在想把屏幕坐标转换为XZ轴平面上的点,实现3D物体在XZ轴平面上的拖动。翻阅了一些帖子说是计算屏幕射线和平面的相交点,但是没有写出具体计算过程的代码,有没有大神可以指导一下,万分感谢。

https://github.com/cocos-creator/test-cases-3d/blob/master/assets/cases/raycast/RaycastCanvasTest.ts

Vector3 CO_Direction = dragObject.transform.position - mainCamera.transform.position;

float cPlane = Vector3.Dot(CO_Direction, mainCamera.transform.forward);

dragObject.transform.position = mainCamera.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, cPlane));
在Unity里有类似的实现,Cocos Creator 3D里如何实现呢?

这里的方法只是检测是否相交,不是计算具体的点

差不多的,在2.3.3是camera.getScreenToWorldPoint,在creator 3d不知道API有没有变

我又想了下,其实实现我的需求只需要把鼠标X,Y的偏移量转换成方块在3D空间中的X,Z轴偏移量,但是这个转换又难到我了。

差不多这样

    const outRay = new geometry.ray();

    this.cameraCom.screenPointToRay(touch.getLocationX(), touch.getLocationY(), outRay);

    const y0Plane = new geometry.plane(0, 1, 0, 0); // y=0 plane

    const t = geometry.intersect.ray_plane(outRay, y0Plane);

    const planePoint = new Vec3();

    if(t){
        Vec3.scaleAndAdd(planePoint, outRay.o, outRay.d, t);
    }
3赞
1赞

谢谢,就是你这个方案实现的

谢谢,学习了