yum-archive/2ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/2ner
50b0b6c
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 40// Source: 41// https://github.com/cnlohr/shadertrixx?tab=readme-ov-file#eye-center-position 42bool isMirror() { return _VRChatMirrorMode != 0; } 43 44// Source: 45// https://github.com/cnlohr/shadertrixx?tab=readme-ov-file#eye-center-position 46float3 getCenterCamPos() { 47#if defined(USING_STEREO_MATRICES) || defined(UNITY_SINGLE_PASS_STEREO) 48 return (unity_StereoWorldSpaceCameraPos[0] + unity_StereoWorldSpaceCameraPos[1]) * 0.5; 49#else 50 return isMirror() ? _VRChatMirrorCameraPos : _WorldSpaceCameraPos.xyz; 51#endif 52} 53 54bool isVR() { 55#if defined(USING_STEREO_MATRICES) 56 return true; 57#else 58 return false; 59#endif 60} 61 62float GetLinearZFromZDepth_WorksWithMirrors(float zDepthFromMap, float2 screenUV) 63{ 64 #if defined(UNITY_REVERSED_Z) 65 zDepthFromMap = 1 - zDepthFromMap; 66 67 // When using a mirror, the far plane is whack. This just checks for it and aborts. 68 if( zDepthFromMap >= 1.0 ) return _ProjectionParams.z; 69 #endif 70 71 float4 clipPos = float4(screenUV.xy, zDepthFromMap, 1.0); 72 clipPos.xyz = 2.0f * clipPos.xyz - 1.0f; 73 float4 camPos = mul(unity_CameraInvProjection, clipPos); 74 return -camPos.z / camPos.w; 75} 76 77void GetScreenUVAndPerspectiveFactor(float3 worldPos, float4 clipPos, out float2 screen_uv, out float perspective_factor) { 78 float3 full_vec_eye_to_geometry = worldPos - _WorldSpaceCameraPos; 79 float perspective_divide = 1.0f / clipPos.w; 80 perspective_factor = length(full_vec_eye_to_geometry * perspective_divide); 81 screen_uv = ComputeScreenPos(clipPos).xy; 82} 83 84#if defined(_SSAO) 85float GetDepthOfWorldPos(float3 worldPos, out float2 debug) 86{ 87 float3 objPos = mul(unity_WorldToObject, float4(worldPos, 1)); 88 float4 clipPos = UnityObjectToClipPos(objPos); 89 float4 screenPos = ComputeScreenPos(clipPos); 90 const float2 screen_uv = screenPos.xy / screenPos.w; 91 92 float zDepthFromMap = SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture, screen_uv); 93 float linearZ = 94 GetLinearZFromZDepth_WorksWithMirrors(zDepthFromMap, screen_uv); 95 linearZ = min(1E3, linearZ); 96 97 debug = screen_uv; 98 return linearZ; 99} 100#endif 101 102#endif // __CNLOHR_INC