问题见这个帖子:https://forum.cocos.org/t/topic/106188
不知道是bug还是我使用的问题,花了不少时间找规律,发现是Y轴反转以及和原位置差了一个canvas的scale,自己打了个补丁,分享出来节约大家时间(附件内容和下面代码一样)
CocosHack.ts.zip (674 字节)
declare module "cc" {
interface Camera {
convertToUINodeWithTexture(worldPos: Vec3, uiNode: Node, out?: Vec3): Vec3;
screenPointToRayWithTexture(x: number, y: number, out?: geometry.Ray): geometry.Ray;
}
}
Camera.prototype.convertToUINodeWithTexture = function (worldPos: Vec3, uiNode: Node, out?: Vec3) {
!out && (out = new Vec3());
const self = this as Camera;
self.convertToUINode(worldPos, uiNode, out);
out.y = -out.y;
out.x *= view.getScaleX();
out.y *= view.getScaleY();
return out;
}
Camera.prototype.screenPointToRayWithTexture = function (x: number, y: number, out?: geometry.Ray) {
!out && (out = new geometry.Ray());
const viewPortHeight = view.getViewportRect().height;
const self = this as Camera;
self.screenPointToRay(x / view.getScaleX(), (viewPortHeight - y) / view.getScaleY(), out);
return out;
}
export default {};