Advertisement
tonynogo

Demo 95 - Flag with lighting

Jul 6th, 2017
6,339
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "Custom/Flag with lighting"
  2. {
  3.     Properties
  4.     {
  5.         _MainTex ("Texture", 2D) = "white" {}
  6.         _Ambient ("Ambient", Range(0., 1.)) = 0.2
  7.         [Header(Waves)]
  8.         _WaveSpeed("Speed", float) = 0.0
  9.         _WaveStrength("Strength", Range(0.0, 1.0)) = 0.0
  10.     }
  11.     SubShader
  12.     {
  13.         Tags { "RenderType"="Opaque" "LightMode"="ForwardBase" }
  14.         Cull Off
  15.  
  16.         Pass
  17.         {
  18.             CGPROGRAM
  19.             #pragma vertex vert
  20.             #pragma fragment frag
  21.             #include "UnityCG.cginc"
  22.  
  23.             struct v2f {
  24.                 float4 pos : SV_POSITION;
  25.                 float4 vertex : TEXCOORD1;
  26.                 float2 uv : TEXCOORD0;
  27.             };
  28.  
  29.             fixed4 _LightColor0;
  30.             float _Ambient;
  31.  
  32.             // Compute the diffuse light
  33.             fixed3 diffuseLambert(float3 normal)
  34.             {
  35.                 float diffuse = max(_Ambient, dot(normalize(normal), _WorldSpaceLightPos0.xyz));
  36.                 return _LightColor0.rgb * diffuse;
  37.             }
  38.  
  39.             float _WaveStrength;
  40.             float _WaveSpeed;
  41.  
  42.             // Deformation
  43.             float4 movement(float4 pos, float2 uv) {
  44.                 float sinOff = (pos.x + pos.y + pos.z) * _WaveStrength;
  45.                 float t = _Time.y * _WaveSpeed;
  46.                 float fx = uv.x;
  47.                 float fy = uv.x * uv.y;
  48.                 pos.x += sin(t * 1.45 + sinOff) * fx * 0.5;
  49.                 pos.y = sin(t * 3.12 + sinOff) * fx * 0.5 - fy * 0.9;
  50.                 pos.z -= sin(t * 2.2 + sinOff) * fx * 0.2;
  51.                 return pos;
  52.             }
  53.  
  54.             v2f vert(appdata_base v) {
  55.                 v2f o;
  56.                 o.vertex = v.vertex;
  57.                 o.pos = mul(UNITY_MATRIX_MVP, movement(v.vertex, v.texcoord));
  58.                 o.uv = v.texcoord;
  59.                 return o;
  60.             }
  61.  
  62.             sampler2D _MainTex;
  63.  
  64.             fixed4 frag(v2f i) : SV_Target {
  65.                 fixed4 col = tex2D(_MainTex, i.uv);
  66.  
  67.                 // Compute the new normal;
  68.                 float3 pos0 = movement(float4(i.vertex.x, i.vertex.y, i.vertex.z, i.vertex.w), i.uv).xyz;
  69.                 float3 pos1 = movement(float4(i.vertex.x + 0.01, i.vertex.y, i.vertex.z, i.vertex.w), i.uv).xyz;
  70.                 float3 pos2 = movement(float4(i.vertex.x, i.vertex.y, i.vertex.z + 00.1, i.vertex.w), i.uv).xyz;
  71.  
  72.                 // Normal in model space
  73.                 float3 normal = cross(normalize(pos2 - pos0), normalize(pos1 - pos0));
  74.  
  75.                 // Normal in world space
  76.                 float3 worldNormal = mul(normal, (float3x3) unity_WorldToObject);
  77.  
  78.                 col.rgb *= diffuseLambert(worldNormal);
  79.                 return col;
  80.             }
  81.  
  82.             ENDCG
  83.         }
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement