- Home /
Cut Texture2D in circle format
I'm trying to make a square facebook profile fit in a rounded border by clipping its edges.
I have the square texture, but I can't find any function to make it rounded, in a circle format
Any help??
Thanks
Is the image always the same size and is the circle always cut in the same place? If so, you could author a mask image in Photoshop. There are shaders that allow you to specify a mask. In addition, you could do it more directly by using GetPixels32 and SetPixels32() and copy the alpha channel of the mask to the image.
make circle texture from your and in unity apply it as Transparent
Answer by jumahsafarty · Jun 02, 2014 at 05:45 AM
Texture2D CalculateTexture(
int h, int w,float r,float cx,float cy,Texture2D sourceTex
){
Color [] c= sourceTex.GetPixels(0, 0, sourceTex.width, sourceTex.height);
Texture2D b=new Texture2D(h,w);
for (int i = 0 ; i<(h*w);i++){
int y=Mathf.FloorToInt(((float)i)/((float)w));
int x=Mathf.FloorToInt(((float)i-((float)(y*w))));
if (r*r>=(x-cx)*(x-cx)+(y-cy)*(y-cy)){
b.SetPixel(x, y, c[i]);
}else{
b.SetPixel(x, y, Color.clear);
}
}
b.Apply ();
return b;
}
this function return a texture2d which represent a circular cut of the source texture with radius (r) and center coordinate cx,cy and the rest of texture size (h*w) will be colored as transparent (alpha =0)
this function take time to load you mustn't but it on (OnGui or Update .... etc) you can but it in Start() or make a condition for it to load
Answer by Chikanut · Dec 29, 2015 at 01:11 PM
Texture2D CalculateTexture(int h, int w,float r,float cx,float cy,Texture2D sourceTex){
Color [] c= sourceTex.GetPixels(0, 0, sourceTex.width, sourceTex.height);
Texture2D b=new Texture2D(h,w);
for(int i = (int)(cx-r) ; i < cx + r ; i ++)
{
for(int j = (int)(cy-r) ; j < cy+r ; j ++)
{
float dx = i - cx;
float dy = j - cy;
float d = Mathf.Sqrt(dx*dx + dy*dy);
if(d <= r)
b.SetPixel(i-(int)(cx-r),j-(int)(cy-r),sourceTex.GetPixel(i,j));
else
b.SetPixel(i-(int)(cx-r),j-(int)(cy-r),Color.clear);
}
}
b.Apply ();
return b;
}