summaryrefslogtreecommitdiff
path: root/VRCSDK3Avatars/Assets/_PoiyomiShaders/Shaders/7.3/Includes/CGI_PoiData.cginc
blob: be2138b569cf6b096d9c6987355aa1e18683cb8c (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
#ifndef POI_DATA
    #define POI_DATA
    
    float _ParallaxBias;
    
    void calculateAttenuation(v2f i)
    {
        #if defined(POINT) || defined(SPOT)
            POI_LIGHT_ATTENUATION(attenuation, shadow, i, i.worldPos.xyz)
                poiLight.additiveShadow = shadow;
        #else
            UNITY_LIGHT_ATTENUATION(attenuation, i, i.worldPos.xyz)
        #endif
        poiLight.attenuation = attenuation;
    }
    
    void calculateLightingData(in v2f i)
    {
        #ifdef FORWARD_BASE_PASS
            //poiLight.color = saturate(_LightColor0.rgb) + saturate(ShadeSH9(normalize(unity_SHAr + unity_SHAg + unity_SHAb)));
            float3 magic = saturate(ShadeSH9(normalize(unity_SHAr + unity_SHAg + unity_SHAb)));
            float3 normalLight = saturate(_LightColor0.rgb);
            poiLight.color = saturate(magic + normalLight);
        #else
            #if defined(POINT) || defined(SPOT)
                poiLight.color = _LightColor0.rgb;
            #endif
        #endif
        
        #ifdef VERTEXLIGHT_ON
            float4 toLightX = unity_4LightPosX0 - i.worldPos.x;
            float4 toLightY = unity_4LightPosY0 - i.worldPos.y;
            float4 toLightZ = unity_4LightPosZ0 - i.worldPos.z;
            float4 lengthSq = 0;
            lengthSq += toLightX * toLightX;
            lengthSq += toLightY * toLightY;
            lengthSq += toLightZ * toLightZ;
            
            float4 lightAttenSq = unity_4LightAtten0;
            float4 atten = 1.0 / (1.0 + lengthSq * lightAttenSq);
            float4 vLightWeight = saturate(1 - (lengthSq * lightAttenSq / 25));
            poiLight.vAttenuation = min(atten, vLightWeight * vLightWeight);
            
            poiLight.vDotNL = 0;
            poiLight.vDotNL += toLightX * poiMesh.normals[1].x;
            poiLight.vDotNL += toLightY * poiMesh.normals[1].y;
            poiLight.vDotNL += toLightZ * poiMesh.normals[1].z;
            
            float4 corr = rsqrt(lengthSq);
            poiLight.vDotNL = max(0, poiLight.vDotNL * corr);
            poiLight.vAttenuationDotNL = poiLight.vAttenuation * poiLight.vDotNL;
            
            for (int index = 0; index < 4; index ++)
            {
                poiLight.vPosition[index] = float3(unity_4LightPosX0[index], unity_4LightPosY0[index], unity_4LightPosZ0[index]);
                
                float3 vertexToLightSource = poiLight.vPosition[index] - poiMesh.worldPos;
                poiLight.vDirection[index] = normalize(vertexToLightSource);
                //poiLight.vAttenuationDotNL[index] = 1.0 / (1.0 + unity_4LightAtten0[index] * poiLight.vDotNL[index]);
                poiLight.vColor[index] = unity_LightColor[index].rgb;
                poiLight.vHalfDir[index] = Unity_SafeNormalize(poiLight.vDirection[index] + poiCam.viewDir);
                poiLight.vDotNL[index] = dot(poiMesh.normals[1], -poiLight.vDirection[index]);
                poiLight.vCorrectedDotNL[index] = .5 * (poiLight.vDotNL[index] + 1);
                
                #ifdef POI_VAR_DOTLH
                    poiLight.vDotLH[index] = saturate(dot(poiLight.vDirection[index], poiLight.vHalfDir[index]));
                #endif
                
                #ifdef POI_VAR_DOTNH
                    poiLight.vDotNH[index] = saturate(dot(poiMesh.normals[1], poiLight.vHalfDir[index]));
                #endif
            }
        #endif
        
        #ifdef FORWARD_BASE_PASS
            poiLight.direction = normalize(_WorldSpaceLightPos0 + unity_SHAr.xyz + unity_SHAg.xyz + unity_SHAb.xyz);
        #else
            #if defined(POINT) || defined(SPOT)
                poiLight.direction = normalize(_WorldSpaceLightPos0.xyz - i.worldPos);
            #endif
        #endif
        
        poiLight.halfDir = normalize(poiLight.direction + poiCam.viewDir);
        
        #ifdef POI_VAR_DOTNH
            poiLight.dotNH = saturate(dot(poiMesh.normals[1], poiLight.halfDir));
        #endif
        
        #ifdef POI_VAR_DOTLH
            poiLight.dotLH = saturate(dot(poiLight.direction, poiLight.halfDir));
        #endif
        
        poiLight.nDotV = dot(poiMesh.normals[1], poiCam.viewDir);
        poiLight.N0DotV = dot(poiMesh.normals[0], poiCam.viewDir);
        poiLight.nDotL = dot(poiMesh.normals[1], poiLight.direction);
        poiLight.nDotH = dot(poiMesh.normals[1], poiLight.halfDir);
        poiLight.lDotv = dot(poiLight.direction, poiCam.viewDir);
        poiLight.lDotH = dot(poiLight.direction, poiLight.halfDir);
    }
    
    float3 CreateBinormal(float3 normal, float3 tangent, float binormalSign)
    {
        return cross(normal, tangent.xyz) * (binormalSign * unity_WorldTransformParams.w);
    }
    
    void InitializeMeshData(inout v2f i, uint facing)
    {
        poiMesh.normals[0] = normalize(i.normal);
        poiMesh.binormal = CreateBinormal(i.normal, i.tangent.xyz, i.tangent.w);
        poiMesh.tangent = i.tangent.xyz;
        
        #ifndef OUTLINE
            if (!facing)
            {
                poiMesh.normals[0] *= -1;
                poiMesh.tangent *= -1;
                poiMesh.binormal *= -1;
            }
        #endif
        
        poiMesh.worldPos = i.worldPos;
        poiMesh.localPos = i.localPos;
        poiMesh.barycentricCoordinates = i.barycentricCoordinates;
        poiMesh.uv[0] = i.uv0.xy;
        poiMesh.uv[1] = i.uv0.zw;
        poiMesh.uv[2] = i.uv1.xy;
        poiMesh.uv[3] = i.uv1.zw;
        
        #ifdef POI_UV_DISTORTION
            poiMesh.uv[4] = calculateDistortionUV(i.uv0.xy);
        #else
            poiMesh.uv[4] = poiMesh.uv[0];
        #endif
        
        poiMesh.vertexColor = i.vertexColor;
        #if defined(LIGHTMAP_ON) || defined(DYNAMICLIGHTMAP_ON)
            poiMesh.lightmapUV = i.lightmapUV;
        #endif
        poiMesh.modelPos = i.modelPos;
        
        #ifdef FUR
            poiMesh.furAlpha = i.furAlpha;
        #endif
    }
    
    void initializeCamera(v2f i)
    {
        poiCam.viewDir = normalize(_WorldSpaceCameraPos - i.worldPos);
        poiCam.forwardDir = getCameraForward();
        poiCam.worldPos = _WorldSpaceCameraPos;
        poiCam.distanceToModel = distance(poiMesh.modelPos, poiCam.worldPos);
        poiCam.distanceToVert = distance(poiMesh.worldPos, poiCam.worldPos);
        poiCam.grabPos = i.grabPos;
        poiCam.screenUV = calcScreenUVs(i.grabPos);
        poiCam.clipPos = i.pos;
        #if defined(GRAIN)
            poiCam.screenPos = i.screenPos;
        #endif
        
        poiCam.tangentViewDir = normalize(i.tangentViewDir);
        poiCam.tangentViewDir.xy /= (poiCam.tangentViewDir.z + _ParallaxBias);
    }
    
    void calculateTangentData()
    {
        poiTData.tangentTransform = float3x3(poiMesh.tangent, poiMesh.binormal, poiMesh.normals[0]);
        poiTData.tangentToWorld = transpose(float3x3(poiMesh.tangent, poiMesh.binormal, poiMesh.normals[0]));
    }
    
    void InitData(inout v2f i, uint facing)
    {
        UNITY_SETUP_INSTANCE_ID(i);
        
        calculateAttenuation(i);
        
        InitializeMeshData(i, facing);
        initializeCamera(i);
        calculateTangentData();
    }
    
    void CalculateReflectionData()
    {
        #if defined(_METALLICGLOSSMAP) || defined(_COLORCOLOR_ON)
            poiCam.reflectionDir = reflect(-poiCam.viewDir, poiMesh.normals[1]);
            poiCam.vertexReflectionDir = reflect(-poiCam.viewDir, poiMesh.normals[0]);
        #endif
    }
#endif