Hi All, I am writing a fragment shader which draws a grid in Unity UI's Image object. But i am getting aliased (jagged) lines and random pixels on line are not being drawn. See angled_view.png and front_view.png. I tried to find a solution to fix aliased lines but none of them are working specifically. I have project with: 1. A Canvas in render mode=WorldSpace 2. A gameObject with Image component. 3. A material with below shader assigned to Image component. Here is my shader: Code (CSharp): Shader "Custom/Grid_shader" { Properties { _MainTex("Texture", 2D) = "" {} _gridColor ("Grid Colour", Color) = (0.5, 1.0, 1.0, 1.0) _bgColor ("BG Color", Color) = (1,1,1,1) _gridThicknessHor ("Thickness Horizontal", Float) = 0.01 _gridThicknessVer ("Thickness Vertical", Float) = 0.01 _rowCount ("Rows", Range(1,50)) = 1 _columnCount ("Columns", Range(1,50)) = 1 _centerlLineColor("Center Line Color", Color) = (1,1,1,1) _centerLineThickness("Center Line Thickness", Float) = 0.01 _StencilComp ("Stencil Comparison", Float) = 8 _Stencil ("Stencil ID", Float) = 0 _StencilOp ("Stencil Operation", Float) = 0 _StencilWriteMask ("Stencil Write Mask", Float) = 255 _StencilReadMask ("Stencil Read Mask", Float) = 255 _bgColorMask ("Color Mask", Float) = 15 [Toggle(UNITY_UI_ALPHACLIP)] _UseUIAlphaClip ("Use Alpha Clip", Float) = 0 } SubShader { Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" "PreviewType"="Plane" "CanUseSpriteAtlas"="True" } Stencil { Ref [_Stencil] Comp [_StencilComp] Pass [_StencilOp] ReadMask [_StencilReadMask] WriteMask [_StencilWriteMask] } Cull Off Lighting Off ZWrite Off ZTest [unity_GUIZTestMode] Blend SrcAlpha OneMinusSrcAlpha ColorMask [_bgColorMask] Pass { CGPROGRAM #pragma exclude_renderers d3d9 gles #pragma vertex vert #pragma fragment frag #pragma target 2.0 #pragma enable_d3d11_debug_symbols #include "UnityCG.cginc" #include "UnityUI.cginc" #pragma multi_compile __ UNITY_UI_ALPHACLIP struct appdata_t { float4 vertex : POSITION; float4 color : COLOR; float4 texcoord : TEXCOORD0; UNITY_VERTEX_INPUT_INSTANCE_ID }; struct v2f { float4 vertex : SV_POSITION; fixed4 color : COLOR; float4 texcoord : TEXCOORD0; float4 worldPosition : TEXCOORD1; float4 worldPosition1 : TEXCOORD2; UNITY_VERTEX_OUTPUT_STEREO }; sampler2D _MainTex; fixed4 _bgColor; fixed4 _TextureSampleAdd; float4 _ClipRect; uniform float _gridThicknessHor; uniform float _gridThicknessVer; uniform float _rowCount; uniform float _columnCount; uniform float4 _gridColor; uniform float4 _centerlLineColor; uniform float _centerLineThickness; v2f vert(appdata_t IN) { v2f OUT; OUT.worldPosition = IN.vertex; OUT.worldPosition1 = mul(unity_ObjectToWorld, IN.vertex); OUT.vertex = UnityObjectToClipPos(IN.vertex); //OUT.texcoord = IN.texcoord; OUT.texcoord.xy = IN.texcoord.xy; OUT.texcoord.zw = IN.vertex.zw; OUT.texcoord.z = length(ObjSpaceViewDir(IN.vertex)); OUT.color = IN.color * _bgColor; return OUT; } fixed4 frag(v2f IN) : SV_Target { fixed4 col = tex2D(_MainTex, IN.texcoord); float2 l_pos = IN.texcoord; float l_leftBoundFactor = (1.0f - _gridThicknessHor) / _columnCount; float l_lowerBoundFactor = (1.0f - _gridThicknessVer) / _rowCount; float4 l_finalColor = IN.color; //Assign BG color as default received from 'vert' function. if((l_pos.y > 0.5 - (_centerLineThickness / 2) && l_pos.y < 0.5 + (_centerLineThickness / 2)) && (l_pos.x > _gridThicknessHor && l_pos.x < (1.0 - _gridThicknessHor)) ) { l_finalColor = _centerlLineColor; //Drawing center line with different color. } else if( (l_pos.x % l_leftBoundFactor > 0 && l_pos.x % l_leftBoundFactor <= _gridThicknessHor) || (l_pos.y % l_lowerBoundFactor > 0 && l_pos.y % l_lowerBoundFactor <= _gridThicknessVer) ) { l_finalColor = _gridColor; //Drawing grid line. } l_finalColor = l_finalColor * col; l_finalColor.a *= UnityGet2DClipping(IN.worldPosition1.xy, _ClipRect); #ifdef UNITY_UI_ALPHACLIP clip (l_finalColor.a - 0.001); #endif return l_finalColor; } ENDCG } } }
Drawing a grid using if statements will invariably lead to aliasing. The trick is to solve the problem in a different way, by solving the problem algebraically and making use of derivatives to get anti-aliased lines: http://iquilezles.org/www/articles/filterableprocedurals/filterableprocedurals.htm That's all in glsl, but it can be trivially changed to the hlsl Unity's shaders prefer.