summaryrefslogtreecommitdiff
path: root/VRCSDK3AvatarsLegacy/Assets/Avatars/Auri/materials/OptimizedShaders/body/Includes/CGI_PoiData.cginc
blob: 31e4dcbd605310004dfd6aa84fc42eecda8b7400 (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#ifndef POI_DATA
#define POI_DATA

float _ParallaxBias;
float _LightingAdditiveLimitIntensity;
float _LightingAdditiveMaxIntensity;
POI_TEXTURE_NOSAMPLER(_BumpMap);
#ifdef FINALPASS
    #if defined(PROP_DETAILMASK) || !defined(OPTIMIZER_ENABLED)
        POI_TEXTURE_NOSAMPLER(_DetailMask);
    #endif
    #if defined(PROP_DETAILNORMALMAP) || !defined(OPTIMIZER_ENABLED)
        POI_TEXTURE_NOSAMPLER(_DetailNormalMap);
    #endif
    float _DetailNormalMapScale;
#endif
float _BumpScale;

void calculateAttenuation(v2f i)
{
    #ifdef FORWARD_ADD_PASS
        #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)
            poiLight.additiveShadow == 0;
        #endif
    #else
        UNITY_LIGHT_ATTENUATION(attenuation, i, i.worldPos.xyz)
        // fix for rare bug where light atten is 0 when there is no directional light in the scene
        #ifdef FORWARD_BASE_PASS
            if (all(_LightColor0.rgb == 0.0))
            {
                attenuation = 1.0;
            }
        #endif
    #endif
    poiLight.attenuation = attenuation;
}

void calculateVertexLightingData(in v2f i)
{
    #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;
            
            if (float(0) == 1)
            {
                float intensity = max(0.001, (0.299 * poiLight.vColor[index].r + 0.587 * poiLight.vColor[index].g + 0.114 * poiLight.vColor[index].b));
                poiLight.vColor[index] = min(poiLight.vColor[index], poiLight.vColor[index] / (intensity / float(1)));
            }
            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);
            poiLight.vDotLH[index] = saturate(dot(poiLight.vDirection[index], poiLight.vHalfDir[index]));
            
            poiLight.vDotNH[index] = saturate(dot(poiMesh.normals[1], poiLight.vHalfDir[index]));
        }
    #endif
}

void calculateLightingData(in v2f i)
{
    poiLight.occlusion = 1;
    #ifdef FORWARD_BASE_PASS
        //poiLight.color = saturate(_LightColor0.rgb) + saturate(ShadeSH9(normalize(unity_SHAr + unity_SHAg + unity_SHAb)));
        float3 magic = max(ShadeSH9(normalize(unity_SHAr + unity_SHAg + unity_SHAb)), 0);
        float3 normalLight = _LightColor0.rgb;
        poiLight.color = magic + normalLight;
    #else
        #ifdef FORWARD_ADD_PASS
            poiLight.color = _LightColor0.rgb;
            
            
            if (float(0) == 1)
            {
                float additiveLightIntensity = max(0.001, (0.299 * poiLight.color.r + 0.587 * poiLight.color.g + 0.114 * poiLight.color.b));
                poiLight.color = min(poiLight.color, poiLight.color / (additiveLightIntensity / float(1)));
            }
        #endif
    #endif
    
    #ifdef FORWARD_BASE_PASS
        poiLight.direction = normalize(_WorldSpaceLightPos0.xyz + unity_SHAr.xyz + unity_SHAg.xyz + unity_SHAb.xyz);
    #else
        #if defined(POINT) || defined(SPOT)
            poiLight.direction = normalize(_WorldSpaceLightPos0.xyz - i.worldPos.xyz);
        #else
            poiLight.direction = _WorldSpaceLightPos0.xyz;
        #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.isFrontFace = facing;
    poiMesh.normals[0] = normalize(i.normal);
    poiMesh.binormal = CreateBinormal(i.normal, i.tangent.xyz, i.tangent.w);
    poiMesh.tangent = i.tangent;
    
    #ifndef OUTLINE
        if (!poiMesh.isFrontFace)
        {
            poiMesh.normals[0] *= -1;
            poiMesh.tangent *= -1;
            poiMesh.binormal *= -1;
        }
    #endif
    
    poiMesh.worldPos = i.worldPos.xyz;
    poiMesh.localPos = i.localPos.xyz;
    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 initPoiMods()
{
    poiMods.audioLink = float4(0, 0, 0, 0);
    poiMods.globalMasks = float4(0, 0, 0, 0);
    #ifdef POI_AUDIOLINK
        initAudioBands();
    #endif
}

void initializeCamera(v2f i)
{
    poiCam.viewDir = normalize(_WorldSpaceCameraPos - i.worldPos.xyz);
    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.worldDirection = i.worldDirection;
    #endif
    
    poiCam.tangentViewDir = normalize(i.tangentViewDir);
    poiCam.decalTangentViewDir = poiCam.tangentViewDir;
    poiCam.tangentViewDir.xy /= (poiCam.tangentViewDir.z + float(0.42));
}

void calculateTangentData()
{
    poiTData.tangentTransform = float3x3(poiMesh.tangent.xyz, poiMesh.binormal, poiMesh.normals[0]);
    poiTData.tangentToWorld = transpose(float3x3(poiMesh.tangent.xyz, poiMesh.binormal, poiMesh.normals[0]));
}

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
}

void calculateNormals(inout half3 detailMask)
{
    half3 mainNormal = UnpackScaleNormal(POI2D_SAMPLER_PAN(_BumpMap, _MainTex, poiMesh.uv[float(0)], float4(0,0,0,0)), float(1));
    
    #ifdef FINALPASS
        #if defined(PROP_DETAILMASK) || !defined(OPTIMIZER_ENABLED)
            detailMask = POI2D_SAMPLER_PAN(_DetailMask, _MainTex, poiMesh.uv[float(0)], float4(0,0,0,0)).rgb;
        #else
            detailMask = 1;
        #endif
        
        if (float(1) > 0)
        {
            #if defined(PROP_DETAILNORMALMAP) || !defined(OPTIMIZER_ENABLED)
                half3 detailNormal = UnpackScaleNormal(POI2D_SAMPLER_PAN(_DetailNormalMap, _MainTex, poiMesh.uv[float(0)], float4(0,0,0,0)), float(1) * detailMask.g);
                poiMesh.tangentSpaceNormal = BlendNormals(mainNormal, detailNormal);
            #else
                poiMesh.tangentSpaceNormal = mainNormal;
            #endif
        }
        else
        {
            poiMesh.tangentSpaceNormal = mainNormal;
        }
    #else
        poiMesh.tangentSpaceNormal = mainNormal;
    #endif
    
    #ifdef POI_RGBMASK
        calculateRGBNormals(poiMesh.tangentSpaceNormal);
    #endif
    
    poiMesh.normals[1] = normalize(
        poiMesh.tangentSpaceNormal.x * poiMesh.tangent.xyz +
        poiMesh.tangentSpaceNormal.y * poiMesh.binormal +
        poiMesh.tangentSpaceNormal.z * poiMesh.normals[0]
    );
    
    poiCam.viewDotNormal = abs(dot(poiCam.viewDir, poiMesh.normals[1]));
}
#endif