All files / engine/Source/Scene ScreenSpaceCameraController.js

79.19% Statements 1047/1322
67.02% Branches 378/564
94.87% Functions 37/39
79.16% Lines 1045/1320

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099                                                                        755x 1x                               754x             754x           754x             754x             754x             754x               754x               754x               754x               754x           754x           754x           754x             754x                       754x                     754x                             754x                                 754x                                           754x         754x             754x     754x           754x                   754x     754x             754x     754x               754x                   754x   754x 754x 754x   754x   754x   754x 754x 754x 754x       754x                       754x 754x   754x   754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x 754x   754x 754x         754x 754x 754x 754x 754x 754x 754x 754x       3x       3x 3x       3x                     1x                     92403x 92403x 2244x               92403x 92403x   92403x 92403x 92403x   92403x 3x   3x 3x         3x                                                                                         74x   72x 72x 23x     72x 72x 53x 53x 147x 147x 64x             1x                   46230x 13x     46217x   46217x 23086x 23086x     46217x 46217x 103935x 103935x 103935x     103935x   103935x   103935x 103930x 74x 74x 103856x 92403x                           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                       41x 41x 26x             41x       41x 41x     41x   41x 41x 41x           41x 41x 41x   41x         37x       37x       37x 1x 36x 2x       41x 41x 41x   41x 41x 41x 41x     41x   41x 41x     41x 39x           39x                   39x 8x             39x 8x 8x         31x     39x 39x 39x     41x 33x 33x     8x   8x 8x     8x 8x                                                                                 8x 5x       5x             1x   4x   4x 4x 4x 4x             4x   4x         4x     4x 4x 4x   4x   4x   4x       4x 4x 4x 4x           4x 4x 4x   4x   4x       4x           4x 4x 4x 4x   4x   4x             4x             4x   4x 4x 4x 4x 4x   4x           4x         4x 4x             4x 4x                                 4x   4x 4x         4x 4x                                 4x         4x     4x       4x     4x 4x   4x 4x                                                               4x     4x   4x         4x         1x   3x     4x 4x 3x               4x   4x         4x 2x       1x 1x 1x     5x 5x 5x       5x   5x 5x   5x 5x   5x 5x 5x         8x       8x 8x   8x                 1x 1x     3x         3x 3x 3x 3x 3x   3x 3x 3x 3x   3x 3x 3x 3x   3x 3x 1x   3x 3x 1x   3x   3x                                                         169x 169x 43x               43x                     126x               126x               126x 3x                       1x 1x 1x     29x 29x 29x     29x 4x           29x 5x     24x 24x 24x             24x     24x       24x 3x     21x     1x     5x 5x 5x 5x   5x 5x 3x       3x 3x     2x   5x 5x 5x     1x     2x 2x 2x       2x 2x 2x 2x       2x 2x   2x           2x   1x 1x     2x                   2x     2x 2x     1x       2x     1x                                 1x 1x 1x 1x 1x 1x 1x 1x 1x     6x 6x     6x 6x     6x         6x         6x 6x 6x 6x       6x 6x   6x 6x     6x 1x   1x 1x       6x       1x 1x 1x             1x 1x 1x 1x 1x 1x     5x   5x 5x           5x 5x           5x             5x         5x 5x 5x 5x 5x 5x 5x 5x       1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     2x       2x 2x 2x     2x         2x 2x   2x         1x 1x   1x         1x 1x 1x   1x 1x 1x 1x 1x   1x 1x   1x 1x 1x     1x             1x 1x   1x 1x   1x 1x 1x   1x           1x 1x 1x 1x 1x 1x   1x 1x   1x   1x 1x 1x   1x 1x 1x       1x 1x 1x       1x   1x     1x 1x     1x                                           1x 1x 1x   1x     1x 1x     1x   1x 1x 1x 1x   1x 1x   1x 1x           1x 1x   1x 1x 1x   1x             1x 1x           1x 1x   1x                 1x 1x 1x 1x 1x 1x   1x   1x 1x   1x         1x   1x   1x 1x 1x 1x               1x 1x   1x   1x         1x                                         1x 1x 1x   1x 1x 1x   1x         1x       1x                                                                         1x 1x 1x     7x       7x 7x 7x   7x       7x 1x   6x 6x 6x     7x 7x 7x 7x     7x 3x       7x 3x     7x 1x         1x 1x           7x 4x 4x       7x                   186x 186x   186x 48x               48x                 138x   138x 2x     138x               138x               138x               138x             138x       136x     136x 1x       138x       1x 1x 1x 1x     2x 2x   2x   2x 2x 1x     2x         2x         2x       2x 2x 1x     2x     1x 1x 1x 1x 1x 1x 1x     7x 7x 7x 7x   7x               7x   7x                                             7x 7x 7x   7x       7x   7x 1x         1x 1x 1x         1x 1x 1x                             1x 1x 1x 1x 1x                           6x                 4x 4x 2x 2x 2x           7x                     15x 15x   15x 15x 15x   15x 15x 10x     15x   15x   15x 10x     15x 1x       15x   15x 15x 15x         15x 15x   15x 3x 3x 3x 3x 2x       15x 12x     15x 12x     15x     1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     4x 4x   4x       4x       4x             4x                                                                                                                                                                                           4x 4x 4x     4x           4x 4x   4x 1x 1x 1x 1x   1x         1x 1x     3x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x   3x 3x 3x 3x 3x 3x   3x 3x       3x 3x 2x     3x     3x         3x     3x 3x       3x           3x   3x             3x     3x 3x       1x 1x   1x     26x     26x   26x 26x 26x 26x   26x       26x 1x   25x 25x 25x     26x     26x           26x     26x     26x 9x       26x 6x 6x     26x 1x         1x 1x           26x 20x     26x       26x                   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x     7x 7x   7x       7x       7x 7x 7x     7x                 7x 7x         7x       5x 5x   2x       1x     5x 5x 5x 5x 5x       5x             5x   5x 5x 5x 5x     5x 5x 5x                                                         5x           5x 5x 5x 5x 5x 5x   5x 5x   5x   5x 5x 5x   5x 5x 5x       2x 2x 2x 2x           2x     2x   2x 1x 1x 1x                               1x     2x 1x 1x   1x     2x 2x     2x   2x 2x 2x 2x   2x 2x 2x   2x 2x         2x     2x   2x         2x           2x 2x 2x 2x 2x 2x   2x   2x 2x   2x         2x   2x 2x 2x         1x     2x 2x   2x   2x         2x 2x   2x                                         2x 2x 2x   2x 2x 2x   2x         2x 1x     2x                                                                 1x 1x 1x 1x 1x 1x     2x 2x   2x 2x 2x 2x 2x 2x   2x 2x 2x       2x                         2x 2x     2x 2x   1x     2x   2x 2x   2x 1x   1x     2x 2x 2x 2x   2x 2x 2x   2x                         2x 2x     2x 2x   2x   2x   2x 2x 1x 1x       1x         1x         1x                             1x       1x         11321x               11321x               11321x               11321x               1x 1x     11669x   11669x 11669x   11669x 175x     11494x 11494x 11494x       11494x 6887x 6887x 6887x     11494x 11494x 11316x   178x     11494x 11494x 8099x 8099x 67x 67x 67x       67x       7x 7x 4x   3x   7x     67x 10x   57x         11494x 6887x 6887x 3x 3x 3x         3x 3x 3x               1x 11568x 11568x 11568x   11568x 165x           11403x     1x 1x         1x 11682x 11682x   11682x 6930x 6930x   4752x 4752x     11682x 11682x         11682x         11682x           11682x   11682x 11682x 11682x   11682x 11682x       11682x         11682x 169x 11513x 186x 186x 11327x 11321x 11321x     11682x     11668x   11668x     11682x                         1x 72x                                   1x 743x 743x 743x      
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian3 from "../Core/Cartesian3.js";
import Cartesian4 from "../Core/Cartesian4.js";
import Cartographic from "../Core/Cartographic.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import Ellipsoid from "../Core/Ellipsoid.js";
import HeadingPitchRoll from "../Core/HeadingPitchRoll.js";
import IntersectionTests from "../Core/IntersectionTests.js";
import KeyboardEventModifier from "../Core/KeyboardEventModifier.js";
import CesiumMath from "../Core/Math.js";
import Matrix3 from "../Core/Matrix3.js";
import Matrix4 from "../Core/Matrix4.js";
import OrthographicFrustum from "../Core/OrthographicFrustum.js";
import Plane from "../Core/Plane.js";
import Quaternion from "../Core/Quaternion.js";
import Ray from "../Core/Ray.js";
import VerticalExaggeration from "../Core/VerticalExaggeration.js";
import Transforms from "../Core/Transforms.js";
import CameraEventAggregator from "./CameraEventAggregator.js";
import CameraEventType from "./CameraEventType.js";
import MapMode2D from "./MapMode2D.js";
import SceneMode from "./SceneMode.js";
import SceneTransforms from "./SceneTransforms.js";
import TweenCollection from "./TweenCollection.js";
 
/**
 * Modifies the camera position and orientation based on mouse input to a canvas.
 * @alias ScreenSpaceCameraController
 * @constructor
 *
 * @param {Scene} scene The scene.
 */
