Addyarb

Shader Hex Grid for Unity

May 17th, 2025
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/HexGrid"
  2. {
  3.     Properties
  4.     {
  5.         _LineWidth ("Line Width", Range(0.001, 0.5)) = 0.05
  6.         _OutlineColor ("Outline Color", Color) = (1, 1, 1, 1)
  7.         _BackgroundColor ("Background Color", Color) = (0, 0, 0, 0)
  8.         _GridScale ("Grid Scale", Float) = 0.2
  9.     }
  10.     SubShader
  11.     {
  12.         Tags
  13.         {
  14.             "RenderType"="Transparent" "Queue"="Transparent"
  15.         }
  16.         LOD 100
  17.  
  18.         Blend SrcAlpha OneMinusSrcAlpha
  19.         ZWrite Off
  20.  
  21.         Pass
  22.         {
  23.             CGPROGRAM
  24.             #pragma vertex vert
  25.             #pragma fragment frag
  26.             #include "UnityCG.cginc"
  27.  
  28.             struct appdata
  29.             {
  30.                 float4 vertex : POSITION;
  31.                 float2 uv : TEXCOORD0;
  32.             };
  33.  
  34.             struct v2f
  35.             {
  36.                 float2 uv : TEXCOORD0;
  37.                 float4 vertex : SV_POSITION;
  38.                 float3 worldPos : TEXCOORD1;
  39.             };
  40.  
  41.             float  _LineWidth;
  42.             float4 _OutlineColor;
  43.             float4 _BackgroundColor;
  44.             float  _GridScale;
  45.  
  46.             // Base scale factor to make hexagons = 10 units flat-to-flat
  47.             static const float BASE_SCALE = 10.0;
  48.             // Fixed hex size (always 1.0 now)
  49.             static const float HEX_SIZE = 1.0;
  50.  
  51.             v2f vert(appdata v)
  52.             {
  53.                 v2f o;
  54.                 o.vertex = UnityObjectToClipPos(v.vertex);
  55.                 o.uv = v.uv;
  56.                 o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
  57.                 return o;
  58.             }
  59.  
  60.             // Helper vector for flat-top hex calculations
  61.             float2 getS()
  62.             {
  63.                 return float2(1.7320508, 1); // Always flat-top
  64.             }
  65.  
  66.             // Hexagon distance function
  67.             float hex(float2 p)
  68.             {
  69.                 p = abs(p);
  70.                 float2 s = getS();
  71.                 return max(dot(p, s * 0.5), p.y); // Flat top hexagon
  72.             }
  73.  
  74.             // Get the hexagon cell coordinate and ID
  75.             float4 getHex(float2 p)
  76.             {
  77.                 float2 s = getS();
  78.  
  79.                 // For flat-top hexagons
  80.                 float4 hC = floor(float4(p, p - float2(1, 0.5)) / s.xyxy) + 0.5;
  81.  
  82.                 // Centering coordinates with hexagon centers
  83.                 float4 h = float4(p - hC.xy * s, p - (hC.zw + 0.5) * s);
  84.  
  85.                 // Return nearest hexagon center and its ID
  86.                 return dot(h.xy, h.xy) < dot(h.zw, h.zw) ? float4(h.xy, hC.xy) : float4(h.zw, hC.zw + 0.5);
  87.             }
  88.  
  89.             fixed4 frag(v2f i) : SV_Target
  90.             {
  91.                 // Use world coordinates centered at (0,0,0)
  92.                 float2 baseCoord = i.worldPos.xz;
  93.  
  94.                 // Scale coordinates for grid spacing, accounting for the BASE_SCALE factor
  95.                 float2 p = baseCoord / _GridScale * BASE_SCALE;
  96.  
  97.                 // Get hexagon cell data with fixed size of 1.0
  98.                 float4 h = getHex(p / HEX_SIZE) * HEX_SIZE;
  99.  
  100.                 // Calculate edge distance
  101.                 float eDist = hex(h.xy) / HEX_SIZE;
  102.  
  103.                 // Calculate line boundary with minimal antialiasing for crisp edges
  104.                 float innerEdge = HEX_SIZE * 0.5 - _LineWidth;
  105.  
  106.                 // Use very small antialiasing range controlled by sharpness
  107.                 float outline = 1.0 - smoothstep(innerEdge, innerEdge, eDist);
  108.  
  109.                 // Blend between background color and outline color
  110.                 fixed4 finalColor = lerp(_BackgroundColor, _OutlineColor, outline);
  111.                 finalColor.a = max(outline * _OutlineColor.a, _BackgroundColor.a * (1.0 - outline));
  112.  
  113.                 return finalColor;
  114.             }
  115.             ENDCG
  116.         }
  117.     }
  118. }
Tags: hex grid
Advertisement
Add Comment
Please, Sign In to add comment