forked from Snapchat/Spectacles-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialImageDepthAnimator.ts
More file actions
94 lines (78 loc) · 2.3 KB
/
Copy pathSpatialImageDepthAnimator.ts
File metadata and controls
94 lines (78 loc) · 2.3 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
91
92
93
94
import { easingFunctions } from "SpectaclesInteractionKit.lspkg/Utils/animate"
import { SpatialImageAngleValidator } from "./SpatialImageAngleValidator"
/**
* Controls the depth scale of the spatial image to reflect the entry of new
* images as well as ensure it's viewed only from correct angles.
*
* @version 1.0.0
*/
@component
export class SpatialImageDepthAnimator extends BaseScriptComponent {
@typename
SpatialImage: keyof ComponentNameMap
@input("SpatialImage")
private spatializer: any
@input
private angleValidator: SpatialImageAngleValidator
/**
* The speed at which the depth value is changed.
*/
@input
public animateSpeed: number = 0.5
/**
* The minimum depth to be applied to the image.
*/
@input
private minDepth: number = 0.05
private shouldFlatten: boolean = false
private baseDepthScale: number = 0
private depthFlattenFollower: number = 0
private onAwake(): void {
this.createEvent("OnStartEvent").bind(() => {
this.initialize()
})
this.createEvent("UpdateEvent").bind(() => {
this.update()
})
this.createEvent("OnEnableEvent").bind(() => {
this.depthFlattenFollower = 0
})
}
/**
* Sets the maximum depth scale for the image.
*/
public setBaseDepthScale(depth: number): void {
this.baseDepthScale = depth
}
private initialize(): void {
this.baseDepthScale = this.spatializer.depthScale
this.angleValidator.addOnValidityCallback((valid: boolean) => {
this.handleAngleValidityChanged(valid)
})
}
private update(): void {
this.setMaxDepthScale(this.baseDepthScale)
}
private setMaxDepthScale(maxDepthScale: number) {
if (!this.spatializer.material) {
return
}
let flatten = this.shouldFlatten ? 0 : 1
const distance = flatten - this.depthFlattenFollower
if (Math.abs(distance) > 0.01) {
this.depthFlattenFollower =
this.depthFlattenFollower +
Math.sign(distance) * getDeltaTime() * this.animateSpeed
}
const easedAngle = easingFunctions["ease-in-out-sine"](
this.depthFlattenFollower
)
this.spatializer.material.mainPass.depthScale = Math.max(
easedAngle * maxDepthScale,
this.minDepth
)
}
private handleAngleValidityChanged(isValid: boolean): void {
this.shouldFlatten = !isValid
}
}