forked from Snapchat/Spectacles-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpatialImageFrame.ts
More file actions
122 lines (106 loc) · 3.8 KB
/
Copy pathSpatialImageFrame.ts
File metadata and controls
122 lines (106 loc) · 3.8 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
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
import { ContainerFrame } from "SpectaclesInteractionKit.lspkg/Components/UI/ContainerFrame/ContainerFrame";
import { SpatialImageSwapper } from "./SpatialImageSwapper";
import { setTimeout } from "SpectaclesInteractionKit.lspkg/Utils/FunctionTimingUtils";
//import { setTimeout } from "SpectaclesInteractionKit/Utils/debounce";
/**
* Reorders rendering order to ensure the image looks correct.
*
* @remarks To be attached to the root {@link ContainerFrame}.
*
* @version 1.0.0
*/
@component
export class SpatialImageFrame extends BaseScriptComponent {
@typename
SpatialImage: keyof ComponentNameMap;
@input
private container: ContainerFrame;
@input("SpatialImage")
private spatializer;
@input
private spatialImageSwapper: SpatialImageSwapper;
@input
private camera: SceneObject;
@input
@allowUndefined
private imageTexture: Texture;
private updateFocalPoint: boolean = false;
private onAwake(): void {
this.createEvent("OnStartEvent").bind(() => {
this.initializeFrame();
this.spatializer.frameOn = true;
this.spatializer.fadeBorder = true;
});
this.createEvent("UpdateEvent").bind(() => {
if (this.updateFocalPoint) {
this.setFocalPoint();
}
});
this.createEvent("OnEnableEvent").bind(() => {
this.setFocalPoint();
});
}
/**
* Toggle call to switch between specialized and flat images.
*/
public setSpatialized(value: boolean): void {
this.spatialImageSwapper.setSpatialized(value);
}
/**
* Updates both spatialized and flat images to the passed texture.
*
* @param image - The image to be spatialized.
* @param swapWhenSpatialized - If true, the spatialized image will be
* displayed as soon as it is returned.
*/
public setImage(image: Texture, swapWhenSpatialized: boolean = false): void {
// update the size of the container to match the dimensions of the new image.
const height: number = this.container.innerSize.y;
const newWidth: number = height * (image.getWidth() / image.getHeight());
this.updateContainerSize(new vec2(newWidth, height));
// if this argument is true, then when the "onLoaded" event is actuated,
// this component should update to display the spatialized image.
if (swapWhenSpatialized) {
const setSpatialCallback = () => {
this.setSpatialized(true);
this.spatializer.onLoaded.remove(setSpatialCallback);
};
this.spatializer.onLoaded.add(setSpatialCallback);
}
// The swapper is passed a reference to the new flat image and set to be
// unspatialized until the spatialization result comes through.
this.spatialImageSwapper.setImage(image);
this.spatialImageSwapper.setSpatialized(false);
this.spatializer.setImage(image);
// A work around to the initialization of the scene
setTimeout(() => {
this.updateContainerSize(new vec2(newWidth, height));
}, 100);
}
private initializeFrame(): void {
this.spatializer.sceneObject.enabled = false;
this.container.renderOrder = -30;
const visual = this.container
.getFrameObject()
.getComponent("Component.Visual") as RenderMeshVisual;
this.container.material.mainPass.cutOutCenter = 1;
if (this.imageTexture) {
this.setImage(this.imageTexture);
}
this.container.onTranslationStart.add(() => {
this.updateFocalPoint = true;
});
this.container.onTranslationEnd.add(() => {
this.updateFocalPoint = false;
});
}
private updateContainerSize(newSize: vec2) {
this.container.innerSize = newSize;
}
private setFocalPoint() {
const cameraPosition = this.camera.getTransform().getWorldPosition();
const imagePos = this.spatializer.getTransform().getWorldPosition();
const distance = cameraPosition.distance(imagePos);
this.spatializer.setFrameOffset(-distance);
}
}