-
Notifications
You must be signed in to change notification settings - Fork 16
/
Surface.shader
144 lines (115 loc) · 3.63 KB
/
Surface.shader
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
Shader "Custom/Terrain" {
Properties{
_MainTex("Texture", 2D) = "white" {}
_NormalMap("Normals", 2D) = "bump" {}
[NoScaleOffset] _StateTex("State", 2D) = "black" {}
[NoScaleOffset] _NormalStrength("NormalStrength", Range(0.1, 100)) = 0.5
_BumpScale("_BumpScale", Range(0.1, 100)) = 1
}
SubShader
{
Tags { "Queue"="Geometry" "RenderType"="Opaque" "IgnoreProjector"="True"}
Pass
{
Tags {"LightMode"="ForwardBase" }
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
// compile shader into multiple variants, with and without shadows
// (we don't care about any lightmaps yet, so skip these variants)
#pragma multi_compile_fwdbase nolightmap nodirlightmap nodynlightmap novertexlight multi_compile_fog
#include "AutoLight.cginc"
struct v2f
{
float2 uv : TEXCOORD0;
float2 uvState : TEXCOORD1;
float4 pos : SV_POSITION;
SHADOW_COORDS(2)
UNITY_FOG_COORDS(3)
};
float _NormalStrength;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _NormalMap;
float _BumpScale;
sampler2D _StateTex;
float2 _StateTex_TexelSize;
#define WATER_HEIGHT(s) (s.g)
#define TERRAIN_HEIGHT(s) (s.r)
#define FULL_HEIGHT(s) (TERRAIN_HEIGHT(s) + WATER_HEIGHT(s))
v2f vert(appdata_base v)
{
float4 state = tex2Dlod(_StateTex, float4(v.texcoord.x, v.texcoord.y, 0, 0));
v.vertex.y += TERRAIN_HEIGHT(state);
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord * _MainTex_ST.xy + _MainTex_ST.zw;
o.uvState = v.texcoord;
TRANSFER_SHADOW(o)
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
float2 du = float2(_StateTex_TexelSize.x * 0.5, 0);
float2 dv = float2(0, _StateTex_TexelSize.y * 0.5);
float4 state_l = tex2D(_StateTex, i.uvState + du);
float4 state_r = tex2D(_StateTex, i.uvState - du);
float4 state_t = tex2D(_StateTex, i.uvState + dv);
float4 state_b = tex2D(_StateTex, i.uvState - dv);
half dhdu = _NormalStrength * 0.5 * (TERRAIN_HEIGHT(state_r) - TERRAIN_HEIGHT(state_l));
half dhdv = _NormalStrength * 0.5 * (TERRAIN_HEIGHT(state_b) - TERRAIN_HEIGHT(state_t));
float3 normal = float3(dhdu, 1, dhdv);
float3 bump = UnpackScaleNormal(tex2D(_NormalMap, i.uv), _BumpScale);
float3 worldNormal = UnityObjectToWorldNormal(normalize(normal + bump));
half nl = max(0, dot(worldNormal, _WorldSpaceLightPos0.xyz));
half diff = nl * _LightColor0.rgb;
half ambient = ShadeSH9(half4(worldNormal, 1));
fixed4 col = tex2D(_MainTex, i.uv);
// compute shadow attenuation (1.0 = fully lit, 0.0 = fully shadowed)
fixed shadow = SHADOW_ATTENUATION(i);
// darken light's illumination with shadow, keep ambient intact
fixed3 lighting = diff * shadow + ambient;
col.rgb *= lighting;
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
Pass
{
Tags {"LightMode" = "ShadowCaster"}
LOD 300
ZWrite On
ZTest Less
Offset 1, 1
Cull Off
//ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#pragma fragmentoption ARB_precision_hint_fastest
#include "UnityCG.cginc"
struct v2f {
V2F_SHADOW_CASTER;
};
sampler2D _StateTex;
v2f vert(appdata_base v)
{
float4 state = tex2Dlod(_StateTex, float4(v.texcoord.x, v.texcoord.y, 0, 0));
v.vertex.y += state.r;
v2f o;
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
return o;
}
float4 frag(v2f i) : SV_Target
{
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
}