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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Remove randomness from Tree Shader

Discussion in 'Shaders' started by ApteryxK, Mar 25, 2015.

  1. ApteryxK

    ApteryxK

    Joined:
    Feb 8, 2015
    Posts:
    40
    Hello, I couldn't find a better title for this as I am not sure what does the effect.
    Basically, I made a thread on External Tools about AlphaCutoff, which is what I thought was making the unwanted effect or whatever. I realised that I have to change the Shader of my tree.
    I found the shader files on Unity.com but I don't know what and how I should change.
    This is what I get :


    This is what I want to get( and automatically get it when I make Detail Grasses on terrains ), (the tree shader is different and incompatible ... ):



    This is an example of tree without the effect I want to get rid of :

    .

    Here is the default TreeCreatorLeavesFast.shader file's content :

    Code (CSharp):
    1. Shader "Nature/Tree Creator Leaves Fast" {
    2. Properties {
    3.     _Color ("Main Color", Color) = (1,1,1,1)
    4.     _TranslucencyColor ("Translucency Color", Color) = (0.73,0.85,0.41,1) // (187,219,106,255)
    5.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.3
    6.     _TranslucencyViewDependency ("View dependency", Range(0,1)) = 0.7
    7.     _ShadowStrength("Shadow Strength", Range(0,1)) = 1.0
    8.    
    9.     _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    10.    
    11.     // These are here only to provide default values
    12.     [HideInInspector] _TreeInstanceColor ("TreeInstanceColor", Vector) = (1,1,1,1)
    13.     [HideInInspector] _TreeInstanceScale ("TreeInstanceScale", Vector) = (1,1,1,1)
    14.     [HideInInspector] _SquashAmount ("Squash", Float) = 1
    15. }
    16.  
    17. SubShader {
    18.     Tags {
    19.         "IgnoreProjector"="True"
    20.         "RenderType" = "TreeLeaf"
    21.     }
    22.     LOD 200
    23.        
    24.     Pass {
    25.         Tags { "LightMode" = "ForwardBase" }
    26.         Name "ForwardBase"
    27.  
    28.     CGPROGRAM
    29.         #include "UnityBuiltin3xTreeLibrary.cginc"
    30.  
    31.         #pragma vertex VertexLeaf
    32.         #pragma fragment FragmentLeaf
    33.         #pragma multi_compile_fwdbase nolightmap
    34.         #pragma multi_compile_fog
    35.        
    36.         sampler2D _MainTex;
    37.         float4 _MainTex_ST;
    38.  
    39.         fixed _Cutoff;
    40.         sampler2D _ShadowMapTexture;
    41.  
    42.         struct v2f_leaf {
    43.             float4 pos : SV_POSITION;
    44.             fixed4 diffuse : COLOR0;
    45.         #if defined(SHADOWS_SCREEN)
    46.             fixed4 mainLight : COLOR1;
    47.         #endif
    48.             float2 uv : TEXCOORD0;
    49.         #if defined(SHADOWS_SCREEN)
    50.             float4 screenPos : TEXCOORD1;
    51.         #endif
    52.             UNITY_FOG_COORDS(2)
    53.         };
    54.  
    55.         v2f_leaf VertexLeaf (appdata_full v)
    56.         {
    57.             v2f_leaf o;
    58.             TreeVertLeaf(v);
    59.             o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    60.  
    61.             fixed ao = v.color.a;
    62.             ao += 0.1; ao = saturate(ao * ao * ao); // emphasize AO
    63.                        
    64.             fixed3 color = v.color.rgb * ao;
    65.            
    66.             float3 worldN = UnityObjectToWorldNormal (v.normal);
    67.  
    68.             fixed4 mainLight;
    69.             mainLight.rgb = ShadeTranslucentMainLight (v.vertex, worldN) * color;
    70.             mainLight.a = v.color.a;
    71.             o.diffuse.rgb = ShadeTranslucentLights (v.vertex, worldN) * color;
    72.             o.diffuse.a = 1;
    73.         #if defined(SHADOWS_SCREEN)
    74.             o.mainLight = mainLight;
    75.             o.screenPos = ComputeScreenPos (o.pos);
    76.         #else
    77.             o.diffuse += mainLight;
    78.         #endif          
    79.             o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    80.             UNITY_TRANSFER_FOG(o,o.pos);
    81.             return o;
    82.         }
    83.  
    84.         fixed4 FragmentLeaf (v2f_leaf IN) : SV_Target
    85.         {
    86.             fixed4 albedo = tex2D(_MainTex, IN.uv);
    87.             fixed alpha = albedo.a;
    88.             clip (alpha - _Cutoff);
    89.  
    90.         #if defined(SHADOWS_SCREEN)
    91.             half4 light = IN.mainLight;
    92.             half atten = tex2Dproj(_ShadowMapTexture, UNITY_PROJ_COORD(IN.screenPos)).r;
    93.             light.rgb *= lerp(1, atten, _ShadowStrength);
    94.             light.rgb += IN.diffuse.rgb;
    95.         #else
    96.             half4 light = IN.diffuse;
    97.         #endif
    98.  
    99.             fixed4 col = fixed4 (albedo.rgb * light, 0.0);
    100.             UNITY_APPLY_FOG(IN.fogCoord, col);
    101.             return col;
    102.         }
    103.  
    104.     ENDCG
    105.     }
    106. }
    107.  
    108. Dependency "OptimizedShader" = "Hidden/Nature/Tree Creator Leaves Fast Optimized"
    109. FallBack "Diffuse"
    110. }
    111.  
    and I don't think this will be useful, but this is WavingGrass.shader ( the style I want) :
    Code (CSharp):
    1. Shader "Hidden/TerrainEngine/Details/WavingDoublePass" {
    2. Properties {
    3.     _WavingTint ("Fade Color", Color) = (.7,.6,.5, 0)
    4.     _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
    5.     _WaveAndDistance ("Wave and distance", Vector) = (12, 3.6, 1, 1)
    6.     _Cutoff ("Cutoff", float) = 0.5
    7. }
    8.  
    9. SubShader {
    10.     Tags {
    11.         "Queue" = "Geometry+200"
    12.         "IgnoreProjector"="True"
    13.         "RenderType"="Grass"
    14.         "DisableBatching"="True"
    15.     }
    16.     Cull Off
    17.     LOD 200
    18.        
    19. CGPROGRAM
    20. #pragma surface surf Lambert vertex:WavingGrassVert addshadow
    21. #include "TerrainEngine.cginc"
    22.  
    23. sampler2D _MainTex;
    24. fixed _Cutoff;
    25.  
    26. struct Input {
    27.     float2 uv_MainTex;
    28.     fixed4 color : COLOR;
    29. };
    30.  
    31. void surf (Input IN, inout SurfaceOutput o) {
    32.     fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * IN.color;
    33.     o.Albedo = c.rgb;
    34.     o.Alpha = c.a;
    35.     clip (o.Alpha - _Cutoff);
    36.     o.Alpha *= IN.color.a;
    37. }
    38. ENDCG
    39. }
    40.    
    41.     SubShader {
    42.         Tags {
    43.             "Queue" = "Geometry+200"
    44.             "IgnoreProjector"="True"
    45.             "RenderType"="Grass"
    46.         }
    47.         Cull Off
    48.         LOD 200
    49.         ColorMask RGB
    50.        
    51.         Pass {
    52.             Tags { "LightMode" = "Vertex" }
    53.             Material {
    54.                 Diffuse (1,1,1,1)
    55.                 Ambient (1,1,1,1)
    56.             }
    57.             Lighting On
    58.             ColorMaterial AmbientAndDiffuse
    59.             AlphaTest Greater [_Cutoff]
    60.             SetTexture [_MainTex] { combine texture * primary DOUBLE, texture }
    61.         }
    62.         Pass {
    63.             Tags { "LightMode" = "VertexLMRGBM" }
    64.             AlphaTest Greater [_Cutoff]
    65.             BindChannels {
    66.                 Bind "Vertex", vertex
    67.                 Bind "texcoord1", texcoord0 // lightmap uses 2nd uv
    68.                 Bind "texcoord", texcoord1 // main uses 1st uv
    69.             }
    70.             SetTexture [unity_Lightmap] {
    71.                 matrix [unity_LightmapMatrix]
    72.                 combine texture * texture alpha DOUBLE
    73.             }
    74.             SetTexture [_MainTex] { combine texture * previous QUAD, texture }
    75.         }
    76.     }
    77.    
    78.     Fallback Off
    79. }
    80.  
    Any tip, clue, solution ?
    I appreciate your help :)
    Kiwi.
     
  2. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    You should have the low res blocky texture at around 128x128 and enable mipmaps + bilinear filtering. Then the process of clip() shader (alpha test) will take care of the rest.
     
    ApteryxK likes this.
  3. ApteryxK

    ApteryxK

    Joined:
    Feb 8, 2015
    Posts:
    40
    Well, thank you very much.
    But I still have these weird edges.
    2015-03-08_00015.jpg
    Pixels are perfect squares, but why by making them smoother, they get random edges ?...
    I really would like a smooth round edge like :