yum-archive/Tooner

A toon shader for Unity's BIRP.

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

yumFix center eye toggle in rim lightinge51760a

master
5.0 KiB149 linesraw
1#ifndef __POI_INC
2#define __POI_INC
3
4/*
5MIT License
6
7Copyright (c) 2023 Poiyomi Inc.
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in all
17copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25SOFTWARE.
26*/
27
28static const float Epsilon = 1e-10;
29float3 RGBtoHCV(in float3 RGB)
30{
31  // Based on work by Sam Hocevar and Emil Persson
32  float4 P = (RGB.g < RGB.b) ? float4(RGB.bg, -1.0, 2.0 / 3.0) : float4(RGB.gb, 0.0, -1.0 / 3.0);
33  float4 Q = (RGB.r < P.x) ? float4(P.xyw, RGB.r) : float4(RGB.r, P.yzx);
34  float C = Q.x - min(Q.w, Q.y);
35  float H = abs((Q.w - Q.y) / (6 * C + Epsilon) + Q.z);
36  return float3(H, C, Q.x);
37}
38
39float3 RGBtoHSV(in float3 RGB)
40{
41  float3 HCV = RGBtoHCV(RGB);
42  float S = HCV.y / (HCV.z + Epsilon);
43  return float3(HCV.x, S, HCV.z);
44}
45
46float3 HUEtoRGB(in float H)
47{
48  float R = abs(H * 6 - 3) - 1;
49  float G = 2 - abs(H * 6 - 2);
50  float B = 2 - abs(H * 6 - 4);
51  return saturate(float3(R, G, B));
52}
53
54float3 HSVtoRGB(in float3 HSV)
55{
56  float3 RGB = HUEtoRGB(HSV.x);
57  return ((RGB - 1) * HSV.y + 1) * HSV.z;
58}
59
60float shEvaluateDiffuseL1Geomerics_local(float L0, float3 L1, float3 n)
61{
62  // average energy
63  float R0 = max(0, L0);
64
65  // avg direction of incoming light
66  float3 R1 = 0.5f * L1;
67
68  // directional brightness
69  float lenR1 = length(R1);
70
71  // linear angle between normal and direction 0-1
72  //float q = 0.5f * (1.0f + dot(R1 / lenR1, n));
73  //float q = dot(R1 / lenR1, n) * 0.5 + 0.5;
74  float q = dot(normalize(R1), n) * 0.5 + 0.5;
75  q = saturate(q); // Thanks to ScruffyRuffles for the bug identity.
76
77  // power for q
78  // lerps from 1 (linear) to 3 (cubic) based on directionality
79  float p = 1.0f + 2.0f * lenR1 / R0;
80
81  // dynamic range constant
82  // should vary between 4 (highly directional) and 0 (ambient)
83  float a = (1.0f - lenR1 / R0) / (1.0f + lenR1 / R0);
84
85  return R0 * (a + (1.0f - a) * (p + 1.0f) * pow(q, p));
86}
87
88half3 BetterSH9(half4 normal)
89{
90  float3 indirect;
91  float3 L0 = float3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w) + float3(unity_SHBr.z, unity_SHBg.z, unity_SHBb.z) / 3.0;
92  indirect.r = shEvaluateDiffuseL1Geomerics_local(L0.r, unity_SHAr.xyz, normal.xyz);
93  indirect.g = shEvaluateDiffuseL1Geomerics_local(L0.g, unity_SHAg.xyz, normal.xyz);
94  indirect.b = shEvaluateDiffuseL1Geomerics_local(L0.b, unity_SHAb.xyz, normal.xyz);
95  indirect = max(0, indirect);
96  indirect += SHEvalLinearL2(normal);
97  return indirect;
98}
99
100float calculateluminance(float3 color)
101{
102  return color.r * 0.299 + color.g * 0.587 + color.b * 0.114;
103}
104
105float3 getPoiLightingDirect(float3 normal) {
106  float3 magic = max(BetterSH9(normalize(unity_SHAr + unity_SHAg + unity_SHAb)), 0);
107  float3 normalLight = _LightColor0.rgb + BetterSH9(float4(0, 0, 0, 1));
108
109  float magiLumi = calculateluminance(magic);
110  float normaLumi = calculateluminance(normalLight);
111  float maginormalumi = magiLumi + normaLumi;
112
113  float magiratio = magiLumi / maginormalumi;
114  float normaRatio = normaLumi / maginormalumi;
115
116  float target = calculateluminance(magic * magiratio + normalLight * normaRatio);
117  float3 properLightColor = magic + normalLight;
118  float properLuminance = calculateluminance(magic + normalLight);
119  return properLightColor * max(0.0001, (target / properLuminance));
120}
121
122float3 getPoiLightingIndirect() {
123  return BetterSH9(float4(0, 0, 0, 1));
124}
125
126inline half Dither8x8Bayer(int x, int y)
127{
128  // Premultiplied by 1/64
129  const half dither[ 64 ] = {
130    0.015625, 0.765625, 0.203125, 0.953125, 0.06250, 0.81250, 0.25000, 1.00000,
131    0.515625, 0.265625, 0.703125, 0.453125, 0.56250, 0.31250, 0.75000, 0.50000,
132    0.140625, 0.890625, 0.078125, 0.828125, 0.18750, 0.93750, 0.12500, 0.87500,
133    0.640625, 0.390625, 0.578125, 0.328125, 0.68750, 0.43750, 0.62500, 0.37500,
134    0.046875, 0.796875, 0.234375, 0.984375, 0.03125, 0.78125, 0.21875, 0.96875,
135    0.546875, 0.296875, 0.734375, 0.484375, 0.53125, 0.28125, 0.71875, 0.46875,
136    0.171875, 0.921875, 0.109375, 0.859375, 0.15625, 0.90625, 0.09375, 0.84375,
137    0.671875, 0.421875, 0.609375, 0.359375, 0.65625, 0.40625, 0.59375, 0.34375
138  };
139  int r = y * 8 + x;
140  return dither[r];
141}
142
143half calcDither(half2 grabPos)
144{
145  return Dither8x8Bayer(glsl_mod(grabPos.x, 8), glsl_mod(grabPos.y, 8));
146}
147
148
149#endif  // __POI_INC