Attention: Here be dragons

This is the latest (unstable) version of this documentation, which may document features not available in or compatible with released stable versions of Godot.

XRInterface

繼承: RefCounted < Object

被繼承: MobileVRInterface, OpenXRInterface, WebXRInterface, XRInterfaceExtension

XR 介面實作的基底類別。

說明

需要實作該類以使 Godot 可以使用 AR 或 VR 平臺,並且這些應作為 C++ 模組或 GDExtension 模組實作。部分介面公開給 GDScript,因此可以偵測、啟用和配置 AR 或 VR 平臺。

介面應該以這樣一種方式編寫,只要簡單地啟用它們就會提供一個工作設定。可以通過 XRServer 查詢可用的介面。

教學

屬性

bool

ar_is_anchor_detection_enabled

false

EnvironmentBlendMode

environment_blend_mode

0

bool

interface_is_primary

false

PlayAreaMode

xr_play_area_mode

0

方法

int

get_camera_feed_id()

int

get_capabilities() const

StringName

get_name() const

PackedVector3Array

get_play_area() const

Projection

get_projection_for_view(view: int, aspect: float, near: float, far: float)

Vector2

get_render_target_size()

Array

get_supported_environment_blend_modes()

Dictionary

get_system_info()

TrackingStatus

get_tracking_status() const

Transform3D

get_transform_for_view(view: int, cam_transform: Transform3D)

int

get_view_count()

bool

initialize()

bool

is_initialized() const

bool

is_passthrough_enabled()

bool

is_passthrough_supported()

bool

set_environment_blend_mode(mode: EnvironmentBlendMode)

bool

set_play_area_mode(mode: PlayAreaMode)

bool

start_passthrough()

void

stop_passthrough()

bool

supports_play_area_mode(mode: PlayAreaMode)

void

trigger_haptic_pulse(action_name: String, tracker_name: StringName, frequency: float, amplitude: float, duration_sec: float, delay_sec: float)

void

uninitialize()


訊號

play_area_changed(mode: int) 🔗

當遊玩區域改變時發出。這可能是玩家重設邊界、進入新的遊玩區域、更改遊玩區域模式、重設其頭戴式裝置方向,或者世界比例改變的結果。


列舉

enum Capabilities: 🔗

Capabilities XR_NONE = 0

沒有 XR 功能。

Capabilities XR_MONO = 1

此介面可以與正常的算繪輸出一起工作(非基於 HMD 的 AR)。

Capabilities XR_STEREO = 2

該介面支援立體算繪。

Capabilities XR_QUAD = 4

該介面支援四邊形算繪(Godot 尚不支援)。

Capabilities XR_VR = 8

該介面支援 VR。

Capabilities XR_AR = 16

該介面支援 AR(影片背景和真實世界追蹤)。

Capabilities XR_EXTERNAL = 32

該介面輸出至外部裝置。如果使用的是主視口,則螢幕上的輸出是未經修改的左眼或右眼緩衝區(如果視口大小未更改至與 get_render_target_size() 相同的縱橫比,則會進行拉伸)。使用單獨的視口節點能夠將主視口讓出來做別的事情。


enum TrackingStatus: 🔗

TrackingStatus XR_NORMAL_TRACKING = 0

追蹤行為符合預期。

TrackingStatus XR_EXCESSIVE_MOTION = 1

過度運動會阻礙追蹤(玩家的移動速度大於追蹤的速度)。

TrackingStatus XR_INSUFFICIENT_FEATURES = 2

追蹤受到功能不足的阻礙,太暗(對於基於相機的追蹤),玩家被阻礙等。

TrackingStatus XR_UNKNOWN_TRACKING = 3

我們不知道追蹤的狀態,或者這個介面未提供回饋。

TrackingStatus XR_NOT_TRACKING = 4

追蹤功能失效(相機未插電或被遮擋、燈塔關閉,等等)。


enum PlayAreaMode: 🔗

PlayAreaMode XR_PLAY_AREA_UNKNOWN = 0

遊玩區域模式未設定或不可用。

PlayAreaMode XR_PLAY_AREA_3DOF = 1

遊玩區域僅支援方向追蹤,不支援位置追蹤,區域以玩家為中心。

PlayAreaMode XR_PLAY_AREA_SITTING = 2