function ScreenSpaceCameraController(scene) {
  //>>includeStart('debug', pragmas.debug);
  if (!defined(scene)) {
    throw new DeveloperError("scene is required.");
  }
  //>>includeEnd('debug');
 
  /**
   * If true, inputs are allowed conditionally with the flags enableTranslate, enableZoom,
   * enableRotate, enableTilt, and enableLook.  If false, all inputs are disabled.
   *
   * NOTE: This setting is for temporary use cases, such as camera flights and
   * drag-selection of regions (see Picking demo).  It is typically set to false at the
   * start of such events, and set true on completion.  To keep inputs disabled
   * past the end of camera flights, you must use the other booleans (enableTranslate,
   * enableZoom, enableRotate, enableTilt, and enableLook).
   * @type {boolean}
   * @default true
   */
  this.enableInputs = true;
  /**
   * If true, allows the user to pan around the map.  If false, the camera stays locked at the current position.
   * This flag only applies in 2D and Columbus view modes.
   * @type {boolean}
   * @default true
   */
  this.enableTranslate = true;
  /**
   * If true, allows the user to zoom in and out.  If false, the camera is locked to the current distance from the ellipsoid.
   * @type {boolean}
   * @default true
   */
  this.enableZoom = true;
  /**
   * If true, allows the user to rotate the world which translates the user's position.
   * This flag only applies in 2D and 3D.
   * @type {boolean}
   * @default true
   */
  this.enableRotate = true;
  /**
   * If true, allows the user to tilt the camera.  If false, the camera is locked to the current heading.
   * This flag only applies in 3D and Columbus view.
   * @type {boolean}
   * @default true
   */
  this.enableTilt = true;
  /**
   * If true, allows the user to use free-look. If false, the camera view direction can only be changed through translating
   * or rotating. This flag only applies in 3D and Columbus view modes.
   * @type {boolean}
   * @default true
   */
  this.enableLook = true;
  /**
   * A parameter in the range <code>[0, 1)</code> used to determine how long
   * the camera will continue to spin because of inertia.
   * With value of zero, the camera will have no inertia.
   * @type {number}
   * @default 0.9
   */
  this.inertiaSpin = 0.9;
  /**
   * A parameter in the range <code>[0, 1)</code> used to determine how long
   * the camera will continue to translate because of inertia.
   * With value of zero, the camera will have no inertia.
   * @type {number}
   * @default 0.9
   */
  this.inertiaTranslate = 0.9;
  /**
   * A parameter in the range <code>[0, 1)</code> used to determine how long
   * the camera will continue to zoom because of inertia.
   * With value of zero, the camera will have no inertia.
   * @type {number}
   * @default 0.8
   */
  this.inertiaZoom = 0.8;
  /**
   * A parameter in the range <code>[0, 1)</code> used to limit the range
   * of various user inputs to a percentage of the window width/height per animation frame.
   * This helps keep the camera under control in low-frame-rate situations.
   * @type {number}
   * @default 0.1
   */
  this.maximumMovementRatio = 0.1;
  /**
   * Sets the duration, in seconds, of the bounce back animations in 2D and Columbus view.
   * @type {number}
   * @default 3.0
   */
  this.bounceAnimationTime = 3.0;
  /**
   * The minimum magnitude, in meters, of the camera position when zooming. Defaults to 1.0.
   * @type {number}
   * @default 1.0
   */
  this.minimumZoomDistance = 1.0;
  /**
   * The maximum magnitude, in meters, of the camera position when zooming. Defaults to positive infinity.
   * @type {number}
   * @default {@link Number.POSITIVE_INFINITY}
   */
  this.maximumZoomDistance = Number.POSITIVE_INFINITY;
 
  /**
   * A multiplier for the speed at which the camera will zoom.
   * @type {number}
   * @default 5.0
   */
  this.zoomFactor = 5.0;
 
  /**
   * The input that allows the user to pan around the map. This only applies in 2D and Columbus view modes.
   * <p>
   * The type can be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
   * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
   * or an array of any of the preceding.
   * </p>
   * @type {CameraEventType|Array|undefined}
   * @default {@link CameraEventType.LEFT_DRAG}
   */
  this.translateEventTypes = CameraEventType.LEFT_DRAG;
  /**
   * The input that allows the user to zoom in/out.
   * <p>
   * The type can be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
   * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
   * or an array of any of the preceding.
   * </p>
   * @type {CameraEventType|Array|undefined}
   * @default [{@link CameraEventType.RIGHT_DRAG}, {@link CameraEventType.WHEEL}, {@link CameraEventType.PINCH}]
   */
  this.zoomEventTypes = [
    CameraEventType.RIGHT_DRAG,
    CameraEventType.WHEEL,
    CameraEventType.PINCH,
  ];
  /**
   * The input that allows the user to rotate around the globe or another object. This only applies in 3D and Columbus view modes.
   * <p>
   * The type can be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
   * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
   * or an array of any of the preceding.
   * </p>
   * @type {CameraEventType|Array|undefined}
   * @default {@link CameraEventType.LEFT_DRAG}
   */
  this.rotateEventTypes = CameraEventType.LEFT_DRAG;
  /**
   * The input that allows the user to tilt in 3D and Columbus view or twist in 2D.
   * <p>
   * The type can be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
   * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
   * or an array of any of the preceding.
   * </p>
   * @type {CameraEventType|Array|undefined}
   * @default [{@link CameraEventType.MIDDLE_DRAG}, {@link CameraEventType.PINCH}, {
   *     eventType : {@link CameraEventType.LEFT_DRAG},
   *     modifier : {@link KeyboardEventModifier.CTRL}
   * }, {
   *     eventType : {@link CameraEventType.RIGHT_DRAG},
   *     modifier : {@link KeyboardEventModifier.CTRL}
   * }]
   */
  this.tiltEventTypes = [
    CameraEventType.MIDDLE_DRAG,
    CameraEventType.PINCH,
    {
      eventType: CameraEventType.LEFT_DRAG,
      modifier: KeyboardEventModifier.CTRL,
    },
    {
      eventType: CameraEventType.RIGHT_DRAG,
      modifier: KeyboardEventModifier.CTRL,
    },
  ];
  /**
   * The input that allows the user to change the direction the camera is viewing. This only applies in 3D and Columbus view modes.
   * <p>
   * The type can be a {@link CameraEventType}, <code>undefined</code>, an object with <code>eventType</code>
   * and <code>modifier</code> properties with types <code>CameraEventType</code> and {@link KeyboardEventModifier},
   * or an array of any of the preceding.
   * </p>
   * @type {CameraEventType|Array|undefined}
   * @default { eventType : {@link CameraEventType.LEFT_DRAG}, modifier : {@link KeyboardEventModifier.SHIFT} }
   */
  this.lookEventTypes = {
    eventType: CameraEventType.LEFT_DRAG,
    modifier: KeyboardEventModifier.SHIFT,
  };
 
  const ellipsoid = scene.ellipsoid ?? Ellipsoid.default;
 
  /**
   * The minimum height the camera must be before picking the terrain or scene content instead of the ellipsoid. Defaults to scene.ellipsoid.minimumRadius * 0.025 when another ellipsoid than WGS84 is used.
   * @type {number}
   * @default 150000.0 or scene.ellipsoid.minimumRadius * 0.025
   */
  this.minimumPickingTerrainHeight = Ellipsoid.WGS84.equals(ellipsoid)
    ? 150000.0
    : ellipsoid.minimumRadius * 0.025;
  this._minimumPickingTerrainHeight = this.minimumPickingTerrainHeight;
  /**
   * The minimum distance the camera must be before testing for collision with terrain when zoom with inertia. Default to scene.ellipsoid.minimumRadius * 0.00063 when another ellipsoid than WGS84 is used.
   * @type {number}
   * @default 4000.0 or scene.ellipsoid.minimumRadius * 0.00063
   */
  this.minimumPickingTerrainDistanceWithInertia = Ellipsoid.WGS84.equals(
    ellipsoid,
  )
    ? 4000.0
    : ellipsoid.minimumRadius * 0.00063;
  /**
   * The minimum height the camera must be before testing for collision with terrain. Default to scene.ellipsoid.minimumRadius * 0.0025 when another ellipsoid than WGS84 is used.
   * @type {number}
   * @default 15000.0 or scene.ellipsoid.minimumRadius * 0.0025.
   */
  this.minimumCollisionTerrainHeight = Ellipsoid.WGS84.equals(ellipsoid)
    ? 15000.0
    : ellipsoid.minimumRadius * 0.0025;
  this._minimumCollisionTerrainHeight = this.minimumCollisionTerrainHeight;
  /**
   * The minimum height the camera must be before switching from rotating a track ball to
   * free look when clicks originate on the sky or in space. Defaults to ellipsoid.minimumRadius * 1.175 when another ellipsoid than WGS84 is used.
   * @type {number}
   * @default 7500000.0 or scene.ellipsoid.minimumRadius * 1.175
   */
  this.minimumTrackBallHeight = Ellipsoid.WGS84.equals(ellipsoid)
    ? 7500000.0
    : ellipsoid.minimumRadius * 1.175;
  this._minimumTrackBallHeight = this.minimumTrackBallHeight;
  /**
   * When disabled, the values of <code>maximumZoomDistance</code> and <code>minimumZoomDistance</code> are ignored.
   * Also used in conjunction with {@link Cesium3DTileset#enableCollision} to prevent the camera from moving through or below a 3D Tileset surface.
   * This may also affect clamping behavior when using {@link HeightReference.CLAMP_TO_GROUND} on 3D Tiles.
   * @type {boolean}
   * @default true
   */
  this.enableCollisionDetection = true;
  /**
   * The angle, relative to the ellipsoid normal, restricting the maximum amount that the user can tilt the camera. If <code>undefined</code>, the angle of the camera tilt is unrestricted.
   * @type {number|undefined}
   * @default undefined
   *
   * @example
   * // Prevent the camera from tilting below the ellipsoid surface
   * viewer.scene.screenSpaceCameraController.maximumTiltAngle = Math.PI / 2.0;
   */
  this.maximumTiltAngle = undefined;
 
  this._scene = scene;
  this._globe = undefined;
  this._ellipsoid = ellipsoid;
 
  this._lastGlobeHeight = 0.0;
 
  this._aggregator = new CameraEventAggregator(scene.canvas);
 
  this._lastInertiaSpinMovement = undefined;
  this._lastInertiaZoomMovement = undefined;
  this._lastInertiaTranslateMovement = undefined;
  this._lastInertiaTiltMovement = undefined;
 
  // Zoom disables tilt, spin, and translate inertia
  // Tilt disables spin and translate inertia
  this._inertiaDisablers = {
    _lastInertiaZoomMovement: [
      "_lastInertiaSpinMovement",
      "_lastInertiaTranslateMovement",
      "_lastInertiaTiltMovement",
    ],
    _lastInertiaTiltMovement: [
      "_lastInertiaSpinMovement",
      "_lastInertiaTranslateMovement",
    ],
  };
 
  this._tweens = new TweenCollection();
  this._tween = undefined;
 
  this._horizontalRotationAxis = undefined;
 
  this._tiltCenterMousePosition = new Cartesian2(-1.0, -1.0);
  this._tiltCenter = new Cartesian3();
  this._rotateMousePosition = new Cartesian2(-1.0, -1.0);
  this._rotateStartPosition = new Cartesian3();
  this._strafeStartPosition = new Cartesian3();
  this._strafeMousePosition = new Cartesian2();
  this._strafeEndMousePosition = new Cartesian2();
  this._zoomMouseStart = new Cartesian2(-1.0, -1.0);
  this._zoomWorldPosition = new Cartesian3();
  this._useZoomWorldPosition = false;
  this._panLastMousePosition = new Cartesian2();
  this._panLastWorldPosition = new Cartesian3();
  this._tiltCVOffMap = false;
  this._looking = false;
  this._rotating = false;
  this._strafing = false;
  this._zoomingOnVector = false;
  this._zoomingUnderground = false;
  this._rotatingZoom = false;
  this._adjustedHeightForTerrain = false;
  this._cameraUnderground = false;
 
  const projection = scene.mapProjection;
  this._maxCoord = projection.project(
    new Cartographic(Math.PI, CesiumMath.PI_OVER_TWO),
  );
 
  // Constants, Make any of these public?
  this._rotateFactor = undefined;
  this._rotateRateRangeAdjustment = undefined;
  this._maximumRotateRate = 1.77;
  this._minimumRotateRate = 1.0 / 5000.0;
  this._minimumZoomRate = 20.0;
  this._maximumZoomRate = 5906376272000.0; // distance from the Sun to Pluto in meters.
  this._minimumUndergroundPickDistance = 2000.0;
  this._maximumUndergroundPickDistance = 10000.0;
}
 
function decay(time, coefficient) {
  Iif (time < 0) {
    return 0.0;
  }
 
  const tau = (1.0 - coefficient) * 25.0;
  return Math.exp(-tau * time);
}
 
function sameMousePosition(movement) {
  return Cartesian2.equalsEpsilon(
    movement.startPosition,
    movement.endPosition,
    CesiumMath.EPSILON14,
  );
}
 
// If the time between mouse down and mouse up is not between
// these thresholds, the camera will not move with inertia.
// This value is probably dependent on the browser and/or the
// hardware. Should be investigated further.
const inertiaMaxClickTimeThreshold = 0.4;
 
function maintainInertia(
  aggregator,
  type,
  modifier,
  decayCoef,
  action,
  object,
  lastMovementName,
) {
  let movementState = object[lastMovementName];
  if (!defined(movementState)) {
    movementState = object[lastMovementName] = {
      startPosition: new Cartesian2(),
      endPosition: new Cartesian2(),
      motion: new Cartesian2(),
      inertiaEnabled: true,
    };
  }
 
  const ts = aggregator.getButtonPressTime(type, modifier);
  const tr = aggregator.getButtonReleaseTime(type, modifier);
 
  const threshold = ts && tr && (tr.getTime() - ts.getTime()) / 1000.0;
  const now = new Date();
  const fromNow = tr && (now.getTime() - tr.getTime()) / 1000.0;
 
  if (ts && tr && threshold < inertiaMaxClickTimeThreshold) {
    const d = decay(fromNow, decayCoef);
 
    const lastMovement = aggregator.getLastMovement(type, modifier);
    Eif (
      !defined(lastMovement) ||
      sameMousePosition(lastMovement) ||
      !movementState.inertiaEnabled
    ) {
      return;
    }
 
    movementState.motion.x =
      (lastMovement.endPosition.x - lastMovement.startPosition.x) * 0.5;
    movementState.motion.y =
      (lastMovement.endPosition.y - lastMovement.startPosition.y) * 0.5;
 
    movementState.startPosition = Cartesian2.clone(
      lastMovement.startPosition,
      movementState.startPosition,
    );
 
    movementState.endPosition = Cartesian2.multiplyByScalar(
      movementState.motion,
      d,
      movementState.endPosition,
    );
    movementState.endPosition = Cartesian2.add(
      movementState.startPosition,
      movementState.endPosition,
      movementState.endPosition,
    );
 
    // If value from the decreasing exponential function is close to zero,
    // the end coordinates may be NaN.
    if (
      isNaN(movementState.endPosition.x) ||
      isNaN(movementState.endPosition.y) ||
      Cartesian2.distance(
        movementState.startPosition,
        movementState.endPosition,
      ) < 0.5
    ) {
      return;
    }
 
    if (!aggregator.isButtonDown(type, modifier)) {
      const startPosition = aggregator.getStartMousePosition(type, modifier);
      action(object, startPosition, movementState);
    }
  }
}
 
function activateInertia(controller, inertiaStateName) {
  if (defined(inertiaStateName)) {
    // Re-enable inertia if it was disabled
    let movementState = controller[inertiaStateName];
    if (defined(movementState)) {
      movementState.inertiaEnabled = true;
    }
    // Disable inertia on other movements
    const inertiasToDisable = controller._inertiaDisablers[inertiaStateName];
    if (defined(inertiasToDisable)) {
      const length = inertiasToDisable.length;
      for (let i = 0; i < length; ++i) {
        movementState = controller[inertiasToDisable[i]];
        if (defined(movementState)) {
          movementState.inertiaEnabled = false;
        }
      }
    }
  }
}
 
const scratchEventTypeArray = [];
 
function reactToInput(
  controller,
  enabled,
  eventTypes,
  action,
  inertiaConstant,
  inertiaStateName,
) {
  if (!defined(eventTypes)) {
    return;
  }
 
  const aggregator = controller._aggregator;
 
  if (!Array.isArray(eventTypes)) {
    scratchEventTypeArray[0] = eventTypes;
    eventTypes = scratchEventTypeArray;
  }
 
  const length = eventTypes.length;
  for (let i = 0; i < length; ++i) {
    const eventType = eventTypes[i];
    const type = defined(eventType.eventType) ? eventType.eventType : eventType;
    const modifier = eventType.modifier;
 
    const movement =
      aggregator.isMoving(type, modifier) &&
      aggregator.getMovement(type, modifier);
    const startPosition = aggregator.getStartMousePosition(type, modifier);
 
    if (controller.enableInputs && enabled) {
      if (movement) {
        action(controller, startPosition, movement);
        activateInertia(controller, inertiaStateName);
      } else if (inertiaConstant < 1.0) {
        maintainInertia(
          aggregator,
          type,
          modifier,
          inertiaConstant,
          action,
          controller,
          inertiaStateName,
        );
      }
    }
  }
}
 
const scratchZoomPickRay = new Ray();
const scratchPickCartesian = new Cartesian3();
const scratchZoomOffset = new Cartesian2();
const scratchZoomDirection = new Cartesian3();
const scratchCenterPixel = new Cartesian2();
const scratchCenterPosition = new Cartesian3();
const scratchPositionNormal = new Cartesian3();
const scratchPickNormal = new Cartesian3();
const scratchZoomAxis = new Cartesian3();
const scratchCameraPositionNormal = new Cartesian3();
 
