- Home /
Stippled Shader?
I am trying to generate a stippled texture with a shader. My goal is that every second pixel is transparent. Unfortunatly my result looks like this:
My shader is applied on a fullscreen quad. I was wondering how I could achive stippling and furthermore define the boarder around a stippled pixel. Anyone has a hint?
Shader "IndieEffects/Alpha-Blended Motion Blur Stippling" {
Properties {
_MainTex ("Texture(RGB)", 2D) = "black" {}
_FrameAccumulation ("Accumulation", float) = 0.65
}
SubShader {
ZTest Always Cull Off ZWrite Off
Pass {
Blend SrcAlpha OneMinusSrcAlpha
//ColorMask RGB
CGPROGRAM
#include "UnityCG.cginc"
#pragma vertex vert
#pragma fragment frag
//#pragma fragmentoption ARB_precision_hint_fastest
#pragma target 3.0
struct v2f {
float4 pos : POSITION;
float2 uv : TEXCOORD0;
};
v2f vert (appdata_img v)
{
v2f o;
o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord.xy;
return o;
}
sampler2D _MainTex;
float _FrameAccumulation;
float4 col;
half4 frag (v2f i) : COLOR
{
col = tex2D(_MainTex, i.uv);
if((int(i.uv.x + i.uv.y)/i.uv.x == i.uv.y))
discard;
else
col.a = _FrameAccumulation;
col.r = 1.0;
col.g = 1.0;
col.b = 1.0;
return col;
}
ENDCG
/*
SetTexture [_MainTex] {
ConstantColor(1,1,1,[_FrameAccumulation])
Combine texture, constant
}*/
}
Pass {
Blend One Zero
ColorMask A
SetTexture [_MainTex] {
Combine texture
}
}
}
Fallback off
}
Answer by RC-1290 · Apr 06, 2014 at 04:50 PM
The texture coordinates don't tell you which pixel you're editing. You can find out by using TexelSize.
A float4 with _TexelSize appended to the texture name, will automagically be filled with information about the dimensions of each texel in uv space. So for _MainTex that would be:
sampler2D _MainTex;
float4 _MainTex_TexelSize;
where _MainTex_TexelSize.xy would be texel size (1 divided by texture size), and .zw would be texture size (resolution).
Keep in mind that in some cases, the texel size will have a negative y.
That should probably give you enough information to make your shader work.
Thank you for your help! It works now to capture the whole screen, but I have a feeling that my if-statement is wrong, because there aren't pixel left which are not transparent. I also tried this one: "if((int($$anonymous$$ainTex_TexelSize.x + $$anonymous$$ainTex_TexelSize.y)%2 == 0)) discard;" but no result. It looks like every pixel is discarded.
The _TexelSize values are the same for every fragment. You can find out which pixel you're currently processing by dividing the current uv coordinate, by the texelSize.
For a texture of 512 by 512, the u coordinate of 0.5 (halfway) would give you 256.
Hey, I know this post is quite old, but I just wanted to thank you. I've been banging my head against the wall for the past few hours trying to get a simple sprite outline shader to work, and you've provided me with that last bit of knowledge that I needed.
Thank you so much! $$anonymous$$eep making the Unity community awesome!
Answer by Cherno · Dec 08, 2017 at 09:50 AM
I found this cool stipple shader some time ago:
I converted your comment into an answer since this shader seems to directly answer the question ^^.
Your answer
![](https://koobas.hobune.stream/wayback/20220613142106im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to force the compilation of a shader in Unity? 5 Answers
How to determine shader center? 0 Answers
Shader to a Camera 1 Answer
How to blur the corners of the screen? 3 Answers
Why does this IOS / OGLES shader not match PC version? 0 Answers