forked from Snapchat/Spectacles-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialImageSwapper.ts
More file actions
81 lines (71 loc) · 2.05 KB
/
Copy pathSpatialImageSwapper.ts
File metadata and controls
81 lines (71 loc) · 2.05 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
/**
* Responsible to changing the active scene object between a flat version and
* the spatialized version when the onLoaded event triggers.
*/
@component
export class SpatialImageSwapper extends BaseScriptComponent {
@typename
SpatialImage: keyof ComponentNameMap
@input("SpatialImage")
private spatializer
@input
private flatImage: Image
/**
* When spatialisation is complete, if true, the image will automatically swap
* to the spatialized version.
*/
@input
private autoSwapToSpatialized: boolean = false
private onAwake(): void {
this.createEvent("OnStartEvent").bind(() => {
this.initialize()
})
}
/**
* Sets the texture of the flat version of the image.
*
* @param image - The texture to be applied.
*/
public setImage(image: Texture): void {
if (this.flatImage) {
this.flatImage.mainMaterial.mainPass.baseTex = image
const aspectRatio = image.getWidth() / image.getHeight()
const invertedScale = new vec3(1 / aspectRatio, 1, 1)
this.flatImage.sceneObject.getTransform().setLocalScale(invertedScale)
}
if (this.spatializer.spatialImage) {
this.spatializer.spatialImage.enabled = false
this.spatializer.spatialImage = null
}
}
/**
* If true, the spatialized image will be displayed and the depth animated in.
* If false, the flat image will be displayed.
*/
public setSpatialized(spatialized: boolean): void {
if (spatialized) {
this.setSpatial()
} else {
this.setFlat()
}
}
private initialize(): void {
if (this.autoSwapToSpatialized) {
this.spatializer.onLoaded.add((status: number) => {
if (status === 1) {
this.setSpatialized(true)
} else {
print("Image did not successfully spatialize.")
}
})
}
}
private setFlat(): void {
this.flatImage.sceneObject.enabled = true
this.spatializer.sceneObject.enabled = false
}
private setSpatial(): void {
this.flatImage.sceneObject.enabled = false
this.spatializer.sceneObject.enabled = true
}
}