// Scratch variables used in zooming algorithm
const scratchTargetNormal = new Cartesian3();
const scratchCameraPosition = new Cartesian3();
const scratchCameraUpNormal = new Cartesian3();
const scratchCameraRightNormal = new Cartesian3();
const scratchForwardNormal = new Cartesian3();
const scratchPositionToTarget = new Cartesian3();
const scratchPositionToTargetNormal = new Cartesian3();
const scratchPan = new Cartesian3();
const scratchCenterMovement = new Cartesian3();
const scratchCenter = new Cartesian3();
const scratchCartesian = new Cartesian3();
const scratchCartesianTwo = new Cartesian3();
const scratchCartesianThree = new Cartesian3();
const scratchZoomViewOptions = {
  orientation: new HeadingPitchRoll(),
};
 
function handleZoom(
  object,
  startPosition,
  movement,
  zoomFactor,
  distanceMeasure,
  unitPositionDotDirection,
) {
  let percentage = 1.0;
  if (defined(unitPositionDotDirection)) {
    percentage = CesiumMath.clamp(
      Math.abs(unitPositionDotDirection),
      0.25,
      1.0,
    );
  }
 
  const diff = movement.endPosition.y - movement.startPosition.y;
 
  // distanceMeasure should be the height above the ellipsoid.
  // When approaching the surface, the zoomRate slows and stops minimumZoomDistance above it.
  const approachingSurface = diff > 0;
  const minHeight = approachingSurface
    ? object.minimumZoomDistance * percentage
    : 0;
  const maxHeight = object.maximumZoomDistance;
 
  const minDistance = distanceMeasure - minHeight;
  let zoomRate = zoomFactor * minDistance;
  zoomRate = CesiumMath.clamp(
    zoomRate,
    object._minimumZoomRate,
    object._maximumZoomRate,
  );
 
  let rangeWindowRatio = diff / object._scene.canvas.clientHeight;
  rangeWindowRatio = Math.min(rangeWindowRatio, object.maximumMovementRatio);
  let distance = zoomRate * rangeWindowRatio;
 
  if (
    object.enableCollisionDetection ||
    object.minimumZoomDistance === 0.0 ||
    !defined(object._globe) // look-at mode
  ) {
    Iif (distance > 0.0 && Math.abs(distanceMeasure - minHeight) < 1.0) {
      return;
    }
 
    Iif (distance < 0.0 && Math.abs(distanceMeasure - maxHeight) < 1.0) {
      return;
    }
 
    if (distanceMeasure - distance < minHeight) {
      distance = distanceMeasure - minHeight - 1.0;
    } else if (distanceMeasure - distance > maxHeight) {
      distance = distanceMeasure - maxHeight;
    }
  }
 
  const scene = object._scene;
  const camera = scene.camera;
  const mode = scene.mode;
 
  const orientation = scratchZoomViewOptions.orientation;
  orientation.heading = camera.heading;
  orientation.pitch = camera.pitch;
  orientation.roll = camera.roll;
 
  const sameStartPosition =
    movement.inertiaEnabled ??
    Cartesian2.equals(startPosition, object._zoomMouseStart);
  let zoomingOnVector = object._zoomingOnVector;
  let rotatingZoom = object._rotatingZoom;
  let pickedPosition;
 
  if (!sameStartPosition) {
    object._zoomMouseStart = Cartesian2.clone(
      startPosition,
      object._zoomMouseStart,
    );
 
    // When camera transform is set, such as tracking an entity, object._globe will be undefined, and no position should be picked
    Iif (defined(object._globe) && mode === SceneMode.SCENE2D) {
      pickedPosition = camera.getPickRay(
        startPosition,
        scratchZoomPickRay,
      ).origin;
      pickedPosition = Cartesian3.fromElements(
        pickedPosition.y,
        pickedPosition.z,
        pickedPosition.x,
      );
    } else if (defined(object._globe)) {
      pickedPosition = pickPosition(
        object,
        startPosition,
        scratchPickCartesian,
      );
    }
 
    if (defined(pickedPosition)) {
      object._useZoomWorldPosition = true;
      object._zoomWorldPosition = Cartesian3.clone(
        pickedPosition,
        object._zoomWorldPosition,
      );
    } else {
      object._useZoomWorldPosition = false;
    }
 
    zoomingOnVector = object._zoomingOnVector = false;
    rotatingZoom = object._rotatingZoom = false;
    object._zoomingUnderground = object._cameraUnderground;
  }
 
  if (!object._useZoomWorldPosition) {
    camera.zoomIn(distance);
    return;
  }
 
  let zoomOnVector = mode === SceneMode.COLUMBUS_VIEW;
 
  Eif (camera.positionCartographic.height < 2000000) {
    rotatingZoom = true;
  }
 
  Eif (!sameStartPosition || rotatingZoom) {
    Iif (mode === SceneMode.SCENE2D) {
      const worldPosition = object._zoomWorldPosition;
      const endPosition = camera.position;
 
      if (
        !Cartesian3.equals(worldPosition, endPosition) &&
        camera.positionCartographic.height < object._maxCoord.x * 2.0
      ) {
        const savedX = camera.position.x;
 
        const direction = Cartesian3.subtract(
          worldPosition,
          endPosition,
          scratchZoomDirection,
        );
        Cartesian3.normalize(direction, direction);
 
        const d =
          (Cartesian3.distance(worldPosition, endPosition) * distance) /
          (camera.getMagnitude() * 0.5);
        camera.move(direction, d * 0.5);
 
        if (
          (camera.position.x < 0.0 && savedX > 0.0) ||
          (camera.position.x > 0.0 && savedX < 0.0)
        ) {
          pickedPosition = camera.getPickRay(
            startPosition,
            scratchZoomPickRay,
          ).origin;
          pickedPosition = Cartesian3.fromElements(
            pickedPosition.y,
            pickedPosition.z,
            pickedPosition.x,
          );
          object._zoomWorldPosition = Cartesian3.clone(
            pickedPosition,
            object._zoomWorldPosition,
          );
        }
      }
    } else if (mode === SceneMode.SCENE3D) {
      const cameraPositionNormal = Cartesian3.normalize(
        camera.position,
        scratchCameraPositionNormal,
      );
      if (
        object._cameraUnderground ||
        object._zoomingUnderground ||
        (camera.positionCartographic.height < 3000.0 &&
          Math.abs(Cartesian3.dot(camera.direction, cameraPositionNormal)) <
            0.6)
      ) {
        zoomOnVector = true;
      } else {
        const canvas = scene.canvas;
 
        const centerPixel = scratchCenterPixel;
        centerPixel.x = canvas.clientWidth / 2;
        centerPixel.y = canvas.clientHeight / 2;
        const centerPosition = pickPosition(
          object,
          centerPixel,
          scratchCenterPosition,
        );
        // If centerPosition is not defined, it means the globe does not cover the center position of screen
 
        Iif (!defined(centerPosition)) {
          zoomOnVector = true;
        } else if (camera.positionCartographic.height < 1000000) {
          // The math in the else block assumes the camera
          // points toward the earth surface, so we check it here.
          // Theoretically, we should check for 90 degree, but it doesn't behave well when parallel
          // to the earth surface
          Iif (Cartesian3.dot(camera.direction, cameraPositionNormal) >= -0.5) {
            zoomOnVector = true;
          } else {
            const cameraPosition = scratchCameraPosition;
            Cartesian3.clone(camera.position, cameraPosition);
            const target = object._zoomWorldPosition;
 
            let targetNormal = scratchTargetNormal;
 
            targetNormal = Cartesian3.normalize(target, targetNormal);
 
            Iif (Cartesian3.dot(targetNormal, cameraPositionNormal) < 0.0) {
              return;
            }
 
            const center = scratchCenter;
            const forward = scratchForwardNormal;
            Cartesian3.clone(camera.direction, forward);
            Cartesian3.add(
              cameraPosition,
              Cartesian3.multiplyByScalar(forward, 1000, scratchCartesian),
              center,
            );
 
            const positionToTarget = scratchPositionToTarget;
            const positionToTargetNormal = scratchPositionToTargetNormal;
            Cartesian3.subtract(target, cameraPosition, positionToTarget);
 
            Cartesian3.normalize(positionToTarget, positionToTargetNormal);
 
            const alphaDot = Cartesian3.dot(
              cameraPositionNormal,
              positionToTargetNormal,
            );
            Iif (alphaDot >= 0.0) {
              // We zoomed past the target, and this zoom is not valid anymore.
              // This line causes the next zoom movement to pick a new starting point.
              object._zoomMouseStart.x = -1;
              return;
            }
            const alpha = Math.acos(-alphaDot);
            const cameraDistance = Cartesian3.magnitude(cameraPosition);
            const targetDistance = Cartesian3.magnitude(target);
            const remainingDistance = cameraDistance - distance;
            const positionToTargetDistance =
              Cartesian3.magnitude(positionToTarget);
 
            const gamma = Math.asin(
              CesiumMath.clamp(
                (positionToTargetDistance / targetDistance) * Math.sin(alpha),
                -1.0,
                1.0,
              ),
            );
            const delta = Math.asin(
              CesiumMath.clamp(
                (remainingDistance / targetDistance) * Math.sin(alpha),
                -1.0,
                1.0,
              ),
            );
            const beta = gamma - delta + alpha;
 
            const up = scratchCameraUpNormal;
            Cartesian3.normalize(cameraPosition, up);
            let right = scratchCameraRightNormal;
            right = Cartesian3.cross(positionToTargetNormal, up, right);
            right = Cartesian3.normalize(right, right);
 
            Cartesian3.normalize(
              Cartesian3.cross(up, right, scratchCartesian),
              forward,
            );
 
            // Calculate new position to move to
            Cartesian3.multiplyByScalar(
              Cartesian3.normalize(center, scratchCartesian),
              Cartesian3.magnitude(center) - distance,
              center,
            );
            Cartesian3.normalize(cameraPosition, cameraPosition);
            Cartesian3.multiplyByScalar(
              cameraPosition,
              remainingDistance,
              cameraPosition,
            );
 
            // Pan
            const pMid = scratchPan;
            Cartesian3.multiplyByScalar(
              Cartesian3.add(
                Cartesian3.multiplyByScalar(
                  up,
                  Math.cos(beta) - 1,
                  scratchCartesianTwo,
                ),
                Cartesian3.multiplyByScalar(
                  forward,
                  Math.sin(beta),
                  scratchCartesianThree,
                ),
                scratchCartesian,
              ),
              remainingDistance,
              pMid,
            );
            Cartesian3.add(cameraPosition, pMid, cameraPosition);
 
            Cartesian3.normalize(center, up);
            Cartesian3.normalize(
              Cartesian3.cross(up, right, scratchCartesian),
              forward,
            );
 
            const cMid = scratchCenterMovement;
            Cartesian3.multiplyByScalar(
              Cartesian3.add(
                Cartesian3.multiplyByScalar(
                  up,
                  Math.cos(beta) - 1,
                  scratchCartesianTwo,
                ),
                Cartesian3.multiplyByScalar(
                  forward,
                  Math.sin(beta),
                  scratchCartesianThree,
                ),
                scratchCartesian,
              ),
              Cartesian3.magnitude(center),
              cMid,
            );
            Cartesian3.add(center, cMid, center);
 
            // Update camera
 
            // Set new position
            Cartesian3.clone(cameraPosition, camera.position);
 
            // Set new direction
            Cartesian3.normalize(
              Cartesian3.subtract(center, cameraPosition, scratchCartesian),
              camera.direction,
            );
            Cartesian3.clone(camera.direction, camera.direction);
 
            // Set new right & up vectors
            Cartesian3.cross(camera.direction, camera.up, camera.right);
            Cartesian3.cross(camera.right, camera.direction, camera.up);
 
            camera.setView(scratchZoomViewOptions);
            return;
          }
        } else E{
          const positionNormal = Cartesian3.normalize(
            centerPosition,
            scratchPositionNormal,
          );
          const pickedNormal = Cartesian3.normalize(
            object._zoomWorldPosition,
            scratchPickNormal,
          );
          const dotProduct = Cartesian3.dot(pickedNormal, positionNormal);
 
          if (dotProduct > 0.0 && dotProduct < 1.0) {
            const angle = CesiumMath.acosClamped(dotProduct);
            const axis = Cartesian3.cross(
              pickedNormal,
              positionNormal,
              scratchZoomAxis,
            );
 
            const denom =
              Math.abs(angle) > CesiumMath.toRadians(20.0)
                ? camera.positionCartographic.height * 0.75
                : camera.positionCartographic.height - distance;
            const scalar = distance / denom;
            camera.rotate(axis, angle * scalar);
          }
        }
      }
    }
 
    object._rotatingZoom = !zoomOnVector;
  }
 
  if ((!sameStartPosition && zoomOnVector) || zoomingOnVector) {
    let ray;
    const zoomMouseStart = SceneTransforms.worldToWindowCoordinates(
      scene,
      object._zoomWorldPosition,
      scratchZoomOffset,
    );
    if (
      mode !== SceneMode.COLUMBUS_VIEW &&
      Cartesian2.equals(startPosition, object._zoomMouseStart) &&
      defined(zoomMouseStart)
    ) {
      ray = camera.getPickRay(zoomMouseStart, scratchZoomPickRay);
    } else {
      ray = camera.getPickRay(startPosition, scratchZoomPickRay);
    }
 
    const rayDirection = ray.direction;
    if (mode === SceneMode.COLUMBUS_VIEW || mode === SceneMode.SCENE2D) {
      Cartesian3.fromElements(
        rayDirection.y,
        rayDirection.z,
        rayDirection.x,
        rayDirection,
      );
    }
 
    camera.move(rayDirection, distance);
 
    object._zoomingOnVector = true;
  } else E{
    camera.zoomIn(distance);
  }
 
  if (!object._cameraUnderground) {
    camera.setView(scratchZoomViewOptions);
  }
}
 
const translate2DStart = new Ray();
const translate2DEnd = new Ray();
const scratchTranslateP0 = new Cartesian3();
 
