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

Blending materials with mask

Discussion in 'Shaders' started by mr_Necturus, Feb 22, 2015.

  1. mr_Necturus

    mr_Necturus

    Joined:
    May 17, 2010
    Posts:
    2,956
    Hey guys.

    I have a scene which I want to bring in Unity from 3D MAX but I have a problem here.
    I have custom polygonal terrain, and I want to use it in Unity as well.

    If you look in attached picture you will see that different ground materials are blending via the gray-scale mask map I painted.

    Is here in Unity something like that, which I can use with imported polygonal surfaces?

    Or how it difficult to make shader like this?

    T Work_progress_123.jpg hanks!




    Work_progress_117.jpg
     
  2. mr_Necturus

    mr_Necturus

    Joined:
    May 17, 2010
    Posts:
    2,956
    OK.. But at least blend textures with masks help is is possible to make?
     
  3. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    You may pack masks in texture like:
    http://gyazo.com/3b5d94dddd788dd3964cb6396a614e4f
    http://gyazo.com/56396127bca517d1e9195e071023a9dd
    And use a shader that blends textures by mask values.
    It's slightly modified first pass of standard terrain shader.
    Code (csharp):
    1.  
    2. Shader "Custom/DiffuseSplatMap"
    3. {
    4.     Properties
    5.     {
    6.         _Control ("Control (RGBA)", 2D) = "red" {}
    7.         _Splat3 ("Layer 3 (A)", 2D) = "white" {}
    8.         _Splat2 ("Layer 2 (B)", 2D) = "white" {}
    9.         _Splat1 ("Layer 1 (G)", 2D) = "white" {}
    10.         _Splat0 ("Layer 0 (R)", 2D) = "white" {}
    11.             // used in fallback on old cards & base map
    12.         _MainTex ("BaseMap (RGB)", 2D) = "white" {}
    13.         _Color ("Main Color", Color) = (1,1,1,1)
    14.     }
    15.        
    16.     SubShader
    17.     {
    18.             Tags
    19.             {
    20.                 "Queue" = "Geometry-100"
    21.                 "RenderType" = "Opaque"
    22.             }
    23.         CGPROGRAM
    24.         #pragma surface surf Lambert
    25.         struct Input
    26.         {
    27.             float2 uv_Control : TEXCOORD0;
    28.             float2 uv_Splat0 : TEXCOORD1;
    29.         };
    30.  
    31.         sampler2D _Control;
    32.         sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    33.  
    34.         void surf (Input IN, inout SurfaceOutput o)
    35.         {
    36.             fixed4 splat_control = tex2D (_Control, IN.uv_Control);
    37.             fixed3 col;
    38.             col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
    39.             col += splat_control.g * tex2D (_Splat1, IN.uv_Splat0).rgb;
    40.             col += splat_control.b * tex2D (_Splat2, IN.uv_Splat0).rgb;
    41.             col += splat_control.a * tex2D (_Splat3, IN.uv_Splat0).rgb;
    42.             o.Albedo = col;
    43.             o.Alpha = 0.0;
    44.         }
    45.         ENDCG  
    46.     }
    47.  
    48.     // Fallback to Diffuse
    49.     Fallback "Diffuse"
    50. }
    51.  
    52.  
    Result:
    http://gyazo.com/a5e4f5bfd466961014559452d3d5922c
    Try google for:"splat mapping".
     
  4. mr_Necturus

    mr_Necturus

    Joined:
    May 17, 2010
    Posts:
    2,956
    Last edited: Feb 24, 2015
  5. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    349
    Yes of course, but I don't think that is production ready.
     
  6. mr_Necturus

    mr_Necturus

    Joined:
    May 17, 2010
    Posts:
    2,956
    Thank you mouurusai.
    I hope it will be enough for demo-scene if there is nothing again I can find.