yum/3ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum/3ner
50e4514
master
1#ifndef __CNLOHR_INC 2#define __CNLOHR_INC 3 4#include "globals.cginc" 5#include "interpolators.cginc" 6 7/* 8 * MIT License 9 * 10 * NOTE: Much content here is originally from others. Content in third party 11 * folder may not be fully MIT-licensable. 12 * 13 * Copyright (c) 2021 cnlohr, et. al. 14 * 15 * All other content in this repository falls under the following terms: 16 * 17 * Permission is hereby granted, free of charge, to any person obtaining a copy 18 * of this software and associated documentation files (the "Software"), to 19 * deal 20 * in the Software without restriction, including without limitation the rights 21 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 22 * copies of the Software, and to permit persons to whom the Software is 23 * furnished to do so, subject to the following conditions: 24 * 25 * The above copyright notice and this permission notice shall be included in 26 * all 27 * copies or substantial portions of the Software. 28 * 29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 30 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 31 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 32 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 33 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 34 * FROM, 35 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 36 * THE 37 * SOFTWARE. 38 */ 39 40UNITY_DECLARE_DEPTH_TEXTURE(_CameraDepthTexture); 41float _VRChatMirrorMode; 42float3 _VRChatMirrorCameraPos; 43 44 45// Source: 46// https://github.com/cnlohr/shadertrixx?tab=readme-ov-file#eye-center-position 47bool isMirror() { return _VRChatMirrorMode != 0; } 48 49// Source: 50// https://github.com/cnlohr/shadertrixx?tab=readme-ov-file#eye-center-position 51float3 getCenterCamPos() { 52#if defined(USING_STEREO_MATRICES) || defined(UNITY_SINGLE_PASS_STEREO) 53 return (unity_StereoWorldSpaceCameraPos[0] + unity_StereoWorldSpaceCameraPos[1]) * 0.5; 54#else 55 return isMirror() ? _VRChatMirrorCameraPos : _WorldSpaceCameraPos.xyz; 56#endif 57} 58 59bool isVR() { 60#if defined(USING_STEREO_MATRICES) 61 return true; 62#else 63 return false; 64#endif 65} 66 67float GetLinearZFromZDepth_WorksWithMirrors(float zDepthFromMap, float2 screenUV) 68{ 69 #if defined(UNITY_REVERSED_Z) 70 zDepthFromMap = 1 - zDepthFromMap; 71 72 // When using a mirror, the far plane is whack. This just checks for it and aborts. 73 if( zDepthFromMap >= 1.0 ) return _ProjectionParams.z; 74 #endif 75 76 float4 clipPos = float4(screenUV.xy, zDepthFromMap, 1.0); 77 clipPos.xyz = 2.0f * clipPos.xyz - 1.0f; 78 float4 camPos = mul(unity_CameraInvProjection, clipPos); 79 return -camPos.z / camPos.w; 80} 81 82void GetScreenUVAndPerspectiveFactor(float3 worldPos, float4 clipPos, out float2 screen_uv, out float perspective_factor) { 83 float3 full_vec_eye_to_geometry = worldPos - _WorldSpaceCameraPos; 84 float perspective_divide = 1.0f / clipPos.w; 85 perspective_factor = length(full_vec_eye_to_geometry * perspective_divide); 86 screen_uv = ComputeScreenPos(clipPos).xy; 87} 88 89#if defined(_SSAO) 90float GetDepthOfWorldPos(float3 worldPos) 91{ 92 float3 objPos = mul(unity_WorldToObject, float4(worldPos, 1)); 93 float4 clipPos = UnityObjectToClipPos(objPos); 94 float4 screenPos = ComputeScreenPos(clipPos); 95 const float2 screen_uv = screenPos.xy / screenPos.w; 96 97 float zDepthFromMap = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screen_uv); 98 float linearZ = 99 GetLinearZFromZDepth_WorksWithMirrors(zDepthFromMap, screen_uv); 100 linearZ = min(1E3, linearZ); 101 102 return linearZ; 103} 104#endif 105 106#endif // __CNLOHR_INC