forked from przemyslawzaworski/Unity3D-CG-programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disney.shader
147 lines (124 loc) · 5.06 KB
/
disney.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
145
146
147
// Direct implementation from: https://github.com/wdas/brdf/blob/master/src/brdfs/disney.brdf
// but still requires improvements...
Shader "Disney"
{
Properties
{
metallic ("Metallic", Range (0.0,1.0)) = 0.0
subsurface ("Subsurface", Range (0.0,1.0)) = 0.0
_specular ("Specular", Range (0.0,1.0)) = 0.0
roughness ("Roughness", Range (0.0,1.0)) = 0.5
specularTint ("SpecularTint", Range (0.0,1.0)) = 0.0
anisotropic ("Anisotropic", Range (0.0,1.0)) = 0.0
sheen ("Sheen", Range (0.0,1.0)) = 0.0
sheenTint ("SheenTint", Range (0.0,1.0)) = 0.5
clearcoat ("Clearcoat", Range (0.0,1.0)) = 0.0
clearcoatGloss ("ClearcoatGloss", Range (0.0,1.0)) = 1.0
}
SubShader
{
Pass
{
CGPROGRAM
#pragma vertex VSMain
#pragma fragment PSMain
static const float3 baseColor = float3(1,1,1);
float metallic, subsurface, _specular, roughness, specularTint, anisotropic, sheen,
sheenTint, clearcoat, clearcoatGloss;
static const float PI = 3.14159265358979323846;
float sqr(float x) { return x*x; }
float SchlickFresnel(float u)
{
float m = clamp(1-u, 0, 1);
float m2 = m*m;
return m2*m2*m; // pow(m,5)
}
float GTR1(float NdotH, float a)
{
if (a >= 1) return 1/PI;
float a2 = a*a;
float t = 1 + (a2-1)*NdotH*NdotH;
return (a2-1) / (PI*log(a2)*t);
}
float GTR2(float NdotH, float a)
{
float a2 = a*a;
float t = 1 + (a2-1)*NdotH*NdotH;
return a2 / (PI * t*t);
}
float GTR2_aniso(float NdotH, float HdotX, float HdotY, float ax, float ay)
{
return 1 / (PI * ax*ay * sqr( sqr(HdotX/ax) + sqr(HdotY/ay) + NdotH*NdotH ));
}
float smithG_GGX(float NdotV, float alphaG)
{
float a = alphaG*alphaG;
float b = NdotV*NdotV;
return 1 / (NdotV + sqrt(a + b - a*b));
}
float smithG_GGX_aniso(float NdotV, float VdotX, float VdotY, float ax, float ay)
{
return 1 / (NdotV + sqrt( sqr(VdotX*ax) + sqr(VdotY*ay) + sqr(NdotV) ));
}
float3 mon2lin(float3 x)
{
return float3(pow(x[0], 2.2), pow(x[1], 2.2), pow(x[2], 2.2));
}
float3 BRDF( float3 L, float3 V, float3 N, float3 X, float3 Y )
{
float NdotL = max(dot(N,L),0.0);
float NdotV = max(dot(N,V),0.0);
float3 H = normalize(L+V);
float NdotH = max(dot(N,H),0.0);
float LdotH = max(dot(L,H),0.0);
float3 Cdlin = mon2lin(baseColor);
float Cdlum = .3*Cdlin[0] + .6*Cdlin[1] + .1*Cdlin[2]; // luminance approx.
float3 Ctint = Cdlum > 0 ? Cdlin/Cdlum : float3(1,1,1); // normalize lum. to isolate hue+sat
float3 Cspec0 = lerp(_specular*.08*lerp(float3(1,1,1), Ctint, specularTint), Cdlin, metallic);
float3 Csheen = lerp(float3(1,1,1), Ctint, sheenTint);
// Diffuse fresnel - go from 1 at normal incidence to .5 at grazing
// and lerp in diffuse retro-reflection based on roughness
float FL = SchlickFresnel(NdotL), FV = SchlickFresnel(NdotV);
float Fd90 = 0.5 + 2 * LdotH*LdotH * roughness;
float Fd = lerp(1.0, Fd90, FL) * lerp(1.0, Fd90, FV);
// Based on Hanrahan-Krueger brdf approximation of isotropic bssrdf
// 1.25 scale is used to (roughly) preserve albedo
// Fss90 used to "flatten" retroreflection based on roughness
float Fss90 = LdotH*LdotH*roughness;
float Fss = lerp(1.0, Fss90, FL) * lerp(1.0, Fss90, FV);
float ss = 1.25 * (Fss * (1 / (NdotL + NdotV) - .5) + .5);
// specular
float aspect = sqrt(1-anisotropic*.9);
float ax = max(.001, sqr(roughness)/aspect);
float ay = max(.001, sqr(roughness)*aspect);
float Ds = GTR2_aniso(NdotH, dot(H, X), dot(H, Y), ax, ay);
float FH = SchlickFresnel(LdotH);
float3 Fs = lerp(Cspec0, float3(1,1,1), FH);
float Gs = smithG_GGX_aniso(NdotL, dot(L, X), dot(L, Y), ax, ay);
Gs *= smithG_GGX_aniso(NdotV, dot(V, X), dot(V, Y), ax, ay);
// sheen
float3 Fsheen = FH * sheen * Csheen;
// clearcoat (ior = 1.5 -> F0 = 0.04)
float Dr = GTR1(NdotH, lerp(.1,.001,clearcoatGloss));
float Fr = lerp(.04, 1.0, FH);
float Gr = smithG_GGX(NdotL, .25) * smithG_GGX(NdotV, .25);
return ((1/PI) * lerp(Fd, ss, subsurface)*Cdlin + Fsheen) * (1-metallic) + Gs*Fs*Ds + .25*clearcoat*Gr*Fr*Dr;
}
void VSMain (inout float4 vertex:POSITION, inout float2 uv:TEXCOORD0, inout float3 normal:NORMAL, inout float4 tangent:TANGENT, out float3 world:TEXCOORD1)
{
world = mul(unity_ObjectToWorld, vertex).xyz;
vertex = UnityObjectToClipPos(vertex);
}
float4 PSMain (float4 vertex:POSITION, float2 uv:TEXCOORD0, float3 normal:NORMAL, float4 tangent:TANGENT, float3 world:TEXCOORD1) : SV_TARGET
{
float3 LightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - world,_WorldSpaceLightPos0.w));
float3 NormalDirection = normalize(mul((float3x3)unity_ObjectToWorld,normal));
float3 ViewDirection = normalize( _WorldSpaceCameraPos.xyz - world);
float3 WorldTangent = mul((float3x3)unity_ObjectToWorld,tangent.xyz);
float3 WorldBinormal = cross(NormalDirection,WorldTangent)*tangent.w;
return float4(BRDF( LightDirection, ViewDirection, NormalDirection, WorldTangent, WorldBinormal ), 1.0);
}
ENDCG
}
}
}