yum-archive/Tooner

A toon shader for Unity's BIRP.

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

yumInitial commitdcb15c0

v0.0.0
3.4 KiB126 linesraw
1/*
2 * MIT License
3 *
4 * Copyright (c) 2023 yum_food
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all
16 * copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * THE
26 * SOFTWARE.
27 */
28
29#ifndef __OKLAB_INC
30#define __OKLAB_INC
31
32#if defined(_OKLAB)
33
34// Utilities relating to the OKLAB color space, as defined here:
35//   https://bottosson.github.io/posts/oklab/
36// Code is derived from the samples there, which are in the public domain.
37
38// Weights: https://en.wikipedia.org/wiki/SRGB
39float3 LRGBtoXYZ(float3 c) {
40  float3x3 rgb_to_xyz = float3x3(
41      0.4124, 0.3576, 0.1805,
42      0.2126, 0.7152, 0.0722,
43      0.0193, 0.1192, 0.9505
44      );
45  return mul(rgb_to_xyz, c);
46}
47
48// Weights: https://en.wikipedia.org/wiki/SRGB
49float3 XYZtoLRGB(float3 c) {
50  float3x3 xyz_to_rgb = float3x3(
51      3.24062548, -1.53720797, -0.4986286,
52      -0.96893071,  1.87575606,  0.04151752,
53      0.05571012, -0.20402105,  1.05699594
54      );
55  return mul(xyz_to_rgb, c);
56}
57
58// Source: https://bottosson.github.io/posts/oklab/
59float3 XYZtoOKLAB(float3 c) {
60  float3x3 m1 = float3x3(
61      0.8189, 0.3618, -0.1288,
62      0.0329, 0.9293, 0.0361,
63      0.0482, 0.2643, 0.6338
64      );
65  float3x3 m2 = float3x3(
66      0.2104, 0.7936, -0.0040,
67      1.9779, -2.4285, 0.4505,
68      0.0259, 0.7827, -0.8086
69      );
70  c = mul(m1, c);
71  c = pow(c, 0.33333333333);
72  return mul(m2, c);
73}
74
75// Source: https://bottosson.github.io/posts/oklab/
76float3 OKLABtoXYZ(float3 c) {
77  float3x3 m1i = float3x3(
78      1.22700842, -0.5576564 ,  0.28111404,
79      -0.04047048,  1.11219073, -0.07157255,
80      -0.07643651, -0.42138367,  1.58625265
81      );
82  float3x3 m2i = float3x3(
83      1.00003964,  0.39638005,  0.21589049,
84      0.99998945, -0.10553958, -0.06374665,
85      0.99999105, -0.08946276, -1.291495
86      );
87  c = mul(m2i, c);
88  c = pow(c, 3);
89  return mul(m1i, c);
90}
91
92// Source: https://bottosson.github.io/posts/oklab/
93float3 OKLABtoOKLCH(float3 c) {
94  float c_ = length(c.yz);
95  float h_ = atan2(c.z, c.y);
96  return float3(c.x, c_, h_);
97}
98
99// Source: https://bottosson.github.io/posts/oklab/
100// Note: hue must be in units of radians.
101float3 OKLCHtoOKLAB(float3 c) {
102  float a = c.y * cos(c.z);
103  float b = c.y * sin(c.z);
104  return float3(c.x, a, b);
105}
106
107float3 LRGBtoOKLAB(float3 c) {
108  return XYZtoOKLAB(LRGBtoXYZ(c));
109}
110
111float3 OKLABtoLRGB(float3 c) {
112  return XYZtoLRGB(OKLABtoXYZ(c));
113}
114
115float3 LRGBtoOKLCH(float3 c) {
116  return OKLABtoOKLCH(XYZtoOKLAB(LRGBtoXYZ(c)));
117}
118
119float3 OKLCHtoLRGB(float3 c) {
120  return XYZtoLRGB(OKLABtoXYZ(OKLCHtoOKLAB(c)));
121}
122
123#endif  // _OKLAB
124
125#endif  // __OKLAB_INC
126