Search Unity

Splat map shader with more then 4 textues

Discussion in 'Shaders' started by Nieles, Sep 8, 2014.

  1. Nieles

    Nieles

    Joined:
    Dec 27, 2010
    Posts:
    50
    I'm working on a procedural universe and it's coming of pretty nice so far, here are some first planet tests:




    But currently I'm hold back because I can't get more then 4 textures on my planet.
    Currently I use the Unity terrain FirstPass shader:


    Code (CSharp):
    1. Shader "Custom/Splatmap/Lightmap-FirstPass" {
    2. Properties {
    3.     _Control ("Control (RGBA)", 2D) = "red" {}
    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.     // used in fallback on old cards
    9.     _MainTex ("BaseMap (RGB)", 2D) = "white" {}
    10.     _Color ("Main Color", Color) = (1,1,1,1)
    11. }
    12.    
    13. SubShader {
    14.     Tags {
    15.         "SplatCount" = "4"
    16.         "Queue" = "Geometry-100"
    17.         "RenderType" = "Opaque"
    18.     }
    19.  
    20. CGPROGRAM
    21. #pragma surface surf Lambert
    22. struct Input {
    23.     float2 uv_Control : TEXCOORD0;
    24.     float2 uv_Splat0 : TEXCOORD1;
    25.     float2 uv_Splat1 : TEXCOORD2;
    26.     float2 uv_Splat2 : TEXCOORD3;
    27.     float2 uv_Splat3 : TEXCOORD4;
    28. };
    29.  
    30. sampler2D _Control;
    31. sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    32.  
    33. void surf (Input IN, inout SurfaceOutput o) {
    34.     fixed4 splat_control = tex2D (_Control, IN.uv_Control);
    35.     fixed3 col;
    36.     col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
    37.     col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
    38.     col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
    39.     col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
    40.     o.Albedo = col;
    41.     o.Alpha = 0.0;
    42. }
    43. ENDCG
    44. }
    45.  
    46. // Fallback to Diffuse
    47. Fallback "Diffuse"
    48. }
    49.  
    Now I know that Unity uses this second shader AddPass when the terrain uses more then 4 textures:


    Code (CSharp):
    1. Shader "Custom/Splatmap/Lightmap-AddPass" {
    2. Properties {
    3.     _Control ("Control (RGBA)", 2D) = "black" {}
    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. }
    9.    
    10. SubShader {
    11.     Tags {
    12.         "SplatCount" = "4"
    13.         "Queue" = "Geometry-99"
    14.         "IgnoreProjector"="True"
    15.         "RenderType" = "Opaque"
    16.     }
    17.    
    18. CGPROGRAM
    19. #pragma surface surf Lambert decal:add
    20. struct Input {
    21.     float2 uv_Control : TEXCOORD0;
    22.     float2 uv_Splat0 : TEXCOORD1;
    23.     float2 uv_Splat1 : TEXCOORD2;
    24.     float2 uv_Splat2 : TEXCOORD3;
    25.     float2 uv_Splat3 : TEXCOORD4;
    26. };
    27.  
    28. sampler2D _Control;
    29. sampler2D _Splat0,_Splat1,_Splat2,_Splat3;
    30.  
    31. void surf (Input IN, inout SurfaceOutput o) {
    32.     fixed4 splat_control = tex2D (_Control, IN.uv_Control);
    33.     fixed3 col;
    34.     col  = splat_control.r * tex2D (_Splat0, IN.uv_Splat0).rgb;
    35.     col += splat_control.g * tex2D (_Splat1, IN.uv_Splat1).rgb;
    36.     col += splat_control.b * tex2D (_Splat2, IN.uv_Splat2).rgb;
    37.     col += splat_control.a * tex2D (_Splat3, IN.uv_Splat3).rgb;
    38.     o.Albedo = col;
    39.     o.Alpha = 0.0;
    40. }
    41. ENDCG
    42. }
    43.  
    44. Fallback off
    45. }
    46.  
    But I need some help to get this to work on my planets, how do I get this AddPass to work?
    Do I need to set it up from my script? And how do I do this?

    I also tried adding more splat maps to the FirstPass shader but then I got out of resources.
     
  2. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Add more materials to Mesh Renderer materials.
     
  3. Nieles

    Nieles

    Joined:
    Dec 27, 2010
    Posts:
    50
    ooh wauw! That easy...
    Thanks man! I spend so many hours on this, didn't know you could add more materials... :)
     
  4. BigBGros

    BigBGros

    Joined:
    Aug 30, 2013
    Posts:
    21
    Hi,

    Nice Work Nieles!!!

    I'm pretty interested how you UVMap the sphere to make no seams...

    Can you explain me that?

    Thanks!!!
     
  5. Benjames

    Benjames

    Joined:
    Oct 18, 2013
    Posts:
    3
    In computer graphics, cube mapping is a method of environment mappingthat uses the six faces of a cube as the map shape. The environment is projected onto the sides of a cube and stored as six square textures, or unfolded into six regions of a single texture.
     
  6. stuepfnick

    stuepfnick

    Joined:
    Apr 19, 2017
    Posts:
    18
    Hi!
    I would like to know how to get that kind of shader to work with custom made meshes (like here). I have Unity 2019.4 and downloaded the built-in shaders already, there is a folder "TerrainShaders/Splats" - 12 files in it.
    All take only a Basemap, maximum a Holemap. I guess not really suited?