From 1271986faf38383aa679c951df925539adc9272d Mon Sep 17 00:00:00 2001 From: Nitwel Date: Mon, 23 Oct 2023 01:58:18 +0200 Subject: [PATCH] basic light implementation --- light.gd | 27 +++++++++++++++++++++++++ main.tscn | 55 +++++++++++++++++++++++++++++++++++++++++++++----- project.godot | 2 ++ src/model.gd | 23 +++++++++++++++++++++ src/raycast.gd | 27 +++++++++++++++++++++++++ 5 files changed, 129 insertions(+), 5 deletions(-) create mode 100644 light.gd create mode 100644 src/model.gd create mode 100644 src/raycast.gd diff --git a/light.gd b/light.gd new file mode 100644 index 0000000..cc8d680 --- /dev/null +++ b/light.gd @@ -0,0 +1,27 @@ +extends StaticBody3D + +@onready var http_request = HTTPRequest.new() +@export var id = "switch.plug_printer_2" +@export var token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiIzZjQ0ZGM2N2Y3YzY0MDc1OGZlMWI2ZjJlNmIxZjRkNSIsImlhdCI6MTY5ODAxMDcyOCwiZXhwIjoyMDEzMzcwNzI4fQ.K6ydLUC-4Q7BNIRCU1nWlI2s6sg9UCiOu-Lpedw2zJc" + +# Called when the node enters the scene tree for the first time. +func _ready(): + add_child(http_request) + http_request.request_completed.connect(self._on_request_completed) + + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + pass + +func _on_toggle(): + print("Toggling " + id) + var headers = PackedStringArray(["Authorization: Bearer " + token, "Content-Type: application/json"]) + + var error = http_request.request("http://192.168.33.33:8123/api/services/switch/toggle", headers, HTTPClient.METHOD_POST, "{\"entity_id\": \"" + id + "\"}") + + if error != OK: + push_error("An error occurred in the HTTP request.") + +func _on_request_completed(): + pass diff --git a/main.tscn b/main.tscn index 96fbf37..e79d8fc 100644 --- a/main.tscn +++ b/main.tscn @@ -1,24 +1,55 @@ -[gd_scene load_steps=6 format=3 uid="uid://eecv28y6jxk4"] +[gd_scene load_steps=12 format=3 uid="uid://eecv28y6jxk4"] [ext_resource type="PackedScene" uid="uid://clc5dre31iskm" path="res://addons/godot-xr-tools/xr/start_xr.tscn" id="1_i4c04"] +[ext_resource type="Script" path="res://src/raycast.gd" id="1_tsqxc"] +[ext_resource type="Script" path="res://src/model.gd" id="2_7f1x4"] +[ext_resource type="Script" path="res://light.gd" id="4_bm5yj"] + +[sub_resource type="SphereShape3D" id="SphereShape3D_2igmd"] +radius = 0.1 + +[sub_resource type="SphereMesh" id="SphereMesh_eme7e"] +radius = 0.1 +height = 0.2 + +[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_m58yb"] +ao_enabled = true [sub_resource type="BoxMesh" id="BoxMesh_ir3co"] -size = Vector3(0.1, 0.1, 0.1) +material = SubResource("StandardMaterial3D_m58yb") +size = Vector3(0.01, 0.01, 0.01) [sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_p6hri"] -sky_top_color = Color(0.384314, 0.454902, 0.54902, 1) +sky_top_color = Color(0, 0, 0, 1) +sky_horizon_color = Color(0, 0, 0, 1) +sky_curve = 9.13367e-05 +sky_cover_modulate = Color(0, 0, 0, 1) +ground_bottom_color = Color(0, 0, 0, 1) +ground_horizon_color = Color(0, 0, 0, 1) use_debanding = false [sub_resource type="Sky" id="Sky_vhymk"] sky_material = SubResource("ProceduralSkyMaterial_p6hri") [sub_resource type="Environment" id="Environment_7ghp0"] -background_mode = 1 -background_color = Color(1, 1, 1, 0) +background_mode = 2 +background_color = Color(0.466667, 0.47451, 0.462745, 0) sky = SubResource("Sky_vhymk") +ambient_light_color = Color(1, 1, 1, 1) +ambient_light_sky_contribution = 0.72 [node name="Main" type="Node3D"] +[node name="Light" type="StaticBody3D" parent="."] +transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 1.45064, 0, 0.20152) +script = ExtResource("4_bm5yj") + +[node name="CollisionShape3D" type="CollisionShape3D" parent="Light"] +shape = SubResource("SphereShape3D_2igmd") + +[node name="MeshInstance3D" type="MeshInstance3D" parent="Light"] +mesh = SubResource("SphereMesh_eme7e") + [node name="XROrigin3D" type="XROrigin3D" parent="."] [node name="XRCamera3D" type="XRCamera3D" parent="XROrigin3D"] @@ -32,6 +63,10 @@ pose = &"aim" [node name="MeshInstance3D" type="MeshInstance3D" parent="XROrigin3D/XRControllerLeft"] mesh = SubResource("BoxMesh_ir3co") +[node name="Model" type="Node3D" parent="XROrigin3D/XRControllerLeft" node_paths=PackedStringArray("light")] +script = ExtResource("2_7f1x4") +light = NodePath("../../../Light") + [node name="XRControllerRight" type="XRController3D" parent="XROrigin3D"] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.488349, 0.559219, -0.2988) tracker = &"right_hand" @@ -40,6 +75,14 @@ pose = &"aim" [node name="MeshInstance3D" type="MeshInstance3D" parent="XROrigin3D/XRControllerRight"] mesh = SubResource("BoxMesh_ir3co") +[node name="Raycast" type="Node3D" parent="XROrigin3D/XRControllerRight" node_paths=PackedStringArray("ray")] +script = ExtResource("1_tsqxc") +ray = NodePath("RayCast3D") + +[node name="RayCast3D" type="RayCast3D" parent="XROrigin3D/XRControllerRight/Raycast"] +transform = Transform3D(1.91069e-15, 4.37114e-08, 1, 1, -4.37114e-08, 0, 4.37114e-08, 1, -4.37114e-08, 0, 0, 0) +target_position = Vector3(0, -5, 0) + [node name="StartXR" parent="." instance=ExtResource("1_i4c04")] enable_passthrough = true @@ -47,3 +90,5 @@ enable_passthrough = true environment = SubResource("Environment_7ghp0") [node name="DirectionalLight3D" type="DirectionalLight3D" parent="."] +transform = Transform3D(0.834925, -0.386727, -0.39159, 0.550364, 0.586681, 0.594058, 0, -0.711511, 0.702675, 0, 7.21041, 2.06458) +shadow_enabled = true diff --git a/project.godot b/project.godot index 2fbc9db..482aeb0 100644 --- a/project.godot +++ b/project.godot @@ -27,6 +27,8 @@ enabled=PackedStringArray("res://addons/godot-xr-tools/plugin.cfg") renderer/rendering_method="mobile" textures/vram_compression/import_etc2_astc=true +lights_and_shadows/directional_shadow/soft_shadow_filter_quality=4 +lights_and_shadows/directional_shadow/soft_shadow_filter_quality.mobile=4 [xr] diff --git a/src/model.gd b/src/model.gd new file mode 100644 index 0000000..4eeb541 --- /dev/null +++ b/src/model.gd @@ -0,0 +1,23 @@ +extends Node3D + +@onready var _controller := XRHelpers.get_xr_controller(self) +@export var light: StaticBody3D + +# Called when the node enters the scene tree for the first time. +func _ready(): + _controller.button_pressed.connect(self._on_button_pressed) + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + pass + +func _on_button_pressed(button): + print("right: ", button) + if button != "trigger_click": + return + + if light == null: + return + + # set light position to controller position + light.transform.origin = _controller.transform.origin diff --git a/src/raycast.gd b/src/raycast.gd new file mode 100644 index 0000000..badbf27 --- /dev/null +++ b/src/raycast.gd @@ -0,0 +1,27 @@ +extends Node3D + +@onready var _controller := XRHelpers.get_xr_controller(self) +@export var ray: RayCast3D + +# Called when the node enters the scene tree for the first time. +func _ready(): + _controller.button_pressed.connect(self._on_button_pressed) + +# Called every frame. 'delta' is the elapsed time since the previous frame. +func _process(delta): + pass + +func _on_button_pressed(button): + print(button) + if button != "trigger_click": + return + + var collider = ray.get_collider() + + if collider == null: + return + + print(collider) + + if collider.has_method("_on_toggle"): + collider._on_toggle()