function translate2D(controller, startPosition, movement) {
  const scene = controller._scene;
  const camera = scene.camera;
  let start = camera.getPickRay(
    movement.startPosition,
    translate2DStart,
  ).origin;
  let end = camera.getPickRay(movement.endPosition, translate2DEnd).origin;
 
  start = Cartesian3.fromElements(start.y, start.z, start.x, start);
  end = Cartesian3.fromElements(end.y, end.z, end.x, end);
 
  const direction = Cartesian3.subtract(start, end, scratchTranslateP0);
  const distance = Cartesian3.magnitude(direction);
 
  Eif (distance > 0.0) {
    Cartesian3.normalize(direction, direction);
    camera.move(direction, distance);
  }
}
 
function zoom2D(controller, startPosition, movement) {
  Iif (defined(movement.distance)) {
    movement = movement.distance;
  }
 
  const scene = controller._scene;
  const camera = scene.camera;
 
  handleZoom(
    controller,
    startPosition,
    movement,
    controller.zoomFactor,
    camera.getMagnitude(),
  );
}
 
const twist2DStart = new Cartesian2();
const twist2DEnd = new Cartesian2();
 
function twist2D(controller, startPosition, movement) {
  Iif (defined(movement.angleAndHeight)) {
    singleAxisTwist2D(controller, startPosition, movement.angleAndHeight);
    return;
  }
 
  const scene = controller._scene;
  const camera = scene.camera;
  const canvas = scene.canvas;
  const width = canvas.clientWidth;
  const height = canvas.clientHeight;
 
  let start = twist2DStart;
  start.x = (2.0 / width) * movement.startPosition.x - 1.0;
  start.y = (2.0 / height) * (height - movement.startPosition.y) - 1.0;
  start = Cartesian2.normalize(start, start);
 
  let end = twist2DEnd;
  end.x = (2.0 / width) * movement.endPosition.x - 1.0;
  end.y = (2.0 / height) * (height - movement.endPosition.y) - 1.0;
  end = Cartesian2.normalize(end, end);
 
  let startTheta = CesiumMath.acosClamped(start.x);
  if (start.y < 0) {
    startTheta = CesiumMath.TWO_PI - startTheta;
  }
  let endTheta = CesiumMath.acosClamped(end.x);
  if (end.y < 0) {
    endTheta = CesiumMath.TWO_PI - endTheta;
  }
  const theta = endTheta - startTheta;
 
  camera.twistRight(theta);
}
 
function singleAxisTwist2D(controller, startPosition, movement) {
  let rotateRate =
    controller._rotateFactor * controller._rotateRateRangeAdjustment;
 
  if (rotateRate > controller._maximumRotateRate) {
    rotateRate = controller._maximumRotateRate;
  }
 
  if (rotateRate < controller._minimumRotateRate) {
    rotateRate = controller._minimumRotateRate;
  }
 
  const scene = controller._scene;
  const camera = scene.camera;
  const canvas = scene.canvas;
 
  let phiWindowRatio =
    (movement.endPosition.x - movement.startPosition.x) / canvas.clientWidth;
  phiWindowRatio = Math.min(phiWindowRatio, controller.maximumMovementRatio);
 
  const deltaPhi = rotateRate * phiWindowRatio * Math.PI * 4.0;
 
  camera.twistRight(deltaPhi);
}
 
function update2D(controller) {
  const rotatable2D = controller._scene.mapMode2D === MapMode2D.ROTATE;
  if (!Matrix4.equals(Matrix4.IDENTITY, controller._scene.camera.transform)) {
    reactToInput(
      controller,
      controller.enableZoom,
      controller.zoomEventTypes,
      zoom2D,
      controller.inertiaZoom,
      "_lastInertiaZoomMovement",
    );
    Iif (rotatable2D) {
      reactToInput(
        controller,
        controller.enableRotate,
        controller.translateEventTypes,
        twist2D,
        controller.inertiaSpin,
        "_lastInertiaSpinMovement",
      );
    }
  } else {
    reactToInput(
      controller,
      controller.enableTranslate,
      controller.translateEventTypes,
      translate2D,
      controller.inertiaTranslate,
      "_lastInertiaTranslateMovement",
    );
    reactToInput(
      controller,
      controller.enableZoom,
      controller.zoomEventTypes,
      zoom2D,
      controller.inertiaZoom,
      "_lastInertiaZoomMovement",
    );
    if (rotatable2D) {
      reactToInput(
        controller,
        controller.enableRotate,
        controller.tiltEventTypes,
        twist2D,
        controller.inertiaSpin,
        "_lastInertiaTiltMovement",
      );
    }
  }
}
 
const pickGlobeScratchRay = new Ray();
const scratchDepthIntersection = new Cartesian3();
const scratchRayIntersection = new Cartesian3();
 
function pickPosition(controller, mousePosition, result) {
  const scene = controller._scene;
  const globe = controller._globe;
  const camera = scene.camera;
 
  let depthIntersection;
  if (scene.pickPositionSupported) {
    depthIntersection = scene.pickPositionWorldCoordinates(
      mousePosition,
      scratchDepthIntersection,
    );
  }
 
  if (!defined(globe)) {
    return Cartesian3.clone(depthIntersection, result);
  }
 
  const cullBackFaces = !controller._cameraUnderground;
  const ray = camera.getPickRay(mousePosition, pickGlobeScratchRay);
  const rayIntersection = globe.pickWorldCoordinates(
    ray,
    scene,
    cullBackFaces,
    scratchRayIntersection,
  );
 
  const pickDistance = defined(depthIntersection)
    ? Cartesian3.distance(depthIntersection, camera.positionWC)
    : Number.POSITIVE_INFINITY;
  const rayDistance = defined(rayIntersection)
    ? Cartesian3.distance(rayIntersection, camera.positionWC)
    : Number.POSITIVE_INFINITY;
 
  if (pickDistance < rayDistance) {
    return Cartesian3.clone(depthIntersection, result);
  }
 
  return Cartesian3.clone(rayIntersection, result);
}
 
const scratchDistanceCartographic = new Cartographic();
 
function getDistanceFromSurface(controller) {
  const ellipsoid = controller._ellipsoid;
  const scene = controller._scene;
  const camera = scene.camera;
  const mode = scene.mode;
 
  let height = 0.0;
  if (mode === SceneMode.SCENE3D) {
    const cartographic = ellipsoid.cartesianToCartographic(
      camera.position,
      scratchDistanceCartographic,
    );
    Eif (defined(cartographic)) {
      height = cartographic.height;
    }
  } else {
    height = camera.position.z;
  }
  const globeHeight = controller._scene.globeHeight ?? 0.0;
  const distanceFromSurface = Math.abs(globeHeight - height);
  return distanceFromSurface;
}
 
const scratchSurfaceNormal = new Cartesian3();
 
function getZoomDistanceUnderground(controller, ray) {
  const origin = ray.origin;
  const direction = ray.direction;
  const distanceFromSurface = getDistanceFromSurface(controller);
 
  // Weight zoom distance based on how strongly the pick ray is pointing inward.
  // Geocentric normal is accurate enough for these purposes
  const surfaceNormal = Cartesian3.normalize(origin, scratchSurfaceNormal);
  let strength = Math.abs(Cartesian3.dot(surfaceNormal, direction));
  strength = Math.max(strength, 0.5) * 2.0;
  return distanceFromSurface * strength;
}
 
function getTiltCenterUnderground(controller, ray, pickedPosition, result) {
  let distance = Cartesian3.distance(ray.origin, pickedPosition);
  const distanceFromSurface = getDistanceFromSurface(controller);
 
  const maximumDistance = CesiumMath.clamp(
    distanceFromSurface * 5.0,
    controller._minimumUndergroundPickDistance,
    controller._maximumUndergroundPickDistance,
  );
 
  if (distance > maximumDistance) {
    // Simulate look-at behavior by tilting around a small invisible sphere
    distance = Math.min(distance, distanceFromSurface / 5.0);
    distance = Math.max(distance, 100.0);
  }
 
  return Ray.getPoint(ray, distance, result);
}
 
function getStrafeStartPositionUnderground(
  controller,
  ray,
  pickedPosition,
  result,
) {
  let distance;
  Iif (!defined(pickedPosition)) {
    distance = getDistanceFromSurface(controller);
  } else {
    distance = Cartesian3.distance(ray.origin, pickedPosition);
    if (distance > controller._maximumUndergroundPickDistance) {
      // If the picked position is too far away set the strafe speed based on the
      // camera's height from the globe surface
      distance = getDistanceFromSurface(controller);
    }
  }
 
  return Ray.getPoint(ray, distance, result);
}
 
const scratchInertialDelta = new Cartesian2();
 
function continueStrafing(controller, movement) {
  // Update the end position continually based on the inertial delta
  const originalEndPosition = movement.endPosition;
  const inertialDelta = Cartesian2.subtract(
    movement.endPosition,
    movement.startPosition,
    scratchInertialDelta,
  );
  const endPosition = controller._strafeEndMousePosition;
  Cartesian2.add(endPosition, inertialDelta, endPosition);
  movement.endPosition = endPosition;
  strafe(controller, movement, controller._strafeStartPosition);
  movement.endPosition = originalEndPosition;
}
 
const translateCVStartRay = new Ray();
const translateCVEndRay = new Ray();
const translateCVStartPos = new Cartesian3();
const translateCVEndPos = new Cartesian3();
const translateCVDifference = new Cartesian3();
const translateCVOrigin = new Cartesian3();
const translateCVPlane = new Plane(Cartesian3.UNIT_X, 0.0);
const translateCVStartMouse = new Cartesian2();
const translateCVEndMouse = new Cartesian2();
 
function translateCV(controller, startPosition, movement) {
  Eif (!Cartesian3.equals(startPosition, controller._translateMousePosition)) {
    controller._looking = false;
  }
 
  Eif (!Cartesian3.equals(startPosition, controller._strafeMousePosition)) {
    controller._strafing = false;
  }
 
  Iif (controller._looking) {
    look3D(controller, startPosition, movement);
    return;
  }
 
  Iif (controller._strafing) {
    continueStrafing(controller, movement);
    return;
  }
 
  const scene = controller._scene;
  const camera = scene.camera;
  const cameraUnderground = controller._cameraUnderground;
  const startMouse = Cartesian2.clone(
    movement.startPosition,
    translateCVStartMouse,
  );
  const endMouse = Cartesian2.clone(movement.endPosition, translateCVEndMouse);
  let startRay = camera.getPickRay(startMouse, translateCVStartRay);
 
  const origin = Cartesian3.clone(Cartesian3.ZERO, translateCVOrigin);
  const normal = Cartesian3.UNIT_X;
 
  let globePos;
  if (camera.position.z < controller._minimumPickingTerrainHeight) {
    globePos = pickPosition(controller, startMouse, translateCVStartPos);
 
    Eif (defined(globePos)) {
      origin.x = globePos.x;
    }
  }
 
  if (
    cameraUnderground ||
    (origin.x > camera.position.z && defined(globePos))
  ) {
    let pickPosition = globePos;
    Eif (cameraUnderground) {
      pickPosition = getStrafeStartPositionUnderground(
        controller,
        startRay,
        globePos,
        translateCVStartPos,
      );
    }
    Cartesian2.clone(startPosition, controller._strafeMousePosition);
    Cartesian2.clone(startPosition, controller._strafeEndMousePosition);
    Cartesian3.clone(pickPosition, controller._strafeStartPosition);
    controller._strafing = true;
    strafe(controller, movement, controller._strafeStartPosition);
    return;
  }
 
  const plane = Plane.fromPointNormal(origin, normal, translateCVPlane);
 
  startRay = camera.getPickRay(startMouse, translateCVStartRay);
  const startPlanePos = IntersectionTests.rayPlane(
    startRay,
    plane,
    translateCVStartPos,
  );
 
  const endRay = camera.getPickRay(endMouse, translateCVEndRay);
  const endPlanePos = IntersectionTests.rayPlane(
    endRay,
    plane,
    translateCVEndPos,
  );
 
  Iif (!defined(startPlanePos) || !defined(endPlanePos)) {
    controller._looking = true;
    look3D(controller, startPosition, movement);
    Cartesian2.clone(startPosition, controller._translateMousePosition);
    return;
  }
 
  const diff = Cartesian3.subtract(
    startPlanePos,
    endPlanePos,
    translateCVDifference,
  );
  const temp = diff.x;
  diff.x = diff.y;
  diff.y = diff.z;
  diff.z = temp;
  const mag = Cartesian3.magnitude(diff);
  Eif (mag > CesiumMath.EPSILON6) {
    Cartesian3.normalize(diff, diff);
    camera.move(diff, mag);
  }
}
 
const rotateCVWindowPos = new Cartesian2();
const rotateCVWindowRay = new Ray();
const rotateCVCenter = new Cartesian3();
const rotateCVVerticalCenter = new Cartesian3();
const rotateCVTransform = new Matrix4();
const rotateCVVerticalTransform = new Matrix4();
const rotateCVOrigin = new Cartesian3();
const rotateCVPlane = new Plane(Cartesian3.UNIT_X, 0.0);
const rotateCVCartesian3 = new Cartesian3();
const rotateCVCart = new Cartographic();
const rotateCVOldTransform = new Matrix4();
const rotateCVQuaternion = new Quaternion();
const rotateCVMatrix = new Matrix3();
const tilt3DCartesian3 = new Cartesian3();
 
function rotateCV(controller, startPosition, movement) {
  Iif (defined(movement.angleAndHeight)) {
    movement = movement.angleAndHeight;
  }
 
  Eif (!Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
    controller._tiltCVOffMap = false;
    controller._looking = false;
  }
 
  Iif (controller._looking) {
    look3D(controller, startPosition, movement);
    return;
  }
 
  const scene = controller._scene;
  const camera = scene.camera;
 
  if (
    controller._tiltCVOffMap ||
    !controller.onMap() ||
    Math.abs(camera.position.z) > controller._minimumPickingTerrainHeight
  ) {
    controller._tiltCVOffMap = true;
    rotateCVOnPlane(controller, startPosition, movement);
  } else {
    rotateCVOnTerrain(controller, startPosition, movement);
  }
}
 
function rotateCVOnPlane(controller, startPosition, movement) {
  const scene = controller._scene;
  const camera = scene.camera;
  const canvas = scene.canvas;
 
  const windowPosition = rotateCVWindowPos;
  windowPosition.x = canvas.clientWidth / 2;
  windowPosition.y = canvas.clientHeight / 2;
  const ray = camera.getPickRay(windowPosition, rotateCVWindowRay);
  const normal = Cartesian3.UNIT_X;
 
  const position = ray.origin;
  const direction = ray.direction;
  let scalar;
  const normalDotDirection = Cartesian3.dot(normal, direction);
  Eif (Math.abs(normalDotDirection) > CesiumMath.EPSILON6) {
    scalar = -Cartesian3.dot(normal, position) / normalDotDirection;
  }
 
  Iif (!defined(scalar) || scalar <= 0.0) {
    controller._looking = true;
    look3D(controller, startPosition, movement);
    Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
    return;
  }
 
  const center = Cartesian3.multiplyByScalar(direction, scalar, rotateCVCenter);
  Cartesian3.add(position, center, center);
 
  const projection = scene.mapProjection;
  const ellipsoid = projection.ellipsoid;
 
  Cartesian3.fromElements(center.y, center.z, center.x, center);
  const cart = projection.unproject(center, rotateCVCart);
  ellipsoid.cartographicToCartesian(cart, center);
 
  const transform = Transforms.eastNorthUpToFixedFrame(
    center,
    ellipsoid,
    rotateCVTransform,
  );
 
  const oldGlobe = controller._globe;
  const oldEllipsoid = controller._ellipsoid;
  controller._globe = undefined;
  controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  controller._rotateFactor = 1.0;
  controller._rotateRateRangeAdjustment = 1.0;
 
  const oldTransform = Matrix4.clone(camera.transform, rotateCVOldTransform);
  camera._setTransform(transform);
 
  rotate3D(controller, startPosition, movement, Cartesian3.UNIT_Z);
 
  camera._setTransform(oldTransform);
  controller._globe = oldGlobe;
  controller._ellipsoid = oldEllipsoid;
 
  const radius = oldEllipsoid.maximumRadius;
  controller._rotateFactor = 1.0 / radius;
  controller._rotateRateRangeAdjustment = radius;
}
 
function rotateCVOnTerrain(controller, startPosition, movement) {
  const scene = controller._scene;
  const camera = scene.camera;
  const cameraUnderground = controller._cameraUnderground;
 
  let center;
  let ray;
  const normal = Cartesian3.UNIT_X;
 
  Iif (Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
    center = Cartesian3.clone(controller._tiltCenter, rotateCVCenter);
  } else {
    Eif (camera.position.z < controller._minimumPickingTerrainHeight) {
      center = pickPosition(controller, startPosition, rotateCVCenter);
    }
 
    Iif (!defined(center)) {
      ray = camera.getPickRay(startPosition, rotateCVWindowRay);
      const position = ray.origin;
      const direction = ray.direction;
 
      let scalar;
      const normalDotDirection = Cartesian3.dot(normal, direction);
      if (Math.abs(normalDotDirection) > CesiumMath.EPSILON6) {
        scalar = -Cartesian3.dot(normal, position) / normalDotDirection;
      }
 
      if (!defined(scalar) || scalar <= 0.0) {
        controller._looking = true;
        look3D(controller, startPosition, movement);
        Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
        return;
      }
 
      center = Cartesian3.multiplyByScalar(direction, scalar, rotateCVCenter);
      Cartesian3.add(position, center, center);
    }
 
    Eif (cameraUnderground) {
      Eif (!defined(ray)) {
        ray = camera.getPickRay(startPosition, rotateCVWindowRay);
      }
      getTiltCenterUnderground(controller, ray, center, center);
    }
 
    Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
    Cartesian3.clone(center, controller._tiltCenter);
  }
 
  const canvas = scene.canvas;
 
  const windowPosition = rotateCVWindowPos;
  windowPosition.x = canvas.clientWidth / 2;
  windowPosition.y = controller._tiltCenterMousePosition.y;
  ray = camera.getPickRay(windowPosition, rotateCVWindowRay);
 
  const origin = Cartesian3.clone(Cartesian3.ZERO, rotateCVOrigin);
  origin.x = center.x;
 
  const plane = Plane.fromPointNormal(origin, normal, rotateCVPlane);
  const verticalCenter = IntersectionTests.rayPlane(
    ray,
    plane,
    rotateCVVerticalCenter,
  );
 
  const projection = camera._projection;
  const ellipsoid = projection.ellipsoid;
 
  Cartesian3.fromElements(center.y, center.z, center.x, center);
  let cart = projection.unproject(center, rotateCVCart);
  ellipsoid.cartographicToCartesian(cart, center);
 
  const transform = Transforms.eastNorthUpToFixedFrame(
    center,
    ellipsoid,
    rotateCVTransform,
  );
 
  let verticalTransform;
  if (defined(verticalCenter)) {
    Cartesian3.fromElements(
      verticalCenter.y,
      verticalCenter.z,
      verticalCenter.x,
      verticalCenter,
    );
    cart = projection.unproject(verticalCenter, rotateCVCart);
    ellipsoid.cartographicToCartesian(cart, verticalCenter);
 
    verticalTransform = Transforms.eastNorthUpToFixedFrame(
      verticalCenter,
      ellipsoid,
      rotateCVVerticalTransform,
    );
  } else E{
    verticalTransform = transform;
  }
 
  const oldGlobe = controller._globe;
  const oldEllipsoid = controller._ellipsoid;
  controller._globe = undefined;
  controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  controller._rotateFactor = 1.0;
  controller._rotateRateRangeAdjustment = 1.0;
 
  let constrainedAxis = Cartesian3.UNIT_Z;
 
  const oldTransform = Matrix4.clone(camera.transform, rotateCVOldTransform);
  camera._setTransform(transform);
 
  const tangent = Cartesian3.cross(
    Cartesian3.UNIT_Z,
    Cartesian3.normalize(camera.position, rotateCVCartesian3),
    rotateCVCartesian3,
  );
  const dot = Cartesian3.dot(camera.right, tangent);
 
  rotate3D(controller, startPosition, movement, constrainedAxis, false, true);
 
  camera._setTransform(verticalTransform);
  if (dot < 0.0) {
    const movementDelta = movement.startPosition.y - movement.endPosition.y;
    Iif (
      (cameraUnderground && movementDelta < 0.0) ||
      (!cameraUnderground && movementDelta > 0.0)
    ) {
      // Prevent camera from flipping past the up axis
      constrainedAxis = undefined;
    }
 
    const oldConstrainedAxis = camera.constrainedAxis;
    camera.constrainedAxis = undefined;
 
    rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
 
    camera.constrainedAxis = oldConstrainedAxis;
  } else E{
    rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  }
 
  Iif (defined(camera.constrainedAxis)) {
    const right = Cartesian3.cross(
      camera.direction,
      camera.constrainedAxis,
      tilt3DCartesian3,
    );
    if (
      !Cartesian3.equalsEpsilon(right, Cartesian3.ZERO, CesiumMath.EPSILON6)
    ) {
      if (Cartesian3.dot(right, camera.right) < 0.0) {
        Cartesian3.negate(right, right);
      }
 
      Cartesian3.cross(right, camera.direction, camera.up);
      Cartesian3.cross(camera.direction, camera.up, camera.right);
 
      Cartesian3.normalize(camera.up, camera.up);
      Cartesian3.normalize(camera.right, camera.right);
    }
  }
 
  camera._setTransform(oldTransform);
  controller._globe = oldGlobe;
  controller._ellipsoid = oldEllipsoid;
 
  const radius = oldEllipsoid.maximumRadius;
  controller._rotateFactor = 1.0 / radius;
  controller._rotateRateRangeAdjustment = radius;
 
  const originalPosition = Cartesian3.clone(
    camera.positionWC,
    rotateCVCartesian3,
  );
 
  Iif (controller.enableCollisionDetection) {
    adjustHeightForTerrain(controller, true);
  }
 
  Iif (!Cartesian3.equals(camera.positionWC, originalPosition)) {
    camera._setTransform(verticalTransform);
    camera.worldToCameraCoordinatesPoint(originalPosition, originalPosition);
 
    const magSqrd = Cartesian3.magnitudeSquared(originalPosition);
    if (Cartesian3.magnitudeSquared(camera.position) > magSqrd) {
      Cartesian3.normalize(camera.position, camera.position);
      Cartesian3.multiplyByScalar(
        camera.position,
        Math.sqrt(magSqrd),
        camera.position,
      );
    }
 
    const angle = Cartesian3.angleBetween(originalPosition, camera.position);
    const axis = Cartesian3.cross(
      originalPosition,
      camera.position,
      originalPosition,
    );
    Cartesian3.normalize(axis, axis);
 
    const quaternion = Quaternion.fromAxisAngle(
      axis,
      angle,
      rotateCVQuaternion,
    );
    const rotation = Matrix3.fromQuaternion(quaternion, rotateCVMatrix);
    Matrix3.multiplyByVector(rotation, camera.direction, camera.direction);
    Matrix3.multiplyByVector(rotation, camera.up, camera.up);
    Cartesian3.cross(camera.direction, camera.up, camera.right);
    Cartesian3.cross(camera.right, camera.direction, camera.up);
 
    camera._setTransform(oldTransform);
  }
}
 
const zoomCVWindowPos = new Cartesian2();
const zoomCVWindowRay = new Ray();
const zoomCVIntersection = new Cartesian3();
 
function zoomCV(controller, startPosition, movement) {
  Iif (defined(movement.distance)) {
    movement = movement.distance;
  }
 
  const scene = controller._scene;
  const camera = scene.camera;
  const canvas = scene.canvas;
 
  const cameraUnderground = controller._cameraUnderground;
 
  let windowPosition;
 
  if (cameraUnderground) {
    windowPosition = startPosition;
  } else {
    windowPosition = zoomCVWindowPos;
    windowPosition.x = canvas.clientWidth / 2;
    windowPosition.y = canvas.clientHeight / 2;
  }
 
  const ray = camera.getPickRay(windowPosition, zoomCVWindowRay);
  const position = ray.origin;
  const direction = ray.direction;
  const height = camera.position.z;
 
  let intersection;
  if (height < controller._minimumPickingTerrainHeight) {
    intersection = pickPosition(controller, windowPosition, zoomCVIntersection);
  }
 
  let distance;
  if (defined(intersection)) {
    distance = Cartesian3.distance(position, intersection);
  }
 
  if (cameraUnderground) {
    const distanceUnderground = getZoomDistanceUnderground(
      controller,
      ray,
      height,
    );
    if (defined(distance)) {
      distance = Math.min(distance, distanceUnderground);
    } else E{
      distance = distanceUnderground;
    }
  }
 
  if (!defined(distance)) {
    const normal = Cartesian3.UNIT_X;
    distance =
      -Cartesian3.dot(normal, position) / Cartesian3.dot(normal, direction);
  }
 
  handleZoom(
    controller,
    startPosition,
    movement,
    controller.zoomFactor,
    distance,
  );
}
 
function updateCV(controller) {
  const scene = controller._scene;
  const camera = scene.camera;
 
  if (!Matrix4.equals(Matrix4.IDENTITY, camera.transform)) {
    reactToInput(
      controller,
      controller.enableRotate,
      controller.rotateEventTypes,
      rotate3D,
      controller.inertiaSpin,
      "_lastInertiaSpinMovement",
    );
    reactToInput(
      controller,
      controller.enableZoom,
      controller.zoomEventTypes,
      zoom3D,
      controller.inertiaZoom,
      "_lastInertiaZoomMovement",
    );
  } else {
    const tweens = controller._tweens;
 
    if (controller._aggregator.anyButtonDown) {
      tweens.removeAll();
    }
 
    reactToInput(
      controller,
      controller.enableTilt,
      controller.tiltEventTypes,
      rotateCV,
      controller.inertiaSpin,
      "_lastInertiaTiltMovement",
    );
    reactToInput(
      controller,
      controller.enableTranslate,
      controller.translateEventTypes,
      translateCV,
      controller.inertiaTranslate,
      "_lastInertiaTranslateMovement",
    );
    reactToInput(
      controller,
      controller.enableZoom,
      controller.zoomEventTypes,
      zoomCV,
      controller.inertiaZoom,
      "_lastInertiaZoomMovement",
    );
    reactToInput(
      controller,
      controller.enableLook,
      controller.lookEventTypes,
      look3D,
    );
 
    if (
      !controller._aggregator.anyButtonDown &&
      !tweens.contains(controller._tween)
    ) {
      const tween = camera.createCorrectPositionTween(
        controller.bounceAnimationTime,
      );
      if (defined(tween)) {
        controller._tween = tweens.add(tween);
      }
    }
 
    tweens.update();
  }
}
 
const scratchStrafeRay = new Ray();
const scratchStrafePlane = new Plane(Cartesian3.UNIT_X, 0.0);
const scratchStrafeIntersection = new Cartesian3();
const scratchStrafeDirection = new Cartesian3();
 
