forked from ColinLeung-NiloCat/UnityURPToonLitShaderExample
-
Notifications
You must be signed in to change notification settings - Fork 23
/
NiloOutlineUtil.hlsl
53 lines (46 loc) · 2.06 KB
/
NiloOutlineUtil.hlsl
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
// For more information, visit -> https://github.com/ColinLeung-NiloCat/UnityURPToonLitShaderExample
#ifndef Include_NiloOutlineUtil
#define Include_NiloOutlineUtil
// If your project has a faster way to get camera fov in shader, you can replace this slow function to your method.
// For example, you write cmd.SetGlobalFloat("_CurrentCameraFOV",cameraFOV) using a new RendererFeature in C#.
// For this tutorial shader, we will keep things simple and use this slower but convenient method to get camera fov
float GetCameraFOV()
{
//https://answers.unity.com/questions/770838/how-can-i-extract-the-fov-information-from-the-pro.html
float t = unity_CameraProjection._m11;
float Rad2Deg = 180 / 3.1415;
float fov = atan(1.0f / t) * 2.0 * Rad2Deg;
return fov;
}
float ApplyOutlineDistanceFadeOut(float inputMulFix)
{
//make outline "fadeout" if character is too small in camera's view
return saturate(inputMulFix);
}
float GetOutlineCameraFovAndDistanceFixMultiplier(float positionVS_Z)
{
float cameraMulFix;
if(unity_OrthoParams.w == 0)
{
////////////////////////////////
// Perspective camera case
////////////////////////////////
// keep outline similar width on screen accoss all camera distance
cameraMulFix = abs(positionVS_Z);
// can replace to a tonemap function if a smooth stop is needed
cameraMulFix = ApplyOutlineDistanceFadeOut(cameraMulFix);
// keep outline similar width on screen accoss all camera fov
cameraMulFix *= GetCameraFOV();
}
else
{
////////////////////////////////
// Orthographic camera case
////////////////////////////////
float orthoSize = abs(unity_OrthoParams.y);
orthoSize = ApplyOutlineDistanceFadeOut(orthoSize);
cameraMulFix = orthoSize * 50; // 50 is a magic number to match perspective camera's outline width
}
return cameraMulFix * 0.00005; // mul a const to make return result = default normal expand amount WS
}
#endif