yum-archive/Tooner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum-archive/Tooner

yumBegin work on zwrite abominationdfe6371

master
5.2 KiB158 linesraw
1#include "UnityCG.cginc"
2
3#include "atrix256.cginc"
4#include "audiolink.cginc"
5#include "gerstner.cginc"
6#include "globals.cginc"
7#include "pbr_overlay.cginc"
8#include "interpolators.cginc"
9#include "trochoid_math.cginc"
10
11#ifndef __MOCHIE_SHADOW_CASTER_INC
12#define __MOCHIE_SHADOW_CASTER_INC
13
14// Source: https://github.com/cnlohr/shadertrixx?tab=readme-ov-file#shadowcasting
15// MIT License
16// 
17// NOTE: Much content here is originally from others.  Content in third party
18// folder may not be fully MIT-licensable. 
19// 
20// Copyright (c) 2021 cnlohr, et. al.
21// 
22// All other content in this repository falls under the following terms:
23// 
24// Permission is hereby granted, free of charge, to any person obtaining a copy
25// of this software and associated documentation files (the "Software"), to deal
26// in the Software without restriction, including without limitation the rights
27// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28// copies of the Software, and to permit persons to whom the Software is
29// furnished to do so, subject to the following conditions:
30// 
31// The above copyright notice and this permission notice shall be included in all
32// copies or substantial portions of the Software.
33// 
34// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
40// SOFTWARE.
41
42#pragma multi_compile_instancing
43#pragma multi_compile_shadowcaster
44
45v2f vert (appdata v){
46#if defined(_DISCARD)
47  if (_Discard_Enable_Dynamic) {
48    return (v2f) (0.0 / 0.0);
49  }
50#endif
51#if defined(_GIMMICK_ZWRITE_ABOMINATION)
52  return (v2f) (0.0 / 0.0);
53#endif
54#if defined(_GIMMICK_BOX_DISCARD)
55  if (_Gimmick_Box_Discard_Enable_Static) {
56    float3 p = getCenterCamPos();
57    float3 c1 = _Gimmick_Box_Discard_Corner_1;
58    float3 c2 = _Gimmick_Box_Discard_Corner_2;
59    bool inside = (p.x >= c1.x && p.x <= c2.x &&
60        p.y >= c1.y && p.y <= c2.y &&
61        p.z >= c1.z && p.z <= c2.z);
62    if (_Gimmick_Box_Discard_Invert && !inside ||
63        !_Gimmick_Box_Discard_Invert && inside) {
64      return (v2f) (0.0 / 0.0);
65    }
66  }
67#endif
68#if defined(_TROCHOID)
69  {
70    v.vertex.xyz = cart_to_troch_map(v.vertex.xyz);
71  }
72#endif  // _TROCHOID
73#if !defined(_SCROLL) && defined(_GIMMICK_GERSTNER_WATER)
74  {
75    GerstnerParams p = getGerstnerParams();
76    v.vertex.xyz = gerstner_vert(v.vertex.xyz, p);
77  }
78#endif
79#if defined(_GIMMICK_FOG_00)
80  {
81    float3 ro = mul(unity_WorldToObject, float4(_WorldSpaceCameraPos, 1));
82    float3 rd = normalize(ro - v.vertex.xyz);
83    v.vertex.xyz = ro + rd * _Gimmick_Fog_00_Radius;
84  }
85#endif
86	v2f o = (v2f)0;
87	UNITY_SETUP_INSTANCE_ID(v);
88	UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
89	TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
90  o.uv0 = v.uv0;
91  o.worldPos = mul(unity_ObjectToWorld, v.vertex);
92
93	float2 suv = o.pos * float2(0.5, 0.5 * _ProjectionParams.x);
94  o.screenPos = TransformStereoScreenSpaceTex(suv + 0.5 * o.pos.w, o.pos.w);
95
96  return o;
97}
98
99float4 frag (v2f i) : SV_Target {
100  ToonerData tdata;
101  {
102    float3 full_vec_eye_to_geometry = i.worldPos - _WorldSpaceCameraPos;
103    float3 world_dir = normalize(i.worldPos - _WorldSpaceCameraPos);
104    float perspective_divide = 1.0 / i.pos.w;
105    float perspective_factor = length(full_vec_eye_to_geometry * perspective_divide);
106    tdata.screen_uv = i.screenPos.xy * perspective_divide;
107    tdata.screen_uv_round = floor(tdata.screen_uv * _ScreenParams.xy);
108  }
109
110#if defined(_BASECOLOR_MAP)
111  float4 albedo = _MainTex.SampleBias(GET_SAMPLER_PBR, UV_SCOFF(i, _MainTex_ST, 0), _Global_Sample_Bias);
112  albedo *= _Color;
113#else
114  float4 albedo = _Color;
115#endif  // _BASECOLOR_MAP
116
117  PbrOverlay ov;
118  getOverlayAlbedoRoughnessMetallic(ov, i);
119  float roughness = 0;
120  float metallic = 0;
121  float overlay_glitter_mask;
122  float one = 1;
123  mixOverlayAlbedoRoughnessMetallic(albedo, roughness, metallic, ov, one, overlay_glitter_mask);
124
125#if defined(_RENDERING_CUTOUT)
126#if defined(_FRAME_COUNTER)
127  float frame = floor(_Frame_Counter);
128#else
129  float frame = 0;
130  if (AudioLinkIsAvailable()) {
131    frame = ((float) AudioLinkData(ALPASS_GENERALVU + int2(1, 0)).x);
132  }
133#endif  // _FRAME_COUNTER
134#if defined(_RENDERING_CUTOUT_STOCHASTIC)
135  float ar = rand2(i.uv0);
136  clip(albedo.a - ar);
137#elif defined(_RENDERING_CUTOUT_IGN)
138  float ar = ign(floor(tdata.screen_uv_round * _Rendering_Cutout_Noise_Scale) + _Rendering_Cutout_Ign_Seed);
139  ar = frac(ar + frame * PHI * _Rendering_Cutout_Speed);
140  clip(albedo.a - ar);
141#elif defined(_RENDERING_CUTOUT_NOISE_MASK)
142  float ar = _Rendering_Cutout_Noise_Mask.SampleLevel(point_repeat_s,
143      tdata.screen_uv * _ScreenParams.xy *
144      _Rendering_Cutout_Noise_Mask_TexelSize.xy, 0);
145  ar = frac(ar + frame * PHI * _Rendering_Cutout_Speed);
146  clip(albedo.a - ar);
147#else
148  clip(albedo.a - _Alpha_Cutoff);
149#endif
150  albedo.a = 1;
151#endif  // _RENDERING_CUTOUT
152
153	UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
154	return 0;
155}
156
157#endif  // __MOCHIE_SHADOW_CASTER_INC
158