This repository has been archived by the owner on Sep 22, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
OkUnityBridge.cs
154 lines (141 loc) · 4.87 KB
/
OkUnityBridge.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright(c) 2021 Bjorn Ottosson
//
// 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.
using System;
using UnityEngine;
public static class OkUnityBridge
{
public static double Clamp (in double a, in double lb = 0.0d, in double ub = 1.0d)
{
return Math.Min (Math.Max (a, lb), ub);
}
public static double DistAngleUnsigned (in double origin, in double dest, in double range = 1.0d)
{
double halfRange = range * 0.5d;
return halfRange - Math.Abs (Math.Abs (RemFloor (dest, range) - RemFloor (origin, range)) - halfRange);
}
public static double Lerp (in double a, in double b, in double t = 0.5d)
{
return (1.0d - t) * a + t * b;
}
public static double LerpAngleFar (in double origin, in double dest, in double t = 0.5d, in double range = 1.0d)
{
double halfRange = range * 0.5d;
double o = RemFloor (origin, range);
double d = RemFloor (dest, range);
double diff = d - o;
double u = 1.0d - t;
if (diff == 0.0d || (o < d && diff < halfRange))
{
return RemFloor (u * (o + range) + t * d, range);
}
else if (o > d && diff > -halfRange)
{
return RemFloor (u * o + t * (d + range), range);
}
else
{
return u * o + t * d;
}
}
public static double LerpAngleNear (in double origin, in double dest, in double t = 0.5d, in double range = 1.0d)
{
double halfRange = range * 0.5d;
double o = RemFloor (origin, range);
double d = RemFloor (dest, range);
double diff = d - o;
double u = 1.0d - t;
if (diff == 0.0d)
{
return o;
}
else if (o < d && diff > halfRange)
{
return RemFloor (u * (o + range) + t * d, range);
}
else if (o > d && diff < -halfRange)
{
return RemFloor (u * o + t * (d + range), range);
}
else
{
return u * o + t * d;
}
}
public static double LerpAngleCcw (in double origin, in double dest, in double t = 0.5d, in double range = 1.0d)
{
double o = RemFloor (origin, range);
double d = RemFloor (dest, range);
double diff = d - o;
double u = 1.0d - t;
if (diff == 0.0d)
{
return o;
}
else if (o > d)
{
return RemFloor (u * o + t * (d + range), range);
}
else
{
return u * o + t * d;
}
}
public static double LerpAngleCw (in double origin, in double dest, in double t = 0.5d, in double range = 1.0d)
{
double o = RemFloor (origin, range);
double d = RemFloor (dest, range);
double diff = d - o;
double u = 1.0d - t;
if (diff == 0.0d)
{
return d;
}
else if (o < d)
{
return RemFloor (u * (o + range) + t * d, range);
}
else
{
return u * o + t * d;
}
}
public static (double L, double a, double b) LerpLab (in (double L, double a, double b) a, in (double L, double a, double b) b,
double t = 0.5d)
{
return (
L: Lerp (a.L, b.L, t),
a : Lerp (a.a, b.a, t),
b : Lerp (a.b, b.b, t));
}
public static double RemFloor (in double a, in double b)
{
return b != 0.0d ? a - b * Math.Floor (a / b) : a;
}
public static Color RgbToColor (in (double r, double g, double b) rgb, in double alpha = 1.0d)
{
float rc01 = (float) Clamp (rgb.r, 0.0d, 1.0d);
float gc01 = (float) Clamp (rgb.g, 0.0d, 1.0d);
float bc01 = (float) Clamp (rgb.b, 0.0d, 1.0d);
float ac01 = (float) Clamp (alpha, 0.0d, 1.0d);
return new Color (
r: rc01,
g: gc01,
b: bc01,
a: ac01);
}
}