-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathSnap3DInteractable.ts
More file actions
78 lines (72 loc) · 2.27 KB
/
Copy pathSnap3DInteractable.ts
File metadata and controls
78 lines (72 loc) · 2.27 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
import { setTimeout } from "SpectaclesInteractionKit.lspkg/Utils/FunctionTimingUtils";
@component
export class Snap3DInteractable extends BaseScriptComponent {
@input
private modelParent: SceneObject;
@input
private img: Image;
@input
private promptDisplay: Text;
@input
private spinner: SceneObject;
@input
private mat: Material;
@input
private displayPlate: SceneObject;
@input
private colliderObj: SceneObject;
private tempModel: SceneObject = null;
private finalModel: SceneObject = null;
private size: number = 20;
private sizeVec: vec3 = null;
onAwake() {
// Clone the image material to avoid modifying the original
let imgMaterial = this.img.mainMaterial;
imgMaterial.mainPass.baseTex = this.img.mainPass.baseTex;
this.img.enabled = false;
let offsetBelow = 0;
this.sizeVec = vec3.one().uniformScale(this.size);
this.displayPlate
.getTransform()
.setLocalPosition(new vec3(0, -this.size * 0.5 - offsetBelow, 0));
this.colliderObj.getTransform().setLocalScale(this.sizeVec);
this.img.getTransform().setLocalScale(this.sizeVec);
}
setPrompt(prompt: string) {
this.promptDisplay.text = prompt;
}
setImage(image: Texture) {
this.img.enabled = true;
// Clone material to avoid replicating the preview image across instances
this.img.mainMaterial = this.img.mainMaterial.clone();
this.img.mainPass.baseTex = image;
}
setModel(model: GltfAsset, isFinal: boolean) {
this.img.enabled = false;
if (isFinal) {
if (!isNull(this.finalModel)) {
this.finalModel.destroy();
}
this.spinner.enabled = false;
this.finalModel = model.tryInstantiate(this.modelParent, this.mat);
this.finalModel.getTransform().setLocalScale(this.sizeVec);
} else {
this.tempModel = model.tryInstantiate(this.modelParent, this.mat);
this.tempModel.getTransform().setLocalScale(this.sizeVec);
}
}
onFailure(error: string) {
this.img.enabled = false;
this.spinner.enabled = false;
if (this.tempModel) {
this.tempModel.destroy();
}
if (this.finalModel) {
this.finalModel.destroy();
}
this.promptDisplay.text = "Error: " + error;
setTimeout(() => {
this.destroy();
}, 5000); // Hide error after 5 seconds
}
}