-
Notifications
You must be signed in to change notification settings - Fork 15
/
spr.lua
838 lines (692 loc) · 20.6 KB
/
spr.lua
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
--!strict
--!native
---------------------------------------------------------------------
-- spr - Spring-driven motion library
--
-- Copyright (c) 2024 Fractality. All rights reserved.
-- Released under the MIT license.
--
-- Docs & license can be found at https://github.com/Fraktality/spr
--
-- API Summary:
--
-- spr.target(
-- Instance obj,
-- number dampingRatio,
-- number undampedFrequency,
-- dict<string, Variant> targetProperties)
--
-- Animates the given properties towardes the target values,
-- given damping ratio and undamped frequency.
--
--
-- spr.stop(
-- Instance obj[,
-- string property])
--
-- Stops the specified property on an Instance from animating.
-- If no property is specified, all properties of the Instance
-- will stop animating.
--
-- Visualizer: https://www.desmos.com/calculator/rzvw27ljh9
---------------------------------------------------------------------
local STRICT_RUNTIME_TYPES = true -- assert on parameter and property type mismatch
local SLEEP_OFFSET_SQ_LIMIT = (1/3840)^2 -- square of the offset sleep limit
local SLEEP_VELOCITY_SQ_LIMIT = 1e-2^2 -- square of the velocity sleep limit
local SLEEP_ROTATION_DIFF = math.rad(0.01) -- rad
local SLEEP_ROTATION_VELOCITY = math.rad(0.1) -- rad/s
local EPS = 1e-5 -- epsilon for stability checks around pathological frequency/damping values
local RunService: RunService = game:GetService("RunService")
local pi = math.pi
local exp = math.exp
local sin = math.sin
local cos = math.cos
local min = math.min
local max = math.max
local sqrt = math.sqrt
local atan2 = math.atan2
local round = math.round
local function magnitudeSq(vec: {number})
local out = 0
for _, v in vec do
out += v^2
end
return out
end
local function distanceSq(vec0: {number}, vec1: {number})
local out = 0
for i0, v0 in vec0 do
out += (vec1[i0] - v0)^2
end
return out
end
type TypeMetadata<T> = {
springType: (dampingRatio: number, frequency: number, pos: number, typedat: TypeMetadata<T>, rawTarget: T) -> LinearSpring<T>,
toIntermediate: (T) -> {number},
fromIntermediate: ({number}) -> T,
}
-- Spring for an array of linear values
local LinearSpring = {}
type LinearSpring<T> = typeof(setmetatable({} :: {
d: number,
f: number,
g: {number},
p: {number},
v: {number},
typedat: TypeMetadata<T>,
rawTarget: T,
}, LinearSpring))
do
LinearSpring.__index = LinearSpring
function LinearSpring.new<T>(dampingRatio: number, frequency: number, pos: T, rawGoal: T, typedat)
local linearPos = typedat.toIntermediate(pos)
return setmetatable(
{
d = dampingRatio,
f = frequency,
g = linearPos,
p = linearPos,
v = table.create(#linearPos, 0),
typedat = typedat,
rawGoal = rawGoal
},
LinearSpring
)
end
function LinearSpring.setGoal<T>(self, goal: T)
self.rawGoal = goal
self.g = self.typedat.toIntermediate(goal)
end
function LinearSpring.setDampingRatio<T>(self: LinearSpring<T>, dampingRatio: number)
self.d = dampingRatio
end
function LinearSpring.setFrequency<T>(self: LinearSpring<T>, frequency: number)
self.f = frequency
end
function LinearSpring.canSleep<T>(self)
if magnitudeSq(self.v) > SLEEP_VELOCITY_SQ_LIMIT then
return false
end
if distanceSq(self.p, self.g) > SLEEP_OFFSET_SQ_LIMIT then
return false
end
return true
end
function LinearSpring.step<T>(self: LinearSpring<T>, dt: number)
-- Advance the spring simulation by dt seconds.
-- Take the damped harmonic oscillator ODE:
-- f^2*(X[t] - g) + 2*d*f*X'[t] + X''[t] = 0
-- Where X[t] is position at time t, g is target position,
-- f is undamped angular frequency, and d is damping ratio.
-- Apply constant initial conditions:
-- X[0] = p0
-- X'[0] = v0
-- Solve the IVP to get analytic expressions for X[t] and X'[t].
-- The solution takes one of three forms for 0<=d<1, d=1, and d>1
local d = self.d
local f = self.f*(2*pi) -- Hz -> Rad/s
local g = self.g
local p = self.p
local v = self.v
if d == 1 then -- critically damped
local q = exp(-f*dt)
local w = dt*q
local c0 = q + w*f
local c2 = q - w*f
local c3 = w*f*f
for idx = 1, #p do
local o = p[idx] - g[idx]
p[idx] = o*c0 + v[idx]*w + g[idx]
v[idx] = v[idx]*c2 - o*c3
end
elseif d < 1 then -- underdamped
local q = exp(-d*f*dt)
local c = sqrt(1 - d*d)
local i = cos(dt*f*c)
local j = sin(dt*f*c)
-- Damping ratios approaching 1 can cause division by very small numbers.
-- To mitigate that, group terms around z=j/c and find an approximation for z.
-- Start with the definition of z:
-- z = sin(dt*f*c)/c
-- Substitute a=dt*f:
-- z = sin(a*c)/c
-- Take the Maclaurin expansion of z with respect to c:
-- z = a - (a^3*c^2)/6 + (a^5*c^4)/120 + O(c^6)
-- z ≈ a - (a^3*c^2)/6 + (a^5*c^4)/120
-- Rewrite in Horner form:
-- z ≈ a + ((a*a)*(c*c)*(c*c)/20 - c*c)*(a*a*a)/6
local z
if c > EPS then
z = j/c
else
local a = dt*f
z = a + ((a*a)*(c*c)*(c*c)/20 - c*c)*(a*a*a)/6
end
-- Frequencies approaching 0 present a similar problem.
-- We want an approximation for y as f approaches 0, where:
-- y = sin(dt*f*c)/(f*c)
-- Substitute b=dt*c:
-- y = sin(b*c)/b
-- Now reapply the process from z.
local y
if f*c > EPS then
y = j/(f*c)
else
local b = f*c
y = dt + ((dt*dt)*(b*b)*(b*b)/20 - b*b)*(dt*dt*dt)/6
end
for idx = 1, #p do
local o = p[idx] - g[idx]
p[idx] = (o*(i + z*d) + v[idx]*y)*q + g[idx]
v[idx] = (v[idx]*(i - z*d) - o*(z*f))*q
end
else -- overdamped
local c = sqrt(d*d - 1)
local r1 = -f*(d + c)
local r2 = -f*(d - c)
local ec1 = exp(r1*dt)
local ec2 = exp(r2*dt)
for idx = 1, #p do
local o = p[idx] - g[idx]
local co2 = (v[idx] - o*r1)/(2*f*c)
local co1 = ec1*(o - co2)
p[idx] = co1 + co2*ec2 + g[idx]
v[idx] = co1*r1 + co2*ec2*r2
end
end
return self.typedat.fromIntermediate(self.p)
end
end
local RotationSpring = {}
type RotationSpring = typeof(setmetatable({} :: {
d: number,
f: number,
g: CFrame,
p: CFrame,
v: Vector3,
}, RotationSpring))
do
RotationSpring.__index = RotationSpring
function RotationSpring.new(d: number, f: number, p: CFrame, g: CFrame)
return setmetatable(
{
d = d,
f = f,
g = g:Orthonormalize(),
p = p:Orthonormalize(),
v = Vector3.zero
},
RotationSpring
)
end
function RotationSpring.setGoal(self: RotationSpring, value: CFrame)
self.g = value:Orthonormalize()
end
function RotationSpring.setDampingRatio(self: RotationSpring, dampingRatio: number)
self.d = dampingRatio
end
function RotationSpring.setFrequency(self: RotationSpring, frequency: number)
self.f = frequency
end
-- evaluate dot products in high precision
local function dot(v0: Vector3, v1: Vector3)
return v0.X*v1.X + v0.Y*v1.Y + v0.Z*v1.Z
end
local function areRotationsClose(c0: CFrame, c1: CFrame)
local rx = dot(c0.XVector, c1.XVector)
local ry = dot(c0.YVector, c1.YVector)
local rz = dot(c0.ZVector, c1.ZVector)
local trace = rx + ry + rz
return trace > 1 + 2*cos(SLEEP_ROTATION_DIFF)
end
local function angleDiff(c0: CFrame, c1: CFrame)
local x = dot(c0.XVector, c1.XVector)
local y = dot(c0.YVector, c1.YVector)
local z = dot(c0.ZVector, c1.ZVector)
local w = x + y + z - 1
return atan2(sqrt(max(0, 1 - w*w*0.25)), w*0.5)
end
-- gives approx. 21% accuracy improvement over CFrame.fromAxisAngle near poles
local function fromAxisAngle(axis: Vector3, angle: number)
local c = cos(angle)
local s = sin(angle)
local x, y, z = axis.X, axis.Y, axis.Z
local mxy = x*y*(1 - c)
local myz = y*z*(1 - c)
local mzx = z*x*(1 - c)
local rx = Vector3.new(x*x*(1 - c) + c, mxy + z*s, mzx - y*s)
local ry = Vector3.new(mxy - z*s, y*y*(1 - c) + c, myz + x*s)
local rz = Vector3.new(mzx + y*s, myz - x*s, z*z*(1 - c) + c)
return CFrame.fromMatrix(Vector3.zero, rx, ry, rz):Orthonormalize()
end
local function rotateAxis(r0: Vector3, c1: CFrame)
local c0 = CFrame.identity
local mag = r0.Magnitude
if mag > 1e-6 then
c0 = fromAxisAngle(r0.Unit, mag)
end
return c0 * c1
end
-- axis*angle difference between two cframes
local function axisAngleDiff(c0: CFrame, c1: CFrame)
-- use native axis (stable enough)
local axis = (c0*c1:Inverse()):ToAxisAngle()
-- use full-precision angle calculation to minimize truncation
local angle = angleDiff(c0, c1)
return axis.Unit*angle
end
function RotationSpring.canSleep(self: RotationSpring)
local sleepP = areRotationsClose(self.p, self.g)
local sleepV = self.v.Magnitude < SLEEP_ROTATION_VELOCITY
return sleepP and sleepV
end
function RotationSpring.step(self: RotationSpring, dt: number): CFrame
local d = self.d
local f = self.f*(2*pi)
local g = self.g
local p0 = self.p
local v0 = self.v
local offset = axisAngleDiff(p0, g)
local decay = exp(-d*f*dt)
local pt: CFrame
local vt: Vector3
if d == 1 then -- critically damped
pt = rotateAxis((offset*(1 + f*dt) + v0*dt)*decay, g)
vt = (v0*(1 - dt*f) - offset*(dt*f*f))*decay
elseif d < 1 then -- underdamped
local c = sqrt(1 - d*d)
local i = cos(dt*f*c)
local j = sin(dt*f*c)
local y = j/(f*c)
local z = j/c
pt = rotateAxis((offset*(i + z*d) + v0*y)*decay, g)
vt = (v0*(i - z*d) - offset*(z*f))*decay
else -- overdamped
local c = sqrt(d*d - 1)
local r1 = -f*(d + c)
local r2 = -f*(d - c)
local co2 = (v0 - offset*r1)/(2*f*c)
local co1 = offset - co2
local e1 = co1*exp(r1*dt)
local e2 = co2*exp(r2*dt)
pt = rotateAxis(e1 + e2, g)
vt = e1*r1 + e2*r2
end
self.p = pt
self.v = vt
return pt
end
end
-- Defined early to be used by CFrameSpring
local typeMetadata_Vector3 = {
springType = LinearSpring.new,
toIntermediate = function(value)
return {value.X, value.Y, value.Z}
end,
fromIntermediate = function(value: {number})
return Vector3.new(value[1], value[2], value[3])
end,
}
-- Encapsulates a CFrame - Separates translation from rotation
local CFrameSpring = {}
do
CFrameSpring.__index = CFrameSpring
function CFrameSpring.new(
dampingRatio: number,
frequency: number,
valueCurrent: CFrame,
valueGoal: CFrame,
_: any
)
return setmetatable(
{
rawGoal = valueGoal,
_position = LinearSpring.new(dampingRatio, frequency, valueCurrent.Position, valueGoal.Position, typeMetadata_Vector3),
_rotation = RotationSpring.new(dampingRatio, frequency, valueCurrent.Rotation, valueGoal.Rotation)
},
CFrameSpring
)
end
function CFrameSpring:setGoal(value: CFrame)
self.rawGoal = value
self._position:setGoal(value.Position)
self._rotation:setGoal(value.Rotation)
end
function CFrameSpring:setDampingRatio(value: number)
self._position.d = value
self._rotation.d = value
end
function CFrameSpring:setFrequency(value: number)
self._position.f = value
self._rotation.f = value
end
function CFrameSpring:canSleep()
return self._position:canSleep() and self._rotation:canSleep()
end
function CFrameSpring:step(dt): CFrame
local p: Vector3 = self._position:step(dt)
local r: CFrame = self._rotation:step(dt)
return r + p
end
end
-- Color conversions
local rgbToLuv
local luvToRgb
do
local function inverseGammaCorrectD65(c)
return c < 0.0404482362771076 and c/12.92 or 0.87941546140213*(c + 0.055)^2.4
end
local function gammaCorrectD65(c)
return c < 3.1306684425e-3 and 12.92*c or 1.055*c^(1/2.4) - 0.055
end
function rgbToLuv(value: Color3): {number}
-- convert RGB to a variant of cieluv space
local r, g, b = value.R, value.G, value.B
-- D65 sRGB inverse gamma correction
r = inverseGammaCorrectD65(r)
g = inverseGammaCorrectD65(g)
b = inverseGammaCorrectD65(b)
-- sRGB -> xyz
local x = 0.9257063972951867*r - 0.8333736323779866*g - 0.09209820666085898*b
local y = 0.2125862307855956*r + 0.71517030370341085*g + 0.0722004986433362*b
local z = 3.6590806972265883*r + 11.4426895800574232*g + 4.1149915024264843*b
-- xyz -> scaled cieluv
local l = y > 0.008856451679035631 and 116*y^(1/3) - 16 or 903.296296296296*y
local u, v
if z > 1e-14 then
u = l*x/z
v = l*(9*y/z - 0.46832)
else
u = -0.19783*l
v = -0.46832*l
end
return {l, u, v}
end
function luvToRgb(value: {number}): Color3
-- convert back from modified cieluv to rgb space
local l = value[1]
if l < 0.0197955 then
return Color3.new(0, 0, 0)
end
local u = value[2]/l + 0.19783
local v = value[3]/l + 0.46832
-- cieluv -> xyz
local y = (l + 16)/116
y = y > 0.206896551724137931 and y*y*y or 0.12841854934601665*y - 0.01771290335807126
local x = y*u/v
local z = y*((3 - 0.75*u)/v - 5)
-- xyz -> D65 sRGB
local r = 7.2914074*x - 1.5372080*y - 0.4986286*z
local g = -2.1800940*x + 1.8757561*y + 0.0415175*z
local b = 0.1253477*x - 0.2040211*y + 1.0569959*z
-- clamp minimum sRGB component
if r < 0 and r < g and r < b then
r, g, b = 0, g - r, b - r
elseif g < 0 and g < b then
r, g, b = r - g, 0, b - g
elseif b < 0 then
r, g, b = r - b, g - b, 0
end
-- gamma correction from D65
-- clamp to avoid undesirable overflow wrapping behavior on certain properties (e.g. BasePart.Color)
return Color3.new(
min(gammaCorrectD65(r), 1),
min(gammaCorrectD65(g), 1),
min(gammaCorrectD65(b), 1)
)
end
end
-- Type definitions
-- Transforms Roblox types into intermediate types, converting
-- between spaces as necessary to preserve perceptual linearity
local typeMetadata = {
boolean = {
springType = LinearSpring.new,
toIntermediate = function(value)
return {value and 1 or 0}
end,
fromIntermediate = function(value)
return value[1] >= 0.5
end,
},
number = {
springType = LinearSpring.new,
toIntermediate = function(value)
return {value}
end,
fromIntermediate = function(value)
return value[1]
end,
},
NumberRange = {
springType = LinearSpring.new,
toIntermediate = function(value)
return {value.Min, value.Max}
end,
fromIntermediate = function(value)
return NumberRange.new(value[1], value[2])
end,
},
UDim = {
springType = LinearSpring.new,
toIntermediate = function(value)
return {value.Scale, value.Offset}
end,
fromIntermediate = function(value: {number})
return UDim.new(value[1], round(value[2]))
end,
},
UDim2 = {
springType = LinearSpring.new,
toIntermediate = function(value)
local x = value.X
local y = value.Y
return {x.Scale, x.Offset, y.Scale, y.Offset}
end,
fromIntermediate = function(value: {number})
return UDim2.new(value[1], round(value[2]), value[3], round(value[4]))
end,
},
Vector2 = {
springType = LinearSpring.new,
toIntermediate = function(value)
return {value.X, value.Y}
end,
fromIntermediate = function(value: {number})
return Vector2.new(value[1], value[2])
end,
},
Vector3 = typeMetadata_Vector3,
Color3 = {
springType = LinearSpring.new,
toIntermediate = rgbToLuv,
fromIntermediate = luvToRgb,
},
-- Only interpolates start and end keypoints
ColorSequence = {
springType = LinearSpring.new,
toIntermediate = function(value)
local keypoints = value.Keypoints
local luv0 = rgbToLuv(keypoints[1].Value)
local luv1 = rgbToLuv(keypoints[#keypoints].Value)
return {
luv0[1], luv0[2], luv0[3],
luv1[1], luv1[2], luv1[3],
}
end,
fromIntermediate = function(value: {})
return ColorSequence.new(
luvToRgb{value[1], value[2], value[3]},
luvToRgb{value[4], value[5], value[6]}
)
end,
},
CFrame = {
springType = CFrameSpring.new,
toIntermediate = error, -- custom (CFrameSpring)
fromIntermediate = error, -- custom (CFrameSpring)
}
}
type PropertyOverride = {
[string]: {
class: string,
get: (any)->(),
set: (any, any)->(),
}
}
local PSEUDO_PROPERTIES: PropertyOverride = {
Pivot = {
class = "PVInstance",
get = function(inst: PVInstance)
return inst:GetPivot()
end,
set = function(inst: PVInstance, value: CFrame)
inst:PivotTo(value)
end
},
Scale = {
class = "Model",
get = function(inst: Model)
return inst:GetScale()
end,
set = function(inst: Model, value: number)
local FLOAT_MANTISSA_MIN = 1.402e-45
local FLOAT_MANTISSA_MAX = 2^24
value = math.clamp(value, FLOAT_MANTISSA_MIN, FLOAT_MANTISSA_MAX)
inst:ScaleTo(value)
end
}
}
local function getProperty(instance: Instance, property: string): any
local override = PSEUDO_PROPERTIES[property]
if override and instance:IsA(override.class) then
return override.get(instance)
else
return (instance :: any)[property]
end
end
local function setProperty(instance: Instance, property: string, value: unknown)
local override = PSEUDO_PROPERTIES[property]
if override and instance:IsA(override.class) then
override.set(instance, value)
else
(instance :: any)[property] = value
end
end
-- Frame loop
local springStates_other: {[Instance]: {[string]: any}} = {} -- {[instance] = {[property] = spring}
local springStates_render: {[Instance]: {[string]: any}} = {} -- {[instance] = {[property] = spring}
local completedCallbacks: {[Instance]: {()->()}} = {}
local function processSprings(springStates: typeof(springStates_other), dt: number)
for instance, state in springStates do
for propName, spring in state do
if spring:canSleep() then
state[propName] = nil
setProperty(instance, propName, spring.rawGoal)
else
setProperty(instance, propName, spring:step(dt))
end
end
if not next(state) then
springStates[instance] = nil
-- trigger completed callbacks when all properties finish animating
local callbackList = completedCallbacks[instance]
if callbackList then
-- flush callback list before we run any callbacks in case
-- one of the callbacks recursively adds another callback
completedCallbacks[instance] = nil
for _, callback in callbackList do
task.spawn(callback)
end
end
end
end
end
RunService.PreSimulation:Connect(function(dt)
processSprings(springStates_other, dt)
end)
RunService.PostSimulation:Connect(function(dt)
processSprings(springStates_render, dt)
end)
local function assertType(argNum: number, fnName: string, expectedType: string, value: unknown)
if not expectedType:find(typeof(value)) then
error(`bad argument #{argNum} to {fnName} ({expectedType} expected, got {typeof(value)})`, 3)
end
end
-- API
local spr = {}
function spr.target(instance: Instance, dampingRatio: number, frequency: number, properties: {[string]: any})
if STRICT_RUNTIME_TYPES then
assertType(1, "spr.target", "Instance", instance)
assertType(2, "spr.target", "number", dampingRatio)
assertType(3, "spr.target", "number", frequency)
assertType(4, "spr.target", "table", properties)
end
if dampingRatio ~= dampingRatio or dampingRatio < 0 then
error(("expected damping ratio >= 0; got %.2f"):format(dampingRatio), 2)
end
if frequency ~= frequency or frequency < 0 then
error(("expected undamped frequency >= 0; got %.2f"):format(frequency), 2)
end
local targetRecord = (if instance:IsA("Camera") then springStates_render else springStates_other) :: {[Instance]: {[string]: any}}
local state = targetRecord[instance]
if not state then
state = {}
targetRecord[instance] = state
end
for propName, propTarget in properties do
local propValue = getProperty(instance, propName)
if STRICT_RUNTIME_TYPES and typeof(propTarget) ~= typeof(propValue) then
error(`bad property {propName} to spr.target ({typeof(propValue)} expected, got {typeof(propTarget)})`, 2)
end
-- Special case infinite frequency for an instantaneous change
if frequency == math.huge then
setProperty(instance, propName, propTarget)
state[propName] = nil
continue
end
local spring = state[propName]
if not spring then
local md = typeMetadata[typeof(propTarget)]
if not md then
error("unsupported type: " .. typeof(propTarget), 2)
end
spring = md.springType(dampingRatio, frequency, propValue, propTarget, md)
state[propName] = spring
end
spring:setGoal(propTarget)
spring:setDampingRatio(dampingRatio)
spring:setFrequency(frequency)
end
if not next(state) then
targetRecord[instance] = nil
end
end
function spr.stop(instance: Instance, property: string?)
if STRICT_RUNTIME_TYPES then
assertType(1, "spr.stop", "Instance", instance)
assertType(2, "spr.stop", "string|nil", property)
end
if property then
local state = springStates_other[instance] or springStates_render[instance]
if state then
state[property] = nil
end
else
springStates_other[instance] = nil
springStates_render[instance] = nil
end
end
function spr.completed(instance: Instance, callback: ()->())
if STRICT_RUNTIME_TYPES then
assertType(1, "spr.completed", "Instance", instance)
assertType(2, "spr.completed", "function", callback)
end
local callbackList = completedCallbacks[instance]
if callbackList then
table.insert(callbackList, callback)
else
completedCallbacks[instance] = {callback}
end
end
return table.freeze(spr)