Search Unity

Terrain holes data for custom shader

Discussion in 'World Building' started by corjn, Feb 27, 2020.

  1. corjn

    corjn

    Joined:
    Feb 6, 2014
    Posts:
    168
    Hey,

    Is there any public texture paint data we can acess for terrain holes in unity 2019.3 ? Like you can access the splatmap data with "_Control0". I'm making a custom terrain shader and would like to apply the holes data as an opacity mask.

    Thanks for any help :)
     
  2. wyattt_

    wyattt_

    Unity Technologies

    Joined:
    May 9, 2018
    Posts:
    424
    "_TerrainHolesTexture" I believe
     
    corjn likes this.
  3. corjn

    corjn

    Joined:
    Feb 6, 2014
    Posts:
    168
    Yes, it worked, thanks a lot : )
     
  4. MrD69

    MrD69

    Joined:
    Dec 4, 2013
    Posts:
    11
    What worked? If you could provide a code example for the rest of us facing this issue it would certainly help
     
  5. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,665
  6. MrD69

    MrD69

    Joined:
    Dec 4, 2013
    Posts:
    11
    I Googled the exact error I was getting with the keyword "_TerrainHolesTexture" and there was one. 1 result! on all of Google.
     
  7. MrD69

    MrD69

    Joined:
    Dec 4, 2013
    Posts:
    11
    I was able to figure it out but not because I have a clue what is happening with my code I just brute force tried everything until it worked. And so I'm going to list it below with as much detail as I can so that in the future people can just copy the code. I have no idea what it does, because the documentation around Unity shaders is complete garbage with limited to no explanations.

    In the shader file there's a function that looks like this

    void surf(Input IN, inout SurfaceOutput o)
    {
    ....

    At the top of that function insert this:

    #ifdef _ALPHATEST_ON
    ClipHoles(IN.tc.xy);
    #endif

    Just above and outside the function you must insert this line of code (God knows what it does but it won't work without)
    #pragma multi_compile_local __ _ALPHATEST_ON

    In the Properties section at the top of the shader you must add this line below _Color

    [HideInInspector] _TerrainHolesTexture ("Holes Map (RGB)", 2D) = "white" {}

    All of this together will produce a terrain shader that has the desired effect of having holes.
     
    wyattt_ likes this.
  8. MrD69

    MrD69

    Joined:
    Dec 4, 2013
    Posts:
    11
    Ohh and in the Tags, Category section make sure the Render Type is set to TransparentCutout


    Category {
    Tags {
    "Queue" = "Transparent+1"
    "RenderType" = "TransparentCutout"
    }
     
  9. wyattt_

    wyattt_

    Unity Technologies

    Joined:
    May 9, 2018
    Posts:
    424
    The _ALPHATEST_ON is just a multicompile flag (it's really a shader keyword in Unity) in the shader that compiles code conditionally based on whether or not the flag is set (it usually gets set from C#). Depending on the render pipeline you're using, _ALPHATEST_ON is probably the flag for all alpha testing during a given pass and i'm guessing we are just reusing it for the Terrain Holes Texture since it doesn't add another "keyword" (which would have just increased the number of compiled shaders you have)
     
  10. ILoveMyDada

    ILoveMyDada

    Joined:
    Apr 30, 2014
    Posts:
    5
    Any way to give some guidance on how to apply this to vert frag shaders? Currently struggling adding it in. I've got the property set up along with declaring it. I've got the uv's also set up with the TEXCOORDs and everything, now I just don't understand how to implement it at the end:

    float4 sample = tex2D (_Control, i.uv);

    float4 splat0 = tex2D (_Splat0, i.uv2);
    float4 splat1 = tex2D (_Splat1, i.uv3);
    float4 splat2 = tex2D (_Splat2, i.uv4);
    float4 splat3 = tex2D (_Splat3, i.uv5);

    float hole = tex2D (_TerrainHolesTexture, i.uv7);
    clip(hole == 0.0 ? -1:1);

    float4 splats = splat0 * sample.r + splat1 * sample.g + splat2 * sample.b + splat3 * sample.a;

    //--------------------------------------------------------------------------------------------

    return (light + _AmbientColor + specular + rim) * _Color * splats;

    I've tried replacing each splat in "splats" with hole and unfortunately all I get is a white texture, its not actually clipping a hole. Any help would be greatly appreciated!
     
  11. ILoveMyDada

    ILoveMyDada

    Joined:
    Apr 30, 2014
    Posts:
    5
    Wow never mind I feel very stupid. For some reason when I tried to paint holes on my Terrain using my custom shader, it wouldn't work. So I thought that I would need to write a shader or c# code in order to make this work. Turns out that I don't and the reason it wasn't working was because of the brush I was using. The brush didn't have a bright white color to it and that's all it needed. lol smh
     
  12. wechat_os_Qy0989W4msb7Ot5jx6_iGbPvk

    wechat_os_Qy0989W4msb7Ot5jx6_iGbPvk

    Joined:
    Nov 3, 2020
    Posts:
    10
    Excuse me,how to solve the problem of shadow generated by transparent objects
     
  13. wechat_os_Qy0989W4msb7Ot5jx6_iGbPvk

    wechat_os_Qy0989W4msb7Ot5jx6_iGbPvk

    Joined:
    Nov 3, 2020
    Posts:
    10