Search Unity

Problems with Grid Shader at high granularity, scale and camera distance.

Discussion in 'Shaders' started by noise256, Aug 25, 2016.

  1. noise256

    noise256

    Joined:
    Apr 7, 2013
    Posts:
    22
    Hello,

    I'm currently trying to adapt the following shader for my game. Unfortunately this does not seem to handle (i.e. it goes completely farbot) large camera distances and/or a high granularity of the grid. The grid can be anything between 128 * 128 to 1024 * 1024 and I will need to colour individual tiles, hence the need for the shader. I'm using the default Unity plane mesh with a large scale, approximately 16000, 1, 16000, for a 162 * 162 grid.

    I'm not sure how to solve this, will I have to simply avoid being that zoomed out or using that large a scale? Anyway, you will see the problem in the following gif:



    (Link if embed isn't working: https://gyazo.com/47af3275bf409d159a0b32609acdd18a)

    Any help/advice would be appreciated. I'd like to know if there is a solution in terms of the shader before trying to scale everything down. Thank you.

    Code from, http://forum.unity3d.com/threads/wireframe-grid-shader.60071/#post-1338197, based off https://en.wikibooks.org/wiki/Cg_Programming/Unity/Shading_in_World_Space.

    Code (CSharp):
    1. //Based on http://forum.unity3d.com/threads/wireframe-grid-shader.60071/#post-1338197
    2. Shader "Custom/FactionMap" {
    3.  
    4.     Properties {
    5.       _GridThickness ("Grid Thickness", Float) = 0.01
    6.       _GridSpacing ("Grid Spacing", Float) = 10.0
    7.       _GridColour ("Grid Colour", Color) = (0.5, 1.0, 1.0, 1.0)
    8.       _BaseColour ("Base Colour", Color) = (0.0, 0.0, 0.0, 0.0)
    9.     }
    10.  
    11.     SubShader {
    12.       Tags { "Queue" = "Transparent" }
    13.  
    14.       Pass {
    15.         ZWrite 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.         // Output from vertex shader into fragment shader
    35.         struct vertexOutput {
    36.           float4 pos : SV_POSITION;
    37.           float4 worldPos : TEXCOORD0;
    38.         };
    39.  
    40.         // VERTEX SHADER
    41.         vertexOutput vert(vertexInput input) {
    42.           vertexOutput output;
    43.           output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    44.           // Calculate the world position coordinates to pass to the fragment shader
    45.           output.worldPos = mul(_Object2World, input.vertex);
    46.           return output;
    47.         }
    48.         // FRAGMENT SHADER
    49.         float4 frag(vertexOutput input) : COLOR {
    50.           if (frac(input.worldPos.x/_GridSpacing) < _GridThickness || frac(input.worldPos.z/_GridSpacing) < _GridThickness) {
    51.             return _GridColour;
    52.           }
    53.           else {
    54.             return _BaseColour;
    55.           }
    56.         }
    57.         ENDCG
    58.     }
    59.   }
    60. }
    61.  
     
    Last edited: Aug 25, 2016
  2. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    how about use LOD? Use a different shader pass with different LOD level.

    The grid should not be seen after zoom out a distance.
     
    noise256 likes this.
  3. noise256

    noise256

    Joined:
    Apr 7, 2013
    Posts:
    22
    Thanks, that actually works reasonably well. I hacked it together very quickly but the aim with the grid is to show the owning faction of each tile and the base colour of each tile seems to display OK when the grid lines are gone:

    https://gyazo.com/60b5588acecd63f80b3320b621990bfe
     
    Last edited: Aug 25, 2016
  4. tsangwailam

    tsangwailam

    Joined:
    Jul 30, 2013
    Posts:
    280
    I think it may present better with different grid size in different zoom level.

    (zoom in)
    level 1 - 1x1 unit
    level 2 - 2x2 unit
    level 3 - 3x3 unit
    ....
    (zoom out)

    So, it can show approximate which region owned when zoom out a lot.