function strafe(controller, movement, strafeStartPosition) {
  const scene = controller._scene;
  const camera = scene.camera;
 
  const ray = camera.getPickRay(movement.endPosition, scratchStrafeRay);
 
  let direction = Cartesian3.clone(camera.direction, scratchStrafeDirection);
  if (scene.mode === SceneMode.COLUMBUS_VIEW) {
    Cartesian3.fromElements(direction.z, direction.x, direction.y, direction);
  }
 
  const plane = Plane.fromPointNormal(
    strafeStartPosition,
    direction,
    scratchStrafePlane,
  );
  const intersection = IntersectionTests.rayPlane(
    ray,
    plane,
    scratchStrafeIntersection,
  );
  Iif (!defined(intersection)) {
    return;
  }
 
  direction = Cartesian3.subtract(strafeStartPosition, intersection, direction);
  if (scene.mode === SceneMode.COLUMBUS_VIEW) {
    Cartesian3.fromElements(direction.y, direction.z, direction.x, direction);
  }
 
  Cartesian3.add(camera.position, direction, camera.position);
}
 
const spin3DPick = new Cartesian3();
const scratchCartographic = new Cartographic();
const scratchRadii = new Cartesian3();
const scratchEllipsoid = new Ellipsoid();
const scratchLookUp = new Cartesian3();
const scratchNormal = new Cartesian3();
const scratchMousePosition = new Cartesian3();
 
function spin3D(controller, startPosition, movement) {
  const scene = controller._scene;
  const camera = scene.camera;
  const cameraUnderground = controller._cameraUnderground;
  let ellipsoid = controller._ellipsoid;
 
  Iif (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
    rotate3D(controller, startPosition, movement);
    return;
  }
 
  let magnitude;
  let radii;
 
  const up = ellipsoid.geodeticSurfaceNormal(camera.position, scratchLookUp);
 
  Iif (Cartesian2.equals(startPosition, controller._rotateMousePosition)) {
    if (controller._looking) {
      look3D(controller, startPosition, movement, up);
    } else if (controller._rotating) {
      rotate3D(controller, startPosition, movement);
    } else if (controller._strafing) {
      continueStrafing(controller, movement);
    } else {
      if (
        Cartesian3.magnitude(camera.position) <
        Cartesian3.magnitude(controller._rotateStartPosition)
      ) {
        // Pan action is no longer valid if camera moves below the pan ellipsoid
        return;
      }
      magnitude = Cartesian3.magnitude(controller._rotateStartPosition);
      radii = scratchRadii;
      radii.x = radii.y = radii.z = magnitude;
      ellipsoid = Ellipsoid.fromCartesian3(radii, scratchEllipsoid);
      pan3D(controller, startPosition, movement, ellipsoid);
    }
    return;
  }
  controller._looking = false;
  controller._rotating = false;
  controller._strafing = false;
 
  const height = ellipsoid.cartesianToCartographic(
    camera.positionWC,
    scratchCartographic,
  ).height;
  const globe = controller._globe;
 
  if (defined(globe) && height < controller._minimumPickingTerrainHeight) {
    const mousePos = pickPosition(
      controller,
      movement.startPosition,
      scratchMousePosition,
    );
    if (defined(mousePos)) {
      let strafing = false;
      const ray = camera.getPickRay(
        movement.startPosition,
        pickGlobeScratchRay,
      );
 
      if (cameraUnderground) {
        strafing = true;
        getStrafeStartPositionUnderground(controller, ray, mousePos, mousePos);
      } else E{
        const normal = ellipsoid.geodeticSurfaceNormal(mousePos, scratchNormal);
        const tangentPick =
          Math.abs(Cartesian3.dot(ray.direction, normal)) < 0.05;
 
        if (tangentPick) {
          strafing = true;
        } else {
          strafing =
            Cartesian3.magnitude(camera.position) <
            Cartesian3.magnitude(mousePos);
        }
      }
 
      if (strafing) {
        Cartesian2.clone(startPosition, controller._strafeEndMousePosition);
        Cartesian3.clone(mousePos, controller._strafeStartPosition);
        controller._strafing = true;
        strafe(controller, movement, controller._strafeStartPosition);
      } else E{
        magnitude = Cartesian3.magnitude(mousePos);
        radii = scratchRadii;
        radii.x = radii.y = radii.z = magnitude;
        ellipsoid = Ellipsoid.fromCartesian3(radii, scratchEllipsoid);
        pan3D(controller, startPosition, movement, ellipsoid);
 
        Cartesian3.clone(mousePos, controller._rotateStartPosition);
      }
    } else E{
      controller._looking = true;
      look3D(controller, startPosition, movement, up);
    }
  } else if (
    defined(
      camera.pickEllipsoid(
        movement.startPosition,
        controller._ellipsoid,
        spin3DPick,
      ),
    )
  ) {
    pan3D(controller, startPosition, movement, controller._ellipsoid);
    Cartesian3.clone(spin3DPick, controller._rotateStartPosition);
  } else if (height > controller._minimumTrackBallHeight) {
    controller._rotating = true;
    rotate3D(controller, startPosition, movement);
  } else E{
    controller._looking = true;
    look3D(controller, startPosition, movement, up);
  }
 
  Cartesian2.clone(startPosition, controller._rotateMousePosition);
}
 
function rotate3D(
  controller,
  startPosition,
  movement,
  constrainedAxis,
  rotateOnlyVertical,
  rotateOnlyHorizontal,
) {
  rotateOnlyVertical = rotateOnlyVertical ?? false;
  rotateOnlyHorizontal = rotateOnlyHorizontal ?? false;
 
  const scene = controller._scene;
  const camera = scene.camera;
  const canvas = scene.canvas;
 
  const oldAxis = camera.constrainedAxis;
  if (defined(constrainedAxis)) {
    camera.constrainedAxis = constrainedAxis;
  }
 
  const rho = Cartesian3.magnitude(camera.position);
  let rotateRate =
    controller._rotateFactor * (rho - controller._rotateRateRangeAdjustment);
 
  if (rotateRate > controller._maximumRotateRate) {
    rotateRate = controller._maximumRotateRate;
  }
 
  if (rotateRate < controller._minimumRotateRate) {
    rotateRate = controller._minimumRotateRate;
  }
 
  let phiWindowRatio =
    (movement.startPosition.x - movement.endPosition.x) / canvas.clientWidth;
  let thetaWindowRatio =
    (movement.startPosition.y - movement.endPosition.y) / canvas.clientHeight;
  phiWindowRatio = Math.min(phiWindowRatio, controller.maximumMovementRatio);
  thetaWindowRatio = Math.min(
    thetaWindowRatio,
    controller.maximumMovementRatio,
  );
 
  const deltaPhi = rotateRate * phiWindowRatio * Math.PI * 2.0;
  let deltaTheta = rotateRate * thetaWindowRatio * Math.PI;
 
  if (defined(constrainedAxis) && defined(controller.maximumTiltAngle)) {
    const maximumTiltAngle = controller.maximumTiltAngle;
    const dotProduct = Cartesian3.dot(camera.direction, constrainedAxis);
    const tilt = Math.PI - Math.acos(dotProduct) + deltaTheta;
    if (tilt > maximumTiltAngle) {
      deltaTheta -= tilt - maximumTiltAngle;
    }
  }
 
  if (!rotateOnlyVertical) {
    camera.rotateRight(deltaPhi);
  }
 
  if (!rotateOnlyHorizontal) {
    camera.rotateUp(deltaTheta);
  }
 
  camera.constrainedAxis = oldAxis;
}
 
const pan3DP0 = Cartesian4.clone(Cartesian4.UNIT_W);
const pan3DP1 = Cartesian4.clone(Cartesian4.UNIT_W);
const pan3DTemp0 = new Cartesian3();
const pan3DTemp1 = new Cartesian3();
const pan3DTemp2 = new Cartesian3();
const pan3DTemp3 = new Cartesian3();
const pan3DStartMousePosition = new Cartesian2();
const pan3DEndMousePosition = new Cartesian2();
const pan3DDiffMousePosition = new Cartesian2();
const pan3DPixelDimensions = new Cartesian2();
const panRay = new Ray();
 
function pan3D(controller, startPosition, movement, ellipsoid) {
  const scene = controller._scene;
  const camera = scene.camera;
 
  const startMousePosition = Cartesian2.clone(
    movement.startPosition,
    pan3DStartMousePosition,
  );
  const endMousePosition = Cartesian2.clone(
    movement.endPosition,
    pan3DEndMousePosition,
  );
  const height = ellipsoid.cartesianToCartographic(
    camera.positionWC,
    scratchCartographic,
  ).height;
 
  let p0, p1;
 
  Iif (
    !movement.inertiaEnabled &&
    height < controller._minimumPickingTerrainHeight
  ) {
    p0 = Cartesian3.clone(controller._panLastWorldPosition, pan3DP0);
 
    // Use the last picked world position unless we're starting a new drag
    if (
      !defined(controller._globe) &&
      !Cartesian2.equalsEpsilon(
        startMousePosition,
        controller._panLastMousePosition,
      )
    ) {
      p0 = pickPosition(controller, startMousePosition, pan3DP0);
    }
 
    if (!defined(controller._globe) && defined(p0)) {
      const toCenter = Cartesian3.subtract(p0, camera.positionWC, pan3DTemp1);
      const toCenterProj = Cartesian3.multiplyByScalar(
        camera.directionWC,
        Cartesian3.dot(camera.directionWC, toCenter),
        pan3DTemp1,
      );
      const distanceToNearPlane = Cartesian3.magnitude(toCenterProj);
      const pixelDimensions = camera.frustum.getPixelDimensions(
        scene.drawingBufferWidth,
        scene.drawingBufferHeight,
        distanceToNearPlane,
        scene.pixelRatio,
        pan3DPixelDimensions,
      );
 
      const dragDelta = Cartesian2.subtract(
        endMousePosition,
        startMousePosition,
        pan3DDiffMousePosition,
      );
 
      // Move the camera to the the distance the cursor moved in worldspace
      const right = Cartesian3.multiplyByScalar(
        camera.rightWC,
        dragDelta.x * pixelDimensions.x,
        pan3DTemp1,
      );
 
      // Move the camera towards the picked position in worldspace as the camera is pointed towards a horizon view
      const cameraPositionNormal = Cartesian3.normalize(
        camera.positionWC,
        scratchCameraPositionNormal,
      );
      const endPickDirection = camera.getPickRay(
        endMousePosition,
        panRay,
      ).direction;
      const endPickProj = Cartesian3.subtract(
        endPickDirection,
        Cartesian3.projectVector(endPickDirection, camera.rightWC, pan3DTemp2),
        pan3DTemp2,
      );
      const angle = Cartesian3.angleBetween(endPickProj, camera.directionWC);
      let forward = 1.0;
      if (defined(camera.frustum.fov)) {
        forward = Math.max(Math.tan(angle), 0.1); // Clamp so we don't make the magnitude infinitely large when the angle is small
      }
      let dot = Math.abs(
        Cartesian3.dot(camera.directionWC, cameraPositionNormal),
      );
      const magnitude =
        ((-dragDelta.y * pixelDimensions.y * 2.0) / Math.sqrt(forward)) *
        (1.0 - dot);
      const direction = Cartesian3.multiplyByScalar(
        endPickDirection,
        magnitude,
        pan3DTemp2,
      );
 
      // Move the camera up the distance the cursor moved in worldspace as the camera is pointed towards the center
      dot = Math.abs(Cartesian3.dot(camera.upWC, cameraPositionNormal));
      const up = Cartesian3.multiplyByScalar(
        camera.upWC,
        -dragDelta.y * (1.0 - dot) * pixelDimensions.y,
        pan3DTemp3,
      );
 
      p1 = Cartesian3.add(p0, right, pan3DP1);
      p1 = Cartesian3.add(p1, direction, p1);
      p1 = Cartesian3.add(p1, up, p1);
 
      Cartesian3.clone(p1, controller._panLastWorldPosition);
      Cartesian2.clone(endMousePosition, controller._panLastMousePosition);
    }
  }
 
  Eif (!defined(p0) || !defined(p1)) {
    p0 = camera.pickEllipsoid(startMousePosition, ellipsoid, pan3DP0);
    p1 = camera.pickEllipsoid(endMousePosition, ellipsoid, pan3DP1);
  }
 
  Iif (!defined(p0) || !defined(p1)) {
    controller._rotating = true;
    rotate3D(controller, startPosition, movement);
    return;
  }
 
  p0 = camera.worldToCameraCoordinates(p0, p0);
  p1 = camera.worldToCameraCoordinates(p1, p1);
 
  if (!defined(camera.constrainedAxis)) {
    Cartesian3.normalize(p0, p0);
    Cartesian3.normalize(p1, p1);
    const dot = Cartesian3.dot(p0, p1);
    const axis = Cartesian3.cross(p0, p1, pan3DTemp0);
 
    Eif (
      dot < 1.0 &&
      !Cartesian3.equalsEpsilon(axis, Cartesian3.ZERO, CesiumMath.EPSILON14)
    ) {
      // dot is in [0, 1]
      const angle = Math.acos(dot);
      camera.rotate(axis, angle);
    }
  } else {
    const basis0 = camera.constrainedAxis;
    const basis1 = Cartesian3.mostOrthogonalAxis(basis0, pan3DTemp0);
    Cartesian3.cross(basis1, basis0, basis1);
    Cartesian3.normalize(basis1, basis1);
    const basis2 = Cartesian3.cross(basis0, basis1, pan3DTemp1);
 
    const startRho = Cartesian3.magnitude(p0);
    const startDot = Cartesian3.dot(basis0, p0);
    const startTheta = Math.acos(startDot / startRho);
    const startRej = Cartesian3.multiplyByScalar(basis0, startDot, pan3DTemp2);
    Cartesian3.subtract(p0, startRej, startRej);
    Cartesian3.normalize(startRej, startRej);
 
    const endRho = Cartesian3.magnitude(p1);
    const endDot = Cartesian3.dot(basis0, p1);
    const endTheta = Math.acos(endDot / endRho);
    const endRej = Cartesian3.multiplyByScalar(basis0, endDot, pan3DTemp3);
    Cartesian3.subtract(p1, endRej, endRej);
    Cartesian3.normalize(endRej, endRej);
 
    let startPhi = Math.acos(Cartesian3.dot(startRej, basis1));
    Iif (Cartesian3.dot(startRej, basis2) < 0) {
      startPhi = CesiumMath.TWO_PI - startPhi;
    }
 
    let endPhi = Math.acos(Cartesian3.dot(endRej, basis1));
    if (Cartesian3.dot(endRej, basis2) < 0) {
      endPhi = CesiumMath.TWO_PI - endPhi;
    }
 
    const deltaPhi = startPhi - endPhi;
 
    let east;
    Iif (
      Cartesian3.equalsEpsilon(basis0, camera.position, CesiumMath.EPSILON2)
    ) {
      east = camera.right;
    } else {
      east = Cartesian3.cross(basis0, camera.position, pan3DTemp0);
    }
 
    const planeNormal = Cartesian3.cross(basis0, east, pan3DTemp0);
    const side0 = Cartesian3.dot(
      planeNormal,
      Cartesian3.subtract(p0, basis0, pan3DTemp1),
    );
    const side1 = Cartesian3.dot(
      planeNormal,
      Cartesian3.subtract(p1, basis0, pan3DTemp1),
    );
 
    let deltaTheta;
    Iif (side0 > 0 && side1 > 0) {
      deltaTheta = endTheta - startTheta;
    } else Iif (side0 > 0 && side1 <= 0) {
      if (Cartesian3.dot(camera.position, basis0) > 0) {
        deltaTheta = -startTheta - endTheta;
      } else {
        deltaTheta = startTheta + endTheta;
      }
    } else {
      deltaTheta = startTheta - endTheta;
    }
 
    camera.rotateRight(deltaPhi);
    camera.rotateUp(deltaTheta);
  }
}
 
