summaryrefslogtreecommitdiff
path: root/VRCSDK3AvatarsLegacy/Assets/Avatars/Auri/materials/OptimizedShaders/nails/Includes/CGI_PoiGrab.cginc
blob: e7b1a348e5151531b10ac02f151cd2ea928507d4 (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
#ifndef POI_GRAB
    #define POI_GRAB
    
    float _RefractionIndex;
    float _RefractionOpacity;
    float _RefractionChromaticAberattion;
    float _RefractionEnabled;
    float _GrabSrcBlend;
    float _GrabDstBlend;
    float _GrabPassUseAlpha;
    float _GrabPassBlendFactor;
    float _GrabBlurDistance;
    float _GrabBlurQuality;
    float _GrabBlurDirections;
    POI_TEXTURE_NOSAMPLER(_GrabPassBlendMap);
    
    float4 blur(float2 uv)
    {
        float two_pi = 6.28318530718;
        
        float2 radius = _GrabBlurDistance / _ScreenParams.xy * 100; // Arbitrary constant to match old blur
        float quality = floor(_GrabBlurQuality);
        float directions = floor(_GrabBlurDirections);
        
        // Pixel colour
        float4 color = tex2D(_PoiGrab, uv);
        
        float deltaAngle = two_pi / directions;
        float deltaQuality = 1.0 / quality;
        for (int i = 0; i < directions; i ++)
        {
            for (int j = 0; j < quality; j ++)
            {
                float angle = deltaAngle * i + j;
                float offset = deltaQuality * (j + 1);
                color += tex2D(_PoiGrab, uv + float2(cos(angle), sin(angle)) * radius * offset);
            }
        }
        
        // Output to screen
        color /= quality * directions + 1;
        return color;
    }
    
    inline float4 Refraction(float indexOfRefraction, float chromaticAberration, float2 projectedGrabPos)
    {
        float4 refractionColor;
        float3 worldViewDir = normalize(UnityWorldSpaceViewDir(poiMesh.worldPos));
        float3 refractionOffset = ((((indexOfRefraction - 1.0) * mul(UNITY_MATRIX_V, float4(poiMesh.normals[1], 0.0)).xyz) * (1.0 / (poiCam.grabPos.z + 1.0))) * (1.0 - dot(poiMesh.normals[1], worldViewDir)));
        float2 cameraRefraction = float2(refractionOffset.x, - (refractionOffset.y * _ProjectionParams.x));
        
        
        if (_RefractionChromaticAberattion > 0)
        {
            float4 redAlpha = tex2D(_PoiGrab, (projectedGrabPos + cameraRefraction));
            float green = tex2D(_PoiGrab, (projectedGrabPos + (cameraRefraction * (1.0 - chromaticAberration)))).g;
            float blue = tex2D(_PoiGrab, (projectedGrabPos + (cameraRefraction * (1.0 + chromaticAberration)))).b;
            refractionColor = float4(redAlpha.r, green, blue, redAlpha.a);
        }
        else
        {
            float2 refractedGrab = projectedGrabPos + cameraRefraction;
            
            #ifdef CHROMATIC_ABERRATION_LOW
                refractionColor = blur(refractedGrab);
            #else
                refractionColor = tex2D(_PoiGrab, (refractedGrab));
            #endif
        }
        return refractionColor;
    }
    
    void calculateRefraction(float2 projectedGrabPos, inout float4 finalColor)
    {
        float3 refraction = 1;
        
        if(_RefractionEnabled == 1)
        {
            refraction = Refraction(_RefractionIndex, _RefractionChromaticAberattion, projectedGrabPos).rgb;
        }
        else
        {
            #ifdef CHROMATIC_ABERRATION_LOW
                refraction = blur(projectedGrabPos);
            #else
                refraction = tex2Dproj(_PoiGrab, poiCam.grabPos).rgb;
            #endif
        }
        
        float blendFactor = _GrabPassBlendFactor * POI2D_SAMPLER_PAN(_GrabPassBlendMap, _MainTex, poiMesh.uv[_GrabPassBlendMapUV], _GrabPassBlendMapPan).r;
        
        if(_GrabPassUseAlpha)
        {
            finalColor = poiBlend(_GrabSrcBlend, finalColor, _GrabDstBlend, float4(refraction, 1), blendFactor * (1 - finalColor.a));
            finalColor.a = 1;
        }
        else
        {
            finalColor = poiBlend(_GrabSrcBlend, finalColor, _GrabDstBlend, float4(refraction, 1), blendFactor);
        }
    }
    
    float2 calculateGrabPosition()
    {
        float4 grabPos = poiCam.grabPos;
        #if UNITY_UV_STARTS_AT_TOP
            float scale = -1.0;
        #else
            float scale = 1.0;
        #endif
        float halfPosW = grabPos.w * 0.5;
        grabPos.y = (grabPos.y - halfPosW) * _ProjectionParams.x * scale + halfPosW;
        #if SHADER_API_D3D9 || SHADER_API_D3D11
            grabPos.w += 0.00000000001;
        #endif
        return(grabPos / grabPos.w).xy;
    }
    
    void applyGrabEffects(inout float4 finalColor)
    {
        float2 projectedGrabPos = calculateGrabPosition();
        calculateRefraction(projectedGrabPos, finalColor);
    }
    
#endif