yum-archive/2ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/2ner
97ee1dc
master
1#ifndef __SSAO_INC 2#define __SSAO_INC 3 4#include "audiolink.cginc" 5#include "globals.cginc" 6#include "interpolators.cginc" 7 8#if defined(_SSAO) 9float get_ssao(v2f i, f2f f, out float2 debug) { 10 float3 objPos = mul(unity_WorldToObject, float4(i.worldPos, 1)); 11 float4 clipPos = UnityObjectToClipPos(objPos); 12 float4 screenPos = ComputeScreenPos(clipPos); 13 const float2 screen_uv = screenPos.xy / screenPos.w; 14 const float2 screen_px = screen_uv * _ScreenParams.xy; 15 16 const float fragment_depth = GetDepthOfWorldPos(i.worldPos, debug); 17 const float3 noise = _SSAO_Noise.SampleLevel(point_repeat_s, screen_px * _SSAO_Noise_TexelSize.xy, 0); 18 float ssao_theta = noise.x; 19 const float frame = ((float) AudioLinkData(ALPASS_GENERALVU + int2(1, 0)).x); 20 ssao_theta += frame * PHI; 21 ssao_theta *= TAU; 22 const float2x2 ssao_rot = float2x2( 23 cos(ssao_theta), -sin(ssao_theta), 24 sin(ssao_theta), cos(ssao_theta)); 25 26 [branch] 27 if (round(_SSAO_Samples) < 1) { 28 return 1; 29 } 30 31 float ssao_occlusion = 0; 32 const float ssao_eps = 1E-5; 33 [loop] 34 for (uint ii = 0; ii < round(_SSAO_Samples); ii++) { 35 // Compute random vector in tangent space. 36 // Get the index of the current pixels, on the range [0, screen_width] x 37 // [0, screen_height]. 38 // Map that onto the noise texture's pixels. Add an offset for each 39 // index. 40 float2 noise_uv = (float2(ii % _ScreenParams.x, ii * (_ScreenParams.z - 1.0f))) * _SSAO_Noise_TexelSize.xy; 41#if 1 42 float3 sample_point = _SSAO_Noise.SampleLevel(point_repeat_s, noise_uv, 0).rgb; 43#else 44 float3 sample_point = frac(noise + PHI * ii); 45#endif 46 sample_point.xy = 2.0 * sample_point.xy - 1.0; 47 sample_point.xy = mul(ssao_rot, sample_point.xy); 48 49 // Remap to world space. 50 sample_point = mul(sample_point, f.tbn); 51 float scale = (ii * 1.0f) / _SSAO_Samples; 52 sample_point *= lerp(0.1f, 1.0f, scale * scale) * _SSAO_Radius; 53 54 sample_point += i.worldPos; 55 56 float sample_depth = GetDepthOfWorldPos(sample_point, debug); 57 58 // Depth values we're working with indicate how far you have to go along 59 // the view vector before you hit the object in question. Therefore, we 60 // care when the sample is *closer* to us than the object. 61 float occlusion_amount = saturate((fragment_depth - sample_depth) - _SSAO_Bias); 62 63 ssao_occlusion += occlusion_amount; 64 } 65 66 return saturate(1.0 - _SSAO_Strength * ssao_occlusion / _SSAO_Samples); 67} 68#endif 69 70#endif // __SSAO_INC 71