|
1 | 1 | import { XRDevice, metaQuest3, metaQuest2, metaQuestPro, oculusQuest1 } from 'iwer' |
2 | 2 | import { DevUI } from '@iwer/devui' |
| 3 | +import type { XRDeviceOptions } from 'iwer/lib/device/XRDevice' |
| 4 | +import { Euler, Quaternion, Vector3, Vector3Tuple, Vector4Tuple } from 'three' |
3 | 5 |
|
4 | 6 | const configurations = { metaQuest3, metaQuest2, metaQuestPro, oculusQuest1 } |
5 | 7 |
|
6 | 8 | export type EmulatorType = keyof typeof configurations |
7 | 9 |
|
8 | | -export function emulate(type: EmulatorType) { |
9 | | - const xrdevice = new XRDevice(configurations[type]) |
10 | | - xrdevice.ipd = 0 |
| 10 | +export type EmulatorTransformationOptions = { |
| 11 | + position?: Vector3 | Vector3Tuple |
| 12 | + rotation?: Euler | Vector3Tuple |
| 13 | + quaternion?: Quaternion | Vector4Tuple |
| 14 | +} |
| 15 | + |
| 16 | +export type EmulatorOptions = |
| 17 | + | EmulatorType |
| 18 | + | ({ |
| 19 | + type?: EmulatorType |
| 20 | + primaryInputMode?: XRDevice['primaryInputMode'] |
| 21 | + headset?: EmulatorTransformationOptions |
| 22 | + controller?: Partial<Record<XRHandedness, EmulatorTransformationOptions>> |
| 23 | + hand?: Partial<Record<XRHandedness, EmulatorTransformationOptions>> |
| 24 | + } & Partial<Pick<XRDeviceOptions, 'ipd' | 'fovy' | 'stereoEnabled' | 'canvasContainer'>>) |
| 25 | + |
| 26 | +const handednessList: Array<XRHandedness> = ['left', 'none', 'right'] |
| 27 | + |
| 28 | +export function emulate(options: EmulatorOptions) { |
| 29 | + const type = typeof options === 'string' ? options : (options.type ?? 'metaQuest3') |
| 30 | + const xrdevice = new XRDevice(configurations[type], typeof options === 'string' ? undefined : options) |
| 31 | + if (typeof options != 'string') { |
| 32 | + applyEmulatorTransformOptions(xrdevice, options.headset) |
| 33 | + applyEmulatorInputSourcesOptions(xrdevice.hands, options.hand) |
| 34 | + applyEmulatorInputSourcesOptions(xrdevice.controllers, options.controller) |
| 35 | + xrdevice.primaryInputMode = options.primaryInputMode ?? 'controller' |
| 36 | + } |
| 37 | + xrdevice.ipd = typeof options === 'string' ? 0 : (options.ipd ?? 0) |
11 | 38 | xrdevice.installRuntime() |
12 | 39 | new DevUI(xrdevice) |
| 40 | + return xrdevice |
| 41 | +} |
| 42 | + |
| 43 | +const eulerHelper = new Euler() |
| 44 | +const quaternionHelper = new Quaternion() |
| 45 | + |
| 46 | +function applyEmulatorInputSourcesOptions( |
| 47 | + xrInputSources: XRDevice['controllers'] | XRDevice['hands'], |
| 48 | + options: Partial<Record<XRHandedness, EmulatorTransformationOptions>> | undefined, |
| 49 | +) { |
| 50 | + if (options == null) { |
| 51 | + return |
| 52 | + } |
| 53 | + for (const handedness of handednessList) { |
| 54 | + applyEmulatorTransformOptions(xrInputSources[handedness], options[handedness]) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +function applyEmulatorTransformOptions( |
| 59 | + target: XRDevice['controllers']['left'] | XRDevice['hands']['left'] | XRDevice, |
| 60 | + options: EmulatorTransformationOptions | undefined, |
| 61 | +) { |
| 62 | + if (target == null || options == null) { |
| 63 | + return |
| 64 | + } |
| 65 | + setVector(target.position, options.position) |
| 66 | + setVector(eulerHelper, options.rotation) |
| 67 | + setQuaternion(target.quaternion, quaternionHelper.setFromEuler(eulerHelper)) |
| 68 | + setQuaternion(target.quaternion, options.quaternion) |
| 69 | +} |
| 70 | + |
| 71 | +function setVector( |
| 72 | + target: { x: number; y: number; z: number } | Euler, |
| 73 | + value: Euler | Vector3 | Vector3Tuple | undefined, |
| 74 | +) { |
| 75 | + if (value == null) { |
| 76 | + return |
| 77 | + } |
| 78 | + if (value instanceof Euler && target instanceof Euler) { |
| 79 | + target.copy(value) |
| 80 | + } |
| 81 | + if (Array.isArray(value)) { |
| 82 | + target.x = value[0] |
| 83 | + target.y = value[1] |
| 84 | + target.z = value[2] |
| 85 | + return |
| 86 | + } |
| 87 | + target.x = value.x |
| 88 | + target.y = value.y |
| 89 | + target.z = value.z |
| 90 | +} |
| 91 | + |
| 92 | +function setQuaternion( |
| 93 | + target: { x: number; y: number; z: number; w: number }, |
| 94 | + value: Quaternion | Vector4Tuple | undefined, |
| 95 | +) { |
| 96 | + if (value == null) { |
| 97 | + return |
| 98 | + } |
| 99 | + if (Array.isArray(value)) { |
| 100 | + target.x = value[0] |
| 101 | + target.y = value[1] |
| 102 | + target.z = value[2] |
| 103 | + target.w = value[3] |
| 104 | + return |
| 105 | + } |
| 106 | + target.x = value.x |
| 107 | + target.y = value.y |
| 108 | + target.z = value.z |
| 109 | + target.w = value.w |
13 | 110 | } |
0 commit comments