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

Windy shader from shadowgun

Discussion in 'Shaders' started by ThisIsSparta, Sep 23, 2016.

  1. ThisIsSparta

    ThisIsSparta

    Joined:
    May 4, 2014
    Posts:
    142
    Hi guys! Kinda need help with this one...

    I'm trying to use the windy shader from shadowgun but seems in unity 5 it has problems. No diffuse, no shadow casting and receiving. if someone can help would be very appreciated :)

    I post the shader down here

    Code (CSharp):
    1. // Upgrade NOTE: commented out 'float4 unity_LightmapST', a built-in variable
    2. // Upgrade NOTE: commented out 'sampler2D unity_Lightmap', a built-in variable
    3. // Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
    4. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    5. // Upgrade NOTE: replaced tex2D unity_Lightmap with UNITY_SAMPLE_TEX2D
    6.  
    7. // - Unlit
    8. // - Per-vertex (virtual) camera space specular light
    9. // - SUPPORTS lightmap
    10.  
    11. Shader "MADFINGER/Environment/Lightmap + Wind" {
    12. Properties {
    13.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "white" {}
    14.     _Wind("Wind params",Vector) = (1,1,1,1)
    15.     _WindEdgeFlutter("Wind edge fultter factor", float) = 0.5
    16.     _WindEdgeFlutterFreqScale("Wind edge fultter freq scale",float) = 0.5
    17. }
    18.  
    19. SubShader {
    20.     Tags {"Queue"="Transparent" "RenderType"="Transparent" "LightMode"="ForwardBase"}
    21.  
    22.    
    23.     Blend SrcAlpha OneMinusSrcAlpha
    24.     Cull Off ZWrite Off
    25.    
    26.    
    27.     CGINCLUDE
    28.     #include "UnityCG.cginc"
    29.     #include "TerrainEngine.cginc"
    30.     sampler2D _MainTex;
    31.     float4 _MainTex_ST;
    32.     samplerCUBE _ReflTex;
    33.    
    34.     #ifndef LIGHTMAP_OFF
    35.     // float4 unity_LightmapST;
    36.     // sampler2D unity_Lightmap;
    37.     #endif
    38.    
    39.     float _WindEdgeFlutter;
    40.     float _WindEdgeFlutterFreqScale;
    41.  
    42.     struct v2f {
    43.         float4 pos : SV_POSITION;
    44.         float2 uv : TEXCOORD0;
    45.         #ifndef LIGHTMAP_OFF
    46.         float2 lmap : TEXCOORD1;
    47.         #endif
    48.         fixed3 spec : TEXCOORD2;
    49.     };
    50.  
    51. inline float4 AnimateVertex2(float4 pos, float3 normal, float4 animParams,float4 wind,float2 time)
    52. {  
    53.     // animParams stored in color
    54.     // animParams.x = branch phase
    55.     // animParams.y = edge flutter factor
    56.     // animParams.z = primary factor
    57.     // animParams.w = secondary factor
    58.  
    59.     float fDetailAmp = 0.1f;
    60.     float fBranchAmp = 0.3f;
    61.    
    62.     // Phases (object, vertex, branch)
    63.     float fObjPhase = dot(unity_ObjectToWorld[3].xyz, 1);
    64.     float fBranchPhase = fObjPhase + animParams.x;
    65.    
    66.     float fVtxPhase = dot(pos.xyz, animParams.y + fBranchPhase);
    67.    
    68.     // x is used for edges; y is used for branches
    69.     float2 vWavesIn = time  + float2(fVtxPhase, fBranchPhase );
    70.    
    71.     // 1.975, 0.793, 0.375, 0.193 are good frequencies
    72.     float4 vWaves = (frac( vWavesIn.xxyy * float4(1.975, 0.793, 0.375, 0.193) ) * 2.0 - 1.0);
    73.    
    74.     vWaves = SmoothTriangleWave( vWaves );
    75.     float2 vWavesSum = vWaves.xz + vWaves.yw;
    76.  
    77.     // Edge (xz) and branch bending (y)
    78.     float3 bend = animParams.y * fDetailAmp * normal.xyz;
    79.     bend.y = animParams.w * fBranchAmp;
    80.     pos.xyz += ((vWavesSum.xyx * bend) + (wind.xyz * vWavesSum.y * animParams.w)) * wind.w;
    81.  
    82.     // Primary bending
    83.     // Displace position
    84.     pos.xyz += animParams.z * wind.xyz;
    85.    
    86.     return pos;
    87. }
    88.  
    89.  
    90.    
    91.     v2f vert (appdata_full v)
    92.     {
    93.         v2f o;
    94.        
    95.         float4    wind;
    96.        
    97.         float            bendingFact    = v.color.a;
    98.        
    99.         wind.xyz    = mul((float3x3)unity_WorldToObject,_Wind.xyz);
    100.         wind.w        = _Wind.w  * bendingFact;
    101.        
    102.        
    103.         float4    windParams    = float4(0,_WindEdgeFlutter,bendingFact.xx);
    104.         float         windTime         = _Time.y * float2(_WindEdgeFlutterFreqScale,1);
    105.         float4    mdlPos            = AnimateVertex2(v.vertex,v.normal,windParams,wind,windTime);
    106.        
    107.         o.pos = mul(UNITY_MATRIX_MVP,mdlPos);
    108.         o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    109.        
    110.         o.spec = v.color;
    111.        
    112.         #ifndef LIGHTMAP_OFF
    113.         o.lmap = v.texcoord1.xy * unity_LightmapST.xy + unity_LightmapST.zw;
    114.         #endif
    115.        
    116.         return o;
    117.     }
    118.     ENDCG
    119.  
    120.  
    121.     Pass {
    122.         CGPROGRAM
    123.         #pragma debug
    124.         #pragma vertex vert
    125.         #pragma fragment frag
    126.         #pragma fragmentoption ARB_precision_hint_fastest      
    127.         fixed4 frag (v2f i) : COLOR
    128.         {
    129.             fixed4 tex = tex2D (_MainTex, i.uv);
    130.            
    131.             fixed4 c;
    132.             c.rgb = tex.rgb;
    133.             c.a = tex.a;
    134.            
    135.             #ifndef LIGHTMAP_OFF
    136.             fixed3 lm = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lmap));
    137.             c.rgb *= lm;
    138.             #endif
    139.            
    140.             return c;
    141.         }
    142.         ENDCG
    143.     }  
    144. }
    145. }
     
  2. BuzzKirill

    BuzzKirill

    Joined:
    Nov 14, 2017
    Posts:
    42
    6 years later, I've had this question as well. The answer seems to be that this shader isn't meant to be lit regularly, instead making use of lightmaps:

    Code (CSharp):
    1.  
    2.             fixed4 tex = tex2D (_MainTex, i.uv);
    3.          
    4.             fixed4 c;
    5.             c.rgb = tex.rgb;
    6.             c.a = tex.a;
    7.          
    8.             #ifndef LIGHTMAP_OFF
    9.             fixed3 lm = DecodeLightmap (UNITY_SAMPLE_TEX2D(unity_Lightmap, i.lmap));
    10.             c.rgb *= lm;
    11.             #endif
    12.          
    13.             return c;
    14.          
    I modified the shader as well as I could so that it now accepts diffuse lighting. Also added a "shadow strength" slider so lighting can be somewhat adjusted on the go. I'm a 3D artist not a programmer, so this almost certainly could be done better. But it works, and here it is:

    Code (CSharp):
    1. Shader "Custom/Wind" {
    2. Properties {
    3.     _MainTex ("Base (RGB) Gloss (A)", 2D) = "grey" {}
    4.     _Wind("Wind params",Vector) = (1,1,1,1)
    5.     _WindEdgeFlutter("Wind edge fultter factor", float) = 0.5
    6.     _WindEdgeFlutterFreqScale("Wind edge fultter freq scale", float)= 0.5
    7.     _ShadowStrength("Shadow strength", float) = 0.5
    8. }
    9.  
    10. SubShader {
    11.     Tags {"Queue"="Transparent" "RenderType"="Transparent" "LightMode"="ForwardBase"}
    12.     LOD 100
    13.  
    14.  
    15.     CGINCLUDE
    16.     #include "UnityCG.cginc"
    17.     #include "TerrainEngine.cginc"
    18.     #include "UnityLightingCommon.cginc"
    19.     sampler2D _MainTex;
    20.     float4 _MainTex_ST;
    21.     samplerCUBE _ReflTex;
    22.  
    23.     float _ShadowStrength;
    24.     float _WindEdgeFlutter;
    25.     float _WindEdgeFlutterFreqScale;
    26.  
    27.     struct v2f {
    28.         float4 pos : SV_POSITION;
    29.         float2 uv : TEXCOORD0;
    30.         fixed4 diff : COLOR0;      
    31.     };
    32.  
    33. inline float4 AnimateVertex2(float4 pos, float3 normal, float4 animParams,float4 wind,float2 time)
    34. {  
    35.     // animParams stored in color
    36.     // animParams.x = branch phase
    37.     // animParams.y = edge flutter factor
    38.     // animParams.z = primary factor
    39.     // animParams.w = secondary factor
    40.  
    41.     float fDetailAmp = 0.1f;
    42.     float fBranchAmp = 0.3f;
    43.  
    44.     // Phases (object, vertex, branch)
    45.     float fObjPhase = dot(unity_ObjectToWorld[3].xyz, 1);
    46.     float fBranchPhase = fObjPhase + animParams.x;
    47.  
    48.     float fVtxPhase = dot(pos.xyz, animParams.y + fBranchPhase);
    49.  
    50.     // x is used for edges; y is used for branches
    51.     float2 vWavesIn = time  + float2(fVtxPhase, fBranchPhase );
    52.  
    53.     // 1.975, 0.793, 0.375, 0.193 are good frequencies
    54.     float4 vWaves = (frac( vWavesIn.xxyy * float4(1.975, 0.793, 0.375, 0.193) ) * 2.0 - 1.0);
    55.  
    56.     vWaves = SmoothTriangleWave( vWaves );
    57.     float2 vWavesSum = vWaves.xz + vWaves.yw;
    58.  
    59.     // Edge (xz) and branch bending (y)
    60.     float3 bend = animParams.y * fDetailAmp * normal.xyz;
    61.     bend.y = animParams.w * fBranchAmp;
    62.     pos.xyz += ((vWavesSum.xyx * bend) + (wind.xyz * vWavesSum.y * animParams.w)) * wind.w;
    63.  
    64.     // Primary bending
    65.     // Displace position
    66.     pos.xyz += animParams.z * wind.xyz;
    67.  
    68.     return pos;
    69. }
    70.  
    71.  
    72.  
    73.     v2f vert (appdata_full v)
    74.     {
    75.         v2f o;
    76.      
    77.         float4    wind;
    78.      
    79.         float            bendingFact    = v.color.a;
    80.      
    81.         wind.xyz    = mul((float3x3)unity_WorldToObject,_Wind.xyz);
    82.         wind.w        = _Wind.w  * bendingFact;
    83.      
    84.      
    85.         float4    windParams    = float4(0,_WindEdgeFlutter,bendingFact.xx);
    86.         float         windTime         = _Time.y * float2(_WindEdgeFlutterFreqScale,1);
    87.         float4    mdlPos            = AnimateVertex2(v.vertex,v.normal,windParams,wind,windTime);
    88.  
    89.         half3 worldNormal = UnityObjectToWorldNormal(v.normal);
    90.  
    91.         o.pos = UnityObjectToClipPos(mdlPos);
    92.         o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    93.         half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
    94.         o.diff = nl * _LightColor0;
    95.              
    96.         return o;
    97.     }
    98.     ENDCG
    99.  
    100.  
    101.     Pass {
    102.         CGPROGRAM
    103.         #pragma debug
    104.         #pragma vertex vert
    105.         #pragma fragment frag
    106.         #pragma fragmentoption ARB_precision_hint_fastest
    107.         #pragma multi_compile_instancing      
    108.         fixed4 frag (v2f i) : SV_Target
    109.         {
    110.             fixed4 c = tex2D (_MainTex, i.uv);
    111.             float s = float2(_ShadowStrength, 1);
    112.             c *= i.diff + s;                      
    113.                      
    114.             return c;
    115.         }
    116.         ENDCG
    117.     }  
    118. }
    119. }