Skip to content

Commit

Permalink
Add fb-scene-sample project (#173)
Browse files Browse the repository at this point in the history
  • Loading branch information
devloglogan authored Jun 28, 2024
1 parent 4d12077 commit 6803157
Show file tree
Hide file tree
Showing 24 changed files with 1,594 additions and 277 deletions.
7 changes: 0 additions & 7 deletions demo/assets/cross-grid-material.tres

This file was deleted.

11 changes: 8 additions & 3 deletions demo/export_presets.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ permissions/mount_format_filesystems=false
permissions/mount_unmount_filesystems=false
permissions/nfc=false
permissions/persistent_activity=false
permissions/post_notifications=false
permissions/process_outgoing_calls=false
permissions/read_calendar=false
permissions/read_call_log=false
Expand Down Expand Up @@ -207,7 +208,7 @@ permissions/write_user_dictionary=false
xr_features/enable_meta_plugin=true
meta_xr_features/hand_tracking=1
meta_xr_features/hand_tracking_frequency=1
meta_xr_features/passthrough=1
meta_xr_features/passthrough=0
xr_features/enable_pico_plugin=false
xr_features/enable_lynx_plugin=false
xr_features/enable_khronos_plugin=false
Expand All @@ -216,11 +217,15 @@ meta_xr_features/quest_1_support=false
meta_xr_features/quest_2_support=true
meta_xr_features/quest_3_support=true
meta_xr_features/quest_pro_support=true
meta_xr_features/use_anchor_api=true
meta_xr_features/use_anchor_api=false
meta_xr_features/face_tracking=1
meta_xr_features/render_model=1
meta_xr_features/use_scene_api=true
meta_xr_features/use_scene_api=false
meta_xr_features/use_overlay_keyboard=false
meta_xr_features/use_experimental_features=false
meta_xr_features/boundary_mode=0
meta_xr_features/body_tracking=1
khronos_xr_features/htc/hand_tracking=0
khronos_xr_features/htc/tracker=0
khronos_xr_features/htc/eye_tracking=0
khronos_xr_features/htc/lip_expression=0
169 changes: 1 addition & 168 deletions demo/main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,15 @@ extends Node3D
var xr_interface: XRInterface = null
var hand_tracking_source: Array[OpenXRInterface.HandTrackedSource]
var passthrough_enabled: bool = false
var selected_spatial_anchor_node: Node3D = null

@onready var left_hand: XRController3D = $XROrigin3D/LeftHand
@onready var right_hand: XRController3D = $XROrigin3D/RightHand
@onready var left_hand_mesh: MeshInstance3D = $XROrigin3D/LeftHand/LeftHandMesh
@onready var right_hand_mesh: MeshInstance3D = $XROrigin3D/RightHand/RightHandMesh
@onready var left_controller_model: OpenXRFbRenderModel = $XROrigin3D/LeftHand/LeftControllerFbRenderModel
@onready var right_controller_model: OpenXRFbRenderModel = $XROrigin3D/RightHand/RightControllerFbRenderModel
@onready var left_hand_pointer: XRController3D = $XROrigin3D/LeftHandPointer
@onready var left_hand_pointer_raycast: RayCast3D = $XROrigin3D/LeftHandPointer/RayCast3D
@onready var scene_pointer_mesh: MeshInstance3D = $XROrigin3D/LeftHandPointer/ScenePointerMesh
@onready var scene_colliding_mesh: MeshInstance3D = $XROrigin3D/LeftHandPointer/SceneCollidingMesh
@onready var floor_mesh: MeshInstance3D = $Floor
@onready var world_environment: WorldEnvironment = $WorldEnvironment
@onready var scene_manager: OpenXRFbSceneManager = $XROrigin3D/OpenXRFbSceneManager
@onready var spatial_anchor_manager: OpenXRFbSpatialAnchorManager = $XROrigin3D/OpenXRFbSpatialAnchorManager

const SPATIAL_ANCHORS_FILE = "user://openxr_fb_spatial_anchors.json"
const SpatialAnchor = preload("res://spatial_anchor.tscn")

const COLORS = [
"#FF0000", # Red
Expand All @@ -38,7 +28,6 @@ const COLORS = [
func _ready():
xr_interface = XRServer.find_interface("OpenXR")
if xr_interface and xr_interface.initialize():
xr_interface.session_begun.connect(self.load_spatial_anchors_from_file)
xr_interface.session_stopping.connect(self._on_session_stopping)
var vp: Viewport = get_viewport()
vp.use_xr = true
Expand All @@ -57,86 +46,6 @@ func _on_session_stopping() -> void:
get_tree().quit()


func load_spatial_anchors_from_file() -> void:
var file := FileAccess.open(SPATIAL_ANCHORS_FILE, FileAccess.READ)
if not file:
print ("no file")
return

var json := JSON.new()
if json.parse(file.get_as_text()) != OK:
print("ERROR: Unable to parse ", SPATIAL_ANCHORS_FILE)
return

if not json.data is Dictionary:
print("ERROR: ", SPATIAL_ANCHORS_FILE, " contains invalid data")
return

var anchor_data: Dictionary = json.data
if anchor_data.size() > 0:
spatial_anchor_manager.load_anchors(anchor_data.keys(), anchor_data, OpenXRFbSpatialEntity.STORAGE_LOCAL, true)


func save_spatial_anchors_to_file() -> void:
var file := FileAccess.open(SPATIAL_ANCHORS_FILE, FileAccess.WRITE)
if not file:
print("ERROR: Unable to open file for writing: ", SPATIAL_ANCHORS_FILE)
return

var anchor_data := {}
for uuid in spatial_anchor_manager.get_anchor_uuids():
var entity: OpenXRFbSpatialEntity = spatial_anchor_manager.get_spatial_entity(uuid)
anchor_data[uuid] = entity.custom_data

file.store_string(JSON.stringify(anchor_data))
file.close()


func _on_spatial_anchor_tracked(_anchor_node: XRAnchor3D, _spatial_entity: OpenXRFbSpatialEntity, is_new: bool) -> void:
if is_new:
save_spatial_anchors_to_file()


func _on_spatial_anchor_untracked(_anchor_node: XRAnchor3D, _spatial_entity: OpenXRFbSpatialEntity) -> void:
save_spatial_anchors_to_file()


func enable_passthrough(enable: bool) -> void:
if passthrough_enabled == enable:
return

var supported_blend_modes = xr_interface.get_supported_environment_blend_modes()
print("Supported blend modes: ", supported_blend_modes)
if XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND in supported_blend_modes and XRInterface.XR_ENV_BLEND_MODE_OPAQUE in supported_blend_modes:
print("Passthrough supported.")
if enable:
# Switch to passthrough.
xr_interface.environment_blend_mode = XRInterface.XR_ENV_BLEND_MODE_ALPHA_BLEND
get_viewport().transparent_bg = true
world_environment.environment.background_mode = Environment.BG_COLOR
world_environment.environment.background_color = Color(0.0, 0.0, 0.0, 0.0)
floor_mesh.visible = false
scene_manager.visible = true
spatial_anchor_manager.visible = true
left_hand_pointer.visible = true
left_hand_pointer_raycast.enabled = true
if not scene_manager.are_scene_anchors_created():
scene_manager.create_scene_anchors()
else:
# Switch back to VR.
xr_interface.environment_blend_mode = XRInterface.XR_ENV_BLEND_MODE_OPAQUE
get_viewport().transparent_bg = false
world_environment.environment.background_mode = Environment.BG_SKY
floor_mesh.visible = true
scene_manager.visible = false
spatial_anchor_manager.visible = false
left_hand_pointer.visible = false
left_hand_pointer_raycast.enabled = false
passthrough_enabled = enable
else:
print("Switching to/from passthrough not supported.")


func _physics_process(_delta: float) -> void:
for hand in OpenXRInterface.HAND_MAX:
var source = xr_interface.get_hand_tracking_source(hand)
Expand All @@ -163,86 +72,10 @@ func _physics_process(_delta: float) -> void:

hand_tracking_source[hand] = source

if left_hand_pointer.visible:
var previous_selected_spatial_anchor_node = selected_spatial_anchor_node

if left_hand_pointer_raycast.is_colliding():
var collision_point: Vector3 = left_hand_pointer_raycast.get_collision_point()
scene_colliding_mesh.global_position = collision_point

var pointer_length: float = (collision_point - left_hand_pointer.global_position).length()
scene_pointer_mesh.mesh.size.z = pointer_length
scene_pointer_mesh.position.z = -pointer_length / 2.0

var collider: CollisionObject3D = left_hand_pointer_raycast.get_collider()
if collider and collider.get_collision_layer_value(3):
selected_spatial_anchor_node = collider
else:
selected_spatial_anchor_node = null
else:
scene_pointer_mesh.mesh.size.z = 5
scene_pointer_mesh.position.z = -2.5
selected_spatial_anchor_node = null

if previous_selected_spatial_anchor_node != selected_spatial_anchor_node:
if previous_selected_spatial_anchor_node:
previous_selected_spatial_anchor_node.set_selected(false)
if selected_spatial_anchor_node:
selected_spatial_anchor_node.set_selected(true)
scene_colliding_mesh.visible = false
else:
scene_colliding_mesh.visible = true


func _on_left_hand_button_pressed(name):
if name == "menu_button":
print("Triggering scene capture")
scene_manager.request_scene_capture()

elif name == "trigger_click" and left_hand_pointer.visible:
if left_hand_pointer_raycast.is_colliding():
if selected_spatial_anchor_node:
var anchor_parent = selected_spatial_anchor_node.get_parent()
if anchor_parent is XRAnchor3D:
spatial_anchor_manager.untrack_anchor(anchor_parent.tracker)
else:
var anchor_transform := Transform3D()
anchor_transform.origin = left_hand_pointer_raycast.get_collision_point()

var collision_normal: Vector3 = left_hand_pointer_raycast.get_collision_normal()
if collision_normal.is_equal_approx(Vector3.UP):
anchor_transform.basis = anchor_transform.basis.rotated(Vector3(1.0, 0.0, 0.0), PI / 2.0)
elif collision_normal.is_equal_approx(Vector3.DOWN):
anchor_transform.basis = anchor_transform.basis.rotated(Vector3(1.0, 0.0, 0.0), -PI / 2.0)
else:
anchor_transform.basis = Basis.looking_at(left_hand_pointer_raycast.get_collision_normal())

print ("Attempting to create spatial anchor at: ", anchor_transform)
spatial_anchor_manager.create_anchor(anchor_transform, { color = COLORS[randi() % COLORS.size()] })


func _on_right_hand_button_pressed(name: String) -> void:
match name:
"by_button":
enable_passthrough(not passthrough_enabled)


func _on_left_controller_fb_render_model_render_model_loaded() -> void:
left_hand_mesh.hide()


func _on_right_controller_fb_render_model_render_model_loaded() -> void:
right_hand_mesh.hide()

func _on_scene_manager_scene_capture_completed(success: bool) -> void:
print("Scene Capture Complete: ", success)
if success:
# Recreate scene anchors since the user may have changed them.
if scene_manager.are_scene_anchors_created():
scene_manager.remove_scene_anchors()
scene_manager.create_scene_anchors()

# Switch to passthrough.
enable_passthrough(true)

func _on_scene_manager_scene_data_missing() -> void:
scene_manager.request_scene_capture()
46 changes: 1 addition & 45 deletions demo/main.tscn
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
[gd_scene load_steps=21 format=3 uid="uid://cqsodpswgup8w"]
[gd_scene load_steps=16 format=3 uid="uid://cqsodpswgup8w"]

[ext_resource type="Script" path="res://main.gd" id="1_fsva1"]
[ext_resource type="PackedScene" uid="uid://c0uv4eu2yjm3b" path="res://viewport_2d_in_3d.tscn" id="2_7whgo"]
[ext_resource type="PackedScene" uid="uid://d4b4rllli6tqp" path="res://tablet_content.tscn" id="3_45w5g"]
[ext_resource type="PackedScene" uid="uid://bcjp8kcgde4cs" path="res://scene_anchor.tscn" id="4_3u3ah"]
[ext_resource type="PackedScene" uid="uid://cay8oh2ll7yxi" path="res://assets/test_kun/Test-Kun.fbx" id="4_b317s"]
[ext_resource type="PackedScene" uid="uid://bwo5nq0clfe3c" path="res://scene_global_mesh.tscn" id="5_7ikgf"]
[ext_resource type="PackedScene" uid="uid://dsfd7xrm6o50p" path="res://spatial_anchor.tscn" id="5_g7mio"]
[ext_resource type="Material" uid="uid://bdwh0vc86hsdb" path="res://assets/hand_silhouette_outline_mat.tres" id="7_tpkib"]

[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_0x6cv"]
Expand Down Expand Up @@ -41,13 +38,6 @@ size = Vector3(0.1, 0.1, 0.1)
radius = 0.025
height = 0.05

[sub_resource type="BoxMesh" id="BoxMesh_d27jm"]
size = Vector3(0.01, 0.01, 5)

[sub_resource type="SphereMesh" id="SphereMesh_kdpqm"]
radius = 0.01
height = 0.02

[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_k604q"]

[sub_resource type="PlaneMesh" id="PlaneMesh_mjcgt"]
Expand Down Expand Up @@ -112,24 +102,6 @@ tracker = &"/user/eyes_ext"
transform = Transform3D(1, 0, 0, 0, -0.0133513, 0.999911, 0, -0.999911, -0.0133513, 0, 0, -1.18886)
mesh = SubResource("SphereMesh_5gcab")

[node name="LeftHandPointer" type="XRController3D" parent="XROrigin3D"]
visible = false
tracker = &"left_hand"
pose = &"aim"

[node name="ScenePointerMesh" type="MeshInstance3D" parent="XROrigin3D/LeftHandPointer"]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, -2.5)
mesh = SubResource("BoxMesh_d27jm")

[node name="SceneCollidingMesh" type="MeshInstance3D" parent="XROrigin3D/LeftHandPointer"]
mesh = SubResource("SphereMesh_kdpqm")

[node name="RayCast3D" type="RayCast3D" parent="XROrigin3D/LeftHandPointer"]
enabled = false
target_position = Vector3(0, 0, -10)
collision_mask = 6
collide_with_areas = true

[node name="LeftHandTracker" type="XRNode3D" parent="XROrigin3D"]
tracker = &"/user/hand_tracker/left"

Expand Down Expand Up @@ -174,16 +146,6 @@ bones/25/name = "RightLittleTip"
[node name="RightXRHandModifier3D" type="XRHandModifier3D" parent="XROrigin3D/RightHandTracker/RightHandModel"]
hand_tracker = &"/user/hand_tracker/right"

[node name="OpenXRFbSceneManager" type="OpenXRFbSceneManager" parent="XROrigin3D"]
default_scene = ExtResource("4_3u3ah")
auto_create = false
visible = false
scenes/global_mesh = ExtResource("5_7ikgf")

[node name="OpenXRFbSpatialAnchorManager" type="OpenXRFbSpatialAnchorManager" parent="XROrigin3D"]
scene = ExtResource("5_g7mio")
visible = false

[node name="Floor" type="MeshInstance3D" parent="."]
mesh = SubResource("PlaneMesh_mjcgt")

Expand All @@ -203,13 +165,7 @@ bone_update = 1
[node name="XRFaceModifier3D" type="XRFaceModifier3D" parent="Floor/AvatarOffset/Avatar"]
target = NodePath("../Test-Kun/Armature/GeneralSkeleton/Body")

[connection signal="button_pressed" from="XROrigin3D/LeftHand" to="." method="_on_left_hand_button_pressed"]
[connection signal="openxr_fb_render_model_loaded" from="XROrigin3D/LeftHand/LeftControllerFbRenderModel" to="." method="_on_left_controller_fb_render_model_render_model_loaded"]
[connection signal="button_pressed" from="XROrigin3D/RightHand" to="." method="_on_right_hand_button_pressed"]
[connection signal="openxr_fb_render_model_loaded" from="XROrigin3D/RightHand/RightControllerFbRenderModel" to="." method="_on_right_controller_fb_render_model_render_model_loaded"]
[connection signal="openxr_fb_scene_capture_completed" from="XROrigin3D/OpenXRFbSceneManager" to="." method="_on_scene_manager_scene_capture_completed"]
[connection signal="openxr_fb_scene_data_missing" from="XROrigin3D/OpenXRFbSceneManager" to="." method="_on_scene_manager_scene_data_missing"]
[connection signal="openxr_fb_spatial_anchor_tracked" from="XROrigin3D/OpenXRFbSpatialAnchorManager" to="." method="_on_spatial_anchor_tracked"]
[connection signal="openxr_fb_spatial_anchor_untracked" from="XROrigin3D/OpenXRFbSpatialAnchorManager" to="." method="_on_spatial_anchor_untracked"]

[editable path="Floor/AvatarOffset/Avatar/Test-Kun"]
50 changes: 0 additions & 50 deletions demo/spatial_anchor.gd

This file was deleted.

Empty file added samples/builds/.gdignore
Empty file.
8 changes: 8 additions & 0 deletions samples/fb-scene-sample/assets/cross-grid-material.tres
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[gd_resource type="StandardMaterial3D" load_steps=2 format=3 uid="uid://c2552u4qmlssh"]

[ext_resource type="Texture2D" uid="uid://d0or78w3b0xev" path="res://assets/cross-grid.png" id="1_8nicf"]

[resource]
transparency = 1
shading_mode = 0
albedo_texture = ExtResource("1_8nicf")
File renamed without changes
Loading

0 comments on commit 6803157

Please sign in to comment.