How can I use #ifdef DIRECTIONAL in surf + vert shader?
What conditions?
M.b. some .cgiinc or .include or HLSLPROGRAM only etc
here is full shader
Shader "mitay/cutout tree" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.1, 0.1, 0.1, 1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_BumpMap ("Bump (RGB)", 2D) = "bump" {}
_Smoothness ("Smoothness", Range(0.001,1)) = 1
_Cutoff ("Alpha cutoff", Range(0.25,0.9)) = 0.5
[MaterialToggle] _isToggled("ShakeDirection1", Float) = 0
[MaterialToggle] _isToggled2("ShakeDirec tion2", Float) = 0
_ShakeDisplacement ("Displacement", Range (0, 1.0)) = 1.0
_ShakeTime ("Shake Time", Range (0, 1.0)) = 1.0
_ShakeWindspeed ("Shake Windspeed", Range (0, 1.0)) = 1.0
_ShakeBending ("Shake Bending", Range (0, 1.0)) = 0.2
// These are here only to provide default values
[HideInInspector] _TreeInstanceColor ("TreeInstanceColor", Vector) = (1,1,1,1)
[HideInInspector] _TreeInstanceScale ("TreeInstanceScale", Vector) = (1,1,1,1)
[HideInInspector] _SquashAmount ("Squash", Float) = 1
}
SubShader {
Tags { "RenderType"="TreeTransparentCutout" }
LOD 200
Cull Off
CGPROGRAM
// add "addshadow" to let unity know you're displacing verts
// this will ensure their ShadowCaster + ShadowCollector passes use the vert function and have the correct positions
#pragma surface surf BlinnPhong fullforwardshadows vertex:vert addshadow alphatest:_Cutoff
//#include "UnityBuiltin2xTreeLibrary.cginc"
#pragma target 3.0
float _isToggled;
float _isToggled2;
sampler2D _MainTex;
sampler2D _BumpMap;
fixed4 _Color;
half _Smoothness;
half _Glossiness;
half _Speed;
half _Amount;
half _Distance;
float _ShakeDisplacement;
float _ShakeTime;
float _ShakeWindspeed;
float _ShakeBending;
fixed4 _TreeInstanceColor;
float4 _TreeInstanceScale;
float4x4 _TerrainEngineBendTree;
float4 _SquashPlaneNormal;
float _SquashAmount;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
fixed4 LightingNormalizedBlinnPhong (SurfaceOutput s, fixed3 lightDir, fixed3 halfDir, fixed atten)
{
// TODO: conditional normalization using ifdef
fixed3 nN = normalize(s.Normal);
fixed diff = max( 0, dot(nN, lightDir) );
fixed nh = max( 0, dot(nN, halfDir) );
fixed spec = pow(nh, s.Specular*128) * s.Gloss;
fixed4 c;
c.rgb = _LightColor0.rgb * (s.Albedo * diff + spec) * atten;
UNITY_OPAQUE_ALPHA(c.a);
return c;
}
void FastSinCos (float4 val, out float4 s, out float4 c) {
val = val * 6.408849 - 3.1415927;
float4 r5 = val * val;
float4 r6 = r5 * r5;
float4 r7 = r6 * r5;
float4 r8 = r6 * r5;
float4 r1 = r5 * val;
float4 r2 = r1 * r5;
float4 r3 = r2 * r5;
float4 sin7 = {1, -0.16161616, 0.0083333, -0.00019841} ;
float4 cos8 = {-0.5, 0.041666666, -0.0013888889, 0.000024801587} ;
s = val + r1 * sin7.y + r2 * sin7.z + r3 * sin7.w;
c = 1 + r5 * cos8.x + r6 * cos8.y + r7 * cos8.z + r8 * cos8.w;
}
inline float4 Squash(in float4 pos)
{
// To squash the tree the vertex needs to be moved in the direction
// of the squash plane. The plane is defined by the the:
// plane point - point lying on the plane, defined in model space
// plane normal - _SquashPlaneNormal.xyz
// we're pushing squashed tree plane in direction of planeNormal by amount of _SquashPlaneNormal.w
// this squashing has to match logic of tree billboards
float3 planeNormal = _SquashPlaneNormal.xyz;
// unoptimized version:
//float3 planePoint = -planeNormal * _SquashPlaneNormal.w;
//float3 projectedVertex = pos.xyz + dot(planeNormal, (planePoint - pos)) * planeNormal;
// optimized version:
float3 projectedVertex = pos.xyz - (dot(planeNormal.xyz, pos.xyz) + _SquashPlaneNormal.w) * planeNormal;
pos = float4(lerp(projectedVertex, pos.xyz, _SquashAmount), 1);
return pos;
}
void TerrainAnimateTree( inout float4 pos, float alpha )
{
pos.xyz *= _TreeInstanceScale.xyz;
float3 bent = mul(_TerrainEngineBendTree, float4(pos.xyz, 0.0)).xyz;
pos.xyz = lerp( pos.xyz, bent, alpha );
pos = Squash(pos);
}
void vert (inout appdata_full v) {
float factor = (1 - _ShakeDisplacement) * 0.5;
const float _WindSpeed = (_ShakeWindspeed);
const float _WaveScale = _ShakeDisplacement;
const float4 _waveXSize = float4(0.048, 0.06, 0.24, 0.096);
const float4 _waveZSize = float4 (0.024, .08, 0.08, 0.2);
const float4 waveSpeed = float4 (1.2, 2, 1.6, 4.8);
float4 _waveXmove = float4(0.024, 0.04, -0.12, 0.096);
float4 _waveZmove = float4 (0.006, .02, -0.02, 0.1);
float4 waves;
waves = v.vertex.x * _waveXSize;
waves += v.vertex.z * _waveZSize;
waves += _Time.x * (1 - _ShakeTime * 2 - v.color.b ) * waveSpeed *_WindSpeed;
float4 s, c;
waves = frac (waves);
FastSinCos (waves, s,c);
float waveAmount = 1;
if (_isToggled > 0)
waveAmount = v.texcoord.y * (v.color.a + _ShakeBending);
else
waveAmount = v.texcoord.x * (v.color.a + _ShakeBending);
s *= waveAmount;
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)unity_WorldToObject, waveMove).xz;
v.color *= _TreeInstanceColor;
float3 viewpos = mul(UNITY_MATRIX_MV, v.vertex);
#ifdef DIRECTIONAL
float3 viewpos = mul(UNITY_MATRIX_MV, v.vertex);
float4 lightDir = 0;
float4 lightColor = 0;
lightDir.w = _AO;
float4 light = UNITY_LIGHTMODEL_AMBIENT;
for (int i = 0; i < 4; i++) {
float atten = 1.0;
#ifdef USE_CUSTOM_LIGHT_DIR
lightDir.xyz = _TerrainTreeLightDirections[i];
lightColor = _TerrainTreeLightColors[i];
#else
float3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w;
toLight.z *= -1.0;
lightDir.xyz = mul( (float3x3)unity_CameraToWorld, normalize(toLight) );
float lengthSq = dot(toLight, toLight);
atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z);
lightColor.rgb = unity_LightColor[i].rgb;
#endif
lightDir.xyz *= _Occlusion;
float occ = dot (v.tangent, lightDir);
occ = max(0, occ);
occ += _BaseLight;
light += lightColor * (occ * atten);
}
v.color = light * _Color.rgb * _TreeInstanceColor;
#endif
TerrainAnimateTree(v.vertex, v.color.w);
}
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 tex = tex2D (_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = tex.rgb ;
o.Gloss = tex.a;
o.Alpha = tex.a * _Color.a;
o.Specular = _Smoothness;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
Dependency "BillboardShader" = "Hidden/Nature/Tree Soft Occlusion Leaves Rendertex"
FallBack "Diffuse"
}
↧