1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
| Shader "Custom/Plant" { Properties { _Diffuse ("Diffuse", Color) = (1, 1, 1, 1) _Specular ("Specular", Color) = (1, 1, 1, 1) _Gloss ("Gloss", Range(8.0, 256)) = 20 _MainTex ("Albedo (RGB)", 2D) = "white" {} _Bottom("Bottom",Range(-1,0)) = 0.0 _WindStrength("WindStrength",Range(0,45)) = 0.0 _WindDirection("WindDirection",Range(0,360)) = 0.0 _sample("sample (RGB)",2D) = "black" {} _sampleScale("sampleScale",Range(0,100)) = 1.0 _InteractiveRadius("InteractiveRadius",Float) = 1.0 _GlobalPos("GlobalPos",Vector) = (0,0,0) } SubShader { Pass { Tags { "LightMode"="ForwardBase" }
Cull Back
CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_fwdbase
#include "UnityCG.cginc" #include "Lighting.cginc" #include "AutoLight.cginc"
fixed4 _Diffuse; fixed4 _Specular; float _Gloss;
sampler2D _MainTex; sampler2D _sample;
float _WindStrength; float _WindDirection; float _sampleScale;
float _InteractiveRadius; float3 _GlobalPos; float _Bottom;
struct v2f { float4 pos : SV_POSITION; fixed4 uv : TEXCOORD0; fixed4 worldPos : TEXCOORD1; float3 worldNormal : TEXCOORD2; SHADOW_COORDS(3) };
v2f vert(appdata_base v) { v2f o; float4 worldPos = mul(unity_ObjectToWorld, v.vertex); float2 offsetUV = (worldPos.xz * 1 / _sampleScale) * _Time.x; float4 sample = tex2Dlod(_sample, float4(offsetUV, 0, 0));
float rad = _WindDirection * UNITY_PI / 180; float4x4 rot = float4x4( cos(rad),0,sin(rad),0, 0,1,0,0, -sin(rad),0,cos(rad),0, 0,0,0,1 ); sample = mul(rot,sample); float bottom = v.vertex.y - _Bottom; float2 offset = sample.xz * bottom * _WindStrength; v.vertex.xz += offset * 0.1; v.vertex.y -= sample.y * bottom * 0.1 * _WindStrength; float3 newWoldPos = mul(unity_ObjectToWorld, v.vertex); float2 interactiveVec = newWoldPos.xz - _GlobalPos.xz; float dist = length(interactiveVec); float distRatio = clamp(0, 1, dist / _InteractiveRadius); float2 interactiveOffset = normalize(interactiveVec.xy); float2 interactiveOffsetFinal = clamp(0,0.4,interactiveOffset * (1 - distRatio) * bottom); v.vertex.xz += interactiveOffsetFinal; v.vertex.y -= bottom * (1 - distRatio);
o.pos = UnityObjectToClipPos(v.vertex); o.uv = v.texcoord; o.worldPos = worldPos; o.worldNormal = UnityObjectToWorldNormal(v.normal);
TRANSFER_SHADOW(o); return o; }
float4 frag(v2f i) : SV_Target { fixed4 color = tex2D(_MainTex, i.uv);
fixed3 worldNormal = normalize(i.worldNormal); fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
fixed3 diffuse = _LightColor0.rgb * _Diffuse.rgb * max(0, dot(worldNormal, worldLightDir));
fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz); fixed3 halfDir = normalize(worldLightDir + viewDir); fixed3 specular = _LightColor0.rgb * _Specular.rgb * pow(max(0, dot(worldNormal, halfDir)), _Gloss);
fixed atten = 1.0;
fixed shadow = SHADOW_ATTENUATION(i);
return fixed4(color.xyz * (ambient + (diffuse + specular) * atten * shadow), 1.0); } ENDCG }
Pass { Tags { "LightMode" = "ShadowCaster" }
CGPROGRAM #pragma vertex vert #pragma fragment frag #pragma multi_compile_shadowcaster #include "UnityCG.cginc"
struct v2f { V2F_SHADOW_CASTER; };
sampler2D _sample;
float _WindStrength; float _WindDirection; float _sampleScale;
float _InteractiveRadius; float3 _GlobalPos; float _Bottom;
v2f vert(appdata_base v) { v2f o; float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
float2 offsetUV = (worldPos.xz * 1 / _sampleScale) * _Time.x; float4 sample = tex2Dlod(_sample, float4(offsetUV, 0, 0));
float rad = _WindDirection * UNITY_PI / 180; float4x4 rot = float4x4( cos(rad),0,sin(rad),0, 0,1,0,0, -sin(rad),0,cos(rad),0, 0,0,0,1 );
sample = mul(rot,sample);
float bottom = v.vertex.y - _Bottom;
float2 offset = sample.xz * bottom * _WindStrength;
v.vertex.xz += offset * 0.1;
v.vertex.y -= sample.y * bottom * 0.1 * _WindStrength;
float3 newWoldPos = mul(unity_ObjectToWorld, v.vertex); float2 interactiveVec = newWoldPos.xz - _GlobalPos.xz;
float dist = length(interactiveVec); float distRatio = clamp(0, 1, dist / _InteractiveRadius);
float2 interactiveOffset = normalize(interactiveVec.xy); float2 interactiveOffsetFinal = clamp(0,0.4,interactiveOffset * (1 - distRatio) * bottom);
v.vertex.xz += interactiveOffsetFinal; v.vertex.y -= bottom * (1 - distRatio);
TRANSFER_SHADOW_CASTER(o); return o; }
float4 frag(v2f i) : SV_Target { SHADOW_CASTER_FRAGMENT(i) } ENDCG } } FallBack "Diffuse" }
|