Nature. Шейдер колыхающейся растительности.

Шейдеры и все-все-все.

Nature. Шейдер колыхающейся растительности.

Сообщение Fatalix3d 25 мар 2010, 11:27

Код: Выделить всё
Shader "Nature/Soft Occlusion Windy Leaves" {
   Properties {
      _Color ("Main Color", Color) = (.5, .5, .5, 0)
      _Color2 ("Fade Color", Color) = (1, .9, .8, 0)
      _MainTex ("Base (RGB) Alpha (A)", 2D) = "white" {}
      _Cutoff ("Base Alpha cutoff", Range (0,1)) = 0.5
      _BaseLight ("BaseLight", range (0, 1)) = 0.35
      _AO ("Amb. Occlusion", range (0, 10)) = 2.4
      _Occlusion ("Dir Occlusion", range (0, 20)) = 7.5
      _WindSpeed ("WaveSpeed", range (1, 10)) =2         
      _WaveScale ("WaveScale", range (1, 40)) = 15
      _Scale ("Scale", Vector) = (1,1,1,1)
   }

   // ---- no vertex programs
   SubShader {      
         Pass {         
         Tags {"Queue" = "Transparent-99" "RenderType" = "TreeTransparentCutout"}

         Colormask RGB
         AlphaTest Greater [_Cutoff]
         Cull Off

         CGPROGRAM
         #pragma vertex Vertex
         #include "UnityCG.cginc" // Standard Unity properties
         #include "TerrainEngine.cginc" // Helper wave functions

struct v2f {
   float4 pos : POSITION;
   float4 color : COLOR0;
   float4 uv : TEXCOORD0;
   float fog : FOGC;
};

uniform float _Occlusion, _AO, _BaseLight;
uniform float3[4] _TerrainTreeLightDirections;

uniform float4  _Color,_Color2;   
int _WindSpeed, _WaveScale;

uniform float4x4 _CameraToWorld;

v2f Vertex(appdata_tree v)
{
   v2f o;
   
   const float4 _waveXSize = float4(0.012, 0.02, -0.06, 0.048) * 2;
   const float4 _waveZSize = float4 (0.006, .02, -0.02, 0.1) * 2;
   const float4 waveSpeed = float4 (0.3, .3, .08, .07) * 4;

   float4 _waveXmove = _waveXSize * waveSpeed * _WaveScale;
   float4 _waveZmove = _waveZSize * waveSpeed * _WaveScale;

   // We model the wind as basic waves...

   // Calculate the wind input to leaves from their vertex positions...
   // for now, we transform them into world-space x/z positions...
   // Later on, we should actually be able to do the whole calc's in post-projective space
   float3 worldPos = mul ((float3x4)_Object2World, v.vertex);
   
   // This is the input to the sinusiodal warp
   float4 waves;
   waves = worldPos.x * _waveXSize;
   waves += worldPos.z * _waveZSize;

   // Add in time to model them over time
   waves += _Time.x * waveSpeed *_WindSpeed;

   float4 s, c;
   waves = frac (waves);
   FastSinCos (waves, s,c);

   float waveAmount = v.texcoord.y;
   s *= waveAmount;

   // Faster winds move the grass more than slow winds
   s *= normalize (waveSpeed);

   s = s * s;
   float fade = dot (s, 1.3);
   s = s * s;
   float3 waveMove = float3 (0,0,0);
   waveMove.x = dot (s, _waveXmove);
   waveMove.z = dot (s, _waveZmove);

   v.vertex.xz -= mul ((float3x3)_World2Object, waveMove).xz;

// Soft Occlusion      
//~ TerrainAnimateTree(v.vertex, v.color.w); // conflicted with above v.vertex wave function, not sure how to make it to work.

   float3 viewpos = mul(glstate.matrix.modelview[0], v.vertex).xyz;
   o.pos = mul(glstate.matrix.mvp, v.vertex);
   o.fog = o.pos.w;
   o.fog = o.pos.z;
   o.uv = v.texcoord; 
   
   float4 lightDir;
   lightDir.w = _AO;

   float4 lightColor = glstate.lightmodel.ambient;
   for (int i = 0; i < 4; i++) {
      #ifdef USE_CUSTOM_LIGHT_DIR
      lightDir.xyz = _TerrainTreeLightDirections[i];
      float atten = 1.0;
      #else
      float3 toLight = glstate.light[i].position.xyz - viewpos.xyz * glstate.light[i].position.w;
      toLight.yz *= -1.0;
      lightDir.xyz = mul( (float3x3)_CameraToWorld, normalize(toLight) );
      float lengthSq = dot(toLight, toLight);
      float atten = 1.0 / (1.0 + lengthSq * glstate.light[i].attenuation.z);      
      #endif

      lightDir.xyz *= _Occlusion;
      float occ =  dot (v.tangent, lightDir);
      occ = max(0, occ);
      occ += _BaseLight;
      occ *= atten;
      lightColor += glstate.light[i].diffuse * occ;
   }

   lightColor.a = 1;
//   lightColor = saturate(lightColor);
   
   o.color = lerp (_Color, _Color2, fade.xxxx) * lightColor;
   //~ o.color = _Color * lightColor;
   #ifdef WRITE_ALPHA_1
   o.color.a = 1;
   #endif

   return o;
}
ENDCG
         SetTexture [_MainTex] { combine texture * primary double, texture }   
      }
      
   // Pass to render object as a shadow caster
      Pass {
         Name "ShadowCaster"
         Tags { "LightMode" = "ShadowCaster" }
         
         Fog {Mode Off}
         ZWrite On ZTest Less Cull Off
         Offset [_ShadowBias], [_ShadowBiasSlope]
   
         CGPROGRAM
         #pragma vertex vert
         #pragma fragment frag
         #pragma multi_compile SHADOWS_NATIVE SHADOWS_CUBE
         #pragma fragmentoption ARB_precision_hint_fastest
         #include "UnityCG.cginc"
         #include "TerrainEngine.cginc"
         
         struct v2f {
            V2F_SHADOW_CASTER;
            float2  uv;
         };
         
         struct appdata {
             float4 vertex : POSITION;
             float4 color : COLOR;
             float4 texcoord : TEXCOORD0;
         };
         v2f vert( appdata v )
         {
            v2f o;
            TerrainAnimateTree(v.vertex, v.color.w);
            TRANSFER_SHADOW_CASTER(o)
            o.uv = v.texcoord;
            return o;
         }
         
         uniform sampler2D _MainTex;
         uniform float _Cutoff;
               
         float4 frag( v2f i ) : COLOR
         {
            half4 texcol = tex2D( _MainTex, i.uv );
            clip( texcol.a - _Cutoff );
            SHADOW_CASTER_FRAGMENT(i)
         }
         ENDCG   
      }
   }

   //~ // ---- no vertex programs     
   SubShader {
      Tags { "Queue" = "Transparent-99"
         "IgnoreProjector"="True"
         "BillboardShader" = "Hidden/TerrainEngine/Soft Occlusion Leaves rendertex"
         "RenderType" = "TreeTransparentCutout"}
      Pass {
         Colormask RGB
         AlphaTest Greater [_Cutoff]
         Cull Off
         Color [_Color]
         SetTexture [_MainTex] { combine texture * primary double, texture }      
      }
   }
}
Fatalix3d
UNец
 
Сообщения: 47
Зарегистрирован: 23 дек 2009, 17:43

Re: Nature. Шейдер колыхающейся растительности.

Сообщение Neodrop 25 мар 2010, 12:26

Учимся использовать тег syntax :-w
Добавить neodrop в Skype
Изображение
"Спасибо!" нашему порталу, вы сможете сказать ЗДЕСЬ.
Если проблема не решается честно, нужно её обмануть! || Per stupiditas at Astra!
Страх порождает слабость. Бесстрашных поражают пули.
Протратившись на блядях байтах, на битах не экономят.
Аватара пользователя
Neodrop
Админ
 
Сообщения: 8480
Зарегистрирован: 08 окт 2008, 15:42
Откуда: Питер
Skype: neodrop
  • Сайт


Вернуться в Shader Lab

Кто сейчас на конференции

Сейчас этот форум просматривают: нет зарегистрированных пользователей и гости: 4