- Home /
Question by
JamesGryphon · Jul 15, 2017 at 06:38 PM ·
2dtexturesspriterenderertilingpattern
Tiling/Pattern Filling a Sprite
I've spent hours beating my head against the wall so I thought maybe asking here could help.
I'm wondering if there's a quick way to apply a pattern/texture to a sprite. I wanted to texture a 2D white sprite circle (not a sphere) with a repeating 2x2 pattern, resulting in something like this picture:
Everything I've done so far seems to suggest that this isn't readily possible. I hope that's not the case, because it'll make everything messier. Thanks for any help you can provide.
e.png
(569 B)
Comment
Best Answer
Answer by Cherno · Jul 15, 2017 at 08:29 PM
Here is a "stipple/screen door transparency" shader that does what you need.
Shader "Ocias/Diffuse (Stipple Transparency)" {
Properties {
_MainTex ("Base (RGB)", 2D) = "white" {}
_Transparency ("Transparency", Range(0,1)) = 1.0
}
SubShader {
Tags { "RenderType"="Opaque" }
LOD 150
CGPROGRAM
#pragma surface surf Lambert noforwardadd
sampler2D _MainTex;
struct Input {
float2 uv_MainTex;
float4 screenPos;
};
half _Transparency;
void surf (Input IN, inout SurfaceOutput o) {
fixed4 c = tex2D(_MainTex, IN.uv_MainTex);
o.Albedo = c.rgb;
o.Alpha = c.a;
// Screen-door transparency: Discard pixel if below threshold.
float4x4 thresholdMatrix =
{ 1.0 / 17.0, 9.0 / 17.0, 3.0 / 17.0, 11.0 / 17.0,
13.0 / 17.0, 5.0 / 17.0, 15.0 / 17.0, 7.0 / 17.0,
4.0 / 17.0, 12.0 / 17.0, 2.0 / 17.0, 10.0 / 17.0,
16.0 / 17.0, 8.0 / 17.0, 14.0 / 17.0, 6.0 / 17.0
};
float4x4 _RowAccess = { 1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1 };
float2 pos = IN.screenPos.xy / IN.screenPos.w;
pos *= _ScreenParams.xy; // pixel position
clip(_Transparency - thresholdMatrix[fmod(pos.x, 4)] * _RowAccess[fmod(pos.y, 4)]);
}
ENDCG
}
Fallback "Mobile/VertexLit"
}
Answer by toddisarockstar · Jul 15, 2017 at 11:39 PM
run this:
int i;
int i2;
bool b;
public int size = 64;
public int center;
public Texture2D image;
void Start(){
center=(int)(size*.5f);
image = new Texture2D (size, size);
image.filterMode = FilterMode.Point;
i = image.width;
while(i>0){i--;b=!b;
i2=image.height;
while(i2>0){i2--;b=!b;
if(dist(i,i2)<center){
if(b){image.SetPixel(i,i2,Color.black);}
else{image.SetPixel(i,i2,Color.white);}
}
else{image.SetPixel(i,i2,Color.clear);}}}
image.Apply ();
}
public float dist (int x, int y){
int f1 = (center - x) * (center - x);
int f2 = (center - y) * (center - y);
return Mathf.Sqrt(f1+f2);}
void OnGUI(){
GUI.DrawTexture(new Rect(10,10,400,400),image);
}