yum-archive/Tooner

A toon shader for Unity's BIRP.

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

yumInitial commitdcb15c0

v0.0.0
2.6 KiB115 linesraw
1#ifndef __MATH_INC
2#define __MATH_INC
3
4#include "pema99.cginc"
5
6float4 qmul(float4 q1, float4 q2)
7{
8  return float4(
9      q2.xyz * q1.w + q1.xyz * q2.w + cross(q1.xyz, q2.xyz),
10      q1.w * q2.w - dot(q1.xyz, q2.xyz)
11      );
12}
13
14// Vector rotation with a quaternion
15// http://mathworld.wolfram.com/Quaternion.html
16float3 rotate_vector(float3 v, float4 r)
17{
18  float4 r_c = r * float4(-1, -1, -1, 1);
19  return qmul(r, qmul(float4(v, 0), r_c)).xyz;
20}
21
22float4 get_quaternion(float3 axis_normal, float theta) {
23  return float4(
24      axis_normal * sin(theta / 2), cos(theta / 2));
25}
26
27// Differentiable approximation of the standard `max` function.
28float dmax(float a, float b, float k)
29{
30  return log2(exp2(k * a) + exp2(k * b)) / k;
31}
32
33// Differentiable approximation of the standard `min` function.
34float dmin(float a, float b, float k)
35{
36  return -1.0 * dmax(-1.0 * a, -1.0 * b, k);
37}
38
39float dabs(float a, float k)
40{
41  return log2(exp2(k * a) + exp2(-1.0 * k * a));
42}
43
44float rand(uint seed) {
45  seed = seed * 747796405 + 2891336453;
46  uint result = ((seed >> ((seed >> 28) + 4)) ^ seed) * 277803737;
47  result = (result >> 22) ^ result;
48  return result / 4294967295.0;
49}
50
51// Generate a random number on [0, 1].
52float rand2(float2 p)
53{
54  return glsl_mod(sin(dot(p, float2(561.0, 885.0))) * 776.2, 1.0);
55}
56
57// Generate a random number on [0, 1].
58float rand3(float3 p)
59{
60  return glsl_mod(sin(dot(p, float3(897.0, 367.0, 197.0))) * 1073.6, 1.0);
61}
62
63float length2(float2 p)
64{
65  return p.x * p.x + p.y * p.y;
66}
67
68// 3 dimensional value noise. `p` is assumed to be a point inside a unit cube.
69// Theory: https://en.wikipedia.org/wiki/Value_noise
70float vnoise3d(float3 p)
71{
72  float3 pu = floor(p);
73  float3 pv = glsl_mod(frac(p), 1.0);
74
75  // Assign random numbers to the corner of a cube.
76  float n000 = rand3(pu + float3(0,0,0));
77  float n001 = rand3(pu + float3(0,0,1));
78  float n010 = rand3(pu + float3(0,1,0));
79  float n011 = rand3(pu + float3(0,1,1));
80  float n100 = rand3(pu + float3(1,0,0));
81  float n101 = rand3(pu + float3(1,0,1));
82  float n110 = rand3(pu + float3(1,1,0));
83  float n111 = rand3(pu + float3(1,1,1));
84
85  float n00 = lerp(n000, n001, pv.z);
86  float n01 = lerp(n010, n011, pv.z);
87  float n10 = lerp(n100, n101, pv.z);
88  float n11 = lerp(n110, n111, pv.z);
89
90  float n0 = lerp(n00, n01, pv.y);
91  float n1 = lerp(n10, n11, pv.y);
92  
93  float n = lerp(n0, n1, pv.x);
94
95  return n;
96}
97
98float fbm(float3 p, const int n_octaves, float w)
99{
100  float g = exp2(-w);
101  float a = 1.0;
102  float p_scale = 1.0;
103
104  float res = 0.0;
105  for (int i = 0; i < n_octaves; i++) {
106    res += a * vnoise3d(p * p_scale);
107
108    p_scale /= w;
109    a *= g;
110  }
111  return res;
112}
113
114#endif  // __MATH_INC
115