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