callstack / react-native-visionos

A framework for building native visionOS applications using React
https://callstack.github.io/react-native-visionos-docs/
MIT License
1.02k stars 30 forks source link

feat: implement `XR` API #81

Closed okwasniewski closed 10 months ago

okwasniewski commented 10 months ago

Summary:

This PR introduces the XR API.

Example:

const {XR} = require('@callstack/react-native-visionos');
//...
const OpenXRSession = () => {
  const [isOpen, setIsOpen] = React.useState(false);

  const openXRSession = async () => {
    try {
      if (!XR.supportsMultipleScenes) {
        Alert.alert('Error', 'Multiple scenes are not supported');
        return;
      }
      await XR.requestSession('TestImmersiveSpace');
      setIsOpen(true);
    } catch (e) {
      Alert.alert('Error', e.message);
      setIsOpen(false);
    }
  };

  const closeXRSession = async () => {
    if (isOpen) {
      await XR.endSession();
    }
  };

  return (
    <View style={styles.container}>
      <Text style={styles.title}>Is XR session open: {isOpen}</Text>
      <Button title="Open XR Session" onPress={openXRSession} />
      <Button title="Close XR Session" onPress={closeXRSession} />
    </View>
  );
};

To-Do

Opening immersive space might fail in two scenarios: 1. App doesn't support multi window, user cancels the operation.

Demo

https://github.com/callstack/react-native-visionos/assets/52801365/9c985e79-5079-48e9-bbfa-e24fdc67fe67

matthargett commented 10 months ago

Try to cross-reference the WebXR/OpenXR API to see if we can find a middle ground between them and visionOS specifics. It would be nice to be able to use this API across XR devices.

okwasniewski commented 10 months ago

@matthargett Looks like equivalent for WebXR is navigator.xr.requestSession() we can rename the API to XR and methods to:

WebXR returns session object and it ends the session by calling .end() on this object. I think the closest we can get is XR.endSession()

CleanShot 2024-01-26 at 14 44 16@2x