Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Terrible question about creating 'terrain' shader for custom mesh

Discussion in 'Shaders' started by AlucardJay, Feb 5, 2015.

  1. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    TL;DR : what do I type into a search string to get resources for finding out : how to make a shader that blends 4 textures based on a control texture.

    Sorry for asking what appears to be a 'write-my-code' question. However, I'm only asking how to find resources where this is explained / has been done before, as any search string I use is yielding no results.

    So, custom procedural mesh. I wish to have a shader that takes 4 textures (actually 8, texture and normal), then use a control texture where the RGBA channels determine the blend strength of the 4 textures.

    The closest thing I found to describe it better is this link : https://alastaira.wordpress.com/2013/12/07/custom-unity-terrain-material-shaders/

    So the shader inspector would look like this :

    Code (csharp):
    1. // Splat Map Control Texture
    2. _Control ("Control (RGBA)", 2D) = "red" {}
    3. // Textures
    4. _Splat3 ("Layer 3 (A)", 2D) = "white" {}
    5. _Splat2 ("Layer 2 (B)", 2D) = "white" {}
    6. _Splat1 ("Layer 1 (G)", 2D) = "white" {}
    7. _Splat0 ("Layer 0 (R)", 2D) = "white" {}
    8. // Normal Maps
    9. _Normal3 ("Normal 3 (A)", 2D) = "bump" {}
    10. _Normal2 ("Normal 2 (B)", 2D) = "bump" {}
    11. _Normal1 ("Normal 1 (G)", 2D) = "bump" {}
    12. _Normal0 ("Normal 0 (R)", 2D) = "bump" {}
    and the blending process I guess would look like this :

    Code (csharp):
    1. fixed3 col;
    2. col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
    3. col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
    4. col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
    5. col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
    6. o.Albedo = col;
    My problem now is how (is the fastest way I can learn to...) work this formula into a shader with all the Properties Tags Subshader and other things that structure a shader. Again, sorry, just asking for links to a crash course, or any implementation of this (as I'm sure its been done).

    Now I'm really going to push it and ask if this kind of shader is usable on a mobile platform.

    Thank you for reading.

    Edit : I realize that link has an example terrain toon shader, but with the dependancies and other pass I'm lost, and need a formulated approach rather than hacking away at that example just to see if I could make it spit out anything.

    Other resources I have bookmarked :
    http://www.gamasutra.com/blogs/Andr...196339/Advanced_Terrain_Texture_Splatting.php
    http://frictionalgames.blogspot.fr/2010/11/tech-feature-terrain-textures.html
    http://www.organicvectory.com/index...d=68:blend-mapping&catid=41:shaders&Itemid=78
    http://forum.unity3d.com/threads/blending-multiple-textures-results-in-white-fringes.200029/
    http://forum.unity3d.com/threads/getting-voxeldata-to-shader.290427/
    http://www.blog.radiator.debacle.us/2013/09/hacking-blend-transition-masks-into.html

    The last link is actually where I'm going to start from in my experiments.
     
    Last edited: Feb 12, 2015
  2. chronos78

    chronos78

    Joined:
    Dec 6, 2009
    Posts:
    154
    I know this doesn't tell you how to do it, but I've already created a suite of shaders that do exactly what you are looking for. It is available for only $10 on the asset store located here. The shaders are not compiled so you are free to look at the code to see how it was done or modify it as you see fit for your project.

    Here's an example of what it looks like in use.
     

    Attached Files:

  3. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    chronos78 : thank you for replying. Your asset and video look good, however personally if I was looking for a paid asset, I would look into T4M, as this functionality is what all my work is heading towards. Good Luck with the asset and your endeavours, All the Best.

    So surprisingly, after a quick hack I made a basic shader that seems to work. It is pretty much exactly the same as the terrain shader. Future plans include adding bump textures, but for now just to show I'm not completely lazy (as I assume most viewers think I am) :

    Code (csharp):
    1. //------------------------------//
    2. //  SplatLayers.shader  //
    3. //  Written by Jay Kay  //
    4. //  2015/2/19  //
    5. //------------------------------//
    6.  
    7. // derived from :
    8. //  http://www.blog.radiator.debacle.us/2013/09/hacking-blend-transition-masks-into.html
    9. //  https://alastaira.wordpress.com/2013/12/07/custom-unity-terrain-material-shaders/
    10. //
    11.  
    12.  
    13. Shader "Custom/SplatLayers" {
    14.    Properties {
    15.      // Splat Map Control Texture
    16.      _Control ("Control (RGBA)", 2D) = "red" {}
    17.      // Textures
    18.      _Splat3 ("Layer 3 (B)", 2D) = "white" {}
    19.      _Splat2 ("Layer 2 (G)", 2D) = "white" {}
    20.      _Splat1 ("Layer 1 (R)", 2D) = "white" {}
    21.      _Splat0 ("Layer 0 (A)", 2D) = "white" {}
    22.    }
    23.    SubShader {
    24.      Tags {
    25.        "SplatCount" = "4"
    26.        "Queue" = "Geometry-100"
    27.        "RenderType"="Opaque"
    28.      }
    29.  
    30.      //LOD 200
    31.  
    32.      CGPROGRAM
    33.      #pragma surface surf Lambert
    34.  
    35.      sampler2D _MainTex;
    36.  
    37.      struct Input {
    38.        float2 uv_Control : TEXCOORD0;
    39.        float2 uv_Splat0 : TEXCOORD1;
    40.        float2 uv_Splat1 : TEXCOORD2;
    41.        float2 uv_Splat2 : TEXCOORD3;
    42.        float2 uv_Splat3 : TEXCOORD4;
    43.      };
    44.  
    45.      sampler2D _Control;
    46.      sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    47.  
    48.      void surf (Input IN, inout SurfaceOutput o) {
    49.         fixed4 splat_control = tex2D (_Control, IN.uv_Control);
    50.         fixed3 col;
    51.         col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
    52.         col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
    53.         col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
    54.         col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
    55.         o.Albedo = col;
    56.      }
    57.      ENDCG
    58.    }
    59.    FallBack "Diffuse"
    60. }

    To anyone else who read the question and walked away ... well, 81 views and not a single suggestion on what to search for just seems ****. Sure, its a lousy question; but I'm a one-man-band here, and with everything going into this project it is hard to set aside time for the purpose of learning shader coding from scratch. Anyway, starting to show my disappointment, so shall cut that line of thought off right there. Me just ranting isn't going to change the outcome, nor earn me any credibility on these forums (since I have none, nothing lost, hey!)
     
  4. kebrus

    kebrus

    Joined:
    Oct 10, 2011
    Posts:
    415
    http://en.wikibooks.org/wiki/Cg_Programming/Unity
    http://wiki.unity3d.com/index.php/Shaders

    What you want to do is relatively simple, its just takes time to do it properly, which people don't have, and the knowledge, which people are usually reasonable and actually share a lot for free. Instead of trying to do everything learn to pick your fights, you are better learning how to create shaders in a general way and have proper packages that took a long time to create, and have them solve some of your specific problems.

    I know you have your problem solved now but it doesn't seem to be very performant. But that's just my opinion. And that's why you gotta be willing to spend some money if you are a one man team.

    also take a look at forst assets and see if any of the free ones are of any help
    https://www.assetstore.unity3d.com/en/#!/publisher/408
     
  5. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    There were probably few replies, since your earlier code idea essentially does what you want.

    I you're going to add normal maps, you'll soon run into the texture sampler limit. (Which is usually 8.)