Advertisement
Guest User

grid lines

a guest
Jun 1st, 2017
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.16 KB | None | 0 0
  1. Shader "Unlit/Grid"
  2. {
  3. Properties{
  4. _GridThickness("Grid Thickness", Float) = 0.01
  5. _GridSpacing("Grid Spacing", Float) = 10.0
  6. _GridColour("Grid Colour", Color) = (0.5, 1.0, 1.0, 1.0)
  7. _BaseColour("Base Colour", Color) = (0.0, 0.0, 0.0, 0.0)
  8. }
  9.  
  10. SubShader{
  11. Tags{ "Queue" = "Transparent" }
  12.  
  13. Pass{
  14. ZWrite Off
  15. Cull Off
  16. Blend SrcAlpha OneMinusSrcAlpha
  17.  
  18. CGPROGRAM
  19.  
  20. // Define the vertex and fragment shader functions
  21. #pragma vertex vert
  22. #pragma fragment frag
  23.  
  24. // Access Shaderlab properties
  25. uniform float _GridThickness;
  26. uniform float _GridSpacing;
  27. uniform float4 _GridColour;
  28. uniform float4 _BaseColour;
  29.  
  30. // Input into the vertex shader
  31. struct vertexInput {
  32. float4 vertex : POSITION;
  33. };
  34.  
  35. // Output from vertex shader into fragment shader
  36. struct vertexOutput {
  37. float4 pos : SV_POSITION;
  38. float4 worldPos : TEXCOORD0;
  39. };
  40.  
  41. // VERTEX SHADER
  42. vertexOutput vert(vertexInput input) {
  43. vertexOutput output;
  44. output.pos = UnityObjectToClipPos(input.vertex);
  45. // Calculate the world position coordinates to pass to the fragment shader
  46. output.worldPos = mul(unity_ObjectToWorld, input.vertex);
  47. return output;
  48. }
  49.  
  50. float calcDist(float pos, float spacing)
  51. {
  52. float left = 1.0 - frac(pos / spacing);
  53. float right = frac(pos / spacing);
  54. return right < left ? right : - left;
  55. }
  56.  
  57. // FRAGMENT SHADER
  58. float4 frag(vertexOutput input) : COLOR{
  59.  
  60. float distX = calcDist(input.worldPos.x, _GridSpacing);
  61. float distZ = calcDist(input.worldPos.z, _GridSpacing);
  62.  
  63. float distX5 = calcDist(input.worldPos.x, _GridSpacing * 5.0);
  64. float distZ5 = calcDist(input.worldPos.z, _GridSpacing * 5.0);
  65.  
  66. float dist = abs(distX) < abs(distZ) ? distX : distZ;
  67. float dist5 = abs(distX5) < abs(distZ5) ? distX5 : distZ5;
  68.  
  69. //float delta = fwidth(abs(dist));//replace for non-sighed version
  70. float delta = fwidth(dist);
  71. float delta5 = fwidth(dist5);
  72.  
  73. float alpha = smoothstep(_GridThickness - delta, _GridThickness, abs(dist));
  74. float alpha5 = smoothstep(_GridThickness * 0.5 - delta5, _GridThickness * 0.5, abs(dist5));
  75.  
  76. return lerp(_GridColour, _BaseColour, min(alpha, alpha5));
  77. }
  78. ENDCG
  79. }
  80. }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement