yum-archive/Tooner
A toon shader for Unity's BIRP.
git clone https://git.yummers.dev/yum-archive/Tooner
dfe6371
master
1#include "pema99.cginc" 2 3#ifndef __IQ_SDF_INC 4#define __IQ_SDF_INC 5 6// The MIT License 7// Copyright © 2019-2024 Inigo Quilez 8// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 10float distance_from_octahedron(in float3 p) 11{ 12 float s = 1.0; 13 float3 pp = abs(p); 14 float m = pp.x+pp.y+pp.z-s; 15 float3 q; 16 if( 3.0*pp.x < m ) q = pp.xyz; 17 else if( 3.0*pp.y < m ) q = pp.yzx; 18 else if( 3.0*pp.z < m ) q = pp.zxy; 19 else return m*0.57735027; 20 21 float k = clamp(0.5*(q.z-q.y+s),0.0,s); 22 return length(float3(q.x,q.y-s+k,q.z-k)); 23} 24 25float distance_from_sphere(float3 p, float r) 26{ 27 return length(p) - r; 28} 29 30float distance_from_torus(float3 p, float2 t) 31{ 32 float2 q = float2(length(p.xz) - t.x, p.y); 33 return length(q) - t.y; 34} 35 36float distance_from_capped_torus(float3 p, float2 sc, float ra, float rb) 37{ 38 p.x = abs(p.x); 39 float k = (sc.y*p.x>sc.x*p.y) ? dot(p.xy,sc) : length(p.xy); 40 return sqrt(dot(p,p) + ra*ra - 2.0*ra*k) - rb; 41} 42 43float distance_from_cut_sphere( in float3 p, in float r, in float h ) 44{ 45 float w = sqrt(r*r-h*h); // constant for a given shape 46 47 float2 q = float2( length(p.xz), p.y ); 48 49 float s = max( (h-r)*q.x*q.x+w*w*(h+r-2.0*q.y), h*q.x-w*q.y ); 50 51 return (s<0.0) ? length(q)-r : 52 (q.x<w) ? h - q.y : 53 length(q-float2(w,h)); 54} 55 56float distance_from_cut_hollow_sphere( float3 p, float r, float h, float t ) 57{ 58 float2 q = float2( length(p.xz), p.y ); 59 60 float w = sqrt(r*r-h*h); 61 62 return ((h*q.x<w*q.y) ? length(q-float2(w,h)) : 63 abs(length(q)-r) ) - t; 64} 65 66float distance_from_box(float3 p, float3 b) 67{ 68 float3 q = abs(p) - b; 69 return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0); 70} 71 72float distance_from_round_box(float3 p, float3 b, float r) 73{ 74 float3 q = abs(p) - b + r; 75 return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0) - r; 76} 77 78float distance_from_box_frame(float3 p, float3 b, float e) 79{ 80 p = abs(p)-b; 81 float3 q = abs(p+e)-e; 82 83 return min(min( 84 length(max(float3(p.x,q.y,q.z),0.0))+min(max(p.x,max(q.y,q.z)),0.0), 85 length(max(float3(q.x,p.y,q.z),0.0))+min(max(q.x,max(p.y,q.z)),0.0)), 86 length(max(float3(q.x,q.y,p.z),0.0))+min(max(q.x,max(q.y,p.z)),0.0)); 87} 88 89float distance_from_pyramid(float3 p, float h, bool invert) 90{ 91 float m2 = h*h + 0.25; 92 93 // symmetry 94 p.xz = abs(p.xz); // do p=abs(p) instead for double pyramid 95 p.xz = (p.z>p.x) ? p.zx : p.xz; 96 p.xz -= 0.5; 97 98 // project into face plane (2D) 99 float3 q = float3( p.z, h*p.y-0.5*p.x, h*p.x+0.5*p.y); 100 101 float s = max(-q.x,0.0); 102 float t = clamp( (q.y-0.5*q.x)/(m2+0.25), 0.0, 1.0 ); 103 104 float a = m2*(q.x+s)*(q.x+s) + q.y*q.y; 105 float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t); 106 107 float d2 = max(-q.y,q.x*m2+q.y*0.5) < 0.0 ? 0.0 : min(a,b); 108 109 // recover 3D and scale, and add sign 110 return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y));; 111} 112 113float distance_from_plane(float3 p, float3 n, float h) 114{ 115 // n must be normalized 116 return dot(p,n) + h; 117} 118 119float distance_from_cylinder(float3 p, float3 c) 120{ 121 return length(p.xz-c.xy)-c.z; 122} 123 124float distance_from_capped_cylinder(float3 p, float h, float r) 125{ 126 float2 d = abs(float2(length(p.xz),p.y)) - float2(r,h); 127 return min(max(d.x,d.y),0.0) + length(max(d,0.0)); 128} 129 130float distance_from_hex_prism(float3 p, float2 h) 131{ 132 float3 q = abs(p); 133 134 const float3 k = float3(-0.8660254, 0.5, 0.57735); 135 p = abs(p); 136 p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy; 137 float2 d = float2( 138 length(p.xy - float2(clamp(p.x, -k.z*h.x, k.z*h.x), h.x))*sign(p.y - h.x), 139 p.z-h.y ); 140 return min(max(d.x,d.y),0.0) + length(max(d,0.0)); 141} 142 143float distance_from_capsule(float3 p, float3 a, float3 b, float r) 144{ 145 float3 pa = p - a, ba = b - a; 146 float h = clamp( dot(pa,ba)/dot(ba,ba), 0.0, 1.0 ); 147 return length( pa - ba*h ) - r; 148} 149 150/* 151float sdHexPrism( vec3 p, vec2 h ) 152{ 153} 154*/ 155 156float3 op_rep(in float3 p, in float3 c) 157{ 158 return glsl_mod(p+0.5*c,c)-0.5*c; 159} 160 161float op_sub(float d1, float d2) 162{ 163 return max(-d1,d2); 164} 165 166// End licensed section 167 168#endif // __IQ_SDF_INC