|
| 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