-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBRDFTranslucentSSS.shader
More file actions
289 lines (250 loc) · 12.1 KB
/
Copy pathBRDFTranslucentSSS.shader
File metadata and controls
289 lines (250 loc) · 12.1 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
Shader "Custom/BRDFTranslucentSSS"
{
Properties
{
_MainTex ("Main Texture", 2D) = "black" { }
[Normal] _NormalTex ("Normal Texture", 2D) = "bump" { }
_BrdfTex ("BRDF Texture", 2D) = "white" { }
_ThicknessTex ("Thickness Map", 2D) = "white" { }
_MainCol ("Main Color", Color) = (1, 1, 1, 1)
_Ambient ("Ambient", Color) = (1, 1, 1, 1)
_TranslucentColor ("Translucent Color", Color) = (1, 1, 1, 1)
_Thickness ("Object Thickness", Float) = 1.0
_Distortion ("Light Distortion", Float) = 1.0
_BrdfShininess ("BRDF Shininess", Float) = 1.0
_Power ("Light Power", Float) = 1.0
_Scale ("Total Light Strength", Float) = 1.0
_LightShininess ("Light Shininess", Float) = 1.0
_SpecCol ("Specular Color", Color) = (1, 1, 1, 1)
_SpecShininess ("Specular Shininess", Float) = 1.0
_SpecShininessScale ("Total Specular Shininess Strength", Float) = 1.0
_BackLightSpecShininess ("Back Light Specular Shininess", Float) = 1.0
[Enum(OFF, 0, FRONT, 1, BACK, 2)] _CullMode ("Cull Mode", int) = 0
}
SubShader
{
Tags { "Queue" = "Transparent" }
LOD 100
Cull[_CullMode]
Pass
{
Tags { "LightMode" = "ForwardBase" }
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile_fwdbase
#pragma multi_compile _ VERTEXLIGHT_ON
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "UnityPBSLighting.cginc"
struct appdata
{
float4 vertex: POSITION;
float2 uv: TEXCOORD0;
float3 normal: NORMAL;
float3 tangent: TANGENT;
};
struct v2f
{
float2 uv: TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex: SV_POSITION;
float3 normal: TEXCOORD2;
float4 wpos: TEXCOORD3;
LIGHTING_COORDS(4, 5)
#if defined(VERTEXLIGHT_ON)
fixed3 vertexLightColor: TEXCOORD6;
#endif
float3 tangent: TEXCOORD7;
float3 binormal: TEXCOORD8;
};
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
uniform sampler2D _NormalTex; uniform float4 _NormalTex_ST;
uniform sampler2D _BrdfTex; uniform float4 _BrdfTex_ST;
uniform sampler2D _ThicknessTex; uniform float4 _ThicknessTex_ST;
uniform fixed4 _MainCol;
uniform fixed4 _Ambient;
uniform fixed4 _TranslucentColor;
uniform float _Thickness;
uniform float _Distortion;
uniform float _BrdfShininess;
uniform float _Power;
uniform float _Scale;
uniform float _LightShininess;
uniform fixed4 _SpecCol;
uniform float _SpecShininess;
uniform float _SpecShininessScale;
uniform float _BackLightSpecShininess;
void ComputeVertexLightColor(inout v2f i)
{
#if defined(VERTEXLIGHT_ON)
i.vertexLightColor = Shade4PointLights(
unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0,
unity_LightColor[0].rgb, unity_LightColor[1].rgb,
unity_LightColor[2].rgb, unity_LightColor[3].rgb,
unity_4LightAtten0, i.wpos, i.normal
);
#endif
}
v2f vert(appdata v)
{
v2f o = (v2f)0;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.normal = UnityObjectToWorldNormal(v.normal);
o.wpos = mul(unity_ObjectToWorld, v.vertex);
o.tangent = UnityObjectToWorldNormal(v.tangent);
o.binormal = normalize(cross(o.tangent, o.normal));
ComputeVertexLightColor(o);
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
fixed4 frag(v2f i): SV_Target
{
float3 tangentNormal = float4(UnpackNormal(tex2D(_NormalTex, i.uv)), 1);
float3x3 TBN = float3x3(i.tangent, i.binormal, i.normal);
TBN = transpose(TBN);
float3 worldNormal = mul(TBN, tangentNormal);
float3 normal = lerp(i.normal, worldNormal, saturate(length(tangentNormal) * 100));
fixed4 mainTex = tex2D(_MainTex, i.uv);
float3 lightDir;
#if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT)
lightDir = normalize(_WorldSpaceLightPos0.xyz - i.wpos.xyz);
#else
lightDir = _WorldSpaceLightPos0.xyz;
#endif
fixed4 lightCol;
#if defined(VERTEXLIGHT_ON)
lightCol = fixed4(i.vertexLightColor, 1);
#else
lightCol = _LightColor0;
#endif
lightCol.rgb += max(0, ShadeSH9(float4(normal, 1)));
float3 viewDir = normalize(_WorldSpaceCameraPos - i.wpos).xyz;
float VdotL = saturate(dot(viewDir, lightDir));
float3 a = lightDir + normal * max(0, _Distortion);
float b = pow(saturate(dot(viewDir, -a)), max(0, _Power)) * max(0, _Scale);
UNITY_LIGHT_ATTENUATION(atten, i, i.wpos.xyz);
float thickness = pow(tex2D(_ThicknessTex, i.uv).r, max(0, _Thickness));
float3 c = atten * (b + _Ambient.rgb) * thickness;
float NdotL = saturate(dot(normal, lightDir));
//NdotL = length(lightDir) != 0 ? (NdotL * 0.5) + 0.5 : 0;
NdotL = (NdotL * 0.5) + 0.5;
fixed4 col = lerp(_MainCol, mainTex, saturate(length(mainTex) * 100));
col.rgb *= NdotL;
float NdotV = saturate(dot(normal, viewDir));
float2 brdfUV = float2(NdotV, NdotL);
float3 brdf = tex2D(_BrdfTex, brdfUV.xy).rgb;
float3 specRef = atten * _LightColor0.rgb * _SpecCol.rgb * pow(max(0, dot(reflect(-lightDir, normal), viewDir)), 10 * max(0, _SpecShininess)) * 10 * max(0, _SpecShininessScale) * VdotL;
specRef += atten * _LightColor0.rgb * _SpecCol.rgb * pow(max(0, dot(reflect(-lightDir, normal), viewDir)), max(0, _SpecShininess)) * max(0, _SpecShininessScale) * b * 0.1 * max(0, _BackLightSpecShininess);
col.rgb += lerp(fixed3(0, 0, 0), lightCol.rgb * max(0, _LightShininess) * _TranslucentColor, smoothstep(0, 1, thickness)) * lerp(NdotL, c, smoothstep(0, 1, 1 - dot(normal, lightDir))) * brdf * max(0, _BrdfShininess) + specRef;
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
Pass
{
Tags { "LightMode" = "ForwardAdd" }
Blend One One
ZWrite Off
CGPROGRAM
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#pragma multi_compile_fwdadd
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#include "UnityPBSLighting.cginc"
struct appdata
{
float4 vertex: POSITION;
float2 uv: TEXCOORD0;
float3 normal: NORMAL;
float3 tangent: TANGENT;
};
struct v2f
{
float2 uv: TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex: SV_POSITION;
float3 normal: TEXCOORD2;
float4 wpos: TEXCOORD3;
LIGHTING_COORDS(4, 5)
float3 tangent: TEXCOORD6;
float3 binormal: TEXCOORD7;
};
uniform sampler2D _MainTex; uniform float4 _MainTex_ST;
uniform sampler2D _NormalTex; uniform float4 _NormalTex_ST;
uniform sampler2D _BrdfTex; uniform float4 _BrdfTex_ST;
uniform sampler2D _ThicknessTex; uniform float4 _ThicknessTex_ST;
uniform fixed4 _MainCol;
uniform fixed4 _Ambient;
uniform fixed4 _TranslucentColor;
uniform float _Thickness;
uniform float _Distortion;
uniform float _BrdfShininess;
uniform float _Power;
uniform float _Scale;
uniform float _LightShininess;
uniform fixed4 _SpecCol;
uniform float _SpecShininess;
uniform float _SpecShininessScale;
uniform float _BackLightSpecShininess;
v2f vert(appdata v)
{
v2f o = (v2f)0;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
o.normal = UnityObjectToWorldNormal(v.normal);
o.wpos = mul(unity_ObjectToWorld, v.vertex);
o.tangent = UnityObjectToWorldNormal(v.tangent);
o.binormal = normalize(cross(o.tangent, o.normal));
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
fixed4 frag(v2f i): SV_Target
{
float3 tangentNormal = float4(UnpackNormal(tex2D(_NormalTex, i.uv)), 1);
float3x3 TBN = float3x3(i.tangent, i.binormal, i.normal);
TBN = transpose(TBN);
float3 worldNormal = mul(TBN, tangentNormal);
float3 normal = lerp(i.normal, worldNormal, saturate(length(tangentNormal) * 100));
fixed4 mainTex = tex2D(_MainTex, i.uv);
float3 lightDir;
#if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT)
lightDir = normalize(_WorldSpaceLightPos0.xyz - i.wpos.xyz);
#else
lightDir = _WorldSpaceLightPos0.xyz;
#endif
fixed4 lightCol = _LightColor0;
lightCol.rgb += max(0, ShadeSH9(float4(normal, 1)));
float3 viewDir = normalize(_WorldSpaceCameraPos - i.wpos).xyz;
float VdotL = saturate(dot(viewDir, lightDir));
float3 a = lightDir + normal * max(0, _Distortion);
float b = pow(saturate(dot(viewDir, -a)), max(0, _Power)) * max(0, _Scale);
UNITY_LIGHT_ATTENUATION(atten, i, i.wpos.xyz);
float thickness = pow(tex2D(_ThicknessTex, i.uv).r, max(0, _Thickness));
float3 c = atten * (b + _Ambient.rgb) * thickness;
float NdotL = saturate(dot(normal, lightDir));
//NdotL = length(lightDir) != 0 ? (NdotL * 0.5) + 0.5 : 0;
NdotL = (NdotL * 0.5) + 0.5;
fixed4 col = lerp(_MainCol, mainTex, saturate(length(mainTex) * 100));
col.rgb *= NdotL;
float NdotV = saturate(dot(normal, viewDir));
float2 brdfUV = float2(NdotV, NdotL);
float3 brdf = tex2D(_BrdfTex, brdfUV.xy).rgb;
float3 specRef = atten * _LightColor0.rgb * _SpecCol.rgb * pow(max(0, dot(reflect(-lightDir, normal), viewDir)), 10 * max(0, _SpecShininess)) * 10 * max(0, _SpecShininessScale) * VdotL;
specRef += atten * _LightColor0.rgb * _SpecCol.rgb * pow(max(0, dot(reflect(-lightDir, normal), viewDir)), max(0, _SpecShininess)) * max(0, _SpecShininessScale) * b * 0.1 * max(0, _BackLightSpecShininess);
col.rgb += lerp(fixed3(0, 0, 0), lightCol.rgb * max(0, _LightShininess) * _TranslucentColor, smoothstep(0, 1, thickness)) * lerp(NdotL, c, smoothstep(0, 1, 1 - dot(normal, lightDir))) * brdf * max(0, _BrdfShininess) + specRef;
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
Fallback "Diffuse"
}