安全上下文
此特征只可用于 安全上下文 (HTTPS),在某些或所有 支持浏览器 .

只读 XRInputSource property targetRaySpace 返回 XRSpace (typically an XRReferenceSpace ) representing the position and orientation of the target ray in the virtual space. Its native origin tracks the position of the origin point of the target ray, and its orientation indicates the orientation of the controller device itself. These values, interpreted in the context of the input source's targetRayMode , can be used both to fully interpret the device as an input source.

<<<--- needs diagram showing targetRaySpace relative to gripSpace and world space --->>>

To obtain an XRSpace representing the input controller's position and orientation in virtual space, use the gripSpace 特性。

句法

let targetRaySpace = xrInputSource.targetRaySpace;
					

XRSpace object—typically an XRReferenceSpace or XRBoundedReferenceSpace —which represents the position and orientation of the input controller's target ray in virtual space.

The native origin of the returned XRSpace is located at the point from which the target ray is emitted, and the orientation of the space indicates the direction in which the target ray is pointing.

用法注意事项

All input sources—regardless of their targetRayMode —have a valid targetRaySpace . The exact meaning of this space varies, however, depending on the mode:

  • Every gaze input ( targetRayMode value of gaze ), shares the same XRSpace object as their target ray space, since the gaze input comes from the viewer's head. This shared space represents the same location as the space returned by the XRSession 方法 requestReferenceSpace() , but is maintained as a different object to allow for future enhancements to the API.
  • The target ray space reported by tracked pointer inputs ( targetRayMode of tracked-pointer ) is actually based upon the true spatial position and orientation of the input device.

To determine the position and orientation of the target ray while rendering a frame, pass it into the XRFrame 方法 getPose() method, then use the returned XRPose 对象的 transform to gather the spatial information you need.

范例

This fragment of code shows part of a function to be called once every frame. It looks for inputs which have a non- NULL targetRaySpace . Inputs which have a value for this property represent inputs that project a target ray outward from the user.

For each such input, this example looks for inputs whose targetRayMode is tracked-pointer , indicating that the input is in fact intended to represent a targeting device rather than a gazing device, screen tap, or mouse click. For tracked pointers, a function myRenderTargetRayAsBeam() is called to render a beam from the input controller's virtual position outward in the direction it's pointing.

The code should continue to perform tasks such as drawing controllers or any objects representative of the user's hands' positions in the virtual space, as well as any other input-related tasks.

function updateInputSources(session, frame, refSpace) {
  for (let source of session.getInputSources()) {
    let targetRayPose = frame.getPose(inputSource.targetRaySpace, refSpace);
    if (targetRayPose) {
      if (source.targetRayMode == "tracked-pointer") {
        myRenderTargetRayAsBeam(targetRayPose);
      }
    }
    /* ... */
  }
}
					

See the article Inputs and input sources for more details and a more complete example.

规范

规范 状态 注释
WebXR 设备 API
The definition of 'XRInputSource.targetRaySpace' in that specification.
工作草案 初始定义。

浏览器兼容性

The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.

更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
targetRaySpace Chrome 79 Edge 79 Firefox No IE No Opera No Safari No WebView Android No Chrome Android 79 Firefox Android No Opera Android No Safari iOS No Samsung Internet Android 11.2

图例

完整支持

完整支持

不支持

不支持

另请参阅

元数据

  • 最后修改: