Search Unity

problem with custom terrain shader (added holes-functionality, problem with some post effects)

Discussion in 'Shaders' started by Steven-1, Jun 19, 2015.

  1. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    471
    I wanted holes in my terrain, so I modified the terrain shader (firstpass shader) to allow this, I simply made 1 of the splats be used for clipping. Then I added a ShadowCaster subshader so it would render shadows correctly (I do the same clipping there).

    It all seemed to work, until I used screen space ambient occlusion or obscurance, these effects behave as if the terrain is still completely opaque (no holes).

    So it seems to me as if the depthbuffer or normalbuffer is somehow incorrect, which I find strange as everything (apart from these post effects) is all still correctly rendered.

    I'm using deferred in Unity5

    any ideas?

    Code (Shader):
    1. //added this in the surface shader
    2. clip (0.2-splat_control.a);
    3. //added this at the end to get correct shadows:
    4. SubShader
    5.      {
    6.          // Pass to render object as a shadow caster
    7.          Pass
    8.          {
    9.              Name "ShadowCaster"
    10.              Tags { "LightMode" = "ShadowCaster" }
    11.        
    12.              Fog {Mode Off}
    13.              ZWrite On ZTest LEqual //Cull Off
    14.              //Offset 1, 1
    15.              CGPROGRAM
    16.              #pragma vertex vert
    17.              #pragma fragment frag
    18.              #pragma multi_compile_shadowcaster
    19.              #include "UnityCG.cginc"
    20.              struct v2f
    21.              {
    22.                  V2F_SHADOW_CASTER;
    23.                  float2 tc_Control : TEXCOORD4;
    24.              };
    25.            
    26.             sampler2D _Control;
    27.             float4 _Control_ST;
    28.              v2f vert( appdata_base v )
    29.              {
    30.                  v2f o;
    31.                  o.tc_Control = TRANSFORM_TEX(v.texcoord, _Control);
    32.                  TRANSFER_SHADOW_CASTER(o)
    33.                  return o;
    34.              }
    35.              float4 frag( v2f i ) : COLOR
    36.              {
    37.                  half4 splat_control = tex2D(_Control, i.tc_Control);
    38.                  clip (0.2-splat_control.a);
    39.                  SHADOW_CASTER_FRAGMENT(i)
    40.              }
    41.              ENDCG
    42.          }
    43.      }
     
  2. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    Change your terrain shaders RenderType to TransparentCutout
    EDIT: although that is not enough itself, there also should be a _Cutout and a _Color property involved in your clipping.
     
  3. Steven-1

    Steven-1

    Joined:
    Sep 11, 2010
    Posts:
    471
    You can indeed use the _Cutout property (in combination with setting the alpha correctly for that) instead of doing the clipping manually, but it gives the same result and doesn't fix the problem with the post-effects.
    Changing the rendertype doesn't seem to do anything.
    I don't see how using a _Color property would solve anything?
     
  4. aubergine

    aubergine

    Joined:
    Sep 12, 2009
    Posts:
    2,880
    If you check the internal render-depthnormals replacement shader, TransparentCutOut is calculated via clip(col.a * _Color.a - _Cutout);