summaryrefslogtreecommitdiff
path: root/VRCSDK3AvatarsLegacy/Assets/Avatars/Auri/materials/OptimizedShaders/nails/Includes/CGI_PoiBRDF.cginc
blob: 1c2aacd3cf210e794aa2257ab022c9ede361624b (plain)
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
#ifndef POI_BRDF
    #define POI_BRDF
    
    /*
    * MIT License
    *
    * Copyright (c) 2020 Xiexe
    *
    * Permission is hereby granted, free of charge, to any person obtaining a copy
    * of this software and associated documentation files (the "Software"), to deal
    * in the Software without restriction, including without limitation the rights
    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    * copies of the Software, and to permit persons to whom the Software is
    * furnished to do so, subject to the following conditions:
    *
    * The above copyright notice and this permission notice shall be included in all
    * copies or substantial portions of the Software.
    *
    * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    * SOFTWARE.
    */
    
    #if defined(PROP_BRDFMETALLICGLOSSMAP) || !defined(OPTIMIZER_ENABLED)
        POI_TEXTURE_NOSAMPLER(_BRDFMetallicGlossMap);
    #endif
    #if defined(PROP_BRDFSPECULARMAP) || !defined(OPTIMIZER_ENABLED)
        POI_TEXTURE_NOSAMPLER(_BRDFSpecularMap);
    #endif
    #if defined(PROP_BRDFMETALLICMAP) || !defined(OPTIMIZER_ENABLED)
        POI_TEXTURE_NOSAMPLER(_BRDFMetallicMap);
    #endif
    
    samplerCUBE _BRDFFallback;
    
    float _BRDFMetallic;
    float _BRDFGlossiness;
    float _BRDFReflectance;
    float _BRDFAnisotropy;
    float _BRDFReflectionsEnabled;
    float _BRDFSpecularEnabled;
    float _BRDFInvertGlossiness;
    float _BRDFForceFallback;
    
    bool DoesReflectionProbeExist()
    {
        float4 envSample = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, poiCam.reflectionDir, UNITY_SPECCUBE_LOD_STEPS);
        bool probeExists = !(unity_SpecCube0_HDR.a == 0 && envSample.a == 0);
        return probeExists && !float(0);
    }
    
    float getGeometricSpecularAA(float3 normal)
    {
        float3 vNormalWsDdx = ddx(normal.xyz);
        float3 vNormalWsDdy = ddy(normal.xyz);
        float flGeometricRoughnessFactor = pow(saturate(max(dot(vNormalWsDdx.xyz, vNormalWsDdx.xyz), dot(vNormalWsDdy.xyz, vNormalWsDdy.xyz))), 0.333);
        return max(0, flGeometricRoughnessFactor);
    }
    
    float3 getAnisotropicReflectionVector(float3 viewDir, float3 bitangent, float3 tangent, float3 normal, float roughness, float anisotropy)
    {
        float3 anisotropicDirection = anisotropy >= 0.0 ? bitangent: tangent;
        float3 anisotropicTangent = cross(anisotropicDirection, viewDir);
        float3 anisotropicNormal = cross(anisotropicTangent, anisotropicDirection);
        float bendFactor = abs(anisotropy) * saturate(5.0 * roughness);
        float3 bentNormal = normalize(lerp(normal, anisotropicNormal, bendFactor));
        return reflect(-viewDir, bentNormal);
    }
    
    float3 F_Schlick(float u, float3 f0)
    {
        return f0 + (1.0 - f0) * pow(1.0 - u, 5.0);
    }
    
    float3 F_Schlick(const float3 f0, float f90, float VoH)
    {
        // Schlick 1994, "An Inexpensive BRDF Model for Physically-Based Rendering"
        float pow5 = 1.0 - VoH;
        pow5 = pow5 * pow5 * pow5 * pow5 * pow5;
        return f0 + (f90 - f0) * pow5;
    }
    
    float D_GGX(float NoH, float roughness)
    {
        float a2 = roughness * roughness;
        float f = (NoH * a2 - NoH) * NoH + 1.0;
        return a2 / (UNITY_PI * f * f);
    }
    
    float V_SmithGGXCorrelated(float NoV, float NoL, float a)
    {
        float a2 = a * a;
        float GGXL = NoV * sqrt((-NoL * a2 + NoL) * NoL + a2);
        float GGXV = NoL * sqrt((-NoV * a2 + NoV) * NoV + a2);
        return 0.5 / (GGXV + GGXL);
    }
    
    float D_GGX_Anisotropic(float NoH, const float3 h, const float3 t, const float3 b, float at, float ab)
    {
        float ToH = dot(t, h);
        float BoH = dot(b, h);
        float a2 = at * ab;
        float3 v = float3(ab * ToH, at * BoH, a2 * NoH);
        float v2 = dot(v, v);
        float w2 = a2 / v2;
        return a2 * w2 * w2 * (1.0 / UNITY_PI);
    }
    
    float3 getBoxProjection(float3 direction, float3 position, float4 cubemapPosition, float3 boxMin, float3 boxMax)
    {
        // #if defined(UNITY_SPECCUBE_BOX_PROJECTION) // For some reason this doesn't work?
        if (cubemapPosition.w > 0)
        {
            float3 factors = ((direction > 0 ? boxMax: boxMin) - position) / direction;
            float scalar = min(min(factors.x, factors.y), factors.z);
            direction = direction * scalar + (position - cubemapPosition.xyz);
        }
        // #endif
        return direction;
    }
    
    float3 getDirectSpecular(float roughness, float ndh, float vdn, float ndl, float ldh, float3 f0, float3 halfVector, float3 tangent, float3 bitangent, float anisotropy)
    {
        #if !defined(LIGHTMAP_ON)
            float rough = max(roughness * roughness, 0.0045);
            float Dn = D_GGX(ndh, rough);
            float3 F = F_Schlick(ldh, f0);
            float V = V_SmithGGXCorrelated(vdn, ndl, rough);
            float3 directSpecularNonAniso = max(0, (Dn * V) * F);
            
            anisotropy *= saturate(5.0 * roughness);
            float at = max(rough * (1.0 + anisotropy), 0.001);
            float ab = max(rough * (1.0 - anisotropy), 0.001);
            float D = D_GGX_Anisotropic(ndh, halfVector, tangent, bitangent, at, ab);
            float3 directSpecularAniso = max(0, (D * V) * F);
            
            return lerp(directSpecularNonAniso, directSpecularAniso, saturate(abs(float(0) * 100))) * 3; // * 100 to prevent blending, blend because otherwise tangents are fucked on lightmapped object
        #else
            return 0;
        #endif
    }
    
    float3 getIndirectSpecular(float metallic, float roughness, float3 reflDir, float3 worldPos, float3 lightmap, float3 normal)
    {
        float3 spec = float3(0, 0, 0);
        #if defined(UNITY_PASS_FORWARDBASE)
            float3 indirectSpecular;
            Unity_GlossyEnvironmentData envData;
            envData.roughness = roughness;
            envData.reflUVW = getBoxProjection(
                reflDir, worldPos,
                unity_SpecCube0_ProbePosition,
                unity_SpecCube0_BoxMin.xyz, unity_SpecCube0_BoxMax.xyz
            );
            float3 probe0 = Unity_GlossyEnvironment(UNITY_PASS_TEXCUBE(unity_SpecCube0), unity_SpecCube0_HDR, envData);
            float interpolator = unity_SpecCube0_BoxMin.w;
            
            if (interpolator < 0.99999)
            {
                envData.reflUVW = getBoxProjection(
                    reflDir, worldPos,
                    unity_SpecCube1_ProbePosition,
                    unity_SpecCube1_BoxMin.xyz, unity_SpecCube1_BoxMax.xyz
                );
                float3 probe1 = Unity_GlossyEnvironment(UNITY_PASS_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0), unity_SpecCube0_HDR, envData);
                indirectSpecular = lerp(probe1, probe0, interpolator);
            }
            else
            {
                indirectSpecular = probe0;
            }
            
            if(!DoesReflectionProbeExist())
            {
                indirectSpecular = texCUBElod(_BRDFFallback, float4(envData.reflUVW, roughness * UNITY_SPECCUBE_LOD_STEPS)).rgb * poiLight.finalLighting;
            }
            
            float horizon = min(1 + dot(reflDir, normal), 1);
            indirectSpecular *= horizon * horizon;
            
            spec = indirectSpecular;
            #if defined(LIGHTMAP_ON)
                float specMultiplier = max(0, lerp(1, pow(length(lightmap), float(0.2)), float(0)));
                spec *= specMultiplier;
            #endif
        #endif
        return spec;
    }
    
    void poiBRDF(inout float4 finalColor, const float4 finalColorBeforeLighting)
    {
        float4 ret = float4(1, 0, 0, 1);
        #if defined(PROP_BRDFMETALLICGLOSSMAP) || !defined(OPTIMIZER_ENABLED)
            float4 metallicGlossMap = POI2D_SAMPLER_PAN(_BRDFMetallicGlossMap, _MainTex, poiMesh.uv[float(0)], float4(0,0,0,0));
        #else
            float4 metallicGlossMap = 1;
        #endif
        #if defined(PROP_BRDFSPECULARMAP) || !defined(OPTIMIZER_ENABLED)
            float4 spcularTintMask = POI2D_SAMPLER_PAN(_BRDFSpecularMap, _MainTex, poiMesh.uv[float(0)], float4(0,0,0,0));
        #else
            float4 spcularTintMask = 1;
        #endif
        #if defined(PROP_BRDFMETALLICMAP) || !defined(OPTIMIZER_ENABLED)
            float4 metallicTintMask = POI2D_SAMPLER_PAN(_BRDFMetallicMap, _MainTex, poiMesh.uv[float(0)], float4(0,0,0,0));
        #else
            float4 metallicTintMask = 1;
        #endif
        
        if(float(0) == 1)
        {
            metallicGlossMap.a = 1 - metallicGlossMap.a;
        }
        
        float metallic = metallicGlossMap.r * float(0);
        float reflectance = metallicGlossMap.g * float(0.5);
        float roughness = max(1 - (float(0) * metallicGlossMap.a), getGeometricSpecularAA(poiMesh.normals[1]));
        finalColor.rgb *= lerp(1, 1 - metallic, float(1));
        
        float3 reflViewDir = getAnisotropicReflectionVector(poiCam.viewDir, poiMesh.binormal, poiMesh.tangent.xyz, poiMesh.normals[1], roughness, float(0));
        float3 reflLightDir = reflect(poiLight.direction, poiMesh.normals[1]);
        
        #if defined(FORWARD_BASE_PASS) || defined(POI_META_PASS)
            float attenuation = poiMax(poiLight.rampedLightMap);
        #endif
        #ifdef FORWARD_ADD_PASS
            float attenuation = saturate(poiLight.nDotL);
        #endif
        
        
        float3 f0 = 0.16 * reflectance * reflectance * (1.0 - metallic) + finalColorBeforeLighting.rgb * metallic;
        float3 fresnel = lerp(F_Schlick(poiLight.nDotV, f0), f0, metallic); //Kill fresnel on metallics, it looks bad.
        float3 directSpecular = getDirectSpecular(roughness, saturate(poiLight.nDotH), max(poiLight.nDotV, 0.000001), attenuation, saturate(poiLight.lDotH), f0, poiLight.halfDir, poiMesh.tangent.xyz, poiMesh.binormal, float(0)) * poiLight.attenuation * attenuation * poiLight.color;
        directSpecular = min(directSpecular, poiLight.color);
        
        float3 vDirectSpecular = 0;
        #ifdef VERTEXLIGHT_ON
            for (int index = 0; index < 4; index ++)
            {
                float3 v0directSpecular = getDirectSpecular(roughness, saturate(poiLight.vDotNH[index]), max(poiLight.nDotV, 0.000001), attenuation, saturate(poiLight.lDotH), f0, poiLight.vHalfDir[index], poiMesh.tangent, poiMesh.binormal, float(0)) * poiLight.attenuation * poiLight.vAttenuationDotNL[index] * poiLight.vColor[index];
                vDirectSpecular += min(v0directSpecular, poiLight.vColor[index]);
            }
        #endif
        
        float3 indirectSpecular = getIndirectSpecular(metallic, roughness, reflViewDir, poiMesh.worldPos, /*directDiffuse*/ finalColor.rgb, poiMesh.normals[1]) * lerp(fresnel, f0, roughness);
        float3 specular = indirectSpecular * float(1) * metallicTintMask.a * metallicTintMask.rgb * poiLight.occlusion + (directSpecular + vDirectSpecular) * float(1) * spcularTintMask.a * spcularTintMask.rgb;
        finalColor.rgb += specular;
    }
    
#endif