Search Unity

Help with SpeedTree shaders

Discussion in 'Shaders' started by PiAnkh, Mar 18, 2015.

  1. PiAnkh

    PiAnkh

    Joined:
    Apr 20, 2013
    Posts:
    126
    Hello,

    I wonder if anyone could help me make some changes to the shader code for SpeedTrees.

    Currently the SpeedTrees really do not look good at all. In particular they show a greater contrast in the lighting than other objects in teh scene. It looks like the lighting is more strongly impacted by my single directional light than othe robjects.

    Is the some way I can change the SpeedTree shader code to reduce the impact of th edirectional light and increase the influence of ambient light??????

    I am not very knowledgeable about surface shaders but I can hack code.

    Any help or tips are very welcome. The SpeedTree shader code can be found here

    Thanks for any tips!!!!!!!


    http://unity3d.com/download_unity/builtin_shaders.zip

    Code (CSharp):
    1. #ifndef SPEEDTREE_COMMON_INCLUDED
    2. #define SPEEDTREE_COMMON_INCLUDED
    3.  
    4. #include "UnityCG.cginc"
    5.  
    6. #define SPEEDTREE_Y_UP
    7.  
    8. #if defined(GEOM_TYPE_BRANCH_DETAIL) || defined(GEOM_TYPE_BRANCH_BLEND)
    9.     #define GEOM_TYPE_BRANCH
    10. #endif
    11.  
    12. #include "SpeedTreeVertex.cginc"
    13.  
    14. // Define Input structure
    15.  
    16. struct Input
    17. {
    18.     fixed4 color;
    19.     half3 interpolator1;
    20.     #if defined(GEOM_TYPE_BRANCH_DETAIL) || defined(GEOM_TYPE_BRANCH_BLEND)
    21.         half3 interpolator2;
    22.     #endif
    23.     #ifdef LOD_FADE_CROSSFADE
    24.         half3 myScreenPos;
    25.     #endif
    26. };
    27.    
    28. // Define uniforms
    29.  
    30. #define mainTexUV interpolator1.xy
    31. uniform sampler2D _MainTex;
    32.  
    33. #ifdef GEOM_TYPE_BRANCH_DETAIL
    34.     #define Detail interpolator2.xy
    35.     uniform sampler2D _DetailTex;
    36. #endif
    37.  
    38. #ifdef GEOM_TYPE_BRANCH_BLEND
    39.     #define BranchBlend interpolator2
    40. #endif
    41.  
    42. #if defined(GEOM_TYPE_FROND) || defined(GEOM_TYPE_LEAF) || defined(GEOM_TYPE_FACING_LEAF)
    43.     #define SPEEDTREE_ALPHATEST
    44.     uniform fixed _Cutoff;
    45. #endif
    46.  
    47. #ifdef EFFECT_HUE_VARIATION
    48.     #define HueVariationAmount interpolator1.z
    49.     uniform half4 _HueVariation;
    50. #endif
    51.  
    52. #ifdef EFFECT_BUMP
    53.     uniform sampler2D _BumpMap;
    54. #endif
    55.  
    56. #ifdef LOD_FADE_CROSSFADE
    57.     uniform sampler2D _DitherMaskLOD2D;
    58. #endif
    59.  
    60. uniform fixed4 _Color;
    61. uniform half _Shininess;
    62.  
    63. // Vertex processing
    64.  
    65. void SpeedTreeVert(inout SpeedTreeVB IN, out Input OUT)
    66. {
    67.     UNITY_INITIALIZE_OUTPUT(Input, OUT);
    68.  
    69.     OUT.mainTexUV = IN.texcoord.xy;
    70.     OUT.color = _Color;
    71.     OUT.color.rgb *= IN.color.r; // ambient occlusion factor
    72.  
    73.     #ifdef EFFECT_HUE_VARIATION
    74.         float hueVariationAmount = frac(_Object2World[0].w + _Object2World[1].w + _Object2World[2].w);
    75.         hueVariationAmount += frac(IN.vertex.x + IN.normal.y + IN.normal.x) * 0.5 - 0.3;
    76.         OUT.HueVariationAmount = saturate(hueVariationAmount * _HueVariation.a);
    77.     #endif
    78.  
    79.     #ifdef GEOM_TYPE_BRANCH_DETAIL
    80.         OUT.Detail = IN.texcoord2.xy;
    81.     #endif
    82.  
    83.     #ifdef GEOM_TYPE_BRANCH_BLEND
    84.         OUT.BranchBlend = float3(IN.texcoord2.zw, IN.texcoord1.w);
    85.     #endif
    86.  
    87.     OffsetSpeedTreeVertex(IN, unity_LODFade.x);
    88.  
    89.     #ifdef LOD_FADE_CROSSFADE
    90.         float4 pos = mul(UNITY_MATRIX_MVP, IN.vertex);
    91.         OUT.myScreenPos = ComputeScreenPos(pos).xyw;
    92.         OUT.myScreenPos.xy *= _ScreenParams.xy * 0.25;
    93.     #endif
    94. }
    95.  
    96. // Fragment processing
    97.  
    98. #ifdef EFFECT_BUMP
    99.     #define SPEEDTREE_DATA_NORMAL            fixed3 Normal;
    100.     #define SPEEDTREE_COPY_NORMAL(to, from)    to.Normal = from.Normal;
    101. #else
    102.     #define SPEEDTREE_DATA_NORMAL
    103.     #define SPEEDTREE_COPY_NORMAL(to, from)
    104. #endif
    105.  
    106. #define SPEEDTREE_COPY_FRAG(to, from)    \
    107.     to.Albedo = from.Albedo;            \
    108.     to.Alpha = from.Alpha;                \
    109.     to.Specular = from.Specular;        \
    110.     to.Gloss = from.Gloss;                \
    111.     SPEEDTREE_COPY_NORMAL(to, from)
    112.  
    113. struct SpeedTreeFragOut
    114. {
    115.     fixed3 Albedo;
    116.     fixed Alpha;
    117.     half Specular;
    118.     fixed Gloss;
    119.     SPEEDTREE_DATA_NORMAL
    120. };
    121.  
    122. void SpeedTreeFrag(Input IN, out SpeedTreeFragOut OUT)
    123. {
    124.     #ifdef LOD_FADE_CROSSFADE
    125.         half2 projUV = IN.myScreenPos.xy / IN.myScreenPos.z;
    126.         projUV.y = frac(projUV.y) * 0.0625 /* 1/16 */ + unity_LODFade.y; // quantized lod fade by 16 levels
    127.         clip(tex2D(_DitherMaskLOD2D, projUV).a - 0.5);
    128.     #endif
    129.  
    130.     half4 diffuseColor = tex2D(_MainTex, IN.mainTexUV);
    131.  
    132.     OUT.Alpha = diffuseColor.a * _Color.a;
    133.     #ifdef SPEEDTREE_ALPHATEST
    134.         clip(OUT.Alpha - _Cutoff);
    135.     #endif
    136.  
    137.     #ifdef GEOM_TYPE_BRANCH_DETAIL
    138.         half4 detailColor = tex2D(_DetailTex, IN.Detail);
    139.         diffuseColor.rgb = lerp(diffuseColor.rgb, detailColor.rgb, detailColor.a);
    140.     #endif
    141.  
    142.     #ifdef GEOM_TYPE_BRANCH_BLEND
    143.         half4 blendColor = tex2D(_MainTex, IN.BranchBlend.xy);
    144.         half amount = saturate(IN.BranchBlend.z);
    145.         diffuseColor.rgb = lerp(blendColor.rgb, diffuseColor.rgb, amount);
    146.     #endif
    147.  
    148.     #ifdef EFFECT_HUE_VARIATION
    149.         half3 shiftedColor = lerp(diffuseColor.rgb, _HueVariation.rgb, IN.HueVariationAmount);
    150.         half maxBase = max(diffuseColor.r, max(diffuseColor.g, diffuseColor.b));
    151.         half newMaxBase = max(shiftedColor.r, max(shiftedColor.g, shiftedColor.b));
    152.         maxBase /= newMaxBase;
    153.         maxBase = maxBase * 0.5f + 0.5f;
    154.         // preserve vibrance
    155.         shiftedColor.rgb *= maxBase;
    156.         diffuseColor.rgb = saturate(shiftedColor);
    157.     #endif
    158.  
    159.     OUT.Albedo = diffuseColor.rgb * IN.color.rgb;
    160.     OUT.Gloss = diffuseColor.a;
    161.     OUT.Specular = _Shininess;
    162.  
    163.     #ifdef EFFECT_BUMP
    164.         OUT.Normal = UnpackNormal(tex2D(_BumpMap, IN.mainTexUV));
    165.     #endif
    166. }
    167.  
    168. #endif // SPEEDTREE_COMMON_INCLUDED
     
  2. Yog0

    Yog0

    Joined:
    Oct 1, 2013
    Posts:
    14
    I agree about there being too much contrast between light and shadow on the speed trees. Decreasing the bias on the scene's directional light helped a bit. Best of luck.

    Also if anyone has seen documentation on the speedtree and speedtree billboard shader I'd love a link.
     
  3. Dannyoakes

    Dannyoakes

    Joined:
    Feb 10, 2015
    Posts:
    118