Skip to content

Commit 1d00903

Browse files
committed
feat(soba): add scale factor calculation for pivot controls
1 parent 2f813fa commit 1d00903

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { NgtSize } from 'angular-three';
2+
import { Camera, Vector3 } from 'three';
3+
4+
const tV0 = new Vector3();
5+
const tV1 = new Vector3();
6+
const tV2 = new Vector3();
7+
8+
function getPoint2(point3: Vector3, camera: Camera, size: NgtSize) {
9+
const widthHalf = size.width / 2;
10+
const heightHalf = size.height / 2;
11+
camera.updateMatrixWorld(false);
12+
const vector = point3.project(camera);
13+
vector.x = vector.x * widthHalf + widthHalf;
14+
vector.y = -(vector.y * heightHalf) + heightHalf;
15+
return vector;
16+
}
17+
18+
function getPoint3(point2: Vector3, camera: Camera, size: NgtSize, zValue: number = 1) {
19+
const vector = tV0.set((point2.x / size.width) * 2 - 1, -(point2.y / size.height) * 2 + 1, zValue);
20+
vector.unproject(camera);
21+
return vector;
22+
}
23+
24+
export function calculateScaleFactor(point3: Vector3, radiusPx: number, camera: Camera, size: NgtSize) {
25+
const point2 = getPoint2(tV2.copy(point3), camera, size);
26+
let scale = 0;
27+
for (let i = 0; i < 2; ++i) {
28+
const point2off = tV1.copy(point2).setComponent(i, point2.getComponent(i) + radiusPx);
29+
const point3off = getPoint3(point2off, camera, size, point2off.z);
30+
scale = Math.max(scale, point3.distanceTo(point3off));
31+
}
32+
return scale;
33+
}

0 commit comments

Comments
 (0)