玩家處於坐姿,提供有限的位置追蹤,玩家周圍有固定的防護。

PlayAreaMode XR_PLAY_AREA_ROOMSCALE = 3

玩家可以自由移動,提供完整的位置追蹤。

PlayAreaMode XR_PLAY_AREA_STAGE = 4

Same as XR_PLAY_AREA_ROOMSCALE but origin point is fixed to the center of the physical space. In this mode, system-level recentering may be disabled, requiring the use of XRServer.center_on_hmd().

PlayAreaMode XR_PLAY_AREA_CUSTOM = 2147483647

Custom play area set by a GDExtension.


enum EnvironmentBlendMode: 🔗

EnvironmentBlendMode XR_ENV_BLEND_MODE_OPAQUE = 0

不透明混合模式。通常用於 VR 裝置。

EnvironmentBlendMode XR_ENV_BLEND_MODE_ADDITIVE = 1

加法混合模式。通常用於帶有穿透功能的 AR 或 VR 裝置。

EnvironmentBlendMode XR_ENV_BLEND_MODE_ALPHA_BLEND = 2

Alpha 混合模式。通常用於帶有穿透功能的 AR 或 VR 裝置。Alpha 通道控制穿透的可見程度。Alpha 為 0.0 表示穿透可見、該圖元處於加法模式。Alpha 為 1.0 表示穿透不可見,該圖元處於不透明模式。


enum VRSTextureFormat: 🔗

VRSTextureFormat XR_VRS_TEXTURE_FORMAT_UNIFIED = 0

The texture format is the same as returned by XRVRS.make_vrs_texture().

VRSTextureFormat XR_VRS_TEXTURE_FORMAT_FRAGMENT_SHADING_RATE = 1

The texture format is the same as expected by the Vulkan VK_KHR_fragment_shading_rate extension.

VRSTextureFormat XR_VRS_TEXTURE_FORMAT_FRAGMENT_DENSITY_MAP = 2

The texture format is the same as expected by the Vulkan VK_EXT_fragment_density_map extension.


屬性說明

bool ar_is_anchor_detection_enabled = false 🔗

  • void set_anchor_detection_is_enabled(value: bool)

  • bool get_anchor_detection_is_enabled()

在 AR 介面上,如果啟用錨點偵測,則為 true


EnvironmentBlendMode environment_blend_mode = 0 🔗

指定 XR 應如何融入環境。這特定於某些 AR 和直通裝置,其中相機影像由 XR 合成器混合。


bool interface_is_primary = false 🔗

  • void set_primary(value: bool)

  • bool is_primary()

true 如果這是個主介面。


PlayAreaMode xr_play_area_mode = 0 🔗

該介面的遊玩區域模式。


方法說明

int get_camera_feed_id() 🔗

如果這是一個需要顯示相機畫面作為背景的 AR 介面,此方法返回該介面的 CameraServer 中的畫面 ID。


int get_capabilities() const 🔗

返回 Capabilities 標籤的組合,提供關於這個介面功能的資訊。


StringName get_name() const 🔗

Returns the name of this interface ("OpenXR", "OpenVR", "OpenHMD", "ARKit", etc.).


PackedVector3Array get_play_area() const 🔗

Returns an array of vectors that represent the physical play area mapped to the virtual space around the XROrigin3D point. The points form a convex polygon that can be used to react to or visualize the play area. This returns an empty array if this feature is not supported or if the information is not yet available.


Projection get_projection_for_view(view: int, aspect: float, near: float, far: float) 🔗

返回視圖/眼睛的投影矩陣。


Vector2 get_render_target_size() 🔗

返回在VR平臺套用鏡頭失真等內容之前算繪的中間結果的解析度。


Array get_supported_environment_blend_modes() 🔗

返回支援的環境混合模式陣列,見 EnvironmentBlendMode


Dictionary get_system_info() 🔗

返回包含額外系統資訊的 Dictionary。介面應該返回 XRRuntimeNameXRRuntimeVersion,表示所使用的 XR 運作時資訊。還可以額外提供關於特定介面的條目。

注意:這個資訊可能只在成功呼叫 initialize() 後可用。


TrackingStatus get_tracking_status() const 🔗

如果支援,返回我們的追蹤狀態。這將使你能夠向使用者回饋,是否存在位置追蹤的問題。


