I’m trying to implement a solution for detecting when a touch occurs on a transparent part of an image. So that the goal is touches on transparent parts are not valid.
It’s a tricky thing to solve but I am trying to approach it from the angle of perhaps using a shader to detect the alpha value of the image? But I’m really new to shaders and I don’t understand how to get the alpha value. Also as I was thinking about this and trying to solve it I realize that it would have to work for rotated images as well.
Here’s an example of getting the r,g,b,a values of an image. The red square has 0 alpha and the green square is 255.
viewer.mode=STANDARD
function setup()
rectMode(CENTER)
img=image(200,200)
setContext(img)
background(255,0,0,0)
fill(0,255,0)
rect(100,100,80,80)
setContext()
str=""
end
function draw()
background(0)
sprite(img,100,100)
if r~=nil then
text("r="..r.." g="..g.." b="..b.." a="..a,WIDTH/2,HEIGHT/2)
end
text(str,WIDTH/2,HEIGHT/2+200)
end
function touched(t)
if t.state==CHANGED then
str=""
if t.x>200 or t.y>200 then
str="out of bounds"
else
r,g,b,a=img:get(t.x//1,t.y//1)
end
end
end
Thanks @skar, I will take credit for trying to preserve some of the great work that’s been done here over the years, for precisely opportunities like this.
Of course the code itself was made by the estimable @Ignatz, who contributed so much during his, er… reign? Term? Time here, at least.
Awesome, that’s exactly what I was thinking it could be done via shader, I’ll have to really study this example to see how it’s done thanks @UberGoober !!
There should really be a repo for Codea shared projects. The examples are great but so far I’ve seen tons of stuff on here from being active and asking that isn’t easily accessible.