-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathLoopController.ts
More file actions
90 lines (76 loc) · 2.13 KB
/
LoopController.ts
File metadata and controls
90 lines (76 loc) · 2.13 KB
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
@component
export class LoopController extends BaseScriptComponent {
@input
audio: AudioComponent
private loopMat: Material
private updateEvent: UpdateEvent
private targetOpacity: number
private isInLoopZone: boolean
private isLocking: boolean
private startLockAnimationTime: number
private openRange: vec2
private tr: Transform
start(startTr: Transform) {
this.tr = this.sceneObject.getTransform()
this.tr.setWorldPosition(startTr.getWorldPosition())
this.tr.setWorldRotation(startTr.getWorldRotation())
this.openRange = new vec2(0.05, 0.2)
this.loopMat = this.sceneObject.getComponent("RenderMeshVisual").mainMaterial
this.targetOpacity = 0
this.loopMat.mainPass.GlobalOpacity = 0
this.isLocking = false
this.isInLoopZone = false
this.updateEvent = this.createEvent("UpdateEvent")
this.updateEvent.bind(() => this.onUpdate())
}
stop() {
this.removeEvent(this.updateEvent)
this.updateEvent = undefined
this.targetOpacity = 0
this.loopMat.mainPass.GlobalOpacity = 0
this.isInLoopZone = false
}
onUpdate() {
const dt = getDeltaTime()
// Set opacity
this.loopMat.mainPass.GlobalOpacity = MathUtils.lerp(
this.loopMat.mainPass.GlobalOpacity,
this.targetOpacity,
7 * dt
)
if (this.isLocking) {
const t = getTime() - this.startLockAnimationTime
this.loopMat.mainPass.Tweak_N12 = this.pingPong(this.openRange.x, this.openRange.y, t)
if (t > 0.8) {
this.loopMat.mainPass.Tweak_N12 = this.openRange.x
this.isLocking = false
this.stop()
}
}
}
pingPong(min: number, max: number, t: number) {
const range = max - min
const freq = t * (Math.PI * 2)
return min + 0.5 * (1 + Math.sin(freq)) * range
}
show() {
this.isInLoopZone = true
this.targetOpacity = 1
}
hide() {
this.isInLoopZone = false
if (!this.isLocking) {
this.targetOpacity = 0
}
}
getIsInLoopZone() {
return this.isInLoopZone
}
onLock() {
this.startLockAnimationTime = getTime()
if (!this.isLocking) {
this.isLocking = true
this.audio.play(1)
}
}
}