/
GUIPickHelper.ts
148 lines (128 loc) · 5.01 KB
/
GUIPickHelper.ts
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Triangle } from "../../math/Triangle";
import { Matrix4 } from "../../math/Matrix4";
import { Ray } from "../../math/Ray";
import { Vector2 } from "../../math/Vector2";
import { Vector3 } from "../../math/Vector3";
import { UITransform } from "./uiComponents/UITransform";
import { GUIConfig, GUISpace } from "./GUIConfig";
import { HitInfo } from "../shape/ColliderShape";
/**
* @internal
* @group GPU GUI
*/
export class GUIPickHelper {
private static _pt0: Vector3;
private static _pt1: Vector3;
private static _pt2: Vector3;
private static _pt3: Vector3;
private static _hitPoint: Vector3;
private static _worldMatrix: Matrix4;
private static _ray: Ray;
private static _triangle: Triangle;
private static _isInit: boolean;
private static init(): void {
this._pt0 = new Vector3();
this._pt1 = new Vector3();
this._pt2 = new Vector3();
this._pt3 = new Vector3();
this._ray = new Ray();
this._triangle = new Triangle();
this._hitPoint = new Vector3();
this._worldMatrix = new Matrix4();
}
public static rayPick(ray: Ray, screenPos: Vector2, screenSize: Vector2, space: GUISpace, panelRatio: number, uiTransform: UITransform, worldMatrix: Matrix4): HitInfo {
if (!this._isInit) {
this.init();
this._isInit = true;
}
let helpMatrix = this._worldMatrix;
if (space == GUISpace.World) {
let pickPoint: Vector3;
this.calculateHotArea_World(uiTransform, this._pt0, this._pt1, this._pt2, this._pt3);
helpMatrix.copyFrom(worldMatrix).invert();
let helpRay = this._ray;
helpRay.copy(ray).applyMatrix(helpMatrix);
this._triangle.set(this._pt0, this._pt1, this._pt2);
pickPoint = helpRay.intersectTriangle(helpRay.origin, helpRay.direction, this._triangle);
if (!pickPoint) {
this._triangle.set(this._pt1, this._pt2, this._pt3);
pickPoint = helpRay.intersectTriangle(helpRay.origin, helpRay.direction, this._triangle);
}
if (pickPoint) {
return {
distance: 0,
intersectPoint: pickPoint,
};
}
} else {
this.calculateHotArea_View(uiTransform, panelRatio, this._pt0, this._pt1, this._pt2, this._pt3);
//
let screenSizeX: number = screenSize.x;
let screenSizeY: number = screenSize.y;
let minX: number = Math.min(this._pt0.x, this._pt1.x, this._pt2.x, this._pt3.x) + screenSizeX * 0.5;
let minY: number = Math.min(this._pt0.y, this._pt1.y, this._pt2.y, this._pt3.y) + screenSizeY * 0.5;
let maxX: number = Math.max(this._pt0.x, this._pt1.x, this._pt2.x, this._pt3.x) + screenSizeX * 0.5;
let maxY: number = Math.max(this._pt0.y, this._pt1.y, this._pt2.y, this._pt3.y) + screenSizeY * 0.5;
let pick = screenPos.x <= maxX && screenPos.x >= minX && screenPos.y <= maxY && screenPos.y >= minY;
if (pick) {
this._hitPoint.set(screenPos.x, screenPos.y, 0);
return {
distance: 0,
intersectPoint: this._hitPoint,
};
}
return null;
}
return null;
}
private static calculateHotArea_View(transform: UITransform, panelRatio: number, pt0: Vector3, pt1: Vector3, pt2: Vector3, pt3: Vector3) {
let uiMtx = transform.getWorldMatrix();
//2 3
//0 1
let scaleX: number = uiMtx.getScaleX();
let scaleY: number = uiMtx.getScaleY();
let w = transform.width * 0.5 * scaleX;
let h = transform.height * 0.5 * scaleY;
pt0.set(-w, -h, 0);
pt1.set(w, -h, 0);
pt2.set(-w, h, 0);
pt3.set(w, h, 0);
let offset = uiMtx.tx;
pt0.x += offset;
pt1.x += offset;
pt2.x += offset;
pt3.x += offset;
offset = uiMtx.ty;
pt0.y -= offset;
pt1.y -= offset;
pt2.y -= offset;
pt3.y -= offset;
pt0.multiplyScalar(panelRatio);
pt1.multiplyScalar(panelRatio);
pt2.multiplyScalar(panelRatio);
pt3.multiplyScalar(panelRatio);
}
private static calculateHotArea_World(transform: UITransform, pt0: Vector3, pt1: Vector3, pt2: Vector3, pt3: Vector3) {
let uiMtx = transform.getWorldMatrix();
//2 3
//0 1
let scaleX: number = uiMtx.getScaleX();
let scaleY: number = uiMtx.getScaleY();
let w = transform.width * 0.5 * scaleX;
let h = transform.height * 0.5 * scaleY;
pt0.set(-w, h, 0);
pt1.set(w, h, 0);
pt2.set(-w, -h, 0);
pt3.set(w, -h, 0);
let offset = uiMtx.tx;
pt0.x += offset;
pt1.x += offset;
pt2.x += offset;
pt3.x += offset;
offset = uiMtx.ty;
pt0.y += offset;
pt1.y += offset;
pt2.y += offset;
pt3.y += offset;
}
}