Transform3D get_transform_for_view(view: int, cam_transform: Transform3D) 🔗

返回視圖/眼睛的變換。

view 是視圖/眼睛的索引。

cam_transform 是將裝置座標對應至場景座標的變換,通常是目前 XROrigin3D 的 Node3D.global_transform


int get_view_count() 🔗

返回該裝置算繪所需的視圖數量。1 代表單目平面視圖,2 代表雙目立體視圖。


bool initialize() 🔗

呼叫它來初始化這個介面。被初始化的第一個介面被識別為主介面,它將用於算繪輸出。

初始化想要使用的介面後,需要啟用視口的 AR/VR 模式,並且算繪應該開始。

注意:對於任何使用 Godot 主輸出的裝置,例如移動 VR,必須在主視口上啟用 XR 模式。

如果為處理自己輸出的平臺(例如 OpenVR)執行該操作,則 Godot 將只顯示一隻眼睛而不會在螢幕上失真。或者,可以將單獨的視口節點新增到場景並在該視口上啟用 AR/VR。它將被用於輸出到 HMD,讓你可以在主視窗中自由地做任何你喜歡的事情,例如使用單獨的相機作為旁觀者相機或算繪完全不同的東西。

雖然目前未使用,但可以啟動其他介面。如果想追蹤來自其他平臺的控制器,可能會希望這樣做。但是,此時只有一個介面可以算繪到 HMD。


bool is_initialized() const 🔗

如果這個介面已初始化,則返回 true


bool is_passthrough_enabled() 🔗

已棄用: Check if environment_blend_mode is XR_ENV_BLEND_MODE_ALPHA_BLEND, instead.

Returns true if passthrough is enabled.


bool is_passthrough_supported() 🔗

已棄用: Check that XR_ENV_BLEND_MODE_ALPHA_BLEND is supported using get_supported_environment_blend_modes(), instead.

Returns true if this interface supports passthrough.


bool set_environment_blend_mode(mode: EnvironmentBlendMode) 🔗

Sets the active environment blend mode.

mode is the environment blend mode starting with the next frame.

Note: Not all runtimes support all environment blend modes, so it is important to check this at startup. For example:

func _ready():
    var xr_interface = XRServer.find_interface("OpenXR")
    if xr_interface and xr_interface.is_initialized():
        var vp = get_viewport()
        vp.use_xr = true
        var acceptable_modes = [XRInterface.XR_ENV_BLEND_MODE_OPAQUE, XRInterface.XR_ENV_BLEND_MODE_ADDITIVE]
        var modes = xr_interface.get_supported_environment_blend_modes()
        for mode in acceptable_modes:
            if mode in modes:
                xr_interface.set_environment_blend_mode(mode)
                break

bool set_play_area_mode(mode: PlayAreaMode) 🔗

Sets the active play area mode, will return false if the mode can't be used with this interface.

Note: Changing this after the interface has already been initialized can be jarring for the player, so it's recommended to recenter on the HMD with XRServer.center_on_hmd() (if switching to XR_PLAY_AREA_STAGE) or make the switch during a scene change.


bool start_passthrough() 🔗

已棄用: Set the environment_blend_mode to XR_ENV_BLEND_MODE_ALPHA_BLEND, instead.

啟動穿透,如果無法啟動則會返回 false

注意:XR 所使用的視口必須有透明背景,否則穿透可能無法正確算繪。


void stop_passthrough() 🔗

已棄用: Set the environment_blend_mode to XR_ENV_BLEND_MODE_OPAQUE, instead.

停止穿透。


bool supports_play_area_mode(mode: PlayAreaMode) 🔗

請呼叫這個方法來查詢此介面是否支援給定的遊玩區域模式。


void trigger_haptic_pulse(action_name: String, tracker_name: StringName, frequency: float, amplitude: float, duration_sec: float, delay_sec: float) 🔗

Triggers a haptic pulse on a device associated with this interface.

action_name is the name of the action for this pulse.

tracker_name is optional and can be used to direct the pulse to a specific device provided that device is bound to this haptic.

frequency is the frequency of the pulse, set to 0.0 to have the system use a default frequency.

amplitude is the amplitude of the pulse between 0.0 and 1.0.

duration_sec is the duration of the pulse in seconds.

delay_sec is a delay in seconds before the pulse is given.


void uninitialize() 🔗

關閉介面。