const zoom3DUnitPosition = new Cartesian3();
const zoom3DCartographic = new Cartographic();
 
let preIntersectionDistance = 0;
 
function zoom3D(controller, startPosition, movement) {
  Iif (defined(movement.distance)) {
    movement = movement.distance;
  }
  const inertiaMovement = movement.inertiaEnabled;
 
  const ellipsoid = controller._ellipsoid;
  const scene = controller._scene;
  const camera = scene.camera;
  const canvas = scene.canvas;
 
  const cameraUnderground = controller._cameraUnderground;
 
  let windowPosition;
 
  if (cameraUnderground) {
    windowPosition = startPosition;
  } else {
    windowPosition = zoomCVWindowPos;
    windowPosition.x = canvas.clientWidth / 2;
    windowPosition.y = canvas.clientHeight / 2;
  }
 
  const ray = camera.getPickRay(windowPosition, zoomCVWindowRay);
 
  let intersection;
  const height = ellipsoid.cartesianToCartographic(
    camera.position,
    zoom3DCartographic,
  ).height;
 
  const approachingCollision =
    Math.abs(preIntersectionDistance) <
    controller.minimumPickingTerrainDistanceWithInertia;
 
  const needPickGlobe = inertiaMovement
    ? approachingCollision
    : height < controller._minimumPickingTerrainHeight;
  if (needPickGlobe) {
    intersection = pickPosition(controller, windowPosition, zoomCVIntersection);
  }
 
  let distance;
  if (defined(intersection)) {
    distance = Cartesian3.distance(ray.origin, intersection);
    preIntersectionDistance = distance;
  }
 
  if (cameraUnderground) {
    const distanceUnderground = getZoomDistanceUnderground(
      controller,
      ray,
      height,
    );
    if (defined(distance)) {
      distance = Math.min(distance, distanceUnderground);
    } else E{
      distance = distanceUnderground;
    }
  }
 
  if (!defined(distance)) {
    distance = height;
  }
 
  const unitPosition = Cartesian3.normalize(
    camera.position,
    zoom3DUnitPosition,
  );
  handleZoom(
    controller,
    startPosition,
    movement,
    controller.zoomFactor,
    distance,
    Cartesian3.dot(unitPosition, camera.direction),
  );
}
 
const tilt3DWindowPos = new Cartesian2();
const tilt3DRay = new Ray();
const tilt3DCenter = new Cartesian3();
const tilt3DVerticalCenter = new Cartesian3();
const tilt3DTransform = new Matrix4();
const tilt3DVerticalTransform = new Matrix4();
const tilt3DOldTransform = new Matrix4();
const tilt3DQuaternion = new Quaternion();
const tilt3DMatrix = new Matrix3();
const tilt3DCart = new Cartographic();
const tilt3DLookUp = new Cartesian3();
 
function tilt3D(controller, startPosition, movement) {
  const scene = controller._scene;
  const camera = scene.camera;
 
  Iif (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
    return;
  }
 
  Iif (defined(movement.angleAndHeight)) {
    movement = movement.angleAndHeight;
  }
 
  Eif (!Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
    controller._tiltOnEllipsoid = false;
    controller._looking = false;
  }
 
  Iif (controller._looking) {
    const up = controller._ellipsoid.geodeticSurfaceNormal(
      camera.position,
      tilt3DLookUp,
    );
    look3D(controller, startPosition, movement, up);
    return;
  }
 
  const ellipsoid = controller._ellipsoid;
  const cartographic = ellipsoid.cartesianToCartographic(
    camera.position,
    tilt3DCart,
  );
 
  if (
    controller._tiltOnEllipsoid ||
    cartographic.height > controller._minimumCollisionTerrainHeight
  ) {
    controller._tiltOnEllipsoid = true;
    tilt3DOnEllipsoid(controller, startPosition, movement);
  } else {
    tilt3DOnTerrain(controller, startPosition, movement);
  }
}
 
const tilt3DOnEllipsoidCartographic = new Cartographic();
 
function tilt3DOnEllipsoid(controller, startPosition, movement) {
  const ellipsoid = controller._ellipsoid;
  const scene = controller._scene;
  const camera = scene.camera;
  const minHeight = controller.minimumZoomDistance * 0.25;
  const height = ellipsoid.cartesianToCartographic(
    camera.positionWC,
    tilt3DOnEllipsoidCartographic,
  ).height;
  Iif (
    height - minHeight - 1.0 < CesiumMath.EPSILON3 &&
    movement.endPosition.y - movement.startPosition.y < 0
  ) {
    return;
  }
 
  const canvas = scene.canvas;
 
  const windowPosition = tilt3DWindowPos;
  windowPosition.x = canvas.clientWidth / 2;
  windowPosition.y = canvas.clientHeight / 2;
  const ray = camera.getPickRay(windowPosition, tilt3DRay);
 
  let center;
  const intersection = IntersectionTests.rayEllipsoid(ray, ellipsoid);
  if (defined(intersection)) {
    center = Ray.getPoint(ray, intersection.start, tilt3DCenter);
  } else Eif (height > controller._minimumTrackBallHeight) {
    const grazingAltitudeLocation = IntersectionTests.grazingAltitudeLocation(
      ray,
      ellipsoid,
    );
    if (!defined(grazingAltitudeLocation)) {
      return;
    }
    const grazingAltitudeCart = ellipsoid.cartesianToCartographic(
      grazingAltitudeLocation,
      tilt3DCart,
    );
    grazingAltitudeCart.height = 0.0;
    center = ellipsoid.cartographicToCartesian(
      grazingAltitudeCart,
      tilt3DCenter,
    );
  } else {
    controller._looking = true;
    const up = controller._ellipsoid.geodeticSurfaceNormal(
      camera.position,
      tilt3DLookUp,
    );
    look3D(controller, startPosition, movement, up);
    Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
    return;
  }
 
  const transform = Transforms.eastNorthUpToFixedFrame(
    center,
    ellipsoid,
    tilt3DTransform,
  );
 
  const oldGlobe = controller._globe;
  const oldEllipsoid = controller._ellipsoid;
  controller._globe = undefined;
  controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  controller._rotateFactor = 1.0;
  controller._rotateRateRangeAdjustment = 1.0;
 
  const oldTransform = Matrix4.clone(camera.transform, tilt3DOldTransform);
  camera._setTransform(transform);
 
  rotate3D(controller, startPosition, movement, Cartesian3.UNIT_Z);
 
  camera._setTransform(oldTransform);
  controller._globe = oldGlobe;
  controller._ellipsoid = oldEllipsoid;
 
  const radius = oldEllipsoid.maximumRadius;
  controller._rotateFactor = 1.0 / radius;
  controller._rotateRateRangeAdjustment = radius;
}
 
function tilt3DOnTerrain(controller, startPosition, movement) {
  const ellipsoid = controller._ellipsoid;
  const scene = controller._scene;
  const camera = scene.camera;
  const cameraUnderground = controller._cameraUnderground;
 
  let center;
  let ray;
  let intersection;
 
  Iif (Cartesian2.equals(startPosition, controller._tiltCenterMousePosition)) {
    center = Cartesian3.clone(controller._tiltCenter, tilt3DCenter);
  } else {
    center = pickPosition(controller, startPosition, tilt3DCenter);
 
    if (!defined(center)) {
      ray = camera.getPickRay(startPosition, tilt3DRay);
      intersection = IntersectionTests.rayEllipsoid(ray, ellipsoid);
      Iif (!defined(intersection)) {
        const cartographic = ellipsoid.cartesianToCartographic(
          camera.position,
          tilt3DCart,
        );
        if (cartographic.height <= controller._minimumTrackBallHeight) {
          controller._looking = true;
          const up = controller._ellipsoid.geodeticSurfaceNormal(
            camera.position,
            tilt3DLookUp,
          );
          look3D(controller, startPosition, movement, up);
          Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
        }
        return;
      }
      center = Ray.getPoint(ray, intersection.start, tilt3DCenter);
    }
 
    if (cameraUnderground) {
      Eif (!defined(ray)) {
        ray = camera.getPickRay(startPosition, tilt3DRay);
      }
      getTiltCenterUnderground(controller, ray, center, center);
    }
 
    Cartesian2.clone(startPosition, controller._tiltCenterMousePosition);
    Cartesian3.clone(center, controller._tiltCenter);
  }
 
  const canvas = scene.canvas;
 
  const windowPosition = tilt3DWindowPos;
  windowPosition.x = canvas.clientWidth / 2;
  windowPosition.y = controller._tiltCenterMousePosition.y;
  ray = camera.getPickRay(windowPosition, tilt3DRay);
 
  const mag = Cartesian3.magnitude(center);
  const radii = Cartesian3.fromElements(mag, mag, mag, scratchRadii);
  const newEllipsoid = Ellipsoid.fromCartesian3(radii, scratchEllipsoid);
 
  intersection = IntersectionTests.rayEllipsoid(ray, newEllipsoid);
  Iif (!defined(intersection)) {
    return;
  }
 
  const t =
    Cartesian3.magnitude(ray.origin) > mag
      ? intersection.start
      : intersection.stop;
  const verticalCenter = Ray.getPoint(ray, t, tilt3DVerticalCenter);
 
  const transform = Transforms.eastNorthUpToFixedFrame(
    center,
    ellipsoid,
    tilt3DTransform,
  );
  const verticalTransform = Transforms.eastNorthUpToFixedFrame(
    verticalCenter,
    newEllipsoid,
    tilt3DVerticalTransform,
  );
 
  const oldGlobe = controller._globe;
  const oldEllipsoid = controller._ellipsoid;
  controller._globe = undefined;
  controller._ellipsoid = Ellipsoid.UNIT_SPHERE;
  controller._rotateFactor = 1.0;
  controller._rotateRateRangeAdjustment = 1.0;
 
  let constrainedAxis = Cartesian3.UNIT_Z;
 
  const oldTransform = Matrix4.clone(camera.transform, tilt3DOldTransform);
  camera._setTransform(verticalTransform);
 
  const tangent = Cartesian3.cross(
    verticalCenter,
    camera.positionWC,
    tilt3DCartesian3,
  );
  const dot = Cartesian3.dot(camera.rightWC, tangent);
 
  if (dot < 0.0) {
    const movementDelta = movement.startPosition.y - movement.endPosition.y;
    if (
      (cameraUnderground && movementDelta < 0.0) ||
      (!cameraUnderground && movementDelta > 0.0)
    ) {
      // Prevent camera from flipping past the up axis
      constrainedAxis = undefined;
    }
 
    const oldConstrainedAxis = camera.constrainedAxis;
    camera.constrainedAxis = undefined;
 
    rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
 
    camera.constrainedAxis = oldConstrainedAxis;
  } else E{
    rotate3D(controller, startPosition, movement, constrainedAxis, true, false);
  }
 
  camera._setTransform(transform);
  rotate3D(controller, startPosition, movement, constrainedAxis, false, true);
 
  Iif (defined(camera.constrainedAxis)) {
    const right = Cartesian3.cross(
      camera.direction,
      camera.constrainedAxis,
      tilt3DCartesian3,
    );
    if (
      !Cartesian3.equalsEpsilon(right, Cartesian3.ZERO, CesiumMath.EPSILON6)
    ) {
      if (Cartesian3.dot(right, camera.right) < 0.0) {
        Cartesian3.negate(right, right);
      }
 
      Cartesian3.cross(right, camera.direction, camera.up);
      Cartesian3.cross(camera.direction, camera.up, camera.right);
 
      Cartesian3.normalize(camera.up, camera.up);
      Cartesian3.normalize(camera.right, camera.right);
    }
  }
 
  camera._setTransform(oldTransform);
  controller._globe = oldGlobe;
  controller._ellipsoid = oldEllipsoid;
 
  const radius = oldEllipsoid.maximumRadius;
  controller._rotateFactor = 1.0 / radius;
  controller._rotateRateRangeAdjustment = radius;
 
  const originalPosition = Cartesian3.clone(
    camera.positionWC,
    tilt3DCartesian3,
  );
 
  if (controller.enableCollisionDetection) {
    adjustHeightForTerrain(controller, true);
  }
 
  Iif (!Cartesian3.equals(camera.positionWC, originalPosition)) {
    camera._setTransform(verticalTransform);
    camera.worldToCameraCoordinatesPoint(originalPosition, originalPosition);
 
    const magSqrd = Cartesian3.magnitudeSquared(originalPosition);
    if (Cartesian3.magnitudeSquared(camera.position) > magSqrd) {
      Cartesian3.normalize(camera.position, camera.position);
      Cartesian3.multiplyByScalar(
        camera.position,
        Math.sqrt(magSqrd),
        camera.position,
      );
    }
 
    const angle = Cartesian3.angleBetween(originalPosition, camera.position);
    const axis = Cartesian3.cross(
      originalPosition,
      camera.position,
      originalPosition,
    );
    Cartesian3.normalize(axis, axis);
 
    const quaternion = Quaternion.fromAxisAngle(axis, angle, tilt3DQuaternion);
    const rotation = Matrix3.fromQuaternion(quaternion, tilt3DMatrix);
    Matrix3.multiplyByVector(rotation, camera.direction, camera.direction);
    Matrix3.multiplyByVector(rotation, camera.up, camera.up);
    Cartesian3.cross(camera.direction, camera.up, camera.right);
    Cartesian3.cross(camera.right, camera.direction, camera.up);
 
    camera._setTransform(oldTransform);
  }
}
 
