yum-archive/Tooner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/Tooner
4ec13bd
master
1/* 2 * MIT License 3 * 4 * Copyright (c) 2023-2024 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) || defined(_GIMMICK_LETTER_GRID_COLOR_WAVE) || defined(_GIMMICK_AL_CHROMA_00_HUE_SHIFT) || defined(_GIMMICK_FOG_00) || defined(_GIMMICK_DS2) || defined(_GIMMICK_EPILEPSY_MODE) 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 return saturate(mul(rgb_to_xyz, c)); 45} 46 47// Weights: https://en.wikipedia.org/wiki/SRGB 48float3 XYZtoLRGB(float3 c) { 49 float3x3 xyz_to_rgb = float3x3( 50 3.24062548, -1.53720797, -0.4986286, 51 -0.96893071, 1.87575606, 0.04151752, 52 0.05571012, -0.20402105, 1.05699594); 53 return saturate(mul(xyz_to_rgb, c)); 54} 55 56// Source: https://bottosson.github.io/posts/oklab/ 57float3 XYZtoOKLAB(float3 c) { 58 float3x3 m1 = float3x3( 59 0.8189330101, 0.3618667424, -0.1288597137, 60 0.0329845436, 0.9293118715, 0.0361456387, 61 0.0482003018, 0.2643662691, 0.6338517070); 62 float3x3 m2 = float3x3( 63 0.2104542553, 0.7936177850, -0.0040720468, 64 1.9779984951, -2.4285922050, 0.4505937099, 65 0.0259040371, 0.7827717662, -0.8086757660); 66 c = mul(m1, c); 67 c = pow(c, 0.33333333333); 68 return mul(m2, c); 69} 70 71// Source: https://bottosson.github.io/posts/oklab/ 72float3 OKLABtoXYZ(float3 c) { 73 float3x3 m1i = float3x3( 74 1.22701385, -0.55779998, 0.28125615, 75 -0.04058018, 1.11225687, -0.07167668, 76 -0.07638128, -0.42148198, 1.58616322); 77 float3x3 m2i = float3x3( 78 1.00000000, 0.39633779, 0.21580376, 79 1.00000001, -0.10556134, -0.06385417, 80 1.00000005, -0.08948418, -1.29148554); 81 c = mul(m2i, c); 82 c = pow(c, 3.0); 83 return mul(m1i, c); 84} 85 86// Source: https://bottosson.github.io/posts/oklab/ 87float3 OKLABtoOKLCH(float3 c) { 88 float c_ = length(c.yz); 89 float h_ = atan2(c.z, c.y); 90 return float3(c.x, c_, h_); 91} 92 93// Source: https://bottosson.github.io/posts/oklab/ 94// Note: hue must be in units of radians. 95float3 OKLCHtoOKLAB(float3 c) { 96 float a = c.y * cos(c.z); 97 float b = c.y * sin(c.z); 98 return float3(c.x, a, b); 99} 100 101float3 LRGBtoOKLAB(float3 c) { 102 return XYZtoOKLAB(LRGBtoXYZ(c)); 103} 104 105float3 OKLABtoLRGB(float3 c) { 106 return XYZtoLRGB(OKLABtoXYZ(c)); 107} 108 109float3 LRGBtoOKLCH(float3 c) { 110 return OKLABtoOKLCH(XYZtoOKLAB(LRGBtoXYZ(c))); 111} 112 113float3 OKLCHtoLRGB(float3 c) { 114 return XYZtoLRGB(OKLABtoXYZ(OKLCHtoOKLAB(c))); 115} 116 117#endif // _OKLAB 118 119#endif // __OKLAB_INC 120