this.sprite = this.getComponent(cc.Sprite);
var _this = this;
this.node.on(cc.Node.EventType.TOUCH_START,function(event: cc.Event.EventTouch){
var location = event.getLocation();
var p = _this.node.convertToNodeSpaceAR(location);
p.x = (p.x + _this.node.width/2.) * 0.1 / _this.node.width;
p.y = (p.y + _this.node.height/2.) * 0.1 / _this.node.height;
p.y = 0.1 - p.y;
cc.log(p.toString());
_this.sprite.getMaterial("bomb").setParamValue("bombCenter",p);
// _this.sprite.activateMaterial("bomb");
});
这是点击sprite,向shader传输点击的位置
vert: `
uniform mat4 viewProj;
attribute vec3 a_position;
attribute vec2 a_uv0;
varying vec2 uv0;
void main () {
vec4 pos = viewProj * vec4(a_position, 1);
gl_Position = pos;
uv0 = a_uv0;
}`,
frag:
`
uniform sampler2D texture;
uniform vec4 color;
uniform vec2 highp bombCenter;
uniform vec3 iResolution;
varying vec2 uv0;
void main()
{
vec2 highp onePixel = vec2(1.0 / iResolution.x, 1.0 / iResolution.y);
vec4 src_color = color * texture2D(texture, uv0).rgba;
float width = 0.01;
float highp dis = pow(uv0.x - bombCenter.x,2) + pow(uv0.y - bombCenter.y,2);
dis = sqrt(dis);
gl_FragColor = src_color;
if(dis <= width)
{
gl_FragColor = vec4(0,0,0,0);
}
}`,
这个是shader
在学习shader,下载demo下来,自己尝试实现想要的效果。
但是一直有些问题。
1、点击的位置不在绘制出来的圆心处。
2、计算点击坐标的时候,不乘以0.1,就没法显示出来。uv0的x,y最大貌似就是0.1
按道理不是应该点击刚好绘制在圆心吗?
有人可以解答吗?