const look3DStartPos = new Cartesian2();
const look3DEndPos = new Cartesian2();
const look3DStartRay = new Ray();
const look3DEndRay = new Ray();
const look3DNegativeRot = new Cartesian3();
const look3DTan = new Cartesian3();
 
function look3D(controller, startPosition, movement, rotationAxis) {
  const scene = controller._scene;
  const camera = scene.camera;
 
  const startPos = look3DStartPos;
  startPos.x = movement.startPosition.x;
  startPos.y = 0.0;
  const endPos = look3DEndPos;
  endPos.x = movement.endPosition.x;
  endPos.y = 0.0;
 
  let startRay = camera.getPickRay(startPos, look3DStartRay);
  let endRay = camera.getPickRay(endPos, look3DEndRay);
  let angle = 0.0;
  let start;
  let end;
 
  Iif (camera.frustum instanceof OrthographicFrustum) {
    start = startRay.origin;
    end = endRay.origin;
 
    Cartesian3.add(camera.direction, start, start);
    Cartesian3.add(camera.direction, end, end);
 
    Cartesian3.subtract(start, camera.position, start);
    Cartesian3.subtract(end, camera.position, end);
 
    Cartesian3.normalize(start, start);
    Cartesian3.normalize(end, end);
  } else {
    start = startRay.direction;
    end = endRay.direction;
  }
 
  let dot = Cartesian3.dot(start, end);
  if (dot < 1.0) {
    // dot is in [0, 1]
    angle = Math.acos(dot);
  }
 
  angle = movement.startPosition.x > movement.endPosition.x ? -angle : angle;
 
  const horizontalRotationAxis = controller._horizontalRotationAxis;
  Iif (defined(rotationAxis)) {
    camera.look(rotationAxis, -angle);
  } else if (defined(horizontalRotationAxis)) {
    camera.look(horizontalRotationAxis, -angle);
  } else {
    camera.lookLeft(angle);
  }
 
  startPos.x = 0.0;
  startPos.y = movement.startPosition.y;
  endPos.x = 0.0;
  endPos.y = movement.endPosition.y;
 
  startRay = camera.getPickRay(startPos, look3DStartRay);
  endRay = camera.getPickRay(endPos, look3DEndRay);
  angle = 0.0;
 
  Iif (camera.frustum instanceof OrthographicFrustum) {
    start = startRay.origin;
    end = endRay.origin;
 
    Cartesian3.add(camera.direction, start, start);
    Cartesian3.add(camera.direction, end, end);
 
    Cartesian3.subtract(start, camera.position, start);
    Cartesian3.subtract(end, camera.position, end);
 
    Cartesian3.normalize(start, start);
    Cartesian3.normalize(end, end);
  } else {
    start = startRay.direction;
    end = endRay.direction;
  }
 
  dot = Cartesian3.dot(start, end);
  Eif (dot < 1.0) {
    // dot is in [0, 1]
    angle = Math.acos(dot);
  }
  angle = movement.startPosition.y > movement.endPosition.y ? -angle : angle;
 
  rotationAxis = rotationAxis ?? horizontalRotationAxis;
  if (defined(rotationAxis)) {
    const direction = camera.direction;
    const negativeRotationAxis = Cartesian3.negate(
      rotationAxis,
      look3DNegativeRot,
    );
    const northParallel = Cartesian3.equalsEpsilon(
      direction,
      rotationAxis,
      CesiumMath.EPSILON2,
    );
    const southParallel = Cartesian3.equalsEpsilon(
      direction,
      negativeRotationAxis,
      CesiumMath.EPSILON2,
    );
    Iif (!northParallel && !southParallel) {
      dot = Cartesian3.dot(direction, rotationAxis);
      let angleToAxis = CesiumMath.acosClamped(dot);
      if (angle > 0 && angle > angleToAxis) {
        angle = angleToAxis - CesiumMath.EPSILON4;
      }
 
      dot = Cartesian3.dot(direction, negativeRotationAxis);
      angleToAxis = CesiumMath.acosClamped(dot);
      if (angle < 0 && -angle > angleToAxis) {
        angle = -angleToAxis + CesiumMath.EPSILON4;
      }
 
      const tangent = Cartesian3.cross(rotationAxis, direction, look3DTan);
      camera.look(tangent, angle);
    } else Iif ((northParallel && angle < 0) || (southParallel && angle > 0)) {
      camera.look(camera.right, -angle);
    }
  } else {
    camera.lookUp(angle);
  }
}
 
function update3D(controller) {
  reactToInput(
    controller,
    controller.enableRotate,
    controller.rotateEventTypes,
    spin3D,
    controller.inertiaSpin,
    "_lastInertiaSpinMovement",
  );
  reactToInput(
    controller,
    controller.enableZoom,
    controller.zoomEventTypes,
    zoom3D,
    controller.inertiaZoom,
    "_lastInertiaZoomMovement",
  );
  reactToInput(
    controller,
    controller.enableTilt,
    controller.tiltEventTypes,
    tilt3D,
    controller.inertiaSpin,
    "_lastInertiaTiltMovement",
  );
  reactToInput(
    controller,
    controller.enableLook,
    controller.lookEventTypes,
    look3D,
  );
}
 
const scratchAdjustHeightTransform = new Matrix4();
const scratchAdjustHeightCartographic = new Cartographic();
 
function adjustHeightForTerrain(controller, cameraChanged) {
  controller._adjustedHeightForTerrain = true;
 
  const scene = controller._scene;
  const mode = scene.mode;
 
  if (mode === SceneMode.SCENE2D || mode === SceneMode.MORPHING) {
    return;
  }
 
  const camera = scene.camera;
  const ellipsoid = scene.ellipsoid ?? Ellipsoid.WGS84;
  const projection = scene.mapProjection;
 
  let transform;
  let mag;
  if (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
    transform = Matrix4.clone(camera.transform, scratchAdjustHeightTransform);
    mag = Cartesian3.magnitude(camera.position);
    camera._setTransform(Matrix4.IDENTITY);
  }
 
  const cartographic = scratchAdjustHeightCartographic;
  if (mode === SceneMode.SCENE3D) {
    ellipsoid.cartesianToCartographic(camera.position, cartographic);
  } else {
    projection.unproject(camera.position, cartographic);
  }
 
  let heightUpdated = false;
  if (cartographic.height < controller._minimumCollisionTerrainHeight) {
    const globeHeight = controller._scene.globeHeight;
    if (defined(globeHeight)) {
      const height = globeHeight + controller.minimumZoomDistance;
      const difference = globeHeight - controller._lastGlobeHeight;
      const percentDifference = difference / controller._lastGlobeHeight;
 
      // Unless the camera has been moved by user input, to avoid big jumps during tile loads
      // only make height updates when the globe height has been fairly stable across several frames
      if (
        cartographic.height < height &&
        (cameraChanged || Math.abs(percentDifference) <= 0.1)
      ) {
        cartographic.height = height;
        if (mode === SceneMode.SCENE3D) {
          ellipsoid.cartographicToCartesian(cartographic, camera.position);
        } else {
          projection.project(cartographic, camera.position);
        }
        heightUpdated = true;
      }
 
      if (cameraChanged || Math.abs(percentDifference) <= 0.1) {
        controller._lastGlobeHeight = globeHeight;
      } else {
        controller._lastGlobeHeight += difference * 0.1;
      }
    }
  }
 
  if (defined(transform)) {
    camera._setTransform(transform);
    if (heightUpdated) {
      Cartesian3.normalize(camera.position, camera.position);
      Cartesian3.negate(camera.position, camera.direction);
      Cartesian3.multiplyByScalar(
        camera.position,
        Math.max(mag, controller.minimumZoomDistance),
        camera.position,
      );
      Cartesian3.normalize(camera.direction, camera.direction);
      Cartesian3.cross(camera.direction, camera.up, camera.right);
      Cartesian3.cross(camera.right, camera.direction, camera.up);
    }
  }
}
 
/**
 * @private
 */
ScreenSpaceCameraController.prototype.onMap = function () {
  const scene = this._scene;
  const mode = scene.mode;
  const camera = scene.camera;
 
  if (mode === SceneMode.COLUMBUS_VIEW) {
    return (
      Math.abs(camera.position.x) - this._maxCoord.x < 0 &&
      Math.abs(camera.position.y) - this._maxCoord.y < 0
    );
  }
 
  return true;
};
 
const scratchPreviousPosition = new Cartesian3();
const scratchPreviousDirection = new Cartesian3();
 
/**
 * @private
 */
ScreenSpaceCameraController.prototype.update = function () {
  const scene = this._scene;
  const { camera, globe, mode } = scene;
 
  if (!Matrix4.equals(camera.transform, Matrix4.IDENTITY)) {
    this._globe = undefined;
    this._ellipsoid = Ellipsoid.UNIT_SPHERE;
  } else {
    this._globe = globe;
    this._ellipsoid = scene.ellipsoid ?? Ellipsoid.default;
  }
 
  const { verticalExaggeration, verticalExaggerationRelativeHeight } = scene;
  this._minimumCollisionTerrainHeight = VerticalExaggeration.getHeight(
    this.minimumCollisionTerrainHeight,
    verticalExaggeration,
    verticalExaggerationRelativeHeight,
  );
  this._minimumPickingTerrainHeight = VerticalExaggeration.getHeight(
    this.minimumPickingTerrainHeight,
    verticalExaggeration,
    verticalExaggerationRelativeHeight,
  );
  this._minimumTrackBallHeight = VerticalExaggeration.getHeight(
    this.minimumTrackBallHeight,
    verticalExaggeration,
    verticalExaggerationRelativeHeight,
  );
 
  this._cameraUnderground = scene.cameraUnderground && defined(this._globe);
 
  const radius = this._ellipsoid.maximumRadius;
  this._rotateFactor = 1.0 / radius;
  this._rotateRateRangeAdjustment = radius;
 
  this._adjustedHeightForTerrain = false;
  const previousPosition = Cartesian3.clone(
    camera.positionWC,
    scratchPreviousPosition,
  );
  const previousDirection = Cartesian3.clone(
    camera.directionWC,
    scratchPreviousDirection,
  );
 
  if (mode === SceneMode.SCENE2D) {
    update2D(this);
  } else if (mode === SceneMode.COLUMBUS_VIEW) {
    this._horizontalRotationAxis = Cartesian3.UNIT_Z;
    updateCV(this);
  } else if (mode === SceneMode.SCENE3D) {
    this._horizontalRotationAxis = undefined;
    update3D(this);
  }
 
  if (this.enableCollisionDetection && !this._adjustedHeightForTerrain) {
    // Adjust the camera height if the camera moved at all (user input or inertia) and an action didn't already adjust the camera height
    const cameraChanged =
      !Cartesian3.equals(previousPosition, camera.positionWC) ||
      !Cartesian3.equals(previousDirection, camera.directionWC);
    adjustHeightForTerrain(this, cameraChanged);
  }
 
  this._aggregator.reset();
};
 
/**
 * Returns true if this object was destroyed; otherwise, false.
 * <br /><br />
 * If this object was destroyed, it should not be used; calling any function other than
 * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
 *
 * @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
 *
 * @see ScreenSpaceCameraController#destroy
 */
ScreenSpaceCameraController.prototype.isDestroyed = function () {
  return false;
};
 
/**
 * Removes mouse listeners held by this object.
 * <br /><br />
 * Once an object is destroyed, it should not be used; calling any function other than
 * <code>isDestroyed</code> will result in a {@link DeveloperError} exception.  Therefore,
 * assign the return value (<code>undefined</code>) to the object as done in the example.
 *
 * @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
 *
 *
 * @example
 * controller = controller && controller.destroy();
 *
 * @see ScreenSpaceCameraController#isDestroyed
 */
ScreenSpaceCameraController.prototype.destroy = function () {
  this._tweens.removeAll();
  this._aggregator = this._aggregator && this._aggregator.destroy();
  return destroyObject(this);
};
export default ScreenSpaceCameraController;