LCOV - code coverage report
Current view: top level - packages/viewport3d/src/cameraAngle - cameraAngleMath.ts Coverage Total Hit
Test: ComfyUI E2E Coverage Lines: 16.1 % 56 9
Test Date: 2026-11-17 04:17:53

            Line data    Source code
       1              : import { clamp } from 'es-toolkit'
       2              : 
       3              : import type { Vector3Like } from 'three'
       4              : 
       5              : import { DEFAULT_CAMERA_INFO_STATE } from '#/cameraInfo/types'
       6              : import type { CameraInfoState } from '#/cameraInfo/types'
       7              : import type { OrbitHandleType } from '#/cameraInfo/handles/OrbitHandles'
       8              : import {
       9              :   pointToDistance,
      10              :   pointToPitchAngle,
      11              :   pointToYawAngle
      12              : } from '#/cameraInfo/handles/orbitDragMath'
      13              : 
      14              : import {
      15              :   CAMERA_ANGLE_FOV,
      16              :   CAMERA_ANGLE_LIMITS,
      17              :   MAX_DISPLAY_DISTANCE,
      18              :   MAX_LENS_ZOOM,
      19              :   MIN_DISPLAY_DISTANCE,
      20              :   MIN_LENS_ZOOM,
      21              :   ORBIT_SPHERE_RADIUS,
      22              :   SUBJECT_CENTER,
      23              :   SUBJECT_DISTANCE
      24              : } from './types'
      25              : import type { CameraAngleState } from './types'
      26              : 
      27         2355 : const DRAG_DEGREES_PER_PIXEL = 0.5
      28         2355 : const WHEEL_ZOOM_PER_PIXEL = 0.01
      29         2355 : const ZOOM_DECIMALS = 1
      30              : 
      31              : export interface CameraAngleTerm {
      32              :   key: string
      33              :   prompt: string
      34              :   preset: number
      35              : }
      36              : 
      37         2355 : export const HORIZONTAL_TERMS: readonly CameraAngleTerm[] = [
      38              :   { key: 'front', prompt: 'front view', preset: 0 },
      39              :   { key: 'frontRight', prompt: 'front-right quarter view', preset: 45 },
      40              :   { key: 'right', prompt: 'right side view', preset: 90 },
      41              :   { key: 'backRight', prompt: 'back-right quarter view', preset: 135 },
      42              :   { key: 'back', prompt: 'back view', preset: 180 },
      43              :   { key: 'backLeft', prompt: 'back-left quarter view', preset: 225 },
      44              :   { key: 'left', prompt: 'left side view', preset: 270 },
      45              :   { key: 'frontLeft', prompt: 'front-left quarter view', preset: 315 }
      46              : ]
      47              : 
      48         2355 : export const VERTICAL_TERMS: readonly CameraAngleTerm[] = [
      49              :   { key: 'lowAngle', prompt: 'low-angle shot', preset: -20 },
      50              :   { key: 'eyeLevel', prompt: 'eye-level shot', preset: 0 },
      51              :   { key: 'elevated', prompt: 'elevated shot', preset: 30 },
      52              :   { key: 'highAngle', prompt: 'high-angle shot', preset: 55 }
      53              : ]
      54         2355 : const VERTICAL_UPPER_BOUNDS = [-15, 15, 45]
      55              : 
      56         2355 : export const DISTANCE_TERMS: readonly CameraAngleTerm[] = [
      57              :   { key: 'wide', prompt: 'wide shot', preset: 1 },
      58              :   { key: 'medium', prompt: 'medium shot', preset: 5 },
      59              :   { key: 'closeUp', prompt: 'close-up', preset: 8 }
      60              : ]
      61         2355 : const DISTANCE_UPPER_BOUNDS = [2, 6]
      62              : 
      63            0 : export function normalizeHorizontal(degrees: number): number {
      64            0 :   return ((degrees % 360) + 360) % 360
      65              : }
      66              : 
      67            0 : export function clampState(state: CameraAngleState): CameraAngleState {
      68            0 :   return {
      69              :     horizontal: clamp(
      70              :       state.horizontal,
      71              :       CAMERA_ANGLE_LIMITS.horizontal.min,
      72              :       CAMERA_ANGLE_LIMITS.horizontal.max
      73              :     ),
      74              :     vertical: clamp(
      75              :       state.vertical,
      76              :       CAMERA_ANGLE_LIMITS.vertical.min,
      77              :       CAMERA_ANGLE_LIMITS.vertical.max
      78              :     ),
      79              :     zoom: clamp(
      80              :       state.zoom,
      81              :       CAMERA_ANGLE_LIMITS.zoom.min,
      82              :       CAMERA_ANGLE_LIMITS.zoom.max
      83              :     )
      84              :   }
      85              : }
      86              : 
      87            0 : export function roundState(state: CameraAngleState): CameraAngleState {
      88            0 :   const zoomScale = 10 ** ZOOM_DECIMALS
      89            0 :   return clampState({
      90              :     horizontal: Math.round(state.horizontal),
      91              :     vertical: Math.round(state.vertical),
      92              :     zoom: Math.round(state.zoom * zoomScale) / zoomScale
      93              :   })
      94              : }
      95              : 
      96            0 : function bucket(
      97              :   value: number,
      98              :   upperBounds: readonly number[],
      99              :   terms: readonly CameraAngleTerm[]
     100              : ): CameraAngleTerm {
     101            0 :   const index = upperBounds.findIndex((upper) => value < upper)
     102            0 :   return terms[index === -1 ? terms.length - 1 : index]
     103              : }
     104              : 
     105            0 : export function horizontalTerm(degrees: number): CameraAngleTerm {
     106              :   const sector =
     107              :     Math.floor((normalizeHorizontal(degrees) + 22.5) / 45) %
     108              :     HORIZONTAL_TERMS.length
     109              :   return HORIZONTAL_TERMS[sector]
     110              : }
     111              : 
     112            0 : export function verticalTerm(degrees: number): CameraAngleTerm {
     113            0 :   return bucket(degrees, VERTICAL_UPPER_BOUNDS, VERTICAL_TERMS)
     114              : }
     115              : 
     116            0 : export function distanceTerm(zoom: number): CameraAngleTerm {
     117            0 :   return bucket(zoom, DISTANCE_UPPER_BOUNDS, DISTANCE_TERMS)
     118              : }
     119              : 
     120            0 : export function describeCameraAngle(state: CameraAngleState): string {
     121            0 :   return [
     122              :     horizontalTerm(state.horizontal).prompt,
     123              :     verticalTerm(state.vertical).prompt,
     124              :     distanceTerm(state.zoom).prompt
     125              :   ].join(' ')
     126              : }
     127              : 
     128            0 : function zoomToRange(zoom: number, near: number, far: number): number {
     129            0 :   return far - ((far - near) * zoom) / CAMERA_ANGLE_LIMITS.zoom.max
     130              : }
     131              : 
     132            0 : function rangeToZoom(distance: number, near: number, far: number): number {
     133            0 :   return ((far - distance) * CAMERA_ANGLE_LIMITS.zoom.max) / (far - near)
     134              : }
     135              : 
     136            0 : export function zoomToLensZoom(zoom: number): number {
     137            0 :   return (
     138              :     MIN_LENS_ZOOM +
     139              :     ((MAX_LENS_ZOOM - MIN_LENS_ZOOM) * zoom) / CAMERA_ANGLE_LIMITS.zoom.max
     140              :   )
     141              : }
     142              : 
     143            0 : export function zoomToDisplayDistance(zoom: number): number {
     144            0 :   return zoomToRange(zoom, MIN_DISPLAY_DISTANCE, MAX_DISPLAY_DISTANCE)
     145              : }
     146              : 
     147            0 : export function displayDistanceToZoom(distance: number): number {
     148            0 :   return rangeToZoom(distance, MIN_DISPLAY_DISTANCE, MAX_DISPLAY_DISTANCE)
     149              : }
     150              : 
     151            0 : function orbitState(
     152              :   state: CameraAngleState,
     153              :   distance: number,
     154              :   zoom = DEFAULT_CAMERA_INFO_STATE.zoom
     155              : ): CameraInfoState {
     156            0 :   return {
     157              :     ...DEFAULT_CAMERA_INFO_STATE,
     158              :     mode: 'orbit',
     159              :     target: { ...SUBJECT_CENTER },
     160              :     fov: CAMERA_ANGLE_FOV,
     161              :     zoom,
     162              :     orbit: { yaw: state.horizontal, pitch: state.vertical, distance }
     163              :   }
     164              : }
     165              : 
     166            0 : export function toOrbitCameraInfoState(
     167              :   state: CameraAngleState
     168              : ): CameraInfoState {
     169            0 :   return orbitState(state, SUBJECT_DISTANCE, zoomToLensZoom(state.zoom))
     170              : }
     171              : 
     172            0 : export function toHandleOrbitState(state: CameraAngleState): CameraInfoState {
     173            0 :   return orbitState(state, zoomToDisplayDistance(state.zoom))
     174              : }
     175              : 
     176         2355 : const OVERVIEW_FIT_MARGIN = 1.2
     177              : 
     178            0 : export function overviewDistance(aspect: number, fovDegrees: number): number {
     179            0 :   const halfFov = (fovDegrees / 2) * (Math.PI / 180)
     180            0 :   const shortSide = Math.min(1, aspect)
     181            0 :   return (
     182              :     (ORBIT_SPHERE_RADIUS * OVERVIEW_FIT_MARGIN) /
     183              :     (Math.tan(halfFov) * shortSide)
     184              :   )
     185              : }
     186              : 
     187            0 : export function rotateByDrag(
     188              :   state: CameraAngleState,
     189              :   dx: number,
     190              :   dy: number
     191              : ): CameraAngleState {
     192            0 :   return clampState({
     193              :     ...state,
     194              :     horizontal: normalizeHorizontal(
     195              :       state.horizontal - dx * DRAG_DEGREES_PER_PIXEL
     196              :     ),
     197              :     vertical: state.vertical + dy * DRAG_DEGREES_PER_PIXEL
     198              :   })
     199              : }
     200              : 
     201            0 : export function dollyByWheel(
     202              :   state: CameraAngleState,
     203              :   deltaY: number
     204              : ): CameraAngleState {
     205            0 :   return clampState({
     206              :     ...state,
     207              :     zoom: state.zoom - deltaY * WHEEL_ZOOM_PER_PIXEL
     208              :   })
     209              : }
     210              : 
     211            0 : export function stateFromHandleDrag(
     212              :   type: OrbitHandleType,
     213              :   state: CameraAngleState,
     214              :   point: Vector3Like
     215              : ): CameraAngleState {
     216            0 :   if (type === 'yaw') {
     217            0 :     return clampState({
     218              :       ...state,
     219              :       horizontal: normalizeHorizontal(pointToYawAngle(point, SUBJECT_CENTER))
     220              :     })
     221              :   }
     222            0 :   if (type === 'pitch') {
     223            0 :     return clampState({
     224              :       ...state,
     225              :       vertical: pointToPitchAngle(point, SUBJECT_CENTER, state.horizontal)
     226              :     })
     227              :   }
     228            0 :   return clampState({
     229              :     ...state,
     230              :     zoom: displayDistanceToZoom(
     231              :       pointToDistance(point, SUBJECT_CENTER, state.horizontal, state.vertical)
     232              :     )
     233              :   })
     234              : }
        

Generated by: LCOV version 2.0-1