summaryrefslogtreecommitdiff
path: root/VRCSDK3AvatarsLegacy/Assets/_PoiyomiShaders/Shaders/Pro/Includes/CGI_PoiFrag.cginc
blob: a852d5f06c5fbdaeeb654f7c50898a66e6c86da7 (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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#ifndef POIFRAG
#define POIFRAG

float _MainEmissionStrength;
float _IgnoreFog;
half _GIEmissionMultiplier;
float _IridescenceTime;
float _AlphaToMask;
float _ForceOpaque;
float _commentIfZero_EnableGrabpass;
float _AlphaPremultiply;
float2 _MainTexPan;
float _MainTextureUV;
float _LightingAdditiveEnable;

// Post Processing
float _PPLightingMultiplier;
float _PPEmissionMultiplier;

float4 frag(v2f i, uint facing: SV_IsFrontFace): SV_Target
{
	#ifdef FORWARD_ADD_PASS
		#if !defined(POI_LIGHTING)
			return 0;
		#endif
		UNITY_BRANCH
		if (_LightingAdditiveEnable == 0)
		{
			return 0;
		}
	#endif
	UNITY_SETUP_INSTANCE_ID(i);
	// Color
	float4 albedo = 1;
	float4 finalColor = 1;
	
	// Lighting
	float bakedCubemap = 0; // Whether or not metallic should run before or after lighting multiplication
	float3 finalSpecular0 = 0;
	float3 finalSpecular1 = 0;
	float3 finalSSS = 0;
	fixed lightingAlpha = 1;
	float3 finalEnvironmentalRim = 0;
	
	// Emissions
	float3 finalEmission = 0;
	float3 finalLighting = 1;
	float3 emissionLighting = 1;
	float3 IridescenceEmission = 0;
	float3 spawnInEmission = 0;
	float3 voronoiEmission = 0;
	float3 matcapEmission = 0;
	float3 depthTouchEmission = 0;
	float3 decalEmission = 0;
	float3 glitterEmission = 0;
	float3 panosphereEmission = 0;
	float3 backFaceEmission = 0;
	float3 dissolveEmission = 0;
	float3 rimLightEmission = 0;
	float3 flipbookEmission = 0;
	float3 textOverlayEmission = 0;
	float3 videoEmission = 0;
	float3 pathEmission = 0;
	/**********************************************************************
	Initialize the base data that's needed everywhere else in the shader
	**********************************************************************/
	calculateAttenuation(i);
	InitializeMeshData(i, facing);
	initializeCamera(i);
	calculateTangentData();
	
	
	#ifdef POI_BLACKLIGHT
		createBlackLightMask();
		UNITY_BRANCH
		if (_BlackLightMaskDebug)
		{
			return float4(blackLightMask.rgb, 1);
		}
	#endif
	
	// This has to happen in the initializbecause it alters UV data globally
	#ifdef POI_PARALLAX
		calculateandApplyParallax();
	#endif
	
	// Basically every texture relies on the maintex sampler to function and that's why this is here.
	float4 mainTexture = UNITY_SAMPLE_TEX2D(_MainTex, TRANSFORM_TEX(poiMesh.uv[_MainTextureUV], _MainTex) + _Time.x * _MainTexPan);
	half3 detailMask = 1;
	calculateNormals(detailMask);
	
	//return float4(poiMesh.binormal.xyz, 1);
	calculateVertexLightingData(i);
	/**********************************************************************
	Calculate Light Maps
	**********************************************************************/
	#ifdef POI_DATA
		calculateLightingData(i);
	#endif
	#ifdef POI_LIGHTING
		calculateBasePassLightMaps();
	#endif
	
	/**********************************************************************
	Calculate Color Data
	**********************************************************************/
	
	initTextureData(albedo, mainTexture, backFaceEmission, dissolveEmission, detailMask);
	
	#ifdef POI_PATHING
		applyPathing(albedo, pathEmission);
	#endif

	#ifdef POI_DECAL
		applyDecals(albedo, decalEmission);
	#endif
	
	
	#ifdef POI_IRIDESCENCE
		UNITY_BRANCH
		if (_IridescenceTime == 0)
		{
			applyIridescence(albedo, IridescenceEmission);
		}
	#endif
	
	#ifdef POI_VORONOI
		applyVoronoi(albedo, voronoiEmission);
	#endif
	
	#ifdef POI_MSDF
		ApplyTextOverlayColor(albedo, textOverlayEmission);
	#endif
	
	#ifdef POI_ENVIRONMENTAL_RIM
		finalEnvironmentalRim = calculateEnvironmentalRimLighting(albedo);
	#endif
	
	#if defined(POI_METAL) || defined(POI_CLEARCOAT)
		CalculateReflectionData();
	#endif
	
	#ifdef POI_DATA
		distanceFade(albedo);
	#endif
	
	#ifdef POI_RANDOM
		albedo.a *= i.angleAlpha;
	#endif
	
	#ifdef MATCAP
		applyMatcap(albedo, matcapEmission);
	#endif
	
	#ifdef PANOSPHERE
		applyPanosphereColor(albedo, panosphereEmission);
	#endif
	
	#ifdef POI_FLIPBOOK
		applyFlipbook(albedo, flipbookEmission);
	#endif
	
	#ifdef POI_GLITTER
		applyGlitter(albedo, glitterEmission);
	#endif
	
	#ifdef POI_RIM
		applyRimLighting(albedo, rimLightEmission);
	#endif
	
	#ifdef POI_DEPTH_COLOR
		applyDepthColor(albedo, depthTouchEmission, finalEmission, i.worldDirection);
	#endif
	
	#ifdef POI_IRIDESCENCE
		UNITY_BRANCH
		if (_IridescenceTime == 1)
		{
			applyIridescence(albedo, IridescenceEmission);
		}
	#endif
	
	#ifdef POI_VIDEO
		applyScreenEffect(albedo, videoEmission);
	#endif
	
	applySpawnIn(albedo, spawnInEmission, poiMesh.uv[0], poiMesh.localPos);
	
	/**********************************************************************
	Handle a few alpha options
	**********************************************************************/
	UNITY_BRANCH
	if (_Mode == 1)
	{
		UNITY_BRANCH
		if (_AlphaToMask == 0)
		{
			applyDithering(albedo);
		}
	}
	
	albedo.a = max(_ForceOpaque, albedo.a);
	
	UNITY_BRANCH
	if (_Mode == 0)
	{
		albedo.a = 1;
	}
	
	UNITY_BRANCH
	if (_Mode >= 1)
	{
		clip(albedo.a - _Cutoff);
	}
	
	UNITY_BRANCH
	if (_AlphaPremultiply)
	{
		albedo.rgb *= saturate(albedo.a + 0.0000000001);
	}
	
	/**********************************************************************
	Lighting Time :)
	**********************************************************************/
	
	#ifdef POI_LIGHTING
		finalLighting = calculateFinalLighting(albedo.rgb, finalColor);
		finalLighting = max(finalLighting *= _PPLightingMultiplier, 0);

		if (!_LightingUncapped)
		{
			finalLighting = saturate(finalLighting);
		}

		#ifdef SUBSURFACE
			finalSSS = calculateSubsurfaceScattering();
			//finalSSS = calculateSubsurfaceScattering(albedo);
		#endif
	#endif
	
	float4 finalColorBeforeLighting = albedo;
	finalColor = finalColorBeforeLighting;
	
	#ifdef POI_SPECULAR
		finalSpecular0 = calculateSpecular(finalColorBeforeLighting);
	#endif
	#ifdef POI_PARALLAX
		calculateAndApplyInternalParallax(finalColor);
	#endif
	
	#ifdef POI_ALPHA_TO_COVERAGE
		ApplyAlphaToCoverage(finalColor);
	#endif
	
	UNITY_BRANCH
	if (_Mode == 1)
	{
		UNITY_BRANCH
		if (_AlphaToMask == 1)
		{
			applyDithering(finalColor);
		}
	}
	
	
	#ifdef POI_METAL
		calculateMetallicness();
		bool probeExists = shouldMetalHappenBeforeLighting();
		UNITY_BRANCH
		if (!probeExists)
		{
			ApplyMetallicsFake(finalColor, albedo);
		}
	#endif
	
	
	#ifdef POI_LIGHTING
		emissionLighting = finalLighting;
		#if defined(FORWARD_ADD_PASS) && defined(POI_METAL)
			finalLighting *= 1 - metalicMap;
		#endif
		applyLighting(finalColor, finalLighting);
	#endif
	
	#ifdef POI_BRDF
		poiBRDF(finalColor, finalColorBeforeLighting);
	#endif
	
	#ifdef POI_METAL
		UNITY_BRANCH
		if (probeExists)
		{
			ApplyMetallics(finalColor, albedo);
		}
	#endif
	
	finalColor.rgb += finalSpecular0 + finalEnvironmentalRim + finalSSS;
	
	#ifdef FORWARD_BASE_PASS
		#ifdef POI_CLEARCOAT
			calculateAndApplyClearCoat(finalColor);
		#endif
	#endif
	
	finalColor.a = saturate(finalColor.a);
	
	/**********************************************************************
	Add Up all the emission values :D
	**********************************************************************/
	//#if defined(FORWARD_BASE_PASS) || defined(POI_META_PASS)
	finalEmission += finalColorBeforeLighting.rgb * _MainEmissionStrength * albedo.a;
	finalEmission += wireframeEmission;
	finalEmission += IridescenceEmission;
	finalEmission += spawnInEmission;
	finalEmission += voronoiEmission;
	finalEmission += matcapEmission;
	finalEmission += depthTouchEmission;
	finalEmission += decalEmission;
	finalEmission += glitterEmission;
	finalEmission += panosphereEmission;
	finalEmission += backFaceEmission;
	finalEmission += rimLightEmission;
	finalEmission += flipbookEmission;
	finalEmission += videoEmission;
	finalEmission += textOverlayEmission;
	finalEmission += dissolveEmission;
	finalEmission += pathEmission;
	#ifdef POI_EMISSION
		finalEmission += calculateEmissionNew(finalColorBeforeLighting.rgb, finalColor);
	#endif

	finalEmission = max(finalEmission * _PPEmissionMultiplier, 0);

	//#endif
	// Compensate for HDR lights
	#if defined(FORWARD_ADD_PASS)
		finalEmission *= emissionLighting; // TODO: add in vertex lights
	#else
		finalEmission *= max(1, emissionLighting);
	#endif
	
	/**********************************************************************
	Meta Pass Hype :D
	**********************************************************************/
	#ifdef POI_META_PASS
		UnityMetaInput meta;
		UNITY_INITIALIZE_OUTPUT(UnityMetaInput, meta);
		meta.Emission = finalEmission * _GIEmissionMultiplier;
		meta.Albedo = saturate(finalColor.rgb);
		#ifdef POI_SPECULAR
			meta.SpecularColor = poiLight.color.rgb * _SpecularTint.rgb * lerp(1, albedo.rgb, _SpecularMetallic) * _SpecularTint.a;
		#else
			meta.SpecularColor = poiLight.color.rgb * albedo.rgb;
		#endif
		return UnityMetaFragment(meta);
	#endif
	
	/**********************************************************************
	Apply Emission to finalColor
	**********************************************************************/
	finalColor.rgb += finalEmission;
	
	/**********************************************************************
	Grabpass features
	**********************************************************************/
	
	UNITY_BRANCH
	if (_commentIfZero_EnableGrabpass)
	{
		applyGrabEffects(finalColor);
	}
	
	/**********************************************************************
	Unity Fog
	**********************************************************************/
	#ifdef FORWARD_BASE_PASS
		UNITY_BRANCH
		if (_IgnoreFog == 0)
		{
			UNITY_APPLY_FOG(i.fogCoord, finalColor);
		}
	#endif
	
	#ifdef FORWARD_ADD_PASS
		if (_Mode > 0)
		{
			finalColor.rgb *= finalColor.a;
		}
	#endif
	
	UNITY_BRANCH
	if (_Mode == 0)
	{
		finalColor.a = 1;
	}
	
	#ifdef FORWARD_ADD_PASS
		//finalColor.rgb = smoothstep(_LightingAdditiveGradientStart, _LightingAdditiveGradientEnd, 1 - (.5 * poiLight.nDotL + .5));
	#endif
	
	#ifdef POI_DEBUG
		displayDebugInfo(finalColor);
	#endif

	#ifdef POI_AUDIOLINK
		UNITY_BRANCH
		if (_AudioLinkTextureVisualization)
		{
			finalColor = poiMods.audioLinkTexture;
		}
	#endif

	#ifdef FORWARD_ADD_PASS
		#if defined(_SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A) && defined(DIRECTIONAL)
			return finalColor + albedo * 0.00001;;
		#endif
	#endif
	
	//finalColor.rgb = frac(finalColor.rgb);
	return finalColor + mainTexture * 0.00001;
}
#endif