-
Notifications
You must be signed in to change notification settings - Fork 3
/
Screen.js
314 lines (314 loc) · 14.1 KB
/
Screen.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPerspectiveScale = exports.worldToPercet = exports.worldToPercent = exports.worldToCanvas = exports.worldToFocalPlane = exports.cameraTransformToWrold = exports.cameraTransformToCanvas = exports.cameraTransformToPercent = exports.cameraTransformToFocalPlane = exports.cameraTransformToFocalDistance = exports.getResolution = exports.getHeight = exports.getWidth = exports.getFullscreenSize = exports.focalPlaneToCanvas = exports.canvasToFocalPlane = exports.focalPlaneToPercent = exports.focalPlaneYToPercent = exports.focalPlaneXToPercent = exports.canvasToPercent = exports.canvasToPercentY = exports.canvasToPercentX = exports.percentToCanvas = exports.percentToCanvasY = exports.percentToCanvasX = exports.percentToFocalPlane = exports.percentToFocalPlaneY = exports.percentToFocalPlaneX = exports.screenScale = exports.height = exports.width = exports.createScaler = void 0;
const Reactive_1 = __importDefault(require("Reactive"));
const CameraInfo_1 = __importDefault(require("CameraInfo"));
const Units_1 = __importDefault(require("Units"));
const Scene_1 = __importDefault(require("Scene"));
const mainCamera = Scene_1.default.root.findFirst('Camera');
const units = Units_1.default.cm(10);
const fieldOfView = mainCamera.then(camera => camera.fieldOfView ? camera.fieldOfView : Reactive_1.default.val(86));
class Scaler {
constructor(baseWidth, baseHeight) {
this.baseWidth = baseWidth;
this.baseHeight = baseHeight;
}
get baseRatio() {
return Reactive_1.default.div(this.baseHeight, this.baseWidth);
}
get realRatio() {
return exports.height.div(exports.width);
}
get multiplier() {
return this.baseRatio.div(this.realRatio);
}
get scaleMultiplier() {
return Reactive_1.default.pack3(this.multiplier, this.multiplier, this.multiplier);
}
autoScaleObject(sceneObject, preferredX, preferredY, preferredZ) {
const x = preferredX ? preferredX : sceneObject.transform.scaleX.pinLastValue();
const y = preferredY ? preferredY : sceneObject.transform.scaleY.pinLastValue();
const z = preferredZ ? preferredZ : sceneObject.transform.scaleZ.pinLastValue();
sceneObject.transform.scale = this.scaleMultiplier.mul(Reactive_1.default.pack3(x, y, z));
}
}
function createScaler(baseWidth, baseHeight) {
return new Scaler(baseWidth, baseHeight);
}
exports.createScaler = createScaler;
/**
* The width of the screen resolution in ScalarSignal.
*/
exports.width = CameraInfo_1.default.previewSize.width;
/**
* The height of the screen resolution in ScalarSignal.
*/
exports.height = CameraInfo_1.default.previewSize.height;
/**
* CameraInfo.previewScreenScale.
*/
exports.screenScale = CameraInfo_1.default.previewScreenScale;
/**
* The percentage [0-1] of position X in screen coordinates. `0` is left, `1` is right.
*/
async function percentToFocalPlaneX(percent) {
const fullscreen = await getFullscreenSize();
const widthMax = Reactive_1.default.div(exports.width, exports.height).mul(fullscreen.y.mul(0.5 * units));
return Reactive_1.default.mul(percent, widthMax.mul(2)).sub(widthMax);
}
exports.percentToFocalPlaneX = percentToFocalPlaneX;
/**
* The percentage [0-1] of position Y in screen coordinates. `0` is top, `1` is bottom.
*/
async function percentToFocalPlaneY(percent) {
const fullscreen = await getFullscreenSize();
return Reactive_1.default.mul(percent, fullscreen.y.mul(-units)).add(fullscreen.y.mul(0.5 * units));
}
exports.percentToFocalPlaneY = percentToFocalPlaneY;
/**
* The percentage [0-1] of position in screen coordinates. `(0, 0)` is left top, `(1, 1)` is right bottom.
*/
async function percentToFocalPlane(percentLocation) {
const [x, y] = await Promise.all([percentToFocalPlaneX(percentLocation.x), percentToFocalPlaneY(percentLocation.y)]);
return Reactive_1.default.pack3(x, y, 0);
}
exports.percentToFocalPlane = percentToFocalPlane;
/**
* The percentage [0-1] of position X in canvas coordinates. `0` is left, `1` is right.
*/
function percentToCanvasX(percent) {
return exports.width.div(exports.screenScale).mul(percent);
}
exports.percentToCanvasX = percentToCanvasX;
/**
* The percentage [0-1] of position Y in canvas coordinates. `0` is top, `1` is bottom.
*/
function percentToCanvasY(percent) {
return exports.height.div(exports.screenScale).mul(percent);
}
exports.percentToCanvasY = percentToCanvasY;
/**
* The percentage [0-1] of position in canvas coordinates. `(0, 0)` is left top, `(1, 1)` is right bottom.
*/
function percentToCanvas(location) {
return Reactive_1.default.pack3(percentToCanvasX(location.x), percentToCanvasY(location.y), 0);
}
exports.percentToCanvas = percentToCanvas;
function canvasToPercentX(positionX, centerRef) {
if (centerRef && centerRef.width) {
return Reactive_1.default.add(positionX, centerRef.width.mul(0.5)).div(exports.width).mul(exports.screenScale);
}
else {
return Reactive_1.default.div(positionX, exports.width).mul(exports.screenScale);
}
}
exports.canvasToPercentX = canvasToPercentX;
function canvasToPercentY(positionY, centerRef) {
if (centerRef && centerRef.height) {
return Reactive_1.default.add(positionY, centerRef.height.mul(0.5)).div(exports.height).mul(exports.screenScale);
}
else {
return Reactive_1.default.div(positionY, exports.height).mul(exports.screenScale);
}
}
exports.canvasToPercentY = canvasToPercentY;
function canvasToPercent(location, centerRef) {
return Reactive_1.default.pack2(canvasToPercentX(location.x, centerRef), canvasToPercentY(location.y, centerRef));
}
exports.canvasToPercent = canvasToPercent;
async function focalPlaneXToPercent(positionX) {
const fullscreen = await getFullscreenSize();
const widthMax = Reactive_1.default.div(exports.width, exports.height).mul(fullscreen.y.mul(0.5 * units));
return Reactive_1.default.add(positionX, widthMax).div(widthMax.mul(2));
}
exports.focalPlaneXToPercent = focalPlaneXToPercent;
async function focalPlaneYToPercent(positionY) {
const fullscreen = await getFullscreenSize();
return fullscreen.y.mul(0.5 * units).sub(positionY).div(fullscreen.y.mul(units));
}
exports.focalPlaneYToPercent = focalPlaneYToPercent;
async function focalPlaneToPercent(position) {
const [x, y] = await Promise.all([focalPlaneXToPercent(position.x), focalPlaneYToPercent(position.y)]);
return Reactive_1.default.pack2(x, y);
}
exports.focalPlaneToPercent = focalPlaneToPercent;
/**
* Convert canvas position to focal plane position.
*/
async function canvasToFocalPlane(position, centerRef) {
const percent = canvasToPercent(position, centerRef);
return await percentToFocalPlane(percent);
}
exports.canvasToFocalPlane = canvasToFocalPlane;
async function focalPlaneToCanvas(position, centerRef) {
const percent = await focalPlaneToPercent(position);
if (centerRef && centerRef.width && centerRef.height) {
return Reactive_1.default.pack3(percentToCanvasX(percent.x).sub(centerRef.width.mul(0.5)), percentToCanvasY(percent.y).sub(centerRef.height.mul(0.5)), 0);
}
else {
return Reactive_1.default.pack3(percentToCanvasX(percent.x), percentToCanvasY(percent.y), 0);
}
}
exports.focalPlaneToCanvas = focalPlaneToCanvas;
/**
* The fullscreen size on Focal Plane.
*/
async function getFullscreenSize() {
const camera = await mainCamera;
return Reactive_1.default.pack3(camera.focalPlane.width.mul(Units_1.default.m(10)), camera.focalPlane.height.mul(Units_1.default.m(10)), 1);
}
exports.getFullscreenSize = getFullscreenSize;
/**
* The current width of the screen window in pixels.
*/
function getWidth() {
return new Promise(resolve => { exports.width.monitor({ 'fireOnInitialValue': true }).select('newValue').take(1).subscribe(resolve); });
}
exports.getWidth = getWidth;
/**
* The current height of the screen window in pixels.
*/
function getHeight() {
return new Promise(resolve => { exports.height.monitor({ 'fireOnInitialValue': true }).select('newValue').take(1).subscribe(resolve); });
}
exports.getHeight = getHeight;
/**
* Represents a display resolution.
*/
async function getResolution() {
const [w, h] = await Promise.all([getWidth(), getHeight(),]);
return {
width: w,
height: h,
toString() { return `${w} x ${h}`; },
};
}
exports.getResolution = getResolution;
/**
* Get the same position as faceMesh from tracker's cameraTransform.
*/
async function cameraTransformToFocalDistance(cameraTransformPosition) {
const camera = await mainCamera;
return cameraTransformPosition.add(Reactive_1.default.pack3(0, 0, camera.focalPlane.distance));
}
exports.cameraTransformToFocalDistance = cameraTransformToFocalDistance;
/**
* Get screen position of tracker's cameraTransform.
*/
async function cameraTransformToFocalPlane(cameraTransformPosition) {
const percent = await worldToPixel01(cameraTransformPosition);
const [x, y] = await Promise.all([percentToFocalPlaneX(percent.x), percentToFocalPlaneY(percent.y)]);
return Reactive_1.default.pack3(x, y.neg(), 0);
}
exports.cameraTransformToFocalPlane = cameraTransformToFocalPlane;
async function cameraTransformToPercent(cameraTransformPosition) {
const focalPlane = await cameraTransformToFocalPlane(cameraTransformPosition);
return await focalPlaneToPercent(focalPlane);
}
exports.cameraTransformToPercent = cameraTransformToPercent;
/**
* Convert position of tracker's cameraTransform to canvas.
* @param {SceneObjectBase=} centerRef Reference object's width and height to offset.
*/
async function cameraTransformToCanvas(cameraTransformPosition, centerRef) {
const percent = await worldToPixel01(cameraTransformPosition);
if (centerRef && centerRef.width && centerRef.height) {
return Reactive_1.default.pack3(percentToCanvasX(percent.x).sub(centerRef.width.mul(0.5)), percentToCanvasY(percent.y).neg().add(exports.height.div(exports.screenScale)).sub(centerRef.height.mul(0.5)), 0);
}
else {
return Reactive_1.default.pack3(percentToCanvasX(percent.x), percentToCanvasY(percent.y).neg(), 0);
}
}
exports.cameraTransformToCanvas = cameraTransformToCanvas;
/**
* Convert position of tracker's cameraTransform to world.
*/
async function cameraTransformToWrold(cameraTransformPosition) {
const camera = await mainCamera;
return camera.worldTransform.applyToPoint(cameraTransformPosition);
}
exports.cameraTransformToWrold = cameraTransformToWrold;
/**
* Convert position of worldTransform to focal plane position.
*/
async function worldToFocalPlane(worldPosition) {
const position = await inverseWorldTransform(worldPosition);
const percent = await worldToPixel01(position);
const [x, y] = await Promise.all([percentToFocalPlaneX(percent.x), percentToFocalPlaneY(percent.y)]);
return Reactive_1.default.pack3(x, y.neg(), 0);
}
exports.worldToFocalPlane = worldToFocalPlane;
/**
* Convert position of worldTransform to canvas position.
*/
async function worldToCanvas(worldPosition, centerRef) {
const position = await inverseWorldTransform(worldPosition);
const percent = await worldToPixel01(position);
if (centerRef && centerRef.width && centerRef.height) {
return Reactive_1.default.pack3(percentToCanvasX(percent.x).sub(centerRef.width.mul(0.5)), percentToCanvasY(percent.y).neg().add(exports.height.div(exports.screenScale)).sub(centerRef.height.mul(0.5)), 0);
}
else {
return Reactive_1.default.pack3(percentToCanvasX(percent.x), percentToCanvasY(percent.y).neg(), 0);
}
}
exports.worldToCanvas = worldToCanvas;
/**
* Convert position of worldTransform to focal position in [0-1] percent.
*/
async function worldToPercent(worldPosition) {
const position = await worldToFocalPlane(worldPosition);
return await focalPlaneToPercent(position);
}
exports.worldToPercent = worldToPercent;
/**
* Convert position of worldTransform to focal position in [0-1] percent.
* @deprecated It's typo of `worldToPercent`
*/
async function worldToPercet(worldPosition) {
return worldToPercent(worldPosition);
}
exports.worldToPercet = worldToPercet;
/**
* Get the scale that make the plane in screen space look the same size as the plane in 3d space.
*/
async function getPerspectiveScale(worldPosition) {
const [camera, fov] = await Promise.all([mainCamera, fieldOfView,]);
const distance = camera.worldTransform.position.distance(worldPosition);
const scale = Reactive_1.default.div(1, distance).div(Reactive_1.default.tan(fov / 2 * 0.0174532925).mul(2));
return Reactive_1.default.pack3(scale, scale, scale);
}
exports.getPerspectiveScale = getPerspectiveScale;
/**
* Convert world position to camera position.
*/
async function inverseWorldTransform(worldPosition) {
const camera = await mainCamera;
return camera.worldTransform.inverse().applyToPoint(worldPosition);
}
/**
* Convert position of worldTransform to screen position in 0-1.
*/
async function worldToPixel01(worldPosition) {
const pixel = await worldToPixel(worldPosition);
const perX = pixel.x.div(exports.width.mul(exports.screenScale));
const perY = pixel.y.div(exports.height.mul(exports.screenScale));
return Reactive_1.default.point2d(perX, perY);
}
/**
* Convert position of worldTransform to screen position.
*/
async function worldToPixel(worldPosition) {
const camera = await mainCamera;
const widthTan = camera.focalPlane.width.div(2).div(camera.focalPlane.distance);
const heightTan = camera.focalPlane.height.div(2).div(camera.focalPlane.distance);
const scaled_width = exports.width.mul(exports.screenScale);
const scaled_height = exports.height.mul(exports.screenScale);
const focalPlaneWidthAtObject = worldPosition.z.mul(widthTan).mul(2);
const focalPlaneHeightAtObject = worldPosition.z.mul(heightTan).mul(2);
const pixelX = scaled_width.div(focalPlaneWidthAtObject).mul(worldPosition.x).neg().add(scaled_width.div(2));
const pixelY = scaled_height.div(focalPlaneHeightAtObject).mul(worldPosition.y).neg().add(scaled_height.div(2));
return Reactive_1.default.point2d(pixelX, pixelY);
}