Ice.shader
5.93 KB
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
Shader "Custom/Ice" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_FresnelColor ("_FresnelColor", Color) = (1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_Glossiness ("Smoothness", Range(0,1)) = 0.5
_Metallic ("Metallic", Range(0,1)) = 0.0
_BumpAmt ("Distortion", range (0,1000)) = 10
_BumpMap ("Normalmap", 2D) = "bump" {}
_Fresnel ("Fresnel", Range(1.0,12.0)) = 3.0
_marchDistance ("March Distance", Float) = 3.0
_numSteps ("Steps", Float) = 4.0
_Ramp ("Ramp", 2D) = "white" {}
_InnerRamp ("_InnerRamp", 2D) = "white" {}
}
SubShader {
Tags { "Queue"="Transparent" "RenderType"="Transparent" }
LOD 200
GrabPass {
Name "BASE"
Tags { "LightMode" = "Always" }
}
Pass {
Name "BASE"
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata_t {
float4 vertex : POSITION;
float2 texcoord: TEXCOORD0;
};
struct v2f {
float4 vertex : SV_POSITION;
float4 uvgrab : TEXCOORD0;
float2 uvbump : TEXCOORD1;
float2 uvmain : TEXCOORD2;
UNITY_FOG_COORDS(3)
};
float _BumpAmt;
float4 _BumpMap_ST;
float4 _MainTex_ST;
v2f vert (appdata_t v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
#if UNITY_UV_STARTS_AT_TOP
float scale = -1.0;
#else
float scale = 1.0;
#endif
o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
o.uvgrab.zw = o.vertex.zw;
o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
sampler2D _GrabTexture;
float4 _GrabTexture_TexelSize;
sampler2D _BumpMap;
sampler2D _MainTex;
half4 frag (v2f i) : SV_Target
{
// calculate perturbed coordinates
half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Z
float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
i.uvgrab.xy = offset + i.uvgrab.xy;
half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
//half4 tint = tex2D(_MainTex, i.uvmain);
//col *= tint;
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
//Blend SrcBlend OneMinusSrcAlpha
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
//#pragma surface surf Standard alpha:fade fullforwardshadows
#pragma surface surf RampSpec alpha:fade fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
#include "UnityPBSLighting.cginc"
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _Ramp, _InnerRamp;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 viewDir;
};
half _Glossiness;
half _Metallic;
fixed4 _Color;
float _Fresnel;
fixed3 _FresnelColor;
fixed _marchDistance, _numSteps;
half3 RampShading (float3 normal, half3 lightDir, half3 viewDir, half3 lightCol) {
half NdotL = dot (normal, lightDir);
half diff = NdotL * 0.5 + 0.5;
half3 ramp = tex2D (_Ramp, float2(diff, 0.5)).rgb;
half3 c;
c.rgb = ramp * lightCol;
return c;
}
half4 LightingRampSpec(SurfaceOutputStandardSpecular s, half3 viewDir, UnityGI gi)
{
s.Normal = normalize(s.Normal);
// energy conservation
half oneMinusReflectivity;
s.Albedo = EnergyConservationBetweenDiffuseAndSpecular(s.Albedo, s.Specular, /*out*/ oneMinusReflectivity);
// shader relies on pre-multiply alpha-blend (_SrcBlend = One, _DstBlend = OneMinusSrcAlpha)
// this is necessary to handle transparency in physically correct way - only diffuse component gets affected by alpha
half outputAlpha;
s.Albedo = PreMultiplyAlpha(s.Albedo, s.Alpha, oneMinusReflectivity, /*out*/ outputAlpha);
half4 c = UNITY_BRDF_PBS(s.Albedo, s.Specular, oneMinusReflectivity, s.Smoothness, s.Normal, viewDir, gi.light, gi.indirect);
c.rgb += RampShading(s.Normal, gi.light.dir, viewDir, gi.light.color) * _Color;
//c.rgb += SubsurfaceShadingSimple(_InternalColor, s.Normal, viewDir, s.Alpha*_SSS, gi.light.dir, gi.light.color);
c.a = outputAlpha;
return c;
}
inline void LightingRampSpec_GI(
SurfaceOutputStandardSpecular s,
UnityGIInput data,
inout UnityGI gi)
{
#if defined(UNITY_PASS_DEFERRED) && UNITY_ENABLE_REFLECTION_BUFFERS
gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal);
#else
Unity_GlossyEnvironmentData g = UnityGlossyEnvironmentSetup(s.Smoothness, data.worldViewDir, s.Normal, s.Specular);
gi = UnityGlobalIllumination(data, s.Occlusion, s.Normal, g);
#endif
}
void surf(Input IN, inout SurfaceOutputStandardSpecular o) {
// Albedo comes from a texture tinted by color
fixed4 c = _Color;
//half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg;
//Inner structure parallax
float3 InnerStructure = float3(0, 0, 0);
float2 UV = IN.uv_MainTex;
float offset = 1;
for (float d = 0.0; d < _marchDistance; d += _marchDistance / _numSteps)
{
UV -= (IN.viewDir*d)/_numSteps * tex2D (_MainTex, IN.uv_MainTex).g;
float4 Ldensity = tex2D(_MainTex, UV).r;
InnerStructure += saturate(Ldensity[0])*tex2D(_InnerRamp, float2(1/_numSteps * offset, 0.5));
offset ++;
}
// Metallic and smoothness come from slider variables
o.Normal = UnpackScaleNormal (tex2D (_BumpMap, IN.uv_BumpMap), 0.2);
half rim = saturate(dot (normalize(IN.viewDir), o.Normal));
half rim2 = 1 - saturate(dot (normalize(IN.viewDir), o.Normal));
float fresnel = pow(rim2, _Fresnel);// + pow(rim2, _Fresnel);
o.Alpha = clamp(c.a + fresnel + InnerStructure, 0, 1) + 0.2;
o.Albedo = _Color + InnerStructure * _FresnelColor;
o.Specular = _Metallic;
o.Smoothness = _Glossiness;
//o.Emission = SubsurfaceShadingSimple(_InternalColor, o.Normal, IN.viewDir, c.a*_SSS, IN.lightDir, _LightColor0);
}
ENDCG
}
FallBack "Diffuse"
}