summaryrefslogtreecommitdiff
path: root/VRCSDK3AvatarsLegacy/Assets/_PoiyomiShaders/Shaders/Includes/CGI_PoiClearCoat.cginc
blob: aef58a061b4f82d3e2f4f30cc383fb1b1b0b9afd (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
#ifndef POI_CLEARCOAT
    #define POI_CLEARCOAT
    
    samplerCUBE _ClearCoatCubeMap;
    float _ClearCoatSampleWorld;
    POI_TEXTURE_NOSAMPLER(_ClearCoatMask);
    POI_TEXTURE_NOSAMPLER(_ClearCoatSmoothnessMap);
    float _ClearCoatInvertSmoothness;
    float _ClearCoat;
    float _ClearCoatSmoothness;
    float3 _ClearCoatTint;
    uint _ClearCoatNormalToUse;
    float _ClearCoatForceLighting;
    float lighty_clear_boy_uwu_var;
    
    
    float3 CalculateClearCoatEnvironmentalReflections()
    {
        float3 reflectionColor;
        
        float smoothnessMap = (POI2D_SAMPLER_PAN(_ClearCoatSmoothnessMap, _MainTex, poiMesh.uv[_ClearCoatSmoothnessMapUV], _ClearCoatSmoothnessMapPan));
        if (_ClearCoatInvertSmoothness == 1)
        {
            smoothnessMap = 1 - smoothnessMap;
        }
        smoothnessMap *= _ClearCoatSmoothness;
        float roughness = 1 - smoothnessMap;
        
        lighty_clear_boy_uwu_var = 0;
        
        float3 reflectedDir = _ClearCoatNormalToUse == 0 ? poiCam.vertexReflectionDir: poiCam.reflectionDir;
        
        float4 envSample = UNITY_SAMPLE_TEXCUBE_LOD(unity_SpecCube0, reflectedDir, roughness * UNITY_SPECCUBE_LOD_STEPS);
        bool no_probe = unity_SpecCube0_HDR.a == 0 && envSample.a == 0;
        
        UNITY_BRANCH
        if(_ClearCoatSampleWorld == 0 && no_probe == 0)
        {
            
            Unity_GlossyEnvironmentData envData;
            envData.roughness = roughness;
            envData.reflUVW = BoxProjection(
                reflectedDir, poiMesh.worldPos.xyz,
                unity_SpecCube0_ProbePosition,
                unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax
            );
            float3 probe0 = Unity_GlossyEnvironment(
                UNITY_PASS_TEXCUBE(unity_SpecCube0), unity_SpecCube0_HDR, envData
            );
            envData.reflUVW = BoxProjection(
                reflectedDir, poiMesh.worldPos.xyz,
                unity_SpecCube1_ProbePosition,
                unity_SpecCube1_BoxMin, unity_SpecCube1_BoxMax
            );
            
            float interpolator = unity_SpecCube0_BoxMin.w;
            UNITY_BRANCH
            if(interpolator < 0.99999)
            {
                float3 probe1 = Unity_GlossyEnvironment(
                    UNITY_PASS_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0),
                    unity_SpecCube0_HDR, envData
                );
                reflectionColor = lerp(probe1, probe0, interpolator);
            }
            else
            {
                reflectionColor = probe0;
            }
        }
        else
        {
            lighty_clear_boy_uwu_var = 1;
            reflectionColor = texCUBElod(_ClearCoatCubeMap, float4(reflectedDir, roughness * UNITY_SPECCUBE_LOD_STEPS));
        }
        
        if(_ClearCoatForceLighting)
        {
            lighty_clear_boy_uwu_var = 1;
        }
        
        return reflectionColor * _ClearCoatTint;
    }
    
    void calculateAndApplyClearCoat(inout float4 finalColor)
    {
        half clearCoatMap = POI2D_SAMPLER_PAN(_ClearCoatMask, _MainTex, poiMesh.uv[_ClearCoatMaskUV], _ClearCoatMaskPan);
        
        #ifdef POI_BLACKLIGHT
            if(_BlackLightMaskClearCoat != 4)
            {
                clearCoatMap *= blackLightMask[_BlackLightMaskClearCoat];
            }
        #endif
        
        half3 reflectionColor = CalculateClearCoatEnvironmentalReflections();
        
        float NormalDotView = abs(dot(_ClearCoat, _ClearCoatNormalToUse == 0 ? poiLight.N0DotV: poiLight.nDotV).r);
        #ifdef POI_LIGHTING
            finalColor.rgb = lerp(finalColor.rgb, reflectionColor * lerp(1, poiLight.finalLighting, lighty_clear_boy_uwu_var), clearCoatMap * _ClearCoat * clamp(FresnelTerm(_ClearCoat, NormalDotView), 0, 1));
            //finalColor.rgb += reflectionColor;
            //finalColor.rgb = finalColor.rgb * (1- (reflectionColor.r + reflectionColor.g + reflectionColor.b)/3) + reflectionColor * clearCoatMap * lerp(1, poiLight.finalLighting, lighty_clear_boy_uwu_var);
        #else
            lerp(finalColor.rgb, reflectionColor, clearCoatMap * _ClearCoat * clamp(FresnelTerm(_ClearCoat, NormalDotView), 0, 1));
        #endif
    }
    
#endif