shader 也不好搞 估计要用 stencil(模板测试) 多个pass 不知道cocos 支持不
我是在塔防里用到了一个画路径的虚线,给你提供下参考吧
/**
* 使用graphics画虚线
* @param g
* @param from
* @param to
* @param inverted 是否反向
* @param stroke
* @param length 实线长度
* @param interval 虚线长度
*/
drawLineOfDashes(g: cc.Graphics, from: cc.Vec2, to: cc.Vec2, inverted: boolean, stroke: boolean = true, length: number = 10, interval: number = 10): void {
if (g) {
let off = to.sub(from);
let dir = off.normalize();
let dis = off.mag();
let delta = dir.mul(length + interval);
let delta1 = dir.mul(length);
let delta2 = dir.mul(interval);
let n = Math.floor(dis / (length + interval));
for (let i = 0; i < n; ++i) {
if (inverted) {
let start = from.add(delta.mul(i)).add(delta1);
g.moveTo(start.x, start.y);
let end = start.add(delta2);
g.lineTo(end.x, end.y);
}
else {
let start = from.add(delta.mul(i));
g.moveTo(start.x, start.y);
let end = start.add(delta1);
g.lineTo(end.x, end.y);
}
}
let start1 = from.add(delta.mul(n));
g.moveTo(start1.x, start1.y);
if (length < dis - (length + interval) * n) {
let end = start1.add(delta1);
g.lineTo(end.x, end.y);
} else {
g.lineTo(to.x, to.y);
}
if (stroke) g.stroke();
}
}