yum-archive/2ner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/2ner
d0c27e8
master
1#ifndef UNITY_STANDARD_META_INCLUDED 2#define UNITY_STANDARD_META_INCLUDED 3 4// Functionality for Standard shader "meta" pass 5// (extracts albedo/emission for lightmapper etc.) 6 7#include "UnityCG.cginc" 8#include "UnityMetaPass.cginc" 9 10#include "custom30.cginc" 11#include "eyes.cginc" 12#include "face_me.cginc" 13#include "false_color_visualization.cginc" 14#include "features.cginc" 15#include "fog.cginc" 16#include "globals.cginc" 17#include "harnack_tracing.cginc" 18#include "interpolators.cginc" 19#include "letter_grid.cginc" 20#include "matcaps.cginc" 21#include "math.cginc" 22#include "poi.cginc" 23#include "shatter_wave.cginc" 24#include "ssao.cginc" 25#include "ssfd.cginc" 26#include "tessellation.cginc" 27#include "unigram_letter_grid.cginc" 28#include "vertex_domain_warping.cginc" 29#include "yum_brdf.cginc" 30#include "yum_pbr.cginc" 31#include "yum_lighting.cginc" 32 33struct v2f_meta 34{ 35 float4 pos : SV_POSITION; 36 float4 uv01 : TEXCOORD0; 37 float4 uv23 : TEXCOORD1; 38 float3 worldPos : TEXCOORD2; 39 float3 objPos : TEXCOORD3; 40 float3 normal : TEXCOORD4; 41 float3 tangent : TEXCOORD5; 42 float3 binormal : TEXCOORD6; 43 float4 color : COLOR; 44#ifdef EDITOR_VISUALIZATION 45 float2 vizUV : TEXCOORD7; 46 float4 lightCoord : TEXCOORD8; 47#endif 48}; 49 50v2f_meta vert_meta (appdata v) 51{ 52 v2f_meta o; 53 o.pos = UnityMetaVertexPosition(v.vertex, v.uv1.xy, v.uv2.xy, unity_LightmapST, unity_DynamicLightmapST); 54 55 o.uv01.xy = v.uv0; 56#if defined(LIGHTMAP_ON) 57 o.uv01.zw = v.uv1 * unity_LightmapST.xy + unity_LightmapST.zw; 58#else 59 o.uv01.zw = v.uv1; 60#endif 61 o.uv23.xy = v.uv2; 62 o.uv23.zw = v.uv3; 63 64 o.worldPos = mul(unity_ObjectToWorld, v.vertex); 65 o.objPos = v.vertex; 66 67 o.normal = v.normal; 68 o.tangent = v.tangent.xyz; 69 o.binormal = cross(o.normal, o.tangent) * v.tangent.w; 70 71 o.color = v.color; 72 73#ifdef EDITOR_VISUALIZATION 74 o.vizUV = 0; 75 o.lightCoord = 0; 76 if (unity_VisualizationMode == EDITORVIZ_TEXTURE) 77 o.vizUV = UnityMetaVizUV(unity_EditorViz_UVIndex, v.uv0.xy, v.uv1.xy, v.uv2.xy, unity_EditorViz_Texture_ST); 78 else if (unity_VisualizationMode == EDITORVIZ_SHOWLIGHTMASK) 79 { 80 o.vizUV = v.uv1.xy * unity_LightmapST.xy + unity_LightmapST.zw; 81 o.lightCoord = mul(unity_EditorViz_WorldToLight, mul(unity_ObjectToWorld, float4(v.vertex.xyz, 1))); 82 } 83#endif 84 return o; 85} 86 87float4 frag_meta (v2f_meta i) : SV_Target 88{ 89 i.normal = normalize(UnityObjectToWorldNormal(i.normal)); 90 i.tangent = normalize(UnityObjectToWorldNormal(i.tangent)); 91 i.binormal = normalize(UnityObjectToWorldNormal(i.binormal)); 92 93 float4x4 tangentToWorld = float4x4( 94 float4(i.tangent.x, i.binormal.x, i.normal.x, 0), 95 float4(i.tangent.y, i.binormal.y, i.normal.y, 0), 96 float4(i.tangent.z, i.binormal.z, i.normal.z, 0), 97 float4(0, 0, 0, 1) 98 ); 99 100 // Convert v2f_meta to v2f structure expected by YumPbr 101 v2f pbr_input; 102 pbr_input.uv01 = i.uv01; 103 pbr_input.uv23 = i.uv23; 104 pbr_input.objPos = float4(i.objPos, 1.0); 105 pbr_input.normal = i.normal; 106 pbr_input.tangent = i.tangent; 107#if defined(V2F_COLOR) 108 pbr_input.color = i.color; 109#endif 110 111 f2f f = (f2f) 0; 112 f.binormal = cross(i.tangent, i.normal); 113 f.eyeVec = i.worldPos - _WorldSpaceCameraPos; 114 f.viewDir = normalize(f.eyeVec); 115 116 YumPbr pbr = GetYumPbr(pbr_input, f); 117 118#if defined(_CUSTOM30) 119#if defined(_CUSTOM30_BASICCUBE) 120 Custom30Output c30_out = BasicCube(pbr_input, tangentToWorld); 121#elif defined(_CUSTOM30_BASICWEDGE) 122 Custom30Output c30_out = BasicWedge(pbr_input, tangentToWorld); 123#elif defined(_CUSTOM30_BASICPLATFORM) 124 Custom30Output c30_out = BasicPlatform(pbr_input, tangentToWorld); 125#elif defined(_CUSTOM30_RAINBOW) 126 Custom30Output c30_out = Rainbow(pbr_input, tangentToWorld); 127#else 128 Custom30Output c30_out = (Custom30Output) 0; 129 c30_out.normal = pbr_input.normal; 130 c30_out.objPos = pbr_input.objPos.xyz; 131#endif 132 // Update surface properties with volumetric results 133 pbr_input.normal = c30_out.normal; 134 pbr_input.worldPos = mul(unity_ObjectToWorld, float4(c30_out.objPos, 1)); 135 136 // Recalculate tangent space with new normal 137 tangentToWorld = float4x4( 138 float4(i.tangent.x, i.binormal.x, pbr_input.normal.x, 0), 139 float4(i.tangent.y, i.binormal.y, pbr_input.normal.y, 0), 140 float4(i.tangent.z, i.binormal.z, pbr_input.normal.z, 0), 141 float4(0, 0, 0, 1) 142 ); 143 144 // Get PBR properties with updated surface 145 pbr = GetYumPbr(pbr_input, tangentToWorld); 146#endif 147 148 UnityMetaInput o; 149 UNITY_INITIALIZE_OUTPUT(UnityMetaInput, o); 150 151#ifdef EDITOR_VISUALIZATION 152 o.Albedo = pbr.albedo.rgb; 153 o.VizUV = i.vizUV; 154 o.LightCoord = i.lightCoord; 155#else 156 // For lightmapping, we want the diffuse albedo with some contribution from rough metals 157 half roughness = pbr.roughness_perceptual; 158 half3 res = pbr.albedo.rgb; 159 res += pbr.metallic * roughness * 0.5; 160 o.Albedo = res; 161#endif 162 163 o.SpecularColor = lerp(0.04, pbr.albedo.rgb, pbr.metallic); 164 165#if defined(_EMISSION) 166 o.Emission = pbr.emission; 167#else 168 o.Emission = 0; 169#endif 170 171 return UnityMetaFragment(o); 172} 173 174#endif // UNITY_STANDARD_META_INCLUDED