Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize GetOutlineCameraFovAndDistanceFixMultiplier #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions NiloOutlineUtil.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@
// 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()
float GetFOVFactor()
{
//https://answers.unity.com/questions/770838/how-can-i-extract-the-fov-information-from-the-pro.html
float t = unity_CameraProjection._m11;
/*float t = unity_CameraProjection._m11;
float Rad2Deg = 180 / 3.1415;
float fov = atan(1.0f / t) * 2.0 * Rad2Deg;
return fov;
float fov = atan(1.0f / t) * 2.0 * Rad2Deg;*/

//unity_CameraProjection._m11 is actually cot(FOV),
//so 1/unity_CameraProjection._m11 is tan(FOV), which is (Size of View Plane / Distance to Object)
//in this use case, if our change of FOV cause size of view plane (in world space) become 2x, we want our outline to also become 2x to compensate this change,
//and make the outline width same with original
//converting it back to FOV angle cause some lost of accuracy and speed
return (1.0f/unity_CameraProjection._m11);
}
float ApplyOutlineDistanceFadeOut(float inputMulFix)
{
Expand All @@ -30,12 +36,17 @@ float GetOutlineCameraFovAndDistanceFixMultiplier(float positionVS_Z)

// keep outline similar width on screen accoss all camera distance
cameraMulFix = abs(positionVS_Z);
// keep outline similar width on screen accoss all camera fov
// this is done BEFORE OutlineDistanceFadeOut because, for example,
// a character 1m away from camera with fov of 90
// appears the same size in camera as a character sqrt(3)m away from camera with fov of 60
cameraMulFix *= GetFOVFactor();

// 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();
//to match the outline with before this optimization. Should actually be 180 / 3.1415 (Rad2Deg), but 60 will do
cameraMulFix *= 60;
}
else
{
Expand Down