|
| 1 | +export function convertFOV( |
| 2 | + fov: number, |
| 3 | + inputAspect: number, |
| 4 | + outputAspect: number |
| 5 | +) { |
| 6 | + return ( |
| 7 | + (Math.atan( |
| 8 | + (outputAspect / inputAspect) * Math.tan((fov * Math.PI) / 360) |
| 9 | + ) * |
| 10 | + 360) / |
| 11 | + Math.PI |
| 12 | + ) |
| 13 | +} |
| 14 | + |
| 15 | +export function filmToAspect(filmNotation: string) { |
| 16 | + const startString = filmNotation.split(/M/)[0] |
| 17 | + const endString = filmNotation.split(/M[FLI]/)[1] |
| 18 | + return Number(startString) / Number(endString) |
| 19 | +} |
| 20 | + |
| 21 | +export function filmToTrue( |
| 22 | + fov: number, |
| 23 | + film: string, |
| 24 | + aspectRatio: number |
| 25 | +): fovValues { |
| 26 | + const filmAspect = filmToAspect(film) |
| 27 | + if (/^\d{1,2}MS\d{1,2}$/.test(film)) { |
| 28 | + return { |
| 29 | + horizontalFOV: fov, |
| 30 | + verticalFOV: convertFOV(fov, filmAspect, 1), |
| 31 | + } |
| 32 | + } else if (film.startsWith('H')) { |
| 33 | + return lockHorizontal(fov, aspectRatio) |
| 34 | + } else if (film.startsWith('V')) { |
| 35 | + return lockVertical(fov, aspectRatio) |
| 36 | + } else if (/^\d{1,2}ML\d{1,2}$/.test(film)) { |
| 37 | + return lockVertical(fov, aspectRatio, filmAspect) |
| 38 | + } else if (/^\d{1,2}MF\d{1,2}$/.test(film)) { |
| 39 | + if (aspectRatio > filmAspect) { |
| 40 | + return lockHorizontal(fov, aspectRatio) |
| 41 | + } |
| 42 | + return lockVertical(fov, aspectRatio, filmAspect) |
| 43 | + } else if (/^\d{1,2}MI\d{1,2}$/.test(film)) { |
| 44 | + if (aspectRatio < filmAspect) { |
| 45 | + return lockHorizontal(fov, aspectRatio) |
| 46 | + } |
| 47 | + return lockVertical(fov, aspectRatio, filmAspect) |
| 48 | + } |
| 49 | + throw Error('parsing failed') |
| 50 | +} |
| 51 | + |
| 52 | +export function trueToFILM( |
| 53 | + { horizontalFOV, verticalFOV }: fovValues, |
| 54 | + film: string, |
| 55 | + aspectRatio: number |
| 56 | +): number { |
| 57 | + const filmAspect = filmToAspect(film) |
| 58 | + if (/^\d{1,2}MS\d{1,2}$/.test(film) || film.startsWith('H')) { |
| 59 | + return horizontalFOV |
| 60 | + } else if (film.startsWith('V')) { |
| 61 | + return verticalFOV |
| 62 | + } else { |
| 63 | + return convertFOV(horizontalFOV, filmAspect, aspectRatio) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +export function lockVertical( |
| 68 | + fov: number, |
| 69 | + aspectRatio: number, |
| 70 | + filmAspect?: number |
| 71 | +): fovValues { |
| 72 | + if (filmAspect) { |
| 73 | + return { |
| 74 | + horizontalFOV: convertFOV(fov, filmAspect, aspectRatio), |
| 75 | + verticalFOV: convertFOV(fov, filmAspect, 1), |
| 76 | + } |
| 77 | + } |
| 78 | + return { |
| 79 | + horizontalFOV: convertFOV(fov, 1, aspectRatio), |
| 80 | + verticalFOV: fov, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +export function lockHorizontal(fov: number, aspectRatio: number): fovValues { |
| 85 | + return { |
| 86 | + horizontalFOV: fov, |
| 87 | + verticalFOV: convertFOV(fov, aspectRatio, 1), |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export interface fovValues { |
| 92 | + horizontalFOV: number |
| 93 | + verticalFOV: number |
| 94 | +} |
| 95 | + |
| 96 | +export function filmToFilm( |
| 97 | + fov: number, |
| 98 | + inFILM: string, |
| 99 | + outFILM: string, |
| 100 | + aspectRatio: number |
| 101 | +): number { |
| 102 | + return trueToFILM( |
| 103 | + filmToTrue(fov, inFILM, aspectRatio), |
| 104 | + outFILM, |
| 105 | + aspectRatio |
| 106 | + ) |
| 107 | +} |
0 commit comments