Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Help] Simple Terrain Alpha Blending?

Discussion in 'Shaders' started by Leoo, Aug 18, 2015.

  1. Leoo

    Leoo

    Joined:
    May 13, 2013
    Posts:
    96
    Hello guys!

    I found an old post (2013) by Robert Yang, about how to make some sick transitions using the alpha channel on the terrain textures, but that shader is quite old and is not working anymore...

    Do any of you know a today alternative to this code or help me making one? :=)

    Code (csharp):
    1.  
    2.  
    3. Shader "Nature/Terrain/Diffuse" {
    4. Properties {
    5.   [HideInInspector] _Control ("Control (RGBA)", 2D) = "red" {}
    6.     [HideInInspector] _Splat3 ("Layer 3 (A)", 2D) = "white" {}
    7.     [HideInInspector] _Splat2 ("Layer 2 (B)", 2D) = "white" {}
    8.     [HideInInspector] _Splat1 ("Layer 1 (G)", 2D) = "white" {}
    9.     [HideInInspector] _Splat0 ("Layer 0 (R)", 2D) = "white" {}
    10.     // used in fallback on old cards & base map
    11.     [HideInInspector] _MainTex ("BaseMap (RGB)", 2D) = "white" {}
    12.     [HideInInspector] _Color ("Main Color", Color) = (1,1,1,1)
    13. }
    14.  
    15. SubShader {
    16.     Tags {
    17.         "SplatCount" = "4"
    18.         "Queue" = "Geometry-100"
    19.         "RenderType" = "Opaque"
    20.     }
    21. CGPROGRAM
    22. #pragma surface surf Lambert
    23. struct Input {
    24.     float2 uv_Control : TEXCOORD0;
    25.     float2 uv_Splat0 : TEXCOORD1;
    26.     float2 uv_Splat1 : TEXCOORD2;
    27.     float2 uv_Splat2 : TEXCOORD3;
    28.     float2 uv_Splat3 : TEXCOORD4;
    29. };
    30.  
    31. sampler2D _Control;
    32. sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    33.  
    34. void surf (Input IN, inout SurfaceOutput o) {
    35.     fixed4 splat_control = tex2D (_Control, IN.uv_Control);
    36.     fixed3 col;
    37.     col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
    38.     col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
    39.     col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
    40.     col = col * clamp(pow(1 - splat_control.a * 1.1 - splat_control.a * tex2D (_Splat3, IN.uv_Splat3).a, 3.4), 0, 1) + tex2D (_Splat3, IN.uv_Splat3).rgb * clamp(splat_control.a * 3.0 + splat_control.a * tex2D (_Splat3, IN.uv_Splat3).a * 4, 0.0, 1.0);
    41.     o.Albedo = col;
    42.     o.Alpha = 0.0;
    43. }
    44. ENDCG
    45. }
    46.  
    47. Dependency "AddPassShader" = "Hidden/TerrainEngine/Splatmap/Lightmap-AddPass"
    48. Dependency "BaseMapShader" = "Diffuse"
    49. Dependency "Details0"      = "Hidden/TerrainEngine/Details/Vertexlit"
    50. Dependency "Details1"      = "Hidden/TerrainEngine/Details/WavingDoublePass"
    51. Dependency "Details2"      = "Hidden/TerrainEngine/Details/BillboardWavingDoublePass"
    52. Dependency "Tree0"         = "Hidden/TerrainEngine/BillboardTree"
    53.  
    54. // Fallback to Diffuse
    55. Fallback "Diffuse"
    56. }
    57.  
    58.  
    Source: Radiator Blog
     
  2. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    247
    Hello, and welcome to our club. I'm searching this shader too. For Unity 5.2.1. founded many old versions, end even some for Unity 5, but by some reason they don't work in 5.2.1. I've has concluded that people don't share this shader now, 'cause of they just sell it in Asset Store, unfortunatly.

    You can try download Unity's build-in shaders (for your version) find Defaultresources/TerrainShaders/Splats/ , choose AddPass (or FirstPass, may be wrong) it's diffuse, or Standard-Base, what you need. And they use CGIncludes/TerrainSplatmapCommon.cginc file, SplatMapMix() function for mix.

    You should know, that to use custom include in your custom shader you need to place them inside same folder.

    I've no success in recreating this "Masked" blending, give up and I'm thinking about buying this shader from Asset Store.

    Good luck. Write here what you'll get in result, please.
     
  3. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    It seems like the goal is simple heightmap blending. There's quite a few resources available regarding that, and it's not too complicated to implement. And I think it's a bit incorrect to assume that we should all share our implementations freely - if it takes time and work to create, compensation for our time and work should be expected. Especially because it's not simply the time it took to write the shader, but the time it took to -learn- how to write shaders.

    The singular best resource I found to understand the flow of shaders was Shader Forge, and I went from shader forge for prototyping to hand-creating most of my shaders, using the shader forge ones as reference guides.

    And, here is a reference guide to create your own -
    http://www.gamasutra.com/blogs/Andr...196339/Advanced_Terrain_Texture_Splatting.php
     
  4. mitaywalle

    mitaywalle

    Joined:
    Jul 1, 2013
    Posts:
    247
    Hello, Plutoman.

    I'm not trying to reproach anyone. Everyone is entitled to decide make result of his work free or paid. I've only supposed why we can't find properly working shader with this type of blending.

    I've seen this blog, as prototype and explonation of theoretic part - it's great. But, unfortunatly I haven't no success in implementing it.
     
  5. Plutoman

    Plutoman

    Joined:
    May 24, 2013
    Posts:
    257
    That does happen, on occasion. I've dealt with the same struggles involving voxels; a year into my project, I finally understand code well enough to implement dual marching cubes. It took 3-4 months of research to understand shaders well enough to implement tessellation.

    Unfortunately, the best way for me to learn shaders was through paid software.. but I will mention, if you have a solid drive, there's a great site here to learn algorithms - https://www.shadertoy.com/

    It's written in a different style, but the main methods are generally the same as a fragment shader. There's a lot more you can do when you involve meshes and vertexes, but the main thing to do to understand these types of things would be to just work hard and understand shaders in general. I had to spend quite a bit on shader packages to study them - examples taught me best.

    Just my two cents, and hopefully it can help at all. It definitely helps to have a solid background in mathematical algorithms. If you just jump in to try to make something work, it is very difficult as you're missing a lot of the basics in "how it works" (exactly my issues when I tried writing DMC a year ago.. it flopped).