forked from Snapchat/Spectacles-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopController.ts
More file actions
87 lines (72 loc) · 2.36 KB
/
Copy pathLoopController.ts
File metadata and controls
87 lines (72 loc) · 2.36 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
@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(.05, .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(){
let dt = getDeltaTime();
// Set opacity
this.loopMat.mainPass.GlobalOpacity = MathUtils.lerp(this.loopMat.mainPass.GlobalOpacity, this.targetOpacity, 7 * dt);
if(this.isLocking){
let t = getTime()-this.startLockAnimationTime;
this.loopMat.mainPass.Tweak_N12 = this.pingPong(this.openRange.x, this.openRange.y, t);
if(t>.8){
this.loopMat.mainPass.Tweak_N12 = this.openRange.x;
this.isLocking = false;
this.stop();
}
}
}
pingPong(min:number, max:number, t:number) {
var range = max - min;
var 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);
}
}
}