yum-archive/2ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/2ner
2a5186a
master
1#ifndef __LETTER_GRID_INC 2#define __LETTER_GRID_INC 3 4#include "disinfo.cginc" 5#include "features.cginc" 6#include "globals.cginc" 7#include "interpolators.cginc" 8#include "math.cginc" 9#include "texture_utils.cginc" 10 11#if defined(_LETTER_GRID) 12 13struct LetterGridOutput { 14 float4 albedo; 15 float metallic; 16 float roughness; 17 float3 emission; 18}; 19 20LetterGridOutput LetterGrid(v2f i) { 21 LetterGridOutput output; 22 23 int2 cell_pos; 24 int2 font_res = int2(round(_Letter_Grid_Tex_Res_X), round(_Letter_Grid_Tex_Res_Y)); 25 int2 grid_res = int2(round(_Letter_Grid_Res_X), round(_Letter_Grid_Res_Y)); 26 float2 cell_uv; // uv within each letter cell 27 28 float4 scoff = _Letter_Grid_UV_Scale_Offset; 29 float2 uv = ((i.uv01.xy - 0.5) - scoff.zw) * scoff.xy + 0.5; 30 31 bool in_box = getBoxLoc(uv, 0, 1, grid_res, _Letter_Grid_Padding, cell_pos, cell_uv); 32 33 // Extract char from _Letter_Grid_Data_Row_0 et al using cell_pos. 34 cell_pos.y = (grid_res.y - cell_pos.y) - 1; 35 float c = lerp( 36 lerp( 37 _Letter_Grid_Data_Row_0[cell_pos.x], 38 _Letter_Grid_Data_Row_1[cell_pos.x], 39 cell_pos.y), 40 lerp( 41 _Letter_Grid_Data_Row_2[cell_pos.x], 42 _Letter_Grid_Data_Row_3[cell_pos.x], 43 cell_pos.y - 2), 44 cell_pos.y/2); 45 c += _Letter_Grid_Global_Offset; 46 47 float3 msd = renderInBox(c, uv, cell_uv, _Letter_Grid_Texture, font_res).rgb; 48 float sd = median(msd); 49 50 // Calculate screen pixel range 51 float screen_px_range; 52 { 53 float2 tex_size = float2(_Letter_Grid_Texture_TexelSize.zw); 54 float2 real_cell_size = floor(tex_size / grid_res); // size of cell in texels 55 float2 unit_range = _Letter_Grid_Screen_Px_Range / real_cell_size; 56 float2 screen_tex_size = 1 / fwidth(cell_uv); 57 screen_px_range = max(0.5 * dot(unit_range, screen_tex_size), _Letter_Grid_Min_Screen_Px_Range); 58 } 59 60 float screen_px_distance = screen_px_range * (sd - _Letter_Grid_Alpha_Threshold); 61 float smooth_range = (length(grid_res) / sqrt(screen_px_range)) * _Letter_Grid_Blurriness; 62 float op = smoothstep(-smooth_range, smooth_range, screen_px_distance); 63 64 // Sample mask if enabled 65 #if defined(_LETTER_GRID_MASK) 66 float mask = _Letter_Grid_Mask.Sample(linear_repeat_s, i.uv01.xy).r; 67 #else 68 float mask = 1.0; 69 #endif 70 71 op *= mask; 72 73 // Apply blending to output 74 output.albedo = float4(_Letter_Grid_Color.rgb, op * in_box); 75 output.metallic = _Letter_Grid_Metallic; 76 output.roughness = _Letter_Grid_Roughness; 77 output.emission = _Letter_Grid_Color.rgb * _Letter_Grid_Emission; 78 79 return output; 80} 81 82#endif // _LETTER_GRID 83 84#endif // __LETTER_GRID_INC 85