Search Unity

Custom SpeedTree shader

Discussion in 'Shaders' started by brettj, Apr 1, 2015.

  1. brettj

    brettj

    Joined:
    Feb 9, 2015
    Posts:
    43
    I have a shader I built in Shader Forge that I'm trying to apply to a SpeedTree. SpeedTrees use a special, kind of complex shader that applies things like vertex-baked AO and wind animations. I've been digging in the source (http://unity3d.com/get-unity/download/archive) and trying work in my shader, but it's just not going well. Does anyone have any pointers? Shader scripts are kind of alien to me (hence Shader Forge).

    Edit:
    I did some more research on how shaders work and was able to make some progress. Here's some helpful resources:
    - Unity Cg Shaders Tutorial Series
    - Writing Surface Shaders
     
    Last edited: Apr 1, 2015
  2. Pangamini

    Pangamini

    Joined:
    Aug 8, 2012
    Posts:
    54
    Hello. Any progress on this?
     
  3. magic9cube

    magic9cube

    Joined:
    Jan 30, 2014
    Posts:
    58
  4. Radical435

    Radical435

    Joined:
    Oct 20, 2014
    Posts:
    24
    I recently did this -- take a look at the SpeedTreeCommon.cginc -- you can rip most of it out and put it into your own shader file. That shader file should be based on the speed tree shader you want to change. The fragment shader in that cginclude file is where you should modify your colors -- you can also add your own texture or color property for more customization.
     
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hey here's a version i made as an alternative to the SpeedTree shader
    Code (CSharp):
    1. Shader "Custom/Wind_HueVariation" {
    2.     Properties{
    3.         _Color("Main Color", Color) = (1,1,1,1)
    4.         _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
    5.         _BumpMap("Normalmap", 2D) = "bump" {}
    6.         _Cutoff("Alpha cutoff", Range(0,1)) = 0.5
    7.         [Toggle(HUE)]_Hue("Hue Variation", int) = 0
    8.         _HueVariation("", Color) = (1,0.25,0,0.1)
    9.         [Toggle(POSITION_BASED)]_Positioned("Based on Position", int) = 0
    10.         [Toggle(ACTIVATE_WIND)]_ActivateWind("Activate Wind", int) = 1
    11.         _WindStrength("Wind Strength", Range(0,1)) = 0.25
    12.         _WindSpeed("Wind Speed", Range(0,100)) = 0.25
    13.         _NoiseStrength("Noise Strength", Range(0,1)) = 0.25
    14.         _NoiseSpeed("Noise Speed", Range(0,10)) = 0.25
    15.         _WindYShake("Wind Y Shake", Range(0,10)) = 5
    16.         /*[HideInInspector]*/_WindVector("Wind Vector", Vector) = (1,1,1,0)
    17.         /*[HideInInspector]*/_WindFreq("Wind Frequency", Vector) = (1,1,1,0)
    18.     }
    19.  
    20.     SubShader{
    21.         Tags{ "Queue" = "AlphaTest" "IgnoreProjector" = "True" "RenderType" = "TransparentCutout" }
    22.         LOD 200
    23.         Cull Off
    24.         CGPROGRAM
    25.         #pragma surface surf Lambert alphatest:_Cutoff vertex:vert addshadow
    26.         #pragma shader_feature ACTIVATE_WIND
    27.         #pragma shader_feature POSITION_BASED
    28.         #pragma shader_feature HUE
    29.         sampler2D _MainTex;
    30.         sampler2D _BumpMap;
    31.         fixed4 _Color;
    32.         fixed4 _HueVariation;
    33.         half _WindStrength;
    34.         half _NoiseStrength;
    35.         half _WindSpeed;
    36.         half _NoiseSpeed;
    37.         half _WindYShake;
    38.         fixed4 _WindVector;
    39.         fixed4 _WindFreq;
    40.  
    41.         struct Input
    42.         {
    43.             float2 uv_MainTex;
    44.             float2 uv_BumpMap;
    45.             float3 pos;
    46.         };
    47.  
    48.         float rand(float3 myVector)
    49.         {
    50.             return frac(sin(dot(myVector, float3(12.9898, 78.233, 45.5432))) * 43758.5453);
    51.         }
    52.  
    53.         void vert(inout appdata_full v, out Input o)
    54.         {
    55.             UNITY_INITIALIZE_OUTPUT(Input, o);
    56.             #ifdef HUE
    57.             half r = frac(mul(unity_ObjectToWorld, v.vertex).y * v.vertex.y * rand(mul(unity_ObjectToWorld, v.vertex).xyz));
    58.             half g = frac(mul(unity_ObjectToWorld, v.vertex).y * v.vertex.y * rand(mul(unity_ObjectToWorld, v.vertex).xyz));
    59.             half b = frac(mul(unity_ObjectToWorld, v.vertex).y * v.vertex.y * rand(mul(unity_ObjectToWorld, v.vertex).xyz));
    60.             o.pos = half3(r,g,b) * _HueVariation.rgb;
    61.             //o.pos = mul(unity_ObjectToWorld, v.vertex).xyz * step(_HueVariation.r+ _HueVariation.g+ _HueVariation.b,rand(mul(unity_ObjectToWorld, v.vertex).yyy))* _HueVariation.rgb;
    62.             #endif
    63.             #ifdef POSITION_BASED
    64.                 o.pos *= saturate(rand(mul(unity_ObjectToWorld, float4(0, 0, 0, 1))));
    65.             #endif
    66.                 half gradient = _WindYShake - mul(unity_ObjectToWorld, v.vertex).y;
    67.            
    68.             #ifdef ACTIVATE_WIND  
    69.                 v.vertex.x += ((((_WindVector.x + rand(v.color.rgb)) * sin(_WindFreq.x * (_Time.y *_WindSpeed + v.vertex.y)))*(rand(v.color.rgb) * _WindStrength*0.1)) + (sin(_Time.y *_NoiseSpeed + v.vertex.z)*(rand(v.color.rgb) * _NoiseStrength*0.1))) * (v.vertex.y - _WindYShake);
    70.                 v.vertex.y += ((((_WindVector.y + rand(v.color.rgb)) * sin(_WindFreq.y * (_Time.x *_WindSpeed + v.vertex.x)))*(rand(v.color.rgb) * _WindStrength*0.1)) + (cos(_Time.y *_NoiseSpeed + v.vertex.z)*(rand(v.color.rgb) * _NoiseStrength*0.1))) * (v.vertex.y - _WindYShake);
    71.                 v.vertex.z += ((((_WindVector.z + rand(v.color.rgb)) * cos(_WindFreq.z * (_Time.x *_WindSpeed + v.vertex.y)))*(rand(v.color.rgb) * _WindStrength*0.1)) + (cos(_Time.y *_NoiseSpeed + v.vertex.x)*(rand(v.color.rgb) * _NoiseStrength*0.1))) * (v.vertex.y - _WindYShake);
    72.             #endif
    73.            
    74.  
    75.         }
    76.  
    77.         void surf(Input IN, inout SurfaceOutput o)
    78.         {
    79.             fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
    80.             o.Albedo = c.rgb + (IN.pos * _HueVariation.a);
    81.             o.Alpha = c.a;
    82.             o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
    83.         }
    84.         ENDCG
    85.     }
    86.     FallBack "Legacy Shaders/Transparent/Cutout/Diffuse"
    87. }
     
    Ali-Nagori and magic9cube like this.
  6. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    Hello folks,

    Has any of you been able to add global transparency (without the X-ray effect, so not applied to the individual textures) on Speedtree's shader ? (I asked the same question here but we may as well keep this thread documented...)