-
-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support emulator options; breaking change: set default space in…
… hand and controller to target ray space to align better with the webxr standard
- Loading branch information
1 parent
432aff6
commit f4b5f2d
Showing
11 changed files
with
203 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,110 @@ | ||
import { XRDevice, metaQuest3, metaQuest2, metaQuestPro, oculusQuest1 } from 'iwer' | ||
import { DevUI } from '@iwer/devui' | ||
import type { XRDeviceOptions } from 'iwer/lib/device/XRDevice' | ||
import { Euler, Quaternion, Vector3, Vector3Tuple, Vector4Tuple } from 'three' | ||
|
||
const configurations = { metaQuest3, metaQuest2, metaQuestPro, oculusQuest1 } | ||
|
||
export type EmulatorType = keyof typeof configurations | ||
|
||
export function emulate(type: EmulatorType) { | ||
const xrdevice = new XRDevice(configurations[type]) | ||
xrdevice.ipd = 0 | ||
export type EmulatorTransformationOptions = { | ||
position?: Vector3 | Vector3Tuple | ||
rotation?: Euler | Vector3Tuple | ||
quaternion?: Quaternion | Vector4Tuple | ||
} | ||
|
||
export type EmulatorOptions = | ||
| EmulatorType | ||
| ({ | ||
type?: EmulatorType | ||
primaryInputMode?: XRDevice['primaryInputMode'] | ||
headset?: EmulatorTransformationOptions | ||
controller?: Partial<Record<XRHandedness, EmulatorTransformationOptions>> | ||
hand?: Partial<Record<XRHandedness, EmulatorTransformationOptions>> | ||
} & Partial<Pick<XRDeviceOptions, 'ipd' | 'fovy' | 'stereoEnabled' | 'canvasContainer'>>) | ||
|
||
const handednessList: Array<XRHandedness> = ['left', 'none', 'right'] | ||
|
||
export function emulate(options: EmulatorOptions) { | ||
const type = typeof options === 'string' ? options : (options.type ?? 'metaQuest3') | ||
const xrdevice = new XRDevice(configurations[type], typeof options === 'string' ? undefined : options) | ||
if (typeof options != 'string') { | ||
applyEmulatorTransformOptions(xrdevice, options.headset) | ||
applyEmulatorInputSourcesOptions(xrdevice.hands, options.hand) | ||
applyEmulatorInputSourcesOptions(xrdevice.controllers, options.controller) | ||
xrdevice.primaryInputMode = options.primaryInputMode ?? 'controller' | ||
} | ||
xrdevice.ipd = typeof options === 'string' ? 0 : (options.ipd ?? 0) | ||
xrdevice.installRuntime() | ||
new DevUI(xrdevice) | ||
return xrdevice | ||
} | ||
|
||
const eulerHelper = new Euler() | ||
const quaternionHelper = new Quaternion() | ||
|
||
function applyEmulatorInputSourcesOptions( | ||
xrInputSources: XRDevice['controllers'] | XRDevice['hands'], | ||
options: Partial<Record<XRHandedness, EmulatorTransformationOptions>> | undefined, | ||
) { | ||
if (options == null) { | ||
return | ||
} | ||
for (const handedness of handednessList) { | ||
applyEmulatorTransformOptions(xrInputSources[handedness], options[handedness]) | ||
} | ||
} | ||
|
||
function applyEmulatorTransformOptions( | ||
target: XRDevice['controllers']['left'] | XRDevice['hands']['left'] | XRDevice, | ||
options: EmulatorTransformationOptions | undefined, | ||
) { | ||
if (target == null || options == null) { | ||
return | ||
} | ||
setVector(target.position, options.position) | ||
setVector(eulerHelper, options.rotation) | ||
setQuaternion(target.quaternion, quaternionHelper.setFromEuler(eulerHelper)) | ||
setQuaternion(target.quaternion, options.quaternion) | ||
} | ||
|
||
function setVector( | ||
target: { x: number; y: number; z: number } | Euler, | ||
value: Euler | Vector3 | Vector3Tuple | undefined, | ||
) { | ||
if (value == null) { | ||
return | ||
} | ||
if (value instanceof Euler && target instanceof Euler) { | ||
target.copy(value) | ||
} | ||
if (Array.isArray(value)) { | ||
target.x = value[0] | ||
target.y = value[1] | ||
target.z = value[2] | ||
return | ||
} | ||
target.x = value.x | ||
target.y = value.y | ||
target.z = value.z | ||
} | ||
|
||
function setQuaternion( | ||
target: { x: number; y: number; z: number; w: number }, | ||
value: Quaternion | Vector4Tuple | undefined, | ||
) { | ||
if (value == null) { | ||
return | ||
} | ||
if (Array.isArray(value)) { | ||
target.x = value[0] | ||
target.y = value[1] | ||
target.z = value[2] | ||
target.w = value[3] | ||
return | ||
} | ||
target.x = value.x | ||
target.y = value.y | ||
target.z = value.z | ||
target.w = value.w | ||
} |
Oops, something went wrong.