forked from Snapchat/Spectacles-Sample
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlacingFinishState.ts
More file actions
110 lines (89 loc) · 4.23 KB
/
Copy pathPlacingFinishState.ts
File metadata and controls
110 lines (89 loc) · 4.23 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
import { IPathMakerState } from "./IPathMakerState";
import { PathBuilder } from "../PathBuilder";
import { SoundController } from "../SoundController";
import { SurfaceDetection } from "../../Surface Detection/Scripts/SurfaceDetection";
import { GetVectorsFromQuaternion } from "../Helpers/GetVectorsFromQuaternion";
import { LinearAlgebra } from "../Helpers/LinearAlgebra";
import { FinishSmoothPath } from "../Helpers/FinishSmoothPath";
import { LineController } from "../LineController";
export class PlacingFinishState implements IPathMakerState {
constructor(
protected startObject:SceneObject,
protected ownerScript: ScriptComponent,
protected surfaceDetection: SurfaceDetection,
protected finishPrefab: ObjectPrefab,
protected cameraTransform: Transform,
protected forwardDisplace: number,
protected pathPoints: vec3[],
protected visualPoints: vec3[],
protected visualRmv: RenderMeshVisual,
protected bigMoveDistanceThreshold: number,
protected hermiteResolution: number,
protected resampleResoluton: number,
protected onFinishPlaced: (finishPosition: vec3,
finishRotation: quat,
finishObject: SceneObject,
splinePoints: { position: vec3, rotation: quat }[]) => void) {
}
protected finishTransform: Transform;
start() {
this.surfaceDetection.startGroundCalibration((pos, rot) => { this.onPlacing(pos, rot) }, (pos, rot) => { this.onPlaced(pos, rot) });
this.finishTransform = this.finishPrefab.instantiate(this.ownerScript.getSceneObject()).getTransform();
const finishLineController = this.finishTransform.getSceneObject().getComponent(LineController.getTypeName());
if (!finishLineController) {
throw new Error(`FinishLineController not found on ${this.finishTransform.getSceneObject().name}`);
} else {
finishLineController.init(false);
finishLineController.setHintVisual();
}
this.hideFinishLine();
}
onPlacing(pos: vec3, rot: quat) {
// Rot is upside down and turned around
const { forward, right, up } = GetVectorsFromQuaternion.getInstance().getVectorsFromQuaternion(rot);
rot = quat.angleAxis(-Math.PI / 2, right).multiply(rot);
rot = quat.angleAxis(Math.PI, up).multiply(rot);
// Displaces obj away from surface placement ui
pos = this.displaceAwayFromCamera(pos);
this.setFinishLineTransform(pos, rot);
this.updateArrowsVisual(pos);
}
onPlaced(pos: vec3, rot: quat) {
// Bug: this rot does not match onPlacing one frame earlier
const { forward, right, up } = GetVectorsFromQuaternion.getInstance().getVectorsFromQuaternion(rot);
rot = quat.angleAxis(Math.PI, up).multiply(rot);
// Displaces obj away from surface placement ui
pos = this.displaceAwayFromCamera(pos);
this.setFinishLineTransform(pos, rot);
let smoothPoints = FinishSmoothPath.finishSmoothPath(
this.pathPoints,
this.finishTransform,
this.cameraTransform,
this.bigMoveDistanceThreshold,
this.hermiteResolution,
this.resampleResoluton
);
this.pathPoints = smoothPoints.pathPoints;
const splinePoints = smoothPoints.splinePoints;
this.onFinishPlaced(pos, rot, this.finishTransform.getSceneObject(), splinePoints);
SoundController.getInstance().playSound("stopCreatePath");
}
private setFinishLineTransform(pos: vec3, rot: quat) {
this.finishTransform.setWorldPosition(pos);
this.finishTransform.setWorldRotation(rot);
}
stop() {
this.visualRmv.enabled = false;
}
private displaceAwayFromCamera(pos: vec3) {
return pos.add(LinearAlgebra.flatNor(this.cameraTransform.back).uniformScale(30));
}
protected updateArrowsVisual(position: vec3) {
const newArray = [this.pathPoints[this.pathPoints.length - 1],
position]
this.visualRmv.mesh = PathBuilder.buildFromPoints(newArray, 60);
}
private hideFinishLine() {
this.finishTransform.setWorldPosition(this.cameraTransform.getWorldPosition().add(vec3.up().uniformScale(1000)));
}
}