大佬们,我想请问一下,就是我现在模仿打螺丝做了一个水果版的。然后这些水果都不是正方形或者长方形,模型节点下放了一个遮罩层。但是UITransform 属性只能放置正方形或者长方形,就导致有些地方其实不需要遮罩。导致可能其他地方的钉子可能看起点得到,但是实际被遮挡住了,这种能换个属性来替代呢,或者怎么优化一下呢
用多边形碰撞?然后检测触摸点是否在碰撞区域内来判断是否点击
应该不是碰撞的原因,是遮罩层的原因。导致白菜上的橙色蝴蝶看得到点不到
这个函数可以拯救你. 大概实现,如果有问题,需要你自己除bug.
/**
* 某个节点的触摸位置是否透明
* @param node 目标节点
* @param touchPos 触摸位置 一般是触摸事件的getLocation()返回的值
* @returns
*/
private static isTouchPosTransparent(node: Node, touchPos: Vec3): boolean {
const uiTransform = node.getComponent(UITransform);
if (!uiTransform) return false;
const sprite = node.getComponent(Sprite);
if (!sprite || !sprite.spriteFrame) return false;
const texture = sprite.spriteFrame.texture;
//触摸位置世界坐标系变换到节点坐标系
const worldMat = node.getWorldMatrix();
const invWorldMat = new Mat4();
Mat4.invert(invWorldMat, worldMat);
const localPos = new Vec3();
Vec3.transformMat4(localPos, touchPos, invWorldMat);
localPos.add3f(
uiTransform.width / 2,
uiTransform.height / 2,
0
);
//转为整数
localPos.set(Math.floor(localPos.x), Math.floor(localPos.y), 0);
const uint8Arr = RenderTexture.prototype.readPixels.call(texture, localPos.x, localPos.y, 1, 1);
const r = uint8Arr[0];
const g = uint8Arr[1];
const b = uint8Arr[2];
const a = uint8Arr[3];
return a< 5.0;
}


