edbeeching HF staff commited on
Commit
0cbdcec
1 Parent(s): 0a35afa

Upload folder using huggingface_hub

Browse files
AIController3D.gd ADDED
@@ -0,0 +1,40 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends AIController3D
2
+
3
+
4
+ func _physics_process(_delta):
5
+ n_steps += 1
6
+ if n_steps >= reset_after:
7
+ done = true
8
+ needs_reset = true
9
+
10
+ if needs_reset:
11
+ reset()
12
+
13
+
14
+ func set_action(action):
15
+ _player.move_action = action["move"][0]
16
+ _player.turn_action = action["turn"][0]
17
+
18
+
19
+ func get_obs():
20
+ return {
21
+ "camera_2d": _player.virtual_camera.get_camera_pixel_encoding(),
22
+ }
23
+
24
+
25
+ func get_obs_space():
26
+ # types of obs space: box, discrete, repeated
27
+ return {
28
+ "camera_2d": {"size": _player.virtual_camera.get_camera_shape(), "space": "box"},
29
+ }
30
+
31
+
32
+ func get_action_space():
33
+ return {
34
+ "move": {"size": 1, "action_type": "continuous"},
35
+ "turn": {"size": 1, "action_type": "continuous"}
36
+ }
37
+
38
+
39
+ func get_reward():
40
+ return reward
ColorRoom.tscn CHANGED
@@ -1,118 +1,119 @@
1
- [gd_scene load_steps=12 format=2]
2
 
3
- [ext_resource path="res://Player.tscn" type="PackedScene" id=1]
4
 
5
- [sub_resource type="CubeMesh" id=1]
6
- size = Vector3( 20, 0.1, 20 )
7
 
8
- [sub_resource type="SpatialMaterial" id=9]
9
- albedo_color = Color( 0.145098, 0.145098, 0.145098, 1 )
 
10
 
11
- [sub_resource type="ConcavePolygonShape" id=2]
12
- data = PoolVector3Array( -10, 0.05, 10, 10, 0.05, 10, -10, -0.05, 10, 10, 0.05, 10, 10, -0.05, 10, -10, -0.05, 10, 10, 0.05, -10, -10, 0.05, -10, 10, -0.05, -10, -10, 0.05, -10, -10, -0.05, -10, 10, -0.05, -10, 10, 0.05, 10, 10, 0.05, -10, 10, -0.05, 10, 10, 0.05, -10, 10, -0.05, -10, 10, -0.05, 10, -10, 0.05, -10, -10, 0.05, 10, -10, -0.05, -10, -10, 0.05, 10, -10, -0.05, 10, -10, -0.05, -10, 10, 0.05, 10, -10, 0.05, 10, 10, 0.05, -10, -10, 0.05, 10, -10, 0.05, -10, 10, 0.05, -10, -10, -0.05, 10, 10, -0.05, 10, -10, -0.05, -10, 10, -0.05, 10, 10, -0.05, -10, -10, -0.05, -10 )
13
 
14
- [sub_resource type="CubeMesh" id=3]
15
- size = Vector3( 20, 5, 2 )
16
 
17
- [sub_resource type="SpatialMaterial" id=8]
18
- albedo_color = Color( 0.541176, 0.247059, 0.760784, 1 )
19
 
20
- [sub_resource type="ConcavePolygonShape" id=4]
21
- data = PoolVector3Array( -10, 2.5, 1, 10, 2.5, 1, -10, -2.5, 1, 10, 2.5, 1, 10, -2.5, 1, -10, -2.5, 1, 10, 2.5, -1, -10, 2.5, -1, 10, -2.5, -1, -10, 2.5, -1, -10, -2.5, -1, 10, -2.5, -1, 10, 2.5, 1, 10, 2.5, -1, 10, -2.5, 1, 10, 2.5, -1, 10, -2.5, -1, 10, -2.5, 1, -10, 2.5, -1, -10, 2.5, 1, -10, -2.5, -1, -10, 2.5, 1, -10, -2.5, 1, -10, -2.5, -1, 10, 2.5, 1, -10, 2.5, 1, 10, 2.5, -1, -10, 2.5, 1, -10, 2.5, -1, 10, 2.5, -1, -10, -2.5, 1, 10, -2.5, 1, -10, -2.5, -1, 10, -2.5, 1, 10, -2.5, -1, -10, -2.5, -1 )
22
 
23
- [sub_resource type="SpatialMaterial" id=7]
24
- albedo_color = Color( 0.0627451, 0.670588, 0.215686, 1 )
25
 
26
- [sub_resource type="SpatialMaterial" id=6]
27
- albedo_color = Color( 0.137255, 0.34902, 0.909804, 1 )
28
 
29
- [sub_resource type="SpatialMaterial" id=5]
30
- albedo_color = Color( 0.784314, 0.129412, 0.129412, 1 )
31
 
32
- [sub_resource type="BoxShape" id=10]
33
- extents = Vector3( 0.4, 1, 8.97105 )
34
 
35
- [node name="ColorRoom" type="Spatial"]
36
 
37
- [node name="Player" parent="." instance=ExtResource( 1 )]
 
38
 
39
- [node name="Camera" type="Camera" parent="Player"]
40
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.7, -0.199591 )
41
 
42
- [node name="Walls" type="Spatial" parent="."]
43
 
44
- [node name="MeshInstance" type="MeshInstance" parent="Walls"]
45
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.05, 0 )
46
- mesh = SubResource( 1 )
47
  skeleton = NodePath("../..")
48
- material/0 = SubResource( 9 )
49
 
50
- [node name="StaticBody" type="StaticBody" parent="Walls/MeshInstance"]
51
 
52
- [node name="CollisionShape" type="CollisionShape" parent="Walls/MeshInstance/StaticBody"]
53
- shape = SubResource( 2 )
54
 
55
- [node name="MeshInstance2" type="MeshInstance" parent="Walls"]
56
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.5, -11 )
57
- mesh = SubResource( 3 )
58
  skeleton = NodePath("../..")
59
- material/0 = SubResource( 8 )
60
 
61
- [node name="StaticBody" type="StaticBody" parent="Walls/MeshInstance2"]
62
 
63
- [node name="CollisionShape" type="CollisionShape" parent="Walls/MeshInstance2/StaticBody"]
64
- shape = SubResource( 4 )
65
 
66
- [node name="MeshInstance3" type="MeshInstance" parent="Walls"]
67
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.5, 11 )
68
- mesh = SubResource( 3 )
69
  skeleton = NodePath("../..")
70
- material/0 = SubResource( 7 )
71
 
72
- [node name="StaticBody" type="StaticBody" parent="Walls/MeshInstance3"]
73
 
74
- [node name="CollisionShape" type="CollisionShape" parent="Walls/MeshInstance3/StaticBody"]
75
- shape = SubResource( 4 )
76
 
77
- [node name="MeshInstance4" type="MeshInstance" parent="Walls"]
78
- transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 11, 2.5, 0 )
79
- mesh = SubResource( 3 )
80
  skeleton = NodePath("../..")
81
- material/0 = SubResource( 6 )
82
 
83
- [node name="StaticBody" type="StaticBody" parent="Walls/MeshInstance4"]
84
 
85
- [node name="CollisionShape" type="CollisionShape" parent="Walls/MeshInstance4/StaticBody"]
86
- shape = SubResource( 4 )
87
 
88
- [node name="MeshInstance5" type="MeshInstance" parent="Walls"]
89
- transform = Transform( -4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -11, 2.5, 0 )
90
- mesh = SubResource( 3 )
91
  skeleton = NodePath("../..")
92
- material/0 = SubResource( 5 )
93
 
94
- [node name="StaticBody" type="StaticBody" parent="Walls/MeshInstance5"]
95
 
96
- [node name="CollisionShape" type="CollisionShape" parent="Walls/MeshInstance5/StaticBody"]
97
- shape = SubResource( 4 )
98
 
99
- [node name="Goals" type="Spatial" parent="."]
100
 
101
- [node name="NegativeGoal" type="Area" parent="Goals"]
102
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -8.71839, 0.0416299, -0.292424 )
103
  collision_layer = 0
104
 
105
- [node name="CollisionShape" type="CollisionShape" parent="Goals/NegativeGoal"]
106
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.47999, 0 )
107
- shape = SubResource( 10 )
108
 
109
- [node name="PositiveGoal" type="Area" parent="Goals"]
110
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 8.93574, -0.068549, -0.292424 )
111
  collision_layer = 0
112
 
113
- [node name="CollisionShape" type="CollisionShape" parent="Goals/PositiveGoal"]
114
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.47999, 0 )
115
- shape = SubResource( 10 )
116
 
117
  [connection signal="body_entered" from="Goals/NegativeGoal" to="Player" method="_on_NegativeGoal_body_entered"]
118
  [connection signal="body_entered" from="Goals/PositiveGoal" to="Player" method="_on_PositiveGoal_body_entered"]
 
1
+ [gd_scene load_steps=12 format=3 uid="uid://bf3qfs06lw0gv"]
2
 
3
+ [ext_resource type="PackedScene" uid="uid://dwipllxvgnle4" path="res://Player.tscn" id="1"]
4
 
5
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_ell48"]
6
+ albedo_color = Color(1, 1, 0.705882, 1)
7
 
8
+ [sub_resource type="BoxMesh" id="1"]
9
+ material = SubResource("StandardMaterial3D_ell48")
10
+ size = Vector3(20, 0.1, 20)
11
 
12
+ [sub_resource type="ConcavePolygonShape3D" id="2"]
13
+ data = PackedVector3Array(-10, 0.05, 10, 10, 0.05, 10, -10, -0.05, 10, 10, 0.05, 10, 10, -0.05, 10, -10, -0.05, 10, 10, 0.05, -10, -10, 0.05, -10, 10, -0.05, -10, -10, 0.05, -10, -10, -0.05, -10, 10, -0.05, -10, 10, 0.05, 10, 10, 0.05, -10, 10, -0.05, 10, 10, 0.05, -10, 10, -0.05, -10, 10, -0.05, 10, -10, 0.05, -10, -10, 0.05, 10, -10, -0.05, -10, -10, 0.05, 10, -10, -0.05, 10, -10, -0.05, -10, 10, 0.05, 10, -10, 0.05, 10, 10, 0.05, -10, -10, 0.05, 10, -10, 0.05, -10, 10, 0.05, -10, -10, -0.05, 10, 10, -0.05, 10, -10, -0.05, -10, 10, -0.05, 10, 10, -0.05, -10, -10, -0.05, -10)
14
 
15
+ [sub_resource type="BoxMesh" id="3"]
16
+ size = Vector3(20, 5, 2)
17
 
18
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_1ci10"]
19
+ albedo_color = Color(0, 1, 0.184314, 1)
20
 
21
+ [sub_resource type="ConcavePolygonShape3D" id="4"]
22
+ data = PackedVector3Array(-10, 2.5, 1, 10, 2.5, 1, -10, -2.5, 1, 10, 2.5, 1, 10, -2.5, 1, -10, -2.5, 1, 10, 2.5, -1, -10, 2.5, -1, 10, -2.5, -1, -10, 2.5, -1, -10, -2.5, -1, 10, -2.5, -1, 10, 2.5, 1, 10, 2.5, -1, 10, -2.5, 1, 10, 2.5, -1, 10, -2.5, -1, 10, -2.5, 1, -10, 2.5, -1, -10, 2.5, 1, -10, -2.5, -1, -10, 2.5, 1, -10, -2.5, 1, -10, -2.5, -1, 10, 2.5, 1, -10, 2.5, 1, 10, 2.5, -1, -10, 2.5, 1, -10, 2.5, -1, 10, 2.5, -1, -10, -2.5, 1, 10, -2.5, 1, -10, -2.5, -1, 10, -2.5, 1, 10, -2.5, -1, -10, -2.5, -1)
23
 
24
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_rnma1"]
25
+ albedo_color = Color(1, 0.34902, 1, 1)
26
 
27
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_jlxwb"]
28
+ albedo_color = Color(0.266667, 0.431373, 1, 1)
29
 
30
+ [sub_resource type="StandardMaterial3D" id="StandardMaterial3D_nulvw"]
31
+ albedo_color = Color(1, 0.258824, 0.270588, 1)
32
 
33
+ [sub_resource type="BoxShape3D" id="10"]
34
+ size = Vector3(0.8, 2, 17.9421)
35
 
36
+ [node name="ColorRoom" type="Node3D"]
37
 
38
+ [node name="Player" parent="." instance=ExtResource("1")]
39
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2, 0)
40
 
41
+ [node name="Camera" type="Camera3D" parent="Player"]
42
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.7, -0.199591)
43
 
44
+ [node name="Walls" type="Node3D" parent="."]
45
 
46
+ [node name="MeshInstance" type="MeshInstance3D" parent="Walls"]
47
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0.05, 0)
48
+ mesh = SubResource("1")
49
  skeleton = NodePath("../..")
 
50
 
51
+ [node name="StaticBody" type="StaticBody3D" parent="Walls/MeshInstance"]
52
 
53
+ [node name="CollisionShape" type="CollisionShape3D" parent="Walls/MeshInstance/StaticBody"]
54
+ shape = SubResource("2")
55
 
56
+ [node name="MeshInstance2" type="MeshInstance3D" parent="Walls"]
57
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.5, -11)
58
+ mesh = SubResource("3")
59
  skeleton = NodePath("../..")
60
+ surface_material_override/0 = SubResource("StandardMaterial3D_1ci10")
61
 
62
+ [node name="StaticBody" type="StaticBody3D" parent="Walls/MeshInstance2"]
63
 
64
+ [node name="CollisionShape" type="CollisionShape3D" parent="Walls/MeshInstance2/StaticBody"]
65
+ shape = SubResource("4")
66
 
67
+ [node name="MeshInstance3" type="MeshInstance3D" parent="Walls"]
68
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.5, 11)
69
+ mesh = SubResource("3")
70
  skeleton = NodePath("../..")
71
+ surface_material_override/0 = SubResource("StandardMaterial3D_rnma1")
72
 
73
+ [node name="StaticBody" type="StaticBody3D" parent="Walls/MeshInstance3"]
74
 
75
+ [node name="CollisionShape" type="CollisionShape3D" parent="Walls/MeshInstance3/StaticBody"]
76
+ shape = SubResource("4")
77
 
78
+ [node name="MeshInstance4" type="MeshInstance3D" parent="Walls"]
79
+ transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, 11, 2.5, 0)
80
+ mesh = SubResource("3")
81
  skeleton = NodePath("../..")
82
+ surface_material_override/0 = SubResource("StandardMaterial3D_jlxwb")
83
 
84
+ [node name="StaticBody" type="StaticBody3D" parent="Walls/MeshInstance4"]
85
 
86
+ [node name="CollisionShape" type="CollisionShape3D" parent="Walls/MeshInstance4/StaticBody"]
87
+ shape = SubResource("4")
88
 
89
+ [node name="MeshInstance5" type="MeshInstance3D" parent="Walls"]
90
+ transform = Transform3D(-4.37114e-08, 0, 1, 0, 1, 0, -1, 0, -4.37114e-08, -11, 2.5, 0)
91
+ mesh = SubResource("3")
92
  skeleton = NodePath("../..")
93
+ surface_material_override/0 = SubResource("StandardMaterial3D_nulvw")
94
 
95
+ [node name="StaticBody" type="StaticBody3D" parent="Walls/MeshInstance5"]
96
 
97
+ [node name="CollisionShape" type="CollisionShape3D" parent="Walls/MeshInstance5/StaticBody"]
98
+ shape = SubResource("4")
99
 
100
+ [node name="Goals" type="Node3D" parent="."]
101
 
102
+ [node name="NegativeGoal" type="Area3D" parent="Goals"]
103
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -8.71839, 0.0416299, -0.292424)
104
  collision_layer = 0
105
 
106
+ [node name="CollisionShape" type="CollisionShape3D" parent="Goals/NegativeGoal"]
107
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.47999, 0)
108
+ shape = SubResource("10")
109
 
110
+ [node name="PositiveGoal" type="Area3D" parent="Goals"]
111
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 8.93574, -0.068549, -0.292424)
112
  collision_layer = 0
113
 
114
+ [node name="CollisionShape" type="CollisionShape3D" parent="Goals/PositiveGoal"]
115
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.47999, 0)
116
+ shape = SubResource("10")
117
 
118
  [connection signal="body_entered" from="Goals/NegativeGoal" to="Player" method="_on_NegativeGoal_body_entered"]
119
  [connection signal="body_entered" from="Goals/PositiveGoal" to="Player" method="_on_PositiveGoal_body_entered"]
Env.tscn CHANGED
@@ -1,62 +1,62 @@
1
- [gd_scene load_steps=3 format=2]
2
 
3
- [ext_resource path="res://ColorRoom.tscn" type="PackedScene" id=1]
4
- [ext_resource path="res://addons/godot_rl_agents/sync.gd" type="Script" id=2]
5
 
6
- [node name="Env" type="Spatial"]
7
 
8
- [node name="ColorRoom" parent="." instance=ExtResource( 1 )]
9
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, 25 )
10
 
11
- [node name="ColorRoom2" parent="." instance=ExtResource( 1 )]
12
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, 0 )
13
 
14
- [node name="ColorRoom3" parent="." instance=ExtResource( 1 )]
15
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, -25 )
16
 
17
- [node name="ColorRoom4" parent="." instance=ExtResource( 1 )]
18
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, -50 )
19
 
20
- [node name="ColorRoom5" parent="." instance=ExtResource( 1 )]
21
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 13, 0, 25 )
22
 
23
- [node name="ColorRoom6" parent="." instance=ExtResource( 1 )]
24
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 13, 0, 0 )
25
 
26
- [node name="ColorRoom7" parent="." instance=ExtResource( 1 )]
27
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 13, 0, -25 )
28
 
29
- [node name="ColorRoom8" parent="." instance=ExtResource( 1 )]
30
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 13, 0, -50 )
31
 
32
- [node name="ColorRoom9" parent="." instance=ExtResource( 1 )]
33
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 38, 0, 25 )
34
 
35
- [node name="ColorRoom10" parent="." instance=ExtResource( 1 )]
36
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 38, 0, 0 )
37
 
38
- [node name="ColorRoom11" parent="." instance=ExtResource( 1 )]
39
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 38, 0, -25 )
40
 
41
- [node name="ColorRoom12" parent="." instance=ExtResource( 1 )]
42
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 38, 0, -50 )
43
 
44
- [node name="ColorRoom13" parent="." instance=ExtResource( 1 )]
45
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 63, 0, 25 )
46
 
47
- [node name="ColorRoom14" parent="." instance=ExtResource( 1 )]
48
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 63, 0, 0 )
49
 
50
- [node name="ColorRoom15" parent="." instance=ExtResource( 1 )]
51
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 63, 0, -25 )
52
 
53
- [node name="ColorRoom16" parent="." instance=ExtResource( 1 )]
54
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 63, 0, -50 )
55
 
56
  [node name="Sync" type="Node" parent="."]
57
  process_priority = -1
58
- script = ExtResource( 2 )
59
 
60
- [node name="Camera" type="Camera" parent="."]
61
- transform = Transform( 1, 0, 0, 0, 0.0220418, 0.999757, 0, -0.999757, 0.0220418, 25.3538, 75.4275, -10.0795 )
62
  current = true
 
1
+ [gd_scene load_steps=3 format=3 uid="uid://b6watdl4c1jdq"]
2
 
3
+ [ext_resource type="PackedScene" uid="uid://bf3qfs06lw0gv" path="res://ColorRoom.tscn" id="1"]
4
+ [ext_resource type="Script" path="res://addons/godot_rl_agents/sync.gd" id="2"]
5
 
6
+ [node name="Env" type="Node3D"]
7
 
8
+ [node name="ColorRoom" parent="." instance=ExtResource("1")]
9
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, 25)
10
 
11
+ [node name="ColorRoom2" parent="." instance=ExtResource("1")]
12
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, 0)
13
 
14
+ [node name="ColorRoom3" parent="." instance=ExtResource("1")]
15
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, -25)
16
 
17
+ [node name="ColorRoom4" parent="." instance=ExtResource("1")]
18
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, -50)
19
 
20
+ [node name="ColorRoom5" parent="." instance=ExtResource("1")]
21
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 13, 0, 25)
22
 
23
+ [node name="ColorRoom6" parent="." instance=ExtResource("1")]
24
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 13, 0, 0)
25
 
26
+ [node name="ColorRoom7" parent="." instance=ExtResource("1")]
27
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 13, 0, -25)
28
 
29
+ [node name="ColorRoom8" parent="." instance=ExtResource("1")]
30
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 13, 0, -50)
31
 
32
+ [node name="ColorRoom9" parent="." instance=ExtResource("1")]
33
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 38, 0, 25)
34
 
35
+ [node name="ColorRoom10" parent="." instance=ExtResource("1")]
36
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 38, 0, 0)
37
 
38
+ [node name="ColorRoom11" parent="." instance=ExtResource("1")]
39
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 38, 0, -25)
40
 
41
+ [node name="ColorRoom12" parent="." instance=ExtResource("1")]
42
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 38, 0, -50)
43
 
44
+ [node name="ColorRoom13" parent="." instance=ExtResource("1")]
45
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 63, 0, 25)
46
 
47
+ [node name="ColorRoom14" parent="." instance=ExtResource("1")]
48
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 63, 0, 0)
49
 
50
+ [node name="ColorRoom15" parent="." instance=ExtResource("1")]
51
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 63, 0, -25)
52
 
53
+ [node name="ColorRoom16" parent="." instance=ExtResource("1")]
54
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 63, 0, -50)
55
 
56
  [node name="Sync" type="Node" parent="."]
57
  process_priority = -1
58
+ script = ExtResource("2")
59
 
60
+ [node name="Camera" type="Camera3D" parent="."]
61
+ transform = Transform3D(1, 0, 0, 0, 0.0220418, 0.999757, 0, -0.999757, 0.0220418, 25.3538, 75.4275, -10.0795)
62
  current = true
Env2.tscn CHANGED
@@ -1,17 +1,17 @@
1
- [gd_scene load_steps=3 format=2]
2
 
3
- [ext_resource path="res://ColorRoom.tscn" type="PackedScene" id=1]
4
- [ext_resource path="res://addons/godot_rl_agents/sync.gd" type="Script" id=2]
5
 
6
- [node name="Env" type="Spatial"]
7
 
8
- [node name="T-Maze" parent="." instance=ExtResource( 1 )]
9
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, 25 )
10
 
11
  [node name="Sync" type="Node" parent="."]
12
  process_priority = -1
13
- script = ExtResource( 2 )
14
 
15
- [node name="Camera" type="Camera" parent="."]
16
- transform = Transform( 1, 0, 0, 0, 0.0220418, 0.999757, 0, -0.999757, 0.0220418, 25.3538, 75.4275, -10.0795 )
17
  current = true
 
1
+ [gd_scene load_steps=3 format=3 uid="uid://dvo48erm7rfmf"]
2
 
3
+ [ext_resource type="PackedScene" uid="uid://bf3qfs06lw0gv" path="res://ColorRoom.tscn" id="1"]
4
+ [ext_resource type="Script" path="res://addons/godot_rl_agents/sync.gd" id="2"]
5
 
6
+ [node name="Env" type="Node3D"]
7
 
8
+ [node name="T-Maze" parent="." instance=ExtResource("1")]
9
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -12, 0, 25)
10
 
11
  [node name="Sync" type="Node" parent="."]
12
  process_priority = -1
13
+ script = ExtResource("2")
14
 
15
+ [node name="Camera" type="Camera3D" parent="."]
16
+ transform = Transform3D(1, 0, 0, 0, 0.0220418, 0.999757, 0, -0.999757, 0.0220418, 25.3538, 75.4275, -10.0795)
17
  current = true
Player.gd CHANGED
@@ -1,218 +1,138 @@
1
- extends KinematicBody
2
  class_name Player
3
  const MOVE_SPEED = 12
4
  const JUMP_FORCE = 30
5
  const GRAVITY = 0.98
6
  const MAX_FALL_SPEED = 30
7
  const TURN_SENS = 2.0
8
- const MAX_STEPS = 10000
9
-
10
- onready var cam = $Camera
11
  var move_vec = Vector3()
12
  var y_velo = 0
13
 
14
  # RL related variables
15
- onready var robot = $Robot
16
- onready var virtual_camera = $RGBCameraSensor3D
 
17
 
18
  var next = 1
19
- var done = false
20
- var needs_reset = false
21
  var just_reached_negative = false
22
  var just_reached_positive = false
23
  var just_fell_off = false
24
  var best_goal_distance := 10000.0
25
  var grounded := false
26
- var _heuristic := "player"
27
  var move_action := 0.0
28
  var turn_action := 0.0
29
  var jump_action := false
30
- var n_steps = 0
31
 
32
- var reward = 0.0
33
 
34
  func _ready():
35
- return
36
- #reset()
37
 
38
  func _physics_process(_delta):
39
-
40
- n_steps +=1
41
- if n_steps >= MAX_STEPS:
42
- done = true
43
- needs_reset = true
44
-
45
- if needs_reset:
46
- needs_reset = false
47
- reset()
48
- return
49
-
50
- move_vec *= 0
51
- move_vec = get_move_vec()
52
- #move_vec = move_vec.normalized()
53
-
54
- move_vec = move_vec.rotated(Vector3(0, 1, 0), rotation.y)
55
- move_vec *= MOVE_SPEED
56
- move_vec.y = y_velo
57
- move_and_slide(move_vec, Vector3(0, 1, 0))
58
-
59
- # turning
60
-
61
- var turn_vec = get_turn_vec()
62
- rotation_degrees.y += turn_vec*TURN_SENS
63
-
64
- grounded = is_on_floor()
65
-
66
- y_velo -= GRAVITY
67
- var just_jumped = false
68
-
69
- if grounded and y_velo <= 0:
70
- y_velo = -0.1
71
- if y_velo < -MAX_FALL_SPEED:
72
- y_velo = -MAX_FALL_SPEED
73
-
74
- if y_velo < 0 and !grounded :
75
- robot.set_animation("falling-cycle")
76
-
77
- var horizontal_speed = Vector2(move_vec.x, move_vec.z)
78
- if horizontal_speed.length() < 0.1 and grounded:
79
- robot.set_animation("idle")
80
- elif horizontal_speed.length() >=1.0 and grounded:
81
- robot.set_animation("walk-cycle")
82
  # elif horizontal_speed.length() >= 1.0 and grounded:
83
  # robot.set_animation("run-cycle")
84
-
85
- update_reward()
86
-
87
- if Input.is_action_just_pressed("r_key"):
88
- reset()
89
-
90
 
91
  func get_move_vec() -> Vector3:
92
- if done:
93
- move_vec = Vector3.ZERO
94
- return move_vec
95
-
96
- if _heuristic == "model":
97
- return Vector3(
98
- 0,
99
- 0,
100
- clamp(move_action, -1.0, 0.5)
101
- )
102
-
103
- var move_vec := Vector3(
104
- 0,
105
- 0,
106
- clamp(Input.get_action_strength("move_backwards") - Input.get_action_strength("move_forwards"),-1.0, 0.5)
107
-
108
- )
109
- return move_vec
 
110
 
111
  func get_turn_vec() -> float:
112
- if _heuristic == "model":
113
- return turn_action
114
- var rotation_amount = Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
 
 
 
 
115
 
116
- return rotation_amount
117
 
118
-
119
  func reset():
120
- needs_reset = false
121
- next = 1
122
- n_steps = 0
123
- #done = false
124
- just_reached_negative = false
125
- just_reached_positive = false
126
- jump_action = false
127
- # Replace with function body.
128
- set_translation(Vector3(0,1.5,0))
129
- rotation_degrees.y = rand_range(-180,180)
130
- y_velo = 0.1
131
-
132
- func set_action(action):
133
- move_action = action["move"][0]
134
- turn_action = action["turn"][0]
135
-
136
- func reset_if_done():
137
- if done:
138
- reset()
139
-
140
- func get_obs():
141
- #print(virtual_camera.get_camera_pixel_encoding())
142
- return {
143
- "camera_2d": virtual_camera.get_camera_pixel_encoding(),
144
- }
145
-
146
- func get_obs_space():
147
- # typs of obs space: box, discrete, repeated
148
- return {
149
- "camera_2d":{
150
- "size": virtual_camera.get_camera_shape(),
151
- "space":"box"
152
- },
153
- }
154
-
155
-
156
  func update_reward():
157
- reward -= 0.01 # step penalty
158
- reward += shaping_reward()
159
-
160
-
161
-
162
- func get_reward():
163
- return reward
164
-
165
- func zero_reward():
166
- reward = 0.0
167
-
168
- func shaping_reward():
169
- var s_reward = 0.0
170
- return s_reward
171
-
172
-
173
- func set_heuristic(heuristic):
174
- self._heuristic = heuristic
175
-
176
- func get_obs_size():
177
- return len(get_obs())
178
-
179
- func get_action_space():
180
- return {
181
- "move" : {
182
- "size": 1,
183
- "action_type": "continuous"
184
- },
185
- "turn" : {
186
- "size": 1,
187
- "action_type": "continuous"
188
- }
189
- }
190
-
191
- func get_done():
192
- return done
193
-
194
- func set_done_false():
195
- done = false
196
-
197
-
198
- func calculate_translation(other_pad_translation : Vector3) -> Vector3:
199
- var new_translation := Vector3.ZERO
200
- var distance = rand_range(12,16)
201
- var angle = rand_range(-180,180)
202
- new_translation.z = other_pad_translation.z + sin(deg2rad(angle))*distance
203
- new_translation.x = other_pad_translation.x + cos(deg2rad(angle))*distance
204
-
205
- return new_translation
206
-
207
-
208
-
209
- func _on_NegativeGoal_body_entered(body: Node) -> void:
210
- reward -= 1.0
211
- done = true
212
- reset()
213
-
214
-
215
- func _on_PositiveGoal_body_entered(body: Node) -> void:
216
- reward += 1.0
217
- done = true
218
- reset()
 
1
+ extends CharacterBody3D
2
  class_name Player
3
  const MOVE_SPEED = 12
4
  const JUMP_FORCE = 30
5
  const GRAVITY = 0.98
6
  const MAX_FALL_SPEED = 30
7
  const TURN_SENS = 2.0
8
+
9
+ @onready var cam = $Camera
 
10
  var move_vec = Vector3()
11
  var y_velo = 0
12
 
13
  # RL related variables
14
+ @onready var ai_controller := $AIController3D
15
+ @onready var robot = $Robot
16
+ @onready var virtual_camera = $RGBCameraSensor3D
17
 
18
  var next = 1
 
 
19
  var just_reached_negative = false
20
  var just_reached_positive = false
21
  var just_fell_off = false
22
  var best_goal_distance := 10000.0
23
  var grounded := false
 
24
  var move_action := 0.0
25
  var turn_action := 0.0
26
  var jump_action := false
 
27
 
 
28
 
29
  func _ready():
30
+ ai_controller.init(self)
31
+
32
 
33
  func _physics_process(_delta):
34
+ move_vec = get_move_vec()
35
+ #move_vec = move_vec.normalized()
36
+ move_vec = move_vec.rotated(Vector3(0, 1, 0), rotation.y)
37
+ move_vec *= MOVE_SPEED
38
+ move_vec.y = y_velo
39
+ velocity = move_vec
40
+ move_and_slide()
41
+
42
+ # turning
43
+
44
+ var turn_vec = get_turn_vec()
45
+ rotation_degrees.y += turn_vec * TURN_SENS
46
+
47
+ grounded = is_on_floor()
48
+
49
+ y_velo -= GRAVITY
50
+
51
+ if grounded and y_velo <= 0:
52
+ y_velo = -0.1
53
+ if y_velo < -MAX_FALL_SPEED:
54
+ y_velo = -MAX_FALL_SPEED
55
+
56
+ if y_velo < 0 and !grounded:
57
+ robot.set_animation("falling-cycle")
58
+
59
+ var horizontal_speed = Vector2(move_vec.x, move_vec.z)
60
+ if horizontal_speed.length() < 0.1 and grounded:
61
+ robot.set_animation("idle")
62
+ elif horizontal_speed.length() >= 1.0 and grounded:
63
+ robot.set_animation("walk-cycle")
 
 
 
 
 
 
 
 
 
 
 
 
 
64
  # elif horizontal_speed.length() >= 1.0 and grounded:
65
  # robot.set_animation("run-cycle")
66
+
67
+ update_reward()
68
+
69
+ if Input.is_action_just_pressed("r_key"):
70
+ reset()
71
+
72
 
73
  func get_move_vec() -> Vector3:
74
+ if ai_controller.done:
75
+ return Vector3.ZERO
76
+
77
+ if ai_controller.heuristic == "model":
78
+ return Vector3(0, 0, clamp(move_action, -1.0, 0.5))
79
+
80
+ return Vector3(
81
+ 0,
82
+ 0,
83
+ clamp(
84
+ (
85
+ Input.get_action_strength("move_backwards")
86
+ - Input.get_action_strength("move_forwards")
87
+ ),
88
+ -1.0,
89
+ 0.5
90
+ )
91
+ )
92
+
93
 
94
  func get_turn_vec() -> float:
95
+ if ai_controller.heuristic == "model":
96
+ return turn_action
97
+ var rotation_amount = (
98
+ Input.get_action_strength("turn_left") - Input.get_action_strength("turn_right")
99
+ )
100
+
101
+ return rotation_amount
102
 
 
103
 
 
104
  func reset():
105
+ next = 1
106
+ just_reached_negative = false
107
+ just_reached_positive = false
108
+ jump_action = false
109
+ set_position(Vector3(0, 1.5, 0))
110
+ rotation_degrees.y = randf_range(-180, 180)
111
+ y_velo = 0.1
112
+ ai_controller.reset()
113
+
114
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
  func update_reward():
116
+ ai_controller.reward -= 0.01 # step penalty
117
+
118
+
119
+ func calculate_translation(other_pad_translation: Vector3) -> Vector3:
120
+ var new_translation := Vector3.ZERO
121
+ var distance = randf_range(12, 16)
122
+ var angle = randf_range(-180, 180)
123
+ new_translation.z = other_pad_translation.z + sin(deg_to_rad(angle)) * distance
124
+ new_translation.x = other_pad_translation.x + cos(deg_to_rad(angle)) * distance
125
+
126
+ return new_translation
127
+
128
+
129
+ func _on_NegativeGoal_body_entered(_body: Node) -> void:
130
+ ai_controller.reward -= 1.0
131
+ ai_controller.done = true
132
+ reset()
133
+
134
+
135
+ func _on_PositiveGoal_body_entered(_body: Node) -> void:
136
+ ai_controller.reward += 1.0
137
+ ai_controller.done = true
138
+ reset()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
Player.tscn CHANGED
@@ -1,34 +1,34 @@
1
- [gd_scene load_steps=7 format=2]
2
 
3
- [ext_resource path="res://Player.gd" type="Script" id=1]
4
- [ext_resource path="res://Robot.tscn" type="PackedScene" id=2]
5
- [ext_resource path="res://addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.tscn" type="PackedScene" id=3]
 
6
 
7
- [sub_resource type="CapsuleShape" id=1]
8
  radius = 1.6
 
9
 
10
- [sub_resource type="CapsuleMesh" id=2]
11
 
12
- [sub_resource type="SpatialMaterial" id=3]
13
- albedo_color = Color( 0.109804, 0.317647, 0.458824, 1 )
 
14
 
15
- [node name="Player" type="KinematicBody" groups=["AGENT"]]
16
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0 )
17
- collision_mask = 0
18
- script = ExtResource( 1 )
19
 
20
- [node name="CollisionShape" type="CollisionShape" parent="."]
21
- transform = Transform( 1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0 )
22
- shape = SubResource( 1 )
23
-
24
- [node name="MeshInstance" type="MeshInstance" parent="."]
25
- transform = Transform( 1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0 )
26
  visible = false
27
- mesh = SubResource( 2 )
28
- material/0 = SubResource( 3 )
 
 
29
 
30
- [node name="Robot" parent="." instance=ExtResource( 2 )]
31
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1.43952, 0.0576344 )
32
 
33
- [node name="RGBCameraSensor3D" parent="." instance=ExtResource( 3 )]
34
- transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, -1 )
 
 
1
+ [gd_scene load_steps=7 format=3 uid="uid://dwipllxvgnle4"]
2
 
3
+ [ext_resource type="Script" path="res://Player.gd" id="1"]
4
+ [ext_resource type="PackedScene" uid="uid://b4hphc8dab5h" path="res://Robot.tscn" id="2"]
5
+ [ext_resource type="PackedScene" uid="uid://b30vsuwotx0u2" path="res://VirtualCamera.tscn" id="3_tv0v5"]
6
+ [ext_resource type="Script" path="res://AIController3D.gd" id="4_rq7t7"]
7
 
8
+ [sub_resource type="CapsuleShape3D" id="1"]
9
  radius = 1.6
10
+ height = 3.2
11
 
12
+ [sub_resource type="CapsuleMesh" id="2"]
13
 
14
+ [node name="Player" type="CharacterBody3D"]
15
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1.5, 0)
16
+ script = ExtResource("1")
17
 
18
+ [node name="CollisionShape" type="CollisionShape3D" parent="."]
19
+ transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0)
20
+ shape = SubResource("1")
 
21
 
22
+ [node name="MeshInstance" type="MeshInstance3D" parent="."]
23
+ transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0, 0, 0)
 
 
 
 
24
  visible = false
25
+ mesh = SubResource("2")
26
+
27
+ [node name="Robot" parent="." instance=ExtResource("2")]
28
+ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -1.43952, 0.0576344)
29
 
30
+ [node name="RGBCameraSensor3D" parent="." instance=ExtResource("3_tv0v5")]
 
31
 
32
+ [node name="AIController3D" type="Node3D" parent="."]
33
+ script = ExtResource("4_rq7t7")
34
+ reset_after = 10000
Robot.gd CHANGED
@@ -1,20 +1,10 @@
1
- extends Spatial
2
-
3
-
4
- # Declare member variables here. Examples:
5
- # var a = 2
6
- # var b = "text"
7
-
8
- onready var player = $AnimationPlayer
9
-
10
- # Called when the node enters the scene tree for the first time.
11
- func _ready():
12
- pass # Replace with function body.
13
 
 
14
 
15
 
16
  func set_animation(anim):
17
- if player.current_animation == anim:
18
- return
19
-
20
- player.play(anim)
 
1
+ extends Node3D
 
 
 
 
 
 
 
 
 
 
 
2
 
3
+ @onready var player = $AnimationPlayer
4
 
5
 
6
  func set_animation(anim):
7
+ if player.current_animation == anim:
8
+ return
9
+
10
+ player.play(anim)
Robot.tscn CHANGED
The diff for this file is too large to render. See raw diff
 
VirtualCamera.tscn CHANGED
@@ -1,42 +1,36 @@
1
- [gd_scene load_steps=3 format=2]
2
 
3
- [ext_resource path="res://VirtualCamera.gd" type="Script" id=1]
4
 
5
- [sub_resource type="ViewportTexture" id=1]
6
- viewport_path = NodePath("Viewport")
7
 
8
- [node name="VirtualCamera" type="Spatial"]
9
- script = ExtResource( 1 )
10
 
11
- [node name="RemoteTransform" type="RemoteTransform" parent="."]
12
- remote_path = NodePath("../Viewport/Camera")
13
 
14
- [node name="Viewport" type="Viewport" parent="."]
15
- size = Vector2( 16, 16 )
16
  render_target_update_mode = 3
17
 
18
- [node name="Camera" type="Camera" parent="Viewport"]
19
  near = 0.5
20
 
21
  [node name="Control" type="Control" parent="."]
 
 
22
  anchor_right = 1.0
23
  anchor_bottom = 1.0
24
- __meta__ = {
25
- "_edit_use_anchors_": false
26
- }
27
 
28
  [node name="TextureRect" type="ColorRect" parent="Control"]
29
- margin_left = 1096.0
30
- margin_top = 534.0
31
- margin_right = 1114.0
32
- margin_bottom = 552.0
33
- rect_scale = Vector2( 10, 10 )
34
- color = Color( 0.00784314, 0.00784314, 0.00784314, 1 )
35
- __meta__ = {
36
- "_edit_use_anchors_": false
37
- }
38
-
39
- [node name="CameraTexture" type="Sprite" parent="Control/TextureRect"]
40
- texture = SubResource( 1 )
41
- offset = Vector2( 9, 9 )
42
- flip_v = true
 
1
+ [gd_scene load_steps=3 format=3 uid="uid://b30vsuwotx0u2"]
2
 
3
+ [ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd" id="1_p152a"]
4
 
5
+ [sub_resource type="ViewportTexture" id="1"]
6
+ viewport_path = NodePath("SubViewport")
7
 
8
+ [node name="VirtualCamera" type="Node3D"]
9
+ script = ExtResource("1_p152a")
10
 
11
+ [node name="RemoteTransform" type="RemoteTransform3D" parent="."]
12
+ remote_path = NodePath("../SubViewport/Camera")
13
 
14
+ [node name="SubViewport" type="SubViewport" parent="."]
15
+ size = Vector2i(36, 36)
16
  render_target_update_mode = 3
17
 
18
+ [node name="Camera" type="Camera3D" parent="SubViewport"]
19
  near = 0.5
20
 
21
  [node name="Control" type="Control" parent="."]
22
+ layout_mode = 3
23
+ anchors_preset = 15
24
  anchor_right = 1.0
25
  anchor_bottom = 1.0
26
+ grow_horizontal = 2
27
+ grow_vertical = 2
 
28
 
29
  [node name="TextureRect" type="ColorRect" parent="Control"]
30
+ layout_mode = 0
31
+ color = Color(0.00784314, 0.00784314, 0.00784314, 1)
32
+
33
+ [node name="CameraTexture" type="Sprite2D" parent="Control/TextureRect"]
34
+ scale = Vector2(8, 8)
35
+ texture = SubResource("1")
36
+ centered = false
 
 
 
 
 
 
 
addons/godot_rl_agents/controller/ai_controller_2d.gd CHANGED
@@ -1,8 +1,82 @@
1
- extends AIController
2
  class_name AIController2D
3
 
4
- # ------------------ Godot RL Agents Logic ------------------------------------#
 
 
 
 
 
 
 
5
  var _player: Node2D
6
 
 
 
 
7
  func init(player: Node2D):
8
  _player = player
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node2D
2
  class_name AIController2D
3
 
4
+ @export var reset_after := 1000
5
+
6
+ var heuristic := "human"
7
+ var done := false
8
+ var reward := 0.0
9
+ var n_steps := 0
10
+ var needs_reset := false
11
+
12
  var _player: Node2D
13
 
14
+ func _ready():
15
+ add_to_group("AGENT")
16
+
17
  func init(player: Node2D):
18
  _player = player
19
+
20
+ #-- Methods that need implementing using the "extend script" option in Godot --#
21
+ func get_obs() -> Dictionary:
22
+ assert(false, "the get_obs method is not implemented when extending from ai_controller")
23
+ return {"obs":[]}
24
+
25
+ func get_reward() -> float:
26
+ assert(false, "the get_reward method is not implemented when extending from ai_controller")
27
+ return 0.0
28
+
29
+ func get_action_space() -> Dictionary:
30
+ assert(false, "the get get_action_space method is not implemented when extending from ai_controller")
31
+ return {
32
+ "example_actions_continous" : {
33
+ "size": 2,
34
+ "action_type": "continuous"
35
+ },
36
+ "example_actions_discrete" : {
37
+ "size": 2,
38
+ "action_type": "discrete"
39
+ },
40
+ }
41
+
42
+ func set_action(action) -> void:
43
+ assert(false, "the get set_action method is not implemented when extending from ai_controller")
44
+ # -----------------------------------------------------------------------------#
45
+
46
+ func _physics_process(delta):
47
+ n_steps += 1
48
+ if n_steps > reset_after:
49
+ needs_reset = true
50
+
51
+ func get_obs_space():
52
+ # may need overriding if the obs space is complex
53
+ var obs = get_obs()
54
+ return {
55
+ "obs": {
56
+ "size": [len(obs["obs"])],
57
+ "space": "box"
58
+ },
59
+ }
60
+
61
+ func reset():
62
+ n_steps = 0
63
+ needs_reset = false
64
+
65
+ func reset_if_done():
66
+ if done:
67
+ reset()
68
+
69
+ func set_heuristic(h):
70
+ # sets the heuristic from "human" or "model" nothing to change here
71
+ heuristic = h
72
+
73
+ func get_done():
74
+ return done
75
+
76
+ func set_done_false():
77
+ done = false
78
+
79
+ func zero_reward():
80
+ reward = 0.0
81
+
82
+
addons/godot_rl_agents/controller/ai_controller_3d.gd CHANGED
@@ -1,8 +1,80 @@
1
- extends AIController
2
  class_name AIController3D
3
 
4
- # ------------------ Godot RL Agents Logic ------------------------------------#
 
 
 
 
 
 
 
5
  var _player: Node3D
6
 
 
 
 
7
  func init(player: Node3D):
8
  _player = player
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ extends Node3D
2
  class_name AIController3D
3
 
4
+ @export var reset_after := 1000
5
+
6
+ var heuristic := "human"
7
+ var done := false
8
+ var reward := 0.0
9
+ var n_steps := 0
10
+ var needs_reset := false
11
+
12
  var _player: Node3D
13
 
14
+ func _ready():
15
+ add_to_group("AGENT")
16
+
17
  func init(player: Node3D):
18
  _player = player
19
+
20
+ #-- Methods that need implementing using the "extend script" option in Godot --#
21
+ func get_obs() -> Dictionary:
22
+ assert(false, "the get_obs method is not implemented when extending from ai_controller")
23
+ return {"obs":[]}
24
+
25
+ func get_reward() -> float:
26
+ assert(false, "the get_reward method is not implemented when extending from ai_controller")
27
+ return 0.0
28
+
29
+ func get_action_space() -> Dictionary:
30
+ assert(false, "the get get_action_space method is not implemented when extending from ai_controller")
31
+ return {
32
+ "example_actions_continous" : {
33
+ "size": 2,
34
+ "action_type": "continuous"
35
+ },
36
+ "example_actions_discrete" : {
37
+ "size": 2,
38
+ "action_type": "discrete"
39
+ },
40
+ }
41
+
42
+ func set_action(action) -> void:
43
+ assert(false, "the get set_action method is not implemented when extending from ai_controller")
44
+ # -----------------------------------------------------------------------------#
45
+
46
+ func _physics_process(delta):
47
+ n_steps += 1
48
+ if n_steps > reset_after:
49
+ needs_reset = true
50
+
51
+ func get_obs_space():
52
+ # may need overriding if the obs space is complex
53
+ var obs = get_obs()
54
+ return {
55
+ "obs": {
56
+ "size": [len(obs["obs"])],
57
+ "space": "box"
58
+ },
59
+ }
60
+
61
+ func reset():
62
+ n_steps = 0
63
+ needs_reset = false
64
+
65
+ func reset_if_done():
66
+ if done:
67
+ reset()
68
+
69
+ func set_heuristic(h):
70
+ # sets the heuristic from "human" or "model" nothing to change here
71
+ heuristic = h
72
+
73
+ func get_done():
74
+ return done
75
+
76
+ func set_done_false():
77
+ done = false
78
+
79
+ func zero_reward():
80
+ reward = 0.0
addons/godot_rl_agents/onnx/csharp/ONNXInference.cs CHANGED
@@ -1,93 +1,103 @@
1
  using Godot;
2
- using System.Collections.Generic;
3
- using System.Linq;
4
  using Microsoft.ML.OnnxRuntime;
5
  using Microsoft.ML.OnnxRuntime.Tensors;
 
 
6
 
7
- namespace GodotONNX{
8
- /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/ONNXInference/*'/>
9
- public partial class ONNXInference : Node
10
  {
11
-
12
- private InferenceSession session;
13
- /// <summary>
14
- /// Path to the ONNX model. Use Initialize to change it.
15
- /// </summary>
16
- private string modelPath;
17
- private int batchSize;
18
 
19
- private SessionOptions SessionOpt;
 
 
 
 
 
20
 
21
- //init function
22
- /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Initialize/*'/>
23
- public void Initialize(string Path, int BatchSize)
24
- {
25
- modelPath = Path;
26
- batchSize = BatchSize;
27
- SessionConfigurator.SystemCheck();
28
- SessionOpt = SessionConfigurator.GetSessionOptions();
29
- session = LoadModel(modelPath);
30
 
31
- }
32
- /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Run/*'/>
33
- public Godot.Collections.Dictionary<string, Godot.Collections.Array<float>> RunInference(Godot.Collections.Array<float> obs, int state_ins)
34
- {
35
- //Current model: Any (Godot Rl Agents)
36
- //Expects a tensor of shape [batch_size, input_size] type float named obs and a tensor of shape [batch_size] type float named state_ins
37
-
38
- //Fill the input tensors
39
- // create span from inputSize
40
- var span = new float[obs.Count]; //There's probably a better way to do this
41
- for (int i = 0; i < obs.Count; i++)
42
  {
43
- span[i] = obs[i];
 
 
 
 
44
  }
45
-
46
- IReadOnlyCollection<NamedOnnxValue> inputs = new List<NamedOnnxValue>
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  {
48
  NamedOnnxValue.CreateFromTensor("obs", new DenseTensor<float>(span, new int[] { batchSize, obs.Count })),
49
  NamedOnnxValue.CreateFromTensor("state_ins", new DenseTensor<float>(new float[] { state_ins }, new int[] { batchSize }))
50
- };
51
- IReadOnlyCollection<string> outputNames = new List<string> { "output", "state_outs" }; //ONNX is sensible to these names, as well as the input names
52
 
53
- IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results;
54
-
55
- try{
56
- results = session.Run(inputs, outputNames);
57
- }
58
- catch (OnnxRuntimeException e) {
59
- //This error usually means that the model is not compatible with the input, beacause of the input shape (size)
60
- GD.Print("Error at inference: ", e);
61
- return null;
62
- }
63
- //Can't convert IEnumerable<float> to Variant, so we have to convert it to an array or something
64
- Godot.Collections.Dictionary<string, Godot.Collections.Array<float>> output = new Godot.Collections.Dictionary<string, Godot.Collections.Array<float>>();
65
- DisposableNamedOnnxValue output1 = results.First();
66
- DisposableNamedOnnxValue output2 = results.Last();
67
- Godot.Collections.Array<float> output1Array = new Godot.Collections.Array<float>();
68
- Godot.Collections.Array<float> output2Array = new Godot.Collections.Array<float>();
 
 
69
 
70
- foreach (float f in output1.AsEnumerable<float>()) {
71
- output1Array.Add(f);
72
- }
 
73
 
74
- foreach (float f in output2.AsEnumerable<float>()) {
75
- output2Array.Add(f);
76
- }
 
77
 
78
- output.Add(output1.Name, output1Array);
79
- output.Add(output2.Name, output2Array);
80
-
81
- //Output is a dictionary of arrays, ex: { "output" : [0.1, 0.2, 0.3, 0.4, ...], "state_outs" : [0.5, ...]}
82
- return output;
83
- }
84
- /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Load/*'/>
85
- public InferenceSession LoadModel(string Path)
86
- {
87
- Godot.FileAccess file = FileAccess.Open(Path, Godot.FileAccess.ModeFlags.Read);
88
- byte[] model = file.GetBuffer((int)file.GetLength());
89
- file.Close();
90
- return new InferenceSession(model, SessionOpt); //Load the model
91
- }
 
 
 
 
 
 
92
  }
93
- }
 
1
  using Godot;
 
 
2
  using Microsoft.ML.OnnxRuntime;
3
  using Microsoft.ML.OnnxRuntime.Tensors;
4
+ using System.Collections.Generic;
5
+ using System.Linq;
6
 
7
+ namespace GodotONNX
 
 
8
  {
9
+ /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/ONNXInference/*'/>
10
+ public partial class ONNXInference : GodotObject
11
+ {
 
 
 
 
12
 
13
+ private InferenceSession session;
14
+ /// <summary>
15
+ /// Path to the ONNX model. Use Initialize to change it.
16
+ /// </summary>
17
+ private string modelPath;
18
+ private int batchSize;
19
 
20
+ private SessionOptions SessionOpt;
 
 
 
 
 
 
 
 
21
 
22
+ //init function
23
+ /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Initialize/*'/>
24
+ public void Initialize(string Path, int BatchSize)
 
 
 
 
 
 
 
 
25
  {
26
+ modelPath = Path;
27
+ batchSize = BatchSize;
28
+ SessionOpt = SessionConfigurator.MakeConfiguredSessionOptions();
29
+ session = LoadModel(modelPath);
30
+
31
  }
32
+ /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Run/*'/>
33
+ public Godot.Collections.Dictionary<string, Godot.Collections.Array<float>> RunInference(Godot.Collections.Array<float> obs, int state_ins)
34
+ {
35
+ //Current model: Any (Godot Rl Agents)
36
+ //Expects a tensor of shape [batch_size, input_size] type float named obs and a tensor of shape [batch_size] type float named state_ins
37
+
38
+ //Fill the input tensors
39
+ // create span from inputSize
40
+ var span = new float[obs.Count]; //There's probably a better way to do this
41
+ for (int i = 0; i < obs.Count; i++)
42
+ {
43
+ span[i] = obs[i];
44
+ }
45
+
46
+ IReadOnlyCollection<NamedOnnxValue> inputs = new List<NamedOnnxValue>
47
  {
48
  NamedOnnxValue.CreateFromTensor("obs", new DenseTensor<float>(span, new int[] { batchSize, obs.Count })),
49
  NamedOnnxValue.CreateFromTensor("state_ins", new DenseTensor<float>(new float[] { state_ins }, new int[] { batchSize }))
50
+ };
51
+ IReadOnlyCollection<string> outputNames = new List<string> { "output", "state_outs" }; //ONNX is sensible to these names, as well as the input names
52
 
53
+ IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results;
54
+ //We do not use "using" here so we get a better exception explaination later
55
+ try
56
+ {
57
+ results = session.Run(inputs, outputNames);
58
+ }
59
+ catch (OnnxRuntimeException e)
60
+ {
61
+ //This error usually means that the model is not compatible with the input, beacause of the input shape (size)
62
+ GD.Print("Error at inference: ", e);
63
+ return null;
64
+ }
65
+ //Can't convert IEnumerable<float> to Variant, so we have to convert it to an array or something
66
+ Godot.Collections.Dictionary<string, Godot.Collections.Array<float>> output = new Godot.Collections.Dictionary<string, Godot.Collections.Array<float>>();
67
+ DisposableNamedOnnxValue output1 = results.First();
68
+ DisposableNamedOnnxValue output2 = results.Last();
69
+ Godot.Collections.Array<float> output1Array = new Godot.Collections.Array<float>();
70
+ Godot.Collections.Array<float> output2Array = new Godot.Collections.Array<float>();
71
 
72
+ foreach (float f in output1.AsEnumerable<float>())
73
+ {
74
+ output1Array.Add(f);
75
+ }
76
 
77
+ foreach (float f in output2.AsEnumerable<float>())
78
+ {
79
+ output2Array.Add(f);
80
+ }
81
 
82
+ output.Add(output1.Name, output1Array);
83
+ output.Add(output2.Name, output2Array);
84
+
85
+ //Output is a dictionary of arrays, ex: { "output" : [0.1, 0.2, 0.3, 0.4, ...], "state_outs" : [0.5, ...]}
86
+ results.Dispose();
87
+ return output;
88
+ }
89
+ /// <include file='docs/ONNXInference.xml' path='docs/members[@name="ONNXInference"]/Load/*'/>
90
+ public InferenceSession LoadModel(string Path)
91
+ {
92
+ using Godot.FileAccess file = FileAccess.Open(Path, Godot.FileAccess.ModeFlags.Read);
93
+ byte[] model = file.GetBuffer((int)file.GetLength());
94
+ //file.Close(); file.Dispose(); //Close the file, then dispose the reference.
95
+ return new InferenceSession(model, SessionOpt); //Load the model
96
+ }
97
+ public void FreeDisposables()
98
+ {
99
+ session.Dispose();
100
+ SessionOpt.Dispose();
101
+ }
102
  }
103
+ }
addons/godot_rl_agents/onnx/csharp/SessionConfigurator.cs CHANGED
@@ -1,106 +1,131 @@
1
  using Godot;
2
  using Microsoft.ML.OnnxRuntime;
3
 
4
- namespace GodotONNX{
5
- /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/SessionConfigurator/*'/>
6
-
7
- public static class SessionConfigurator {
8
-
9
- private static SessionOptions options = new SessionOptions();
10
-
11
- /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/GetSessionOptions/*'/>
12
- public static SessionOptions GetSessionOptions() {
13
- options = new SessionOptions();
14
- options.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_WARNING;
15
- // see warnings
16
- SystemCheck();
17
- return options;
18
- }
19
- /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/SystemCheck/*'/>
20
 
21
- static public void SystemCheck()
22
  {
23
- //Most code for this function is verbose only, the only reason it exists is to track
24
- //implementation progress of the different compute APIs.
25
-
26
- //December 2022: CUDA is not working.
27
-
28
- string OSName = OS.GetName(); //Get OS Name
29
- int ComputeAPIID = ComputeCheck(); //Get Compute API
30
- //TODO: Get CPU architecture
31
-
32
- //Linux can use OpenVINO (C#) on x64 and ROCm on x86 (GDNative/C++)
33
- //Windows can use OpenVINO (C#) on x64
34
- //TODO: try TensorRT instead of CUDA
35
- //TODO: Use OpenVINO for Intel Graphics
36
-
37
- string [] ComputeNames = {"CUDA", "DirectML/ROCm", "DirectML", "CoreML", "CPU"};
38
- //match OS and Compute API
39
- options.AppendExecutionProvider_CPU(0); // Always use CPU
40
- GD.Print("OS: " + OSName, " | Compute API: " + ComputeNames[ComputeAPIID]);
41
-
42
- switch (OSName)
43
  {
44
- case "Windows": //Can use CUDA, DirectML
45
- if (ComputeAPIID == 0)
46
- {
47
- //CUDA
48
- //options.AppendExecutionProvider_CUDA(0);
49
- options.AppendExecutionProvider_DML(0);
50
- }
51
- else if (ComputeAPIID == 1)
52
- {
53
- //DirectML
54
- options.AppendExecutionProvider_DML(0);
55
- }
56
- break;
57
- case "X11": //Can use CUDA, ROCm
58
- if (ComputeAPIID == 0)
59
- {
60
- //CUDA
61
- //options.AppendExecutionProvider_CUDA(0);
62
- }
63
- if (ComputeAPIID == 1)
64
- {
65
- //ROCm, only works on x86
66
- //Research indicates that this has to be compiled as a GDNative plugin
67
- GD.Print("ROCm not supported yet, using CPU.");
68
- options.AppendExecutionProvider_CPU(0);
69
- }
70
-
71
- break;
72
- case "OSX": //Can use CoreML
73
- if (ComputeAPIID == 0) { //CoreML
74
- //TODO: Needs testing
75
- options.AppendExecutionProvider_CoreML(0);
76
- //CoreML on ARM64, out of the box, on x64 needs .tar file from GitHub
77
- }
78
- break;
79
- default:
80
- GD.Print("OS not Supported.");
81
- break;
82
  }
83
- }
84
- /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/ComputeCheck/*'/>
85
 
86
- public static int ComputeCheck()
87
- {
88
- string adapterName = Godot.RenderingServer.GetVideoAdapterName();
89
- //string adapterVendor = Godot.RenderingServer.GetVideoAdapterVendor();
90
- adapterName = adapterName.ToUpper(new System.Globalization.CultureInfo(""));
91
- //TODO: GPU vendors for MacOS, what do they even use these days?
92
- if (adapterName.Contains("INTEL")) {
93
- //Return 2, should use DirectML only
94
- return 2;}
95
- if (adapterName.Contains("AMD")) {
96
- //Return 1, should use DirectML, check later for ROCm
97
- return 1;}
98
- if (adapterName.Contains("NVIDIA")){
99
- //Return 0, should use CUDA
100
- return 0;}
101
-
102
- GD.Print("Graphics Card not recognized."); //Return -1, should use CPU
103
- return -1;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
  }
 
 
 
105
  }
106
- }
 
 
1
  using Godot;
2
  using Microsoft.ML.OnnxRuntime;
3
 
4
+ namespace GodotONNX
5
+ {
6
+ /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/SessionConfigurator/*'/>
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
+ public static class SessionConfigurator
9
  {
10
+ public enum ComputeName
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  {
12
+ CUDA,
13
+ ROCm,
14
+ DirectML,
15
+ CoreML,
16
+ CPU
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  }
 
 
18
 
19
+ /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/GetSessionOptions/*'/>
20
+ public static SessionOptions MakeConfiguredSessionOptions()
21
+ {
22
+ SessionOptions sessionOptions = new();
23
+ SetOptions(sessionOptions);
24
+ return sessionOptions;
25
+ }
26
+
27
+ private static void SetOptions(SessionOptions sessionOptions)
28
+ {
29
+ sessionOptions.LogSeverityLevel = OrtLoggingLevel.ORT_LOGGING_LEVEL_WARNING;
30
+ ApplySystemSpecificOptions(sessionOptions);
31
+ }
32
+
33
+ /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/SystemCheck/*'/>
34
+ static public void ApplySystemSpecificOptions(SessionOptions sessionOptions)
35
+ {
36
+ //Most code for this function is verbose only, the only reason it exists is to track
37
+ //implementation progress of the different compute APIs.
38
+
39
+ //December 2022: CUDA is not working.
40
+
41
+ string OSName = OS.GetName(); //Get OS Name
42
+
43
+ //ComputeName ComputeAPI = ComputeCheck(); //Get Compute API
44
+ // //TODO: Get CPU architecture
45
+
46
+ //Linux can use OpenVINO (C#) on x64 and ROCm on x86 (GDNative/C++)
47
+ //Windows can use OpenVINO (C#) on x64
48
+ //TODO: try TensorRT instead of CUDA
49
+ //TODO: Use OpenVINO for Intel Graphics
50
+
51
+ // Temporarily using CPU on all platforms to avoid errors detected with DML
52
+ ComputeName ComputeAPI = ComputeName.CPU;
53
+
54
+ //match OS and Compute API
55
+ GD.Print($"OS: {OSName} Compute API: {ComputeAPI}");
56
+
57
+ // CPU is set by default without appending necessary
58
+ // sessionOptions.AppendExecutionProvider_CPU(0);
59
+
60
+ /*
61
+ switch (OSName)
62
+ {
63
+ case "Windows": //Can use CUDA, DirectML
64
+ if (ComputeAPI is ComputeName.CUDA)
65
+ {
66
+ //CUDA
67
+ //sessionOptions.AppendExecutionProvider_CUDA(0);
68
+ //sessionOptions.AppendExecutionProvider_DML(0);
69
+ }
70
+ else if (ComputeAPI is ComputeName.DirectML)
71
+ {
72
+ //DirectML
73
+ //sessionOptions.AppendExecutionProvider_DML(0);
74
+ }
75
+ break;
76
+ case "X11": //Can use CUDA, ROCm
77
+ if (ComputeAPI is ComputeName.CUDA)
78
+ {
79
+ //CUDA
80
+ //sessionOptions.AppendExecutionProvider_CUDA(0);
81
+ }
82
+ if (ComputeAPI is ComputeName.ROCm)
83
+ {
84
+ //ROCm, only works on x86
85
+ //Research indicates that this has to be compiled as a GDNative plugin
86
+ //GD.Print("ROCm not supported yet, using CPU.");
87
+ //sessionOptions.AppendExecutionProvider_CPU(0);
88
+ }
89
+ break;
90
+ case "macOS": //Can use CoreML
91
+ if (ComputeAPI is ComputeName.CoreML)
92
+ { //CoreML
93
+ //TODO: Needs testing
94
+ //sessionOptions.AppendExecutionProvider_CoreML(0);
95
+ //CoreML on ARM64, out of the box, on x64 needs .tar file from GitHub
96
+ }
97
+ break;
98
+ default:
99
+ GD.Print("OS not Supported.");
100
+ break;
101
+ }
102
+ */
103
+ }
104
+
105
+
106
+ /// <include file='docs/SessionConfigurator.xml' path='docs/members[@name="SessionConfigurator"]/ComputeCheck/*'/>
107
+ public static ComputeName ComputeCheck()
108
+ {
109
+ string adapterName = Godot.RenderingServer.GetVideoAdapterName();
110
+ //string adapterVendor = Godot.RenderingServer.GetVideoAdapterVendor();
111
+ adapterName = adapterName.ToUpper(new System.Globalization.CultureInfo(""));
112
+ //TODO: GPU vendors for MacOS, what do they even use these days?
113
+
114
+ if (adapterName.Contains("INTEL"))
115
+ {
116
+ return ComputeName.DirectML;
117
+ }
118
+ if (adapterName.Contains("AMD") || adapterName.Contains("RADEON"))
119
+ {
120
+ return ComputeName.DirectML;
121
+ }
122
+ if (adapterName.Contains("NVIDIA"))
123
+ {
124
+ return ComputeName.CUDA;
125
  }
126
+
127
+ GD.Print("Graphics Card not recognized."); //Should use CPU
128
+ return ComputeName.CPU;
129
  }
130
+ }
131
+ }
addons/godot_rl_agents/onnx/wrapper/ONNX_wrapper.gd CHANGED
@@ -1,4 +1,4 @@
1
- extends Node
2
  class_name ONNXModel
3
  var inferencer_script = load("res://addons/godot_rl_agents/onnx/csharp/ONNXInference.cs")
4
 
@@ -17,3 +17,8 @@ func run_inference(obs : Array, state_ins : int) -> Dictionary:
17
  printerr("Inferencer not initialized")
18
  return {}
19
  return inferencer.RunInference(obs, state_ins)
 
 
 
 
 
 
1
+ extends Resource
2
  class_name ONNXModel
3
  var inferencer_script = load("res://addons/godot_rl_agents/onnx/csharp/ONNXInference.cs")
4
 
 
17
  printerr("Inferencer not initialized")
18
  return {}
19
  return inferencer.RunInference(obs, state_ins)
20
+
21
+ func _notification(what):
22
+ if what == NOTIFICATION_PREDELETE:
23
+ inferencer.FreeDisposables()
24
+ inferencer.free()
addons/godot_rl_agents/sensors/sensors_2d/ExampleRaycastSensor2D.tscn CHANGED
@@ -1,6 +1,6 @@
1
  [gd_scene load_steps=5 format=3 uid="uid://ddeq7mn1ealyc"]
2
 
3
- [ext_resource type="Script" path="res://sensors/sensors_2d/RaycastSensor2D.gd" id="1"]
4
 
5
  [sub_resource type="GDScript" id="2"]
6
  script/source = "extends Node2D
 
1
  [gd_scene load_steps=5 format=3 uid="uid://ddeq7mn1ealyc"]
2
 
3
+ [ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd" id="1"]
4
 
5
  [sub_resource type="GDScript" id="2"]
6
  script/source = "extends Node2D
addons/godot_rl_agents/sensors/sensors_2d/GridSensor2D.gd ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @tool
2
+ extends ISensor2D
3
+ class_name GridSensor2D
4
+
5
+ @export var debug_view := false:
6
+ get: return debug_view
7
+ set(value):
8
+ debug_view = value
9
+ _update()
10
+
11
+ @export_flags_2d_physics var detection_mask := 0:
12
+ get: return detection_mask
13
+ set(value):
14
+ detection_mask = value
15
+ _update()
16
+
17
+ @export var collide_with_areas := false:
18
+ get: return collide_with_areas
19
+ set(value):
20
+ collide_with_areas = value
21
+ _update()
22
+
23
+ @export var collide_with_bodies := true:
24
+ get: return collide_with_bodies
25
+ set(value):
26
+ collide_with_bodies = value
27
+ _update()
28
+
29
+ @export_range(1, 200, 0.1) var cell_width := 20.0:
30
+ get: return cell_width
31
+ set(value):
32
+ cell_width = value
33
+ _update()
34
+
35
+ @export_range(1, 200, 0.1) var cell_height := 20.0:
36
+ get: return cell_height
37
+ set(value):
38
+ cell_height = value
39
+ _update()
40
+
41
+ @export_range(1, 21, 2, "or_greater") var grid_size_x := 3:
42
+ get: return grid_size_x
43
+ set(value):
44
+ grid_size_x = value
45
+ _update()
46
+
47
+ @export_range(1, 21, 2, "or_greater") var grid_size_y := 3:
48
+ get: return grid_size_y
49
+ set(value):
50
+ grid_size_y = value
51
+ _update()
52
+
53
+ var _obs_buffer: PackedFloat64Array
54
+ var _rectangle_shape: RectangleShape2D
55
+ var _collision_mapping: Dictionary
56
+ var _n_layers_per_cell: int
57
+
58
+ var _highlighted_cell_color: Color
59
+ var _standard_cell_color: Color
60
+
61
+ func get_observation():
62
+ return _obs_buffer
63
+
64
+ func _update():
65
+ if Engine.is_editor_hint():
66
+ if is_node_ready():
67
+ _spawn_nodes()
68
+
69
+ func _ready() -> void:
70
+ _set_colors()
71
+
72
+ if Engine.is_editor_hint():
73
+ if get_child_count() == 0:
74
+ _spawn_nodes()
75
+ else:
76
+ _spawn_nodes()
77
+
78
+
79
+ func _set_colors() -> void:
80
+ _standard_cell_color = Color(100.0/255.0, 100.0/255.0, 100.0/255.0, 100.0/255.0)
81
+ _highlighted_cell_color = Color(255.0/255.0, 100.0/255.0, 100.0/255.0, 100.0/255.0)
82
+
83
+ func _get_collision_mapping() -> Dictionary:
84
+ # defines which layer is mapped to which cell obs index
85
+ var total_bits = 0
86
+ var collision_mapping = {}
87
+ for i in 32:
88
+ var bit_mask = 2**i
89
+ if (detection_mask & bit_mask) > 0:
90
+ collision_mapping[i] = total_bits
91
+ total_bits += 1
92
+
93
+ return collision_mapping
94
+
95
+ func _spawn_nodes():
96
+ for cell in get_children():
97
+ cell.name = "_%s" % cell.name # Otherwise naming below will fail
98
+ cell.queue_free()
99
+
100
+ _collision_mapping = _get_collision_mapping()
101
+ #prints("collision_mapping", _collision_mapping, len(_collision_mapping))
102
+ # allocate memory for the observations
103
+ _n_layers_per_cell = len(_collision_mapping)
104
+ _obs_buffer = PackedFloat64Array()
105
+ _obs_buffer.resize(grid_size_x*grid_size_y*_n_layers_per_cell)
106
+ _obs_buffer.fill(0)
107
+ #prints(len(_obs_buffer), _obs_buffer )
108
+
109
+ _rectangle_shape = RectangleShape2D.new()
110
+ _rectangle_shape.set_size(Vector2(cell_width, cell_height))
111
+
112
+ var shift := Vector2(
113
+ -(grid_size_x/2)*cell_width,
114
+ -(grid_size_y/2)*cell_height,
115
+ )
116
+
117
+ for i in grid_size_x:
118
+ for j in grid_size_y:
119
+ var cell_position = Vector2(i*cell_width, j*cell_height) + shift
120
+ _create_cell(i, j, cell_position)
121
+
122
+
123
+ func _create_cell(i:int, j:int, position: Vector2):
124
+ var cell : = Area2D.new()
125
+ cell.position = position
126
+ cell.name = "GridCell %s %s" %[i, j]
127
+ cell.modulate = _standard_cell_color
128
+
129
+ if collide_with_areas:
130
+ cell.area_entered.connect(_on_cell_area_entered.bind(i, j))
131
+ cell.area_exited.connect(_on_cell_area_exited.bind(i, j))
132
+
133
+ if collide_with_bodies:
134
+ cell.body_entered.connect(_on_cell_body_entered.bind(i, j))
135
+ cell.body_exited.connect(_on_cell_body_exited.bind(i, j))
136
+
137
+ cell.collision_layer = 0
138
+ cell.collision_mask = detection_mask
139
+ cell.monitorable = true
140
+ add_child(cell)
141
+ cell.set_owner(get_tree().edited_scene_root)
142
+
143
+ var col_shape : = CollisionShape2D.new()
144
+ col_shape.shape = _rectangle_shape
145
+ col_shape.name = "CollisionShape2D"
146
+ cell.add_child(col_shape)
147
+ col_shape.set_owner(get_tree().edited_scene_root)
148
+
149
+ if debug_view:
150
+ var quad = MeshInstance2D.new()
151
+ quad.name = "MeshInstance2D"
152
+ var quad_mesh = QuadMesh.new()
153
+
154
+ quad_mesh.set_size(Vector2(cell_width, cell_height))
155
+
156
+ quad.mesh = quad_mesh
157
+ cell.add_child(quad)
158
+ quad.set_owner(get_tree().edited_scene_root)
159
+
160
+ func _update_obs(cell_i:int, cell_j:int, collision_layer:int, entered: bool):
161
+ for key in _collision_mapping:
162
+ var bit_mask = 2**key
163
+ if (collision_layer & bit_mask) > 0:
164
+ var collison_map_index = _collision_mapping[key]
165
+
166
+ var obs_index = (
167
+ (cell_i * grid_size_x * _n_layers_per_cell) +
168
+ (cell_j * _n_layers_per_cell) +
169
+ collison_map_index
170
+ )
171
+ #prints(obs_index, cell_i, cell_j)
172
+ if entered:
173
+ _obs_buffer[obs_index] += 1
174
+ else:
175
+ _obs_buffer[obs_index] -= 1
176
+
177
+ func _toggle_cell(cell_i:int, cell_j:int):
178
+ var cell = get_node_or_null("GridCell %s %s" %[cell_i, cell_j])
179
+
180
+ if cell == null:
181
+ print("cell not found, returning")
182
+
183
+ var n_hits = 0
184
+ var start_index = (cell_i * grid_size_x * _n_layers_per_cell) + (cell_j * _n_layers_per_cell)
185
+ for i in _n_layers_per_cell:
186
+ n_hits += _obs_buffer[start_index+i]
187
+
188
+ if n_hits > 0:
189
+ cell.modulate = _highlighted_cell_color
190
+ else:
191
+ cell.modulate = _standard_cell_color
192
+
193
+ func _on_cell_area_entered(area:Area2D, cell_i:int, cell_j:int):
194
+ #prints("_on_cell_area_entered", cell_i, cell_j)
195
+ _update_obs(cell_i, cell_j, area.collision_layer, true)
196
+ if debug_view:
197
+ _toggle_cell(cell_i, cell_j)
198
+ #print(_obs_buffer)
199
+
200
+ func _on_cell_area_exited(area:Area2D, cell_i:int, cell_j:int):
201
+ #prints("_on_cell_area_exited", cell_i, cell_j)
202
+ _update_obs(cell_i, cell_j, area.collision_layer, false)
203
+ if debug_view:
204
+ _toggle_cell(cell_i, cell_j)
205
+
206
+ func _on_cell_body_entered(body: Node2D, cell_i:int, cell_j:int):
207
+ #prints("_on_cell_body_entered", cell_i, cell_j)
208
+ _update_obs(cell_i, cell_j, body.collision_layer, true)
209
+ if debug_view:
210
+ _toggle_cell(cell_i, cell_j)
211
+
212
+ func _on_cell_body_exited(body: Node2D, cell_i:int, cell_j:int):
213
+ #prints("_on_cell_body_exited", cell_i, cell_j)
214
+ _update_obs(cell_i, cell_j, body.collision_layer, false)
215
+ if debug_view:
216
+ _toggle_cell(cell_i, cell_j)
addons/godot_rl_agents/sensors/sensors_3d/GridSensor3D.gd ADDED
@@ -0,0 +1,233 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ @tool
2
+ extends ISensor3D
3
+ class_name GridSensor3D
4
+
5
+ @export var debug_view := false:
6
+ get: return debug_view
7
+ set(value):
8
+ debug_view = value
9
+ _update()
10
+
11
+ @export_flags_3d_physics var detection_mask := 0:
12
+ get: return detection_mask
13
+ set(value):
14
+ detection_mask = value
15
+ _update()
16
+
17
+ @export var collide_with_areas := false:
18
+ get: return collide_with_areas
19
+ set(value):
20
+ collide_with_areas = value
21
+ _update()
22
+
23
+ @export var collide_with_bodies := false:
24
+ # NOTE! The sensor will not detect StaticBody3D, add an area to static bodies to detect them
25
+ get: return collide_with_bodies
26
+ set(value):
27
+ collide_with_bodies = value
28
+ _update()
29
+
30
+ @export_range(0.1, 2, 0.1) var cell_width := 1.0:
31
+ get: return cell_width
32
+ set(value):
33
+ cell_width = value
34
+ _update()
35
+
36
+ @export_range(0.1, 2, 0.1) var cell_height := 1.0:
37
+ get: return cell_height
38
+ set(value):
39
+ cell_height = value
40
+ _update()
41
+
42
+ @export_range(1, 21, 2, "or_greater") var grid_size_x := 3:
43
+ get: return grid_size_x
44
+ set(value):
45
+ grid_size_x = value
46
+ _update()
47
+
48
+ @export_range(1, 21, 2, "or_greater") var grid_size_z := 3:
49
+ get: return grid_size_z
50
+ set(value):
51
+ grid_size_z = value
52
+ _update()
53
+
54
+ var _obs_buffer: PackedFloat64Array
55
+ var _box_shape: BoxShape3D
56
+ var _collision_mapping: Dictionary
57
+ var _n_layers_per_cell: int
58
+
59
+ var _highlighted_box_material: StandardMaterial3D
60
+ var _standard_box_material: StandardMaterial3D
61
+
62
+ func get_observation():
63
+ return _obs_buffer
64
+
65
+ func reset():
66
+ _obs_buffer.fill(0)
67
+
68
+ func _update():
69
+ if Engine.is_editor_hint():
70
+ if is_node_ready():
71
+ _spawn_nodes()
72
+
73
+ func _ready() -> void:
74
+ _make_materials()
75
+
76
+ if Engine.is_editor_hint():
77
+ if get_child_count() == 0:
78
+ _spawn_nodes()
79
+ else:
80
+ _spawn_nodes()
81
+
82
+ func _make_materials() -> void:
83
+ if _highlighted_box_material != null and _standard_box_material != null:
84
+ return
85
+
86
+ _standard_box_material = StandardMaterial3D.new()
87
+ _standard_box_material.set_transparency(1) # ALPHA
88
+ _standard_box_material.albedo_color = Color(100.0/255.0, 100.0/255.0, 100.0/255.0, 100.0/255.0)
89
+
90
+ _highlighted_box_material = StandardMaterial3D.new()
91
+ _highlighted_box_material.set_transparency(1) # ALPHA
92
+ _highlighted_box_material.albedo_color = Color(255.0/255.0, 100.0/255.0, 100.0/255.0, 100.0/255.0)
93
+
94
+ func _get_collision_mapping() -> Dictionary:
95
+ # defines which layer is mapped to which cell obs index
96
+ var total_bits = 0
97
+ var collision_mapping = {}
98
+ for i in 32:
99
+ var bit_mask = 2**i
100
+ if (detection_mask & bit_mask) > 0:
101
+ collision_mapping[i] = total_bits
102
+ total_bits += 1
103
+
104
+ return collision_mapping
105
+
106
+ func _spawn_nodes():
107
+ for cell in get_children():
108
+ cell.name = "_%s" % cell.name # Otherwise naming below will fail
109
+ cell.queue_free()
110
+
111
+ _collision_mapping = _get_collision_mapping()
112
+ #prints("collision_mapping", _collision_mapping, len(_collision_mapping))
113
+ # allocate memory for the observations
114
+ _n_layers_per_cell = len(_collision_mapping)
115
+ _obs_buffer = PackedFloat64Array()
116
+ _obs_buffer.resize(grid_size_x*grid_size_z*_n_layers_per_cell)
117
+ _obs_buffer.fill(0)
118
+ #prints(len(_obs_buffer), _obs_buffer )
119
+
120
+ _box_shape = BoxShape3D.new()
121
+ _box_shape.set_size(Vector3(cell_width, cell_height, cell_width))
122
+
123
+ var shift := Vector3(
124
+ -(grid_size_x/2)*cell_width,
125
+ 0,
126
+ -(grid_size_z/2)*cell_width,
127
+ )
128
+
129
+ for i in grid_size_x:
130
+ for j in grid_size_z:
131
+ var cell_position = Vector3(i*cell_width, 0.0, j*cell_width) + shift
132
+ _create_cell(i, j, cell_position)
133
+
134
+
135
+ func _create_cell(i:int, j:int, position: Vector3):
136
+ var cell : = Area3D.new()
137
+ cell.position = position
138
+ cell.name = "GridCell %s %s" %[i, j]
139
+
140
+ if collide_with_areas:
141
+ cell.area_entered.connect(_on_cell_area_entered.bind(i, j))
142
+ cell.area_exited.connect(_on_cell_area_exited.bind(i, j))
143
+
144
+ if collide_with_bodies:
145
+ cell.body_entered.connect(_on_cell_body_entered.bind(i, j))
146
+ cell.body_exited.connect(_on_cell_body_exited.bind(i, j))
147
+
148
+ # cell.body_shape_entered.connect(_on_cell_body_shape_entered.bind(i, j))
149
+ # cell.body_shape_exited.connect(_on_cell_body_shape_exited.bind(i, j))
150
+
151
+ cell.collision_layer = 0
152
+ cell.collision_mask = detection_mask
153
+ cell.monitorable = true
154
+ cell.input_ray_pickable = false
155
+ add_child(cell)
156
+ cell.set_owner(get_tree().edited_scene_root)
157
+
158
+ var col_shape : = CollisionShape3D.new()
159
+ col_shape.shape = _box_shape
160
+ col_shape.name = "CollisionShape3D"
161
+ cell.add_child(col_shape)
162
+ col_shape.set_owner(get_tree().edited_scene_root)
163
+
164
+ if debug_view:
165
+ var box = MeshInstance3D.new()
166
+ box.name = "MeshInstance3D"
167
+ var box_mesh = BoxMesh.new()
168
+
169
+ box_mesh.set_size(Vector3(cell_width, cell_height, cell_width))
170
+ box_mesh.material = _standard_box_material
171
+
172
+ box.mesh = box_mesh
173
+ cell.add_child(box)
174
+ box.set_owner(get_tree().edited_scene_root)
175
+
176
+ func _update_obs(cell_i:int, cell_j:int, collision_layer:int, entered: bool):
177
+ for key in _collision_mapping:
178
+ var bit_mask = 2**key
179
+ if (collision_layer & bit_mask) > 0:
180
+ var collison_map_index = _collision_mapping[key]
181
+
182
+ var obs_index = (
183
+ (cell_i * grid_size_x * _n_layers_per_cell) +
184
+ (cell_j * _n_layers_per_cell) +
185
+ collison_map_index
186
+ )
187
+ #prints(obs_index, cell_i, cell_j)
188
+ if entered:
189
+ _obs_buffer[obs_index] += 1
190
+ else:
191
+ _obs_buffer[obs_index] -= 1
192
+
193
+ func _toggle_cell(cell_i:int, cell_j:int):
194
+ var cell = get_node_or_null("GridCell %s %s" %[cell_i, cell_j])
195
+
196
+ if cell == null:
197
+ print("cell not found, returning")
198
+
199
+ var n_hits = 0
200
+ var start_index = (cell_i * grid_size_x * _n_layers_per_cell) + (cell_j * _n_layers_per_cell)
201
+ for i in _n_layers_per_cell:
202
+ n_hits += _obs_buffer[start_index+i]
203
+
204
+ var cell_mesh = cell.get_node_or_null("MeshInstance3D")
205
+ if n_hits > 0:
206
+ cell_mesh.mesh.material = _highlighted_box_material
207
+ else:
208
+ cell_mesh.mesh.material = _standard_box_material
209
+
210
+ func _on_cell_area_entered(area:Area3D, cell_i:int, cell_j:int):
211
+ #prints("_on_cell_area_entered", cell_i, cell_j)
212
+ _update_obs(cell_i, cell_j, area.collision_layer, true)
213
+ if debug_view:
214
+ _toggle_cell(cell_i, cell_j)
215
+ #print(_obs_buffer)
216
+
217
+ func _on_cell_area_exited(area:Area3D, cell_i:int, cell_j:int):
218
+ #prints("_on_cell_area_exited", cell_i, cell_j)
219
+ _update_obs(cell_i, cell_j, area.collision_layer, false)
220
+ if debug_view:
221
+ _toggle_cell(cell_i, cell_j)
222
+
223
+ func _on_cell_body_entered(body: Node3D, cell_i:int, cell_j:int):
224
+ #prints("_on_cell_body_entered", cell_i, cell_j)
225
+ _update_obs(cell_i, cell_j, body.collision_layer, true)
226
+ if debug_view:
227
+ _toggle_cell(cell_i, cell_j)
228
+
229
+ func _on_cell_body_exited(body: Node3D, cell_i:int, cell_j:int):
230
+ #prints("_on_cell_body_exited", cell_i, cell_j)
231
+ _update_obs(cell_i, cell_j, body.collision_layer, false)
232
+ if debug_view:
233
+ _toggle_cell(cell_i, cell_j)
addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd CHANGED
@@ -3,9 +3,19 @@ class_name RGBCameraSensor3D
3
  var camera_pixels = null
4
 
5
  @onready var camera_texture := $Control/TextureRect/CameraTexture as Sprite2D
 
 
6
 
7
  func get_camera_pixel_encoding():
8
- return camera_texture.get_texture().get_data().data["data"].hex_encode()
 
9
 
10
- func get_camera_shape()-> Array:
11
- return [$SubViewport.size[0], $SubViewport.size[1], 4]
 
 
 
 
 
 
 
 
3
  var camera_pixels = null
4
 
5
  @onready var camera_texture := $Control/TextureRect/CameraTexture as Sprite2D
6
+ @onready var sub_viewport := $SubViewport as SubViewport
7
+
8
 
9
  func get_camera_pixel_encoding():
10
+ return camera_texture.get_texture().get_image().get_data().hex_encode()
11
+
12
 
13
+ func get_camera_shape() -> Array:
14
+ assert(
15
+ sub_viewport.size.x >= 36 and sub_viewport.size.y >= 36,
16
+ "SubViewport size must be 36x36 or larger."
17
+ )
18
+ if sub_viewport.transparent_bg:
19
+ return [4, sub_viewport.size.y, sub_viewport.size.x]
20
+ else:
21
+ return [3, sub_viewport.size.y, sub_viewport.size.x]
addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.tscn CHANGED
@@ -1,42 +1,41 @@
1
- [gd_scene load_steps=3 format=2]
2
 
3
- [ext_resource path="res://addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd" type="Script" id=1]
4
 
5
- [sub_resource type="ViewportTexture" id=1]
6
  viewport_path = NodePath("SubViewport")
7
 
8
  [node name="RGBCameraSensor3D" type="Node3D"]
9
- script = ExtResource( 1 )
10
 
11
  [node name="RemoteTransform3D" type="RemoteTransform3D" parent="."]
12
  remote_path = NodePath("../SubViewport/Camera3D")
13
 
14
  [node name="SubViewport" type="SubViewport" parent="."]
15
- size = Vector2( 32, 32 )
16
  render_target_update_mode = 3
17
 
18
  [node name="Camera3D" type="Camera3D" parent="SubViewport"]
19
  near = 0.5
20
 
21
  [node name="Control" type="Control" parent="."]
 
 
22
  anchor_right = 1.0
23
  anchor_bottom = 1.0
24
- __meta__ = {
25
- "_edit_use_anchors_": false
26
- }
27
 
28
  [node name="TextureRect" type="ColorRect" parent="Control"]
 
29
  offset_left = 1096.0
30
  offset_top = 534.0
31
  offset_right = 1114.0
32
  offset_bottom = 552.0
33
- scale = Vector2( 10, 10 )
34
- color = Color( 0.00784314, 0.00784314, 0.00784314, 1 )
35
- __meta__ = {
36
- "_edit_use_anchors_": false
37
- }
38
 
39
  [node name="CameraTexture" type="Sprite2D" parent="Control/TextureRect"]
40
- texture = SubResource( 1 )
41
- offset = Vector2( 9, 9 )
42
  flip_v = true
 
1
+ [gd_scene load_steps=3 format=3 uid="uid://baaywi3arsl2m"]
2
 
3
+ [ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd" id="1"]
4
 
5
+ [sub_resource type="ViewportTexture" id="1"]
6
  viewport_path = NodePath("SubViewport")
7
 
8
  [node name="RGBCameraSensor3D" type="Node3D"]
9
+ script = ExtResource("1")
10
 
11
  [node name="RemoteTransform3D" type="RemoteTransform3D" parent="."]
12
  remote_path = NodePath("../SubViewport/Camera3D")
13
 
14
  [node name="SubViewport" type="SubViewport" parent="."]
15
+ size = Vector2i(32, 32)
16
  render_target_update_mode = 3
17
 
18
  [node name="Camera3D" type="Camera3D" parent="SubViewport"]
19
  near = 0.5
20
 
21
  [node name="Control" type="Control" parent="."]
22
+ layout_mode = 3
23
+ anchors_preset = 15
24
  anchor_right = 1.0
25
  anchor_bottom = 1.0
26
+ grow_horizontal = 2
27
+ grow_vertical = 2
 
28
 
29
  [node name="TextureRect" type="ColorRect" parent="Control"]
30
+ layout_mode = 0
31
  offset_left = 1096.0
32
  offset_top = 534.0
33
  offset_right = 1114.0
34
  offset_bottom = 552.0
35
+ scale = Vector2(10, 10)
36
+ color = Color(0.00784314, 0.00784314, 0.00784314, 1)
 
 
 
37
 
38
  [node name="CameraTexture" type="Sprite2D" parent="Control/TextureRect"]
39
+ texture = SubResource("1")
40
+ offset = Vector2(9, 9)
41
  flip_v = true
addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.tscn CHANGED
@@ -8,26 +8,20 @@ n_rays_width = 4.0
8
  n_rays_height = 2.0
9
  ray_length = 11.0
10
 
11
- [node name="@node_0 0@18991" type="RayCast3D" parent="."]
12
- target_position = Vector3(-4.06608, -2.84701, 9.81639)
13
-
14
- [node name="node_0 1" type="RayCast3D" parent="."]
15
- target_position = Vector3(-4.06608, 2.84701, 9.81639)
16
-
17
- [node name="@node_1 0@18992" type="RayCast3D" parent="."]
18
  target_position = Vector3(-1.38686, -2.84701, 10.5343)
19
 
20
- [node name="@node_1 1@18993" type="RayCast3D" parent="."]
21
  target_position = Vector3(-1.38686, 2.84701, 10.5343)
22
 
23
- [node name="@node_2 0@18994" type="RayCast3D" parent="."]
24
  target_position = Vector3(1.38686, -2.84701, 10.5343)
25
 
26
- [node name="@node_2 1@18995" type="RayCast3D" parent="."]
27
  target_position = Vector3(1.38686, 2.84701, 10.5343)
28
 
29
- [node name="@node_3 0@18996" type="RayCast3D" parent="."]
30
  target_position = Vector3(4.06608, -2.84701, 9.81639)
31
 
32
- [node name="@node_3 1@18997" type="RayCast3D" parent="."]
33
  target_position = Vector3(4.06608, 2.84701, 9.81639)
 
8
  n_rays_height = 2.0
9
  ray_length = 11.0
10
 
11
+ [node name="node_1 0" type="RayCast3D" parent="."]
 
 
 
 
 
 
12
  target_position = Vector3(-1.38686, -2.84701, 10.5343)
13
 
14
+ [node name="node_1 1" type="RayCast3D" parent="."]
15
  target_position = Vector3(-1.38686, 2.84701, 10.5343)
16
 
17
+ [node name="node_2 0" type="RayCast3D" parent="."]
18
  target_position = Vector3(1.38686, -2.84701, 10.5343)
19
 
20
+ [node name="node_2 1" type="RayCast3D" parent="."]
21
  target_position = Vector3(1.38686, 2.84701, 10.5343)
22
 
23
+ [node name="node_3 0" type="RayCast3D" parent="."]
24
  target_position = Vector3(4.06608, -2.84701, 9.81639)
25
 
26
+ [node name="node_3 1" type="RayCast3D" parent="."]
27
  target_position = Vector3(4.06608, 2.84701, 9.81639)
addons/godot_rl_agents/sync.gd CHANGED
@@ -1,7 +1,7 @@
1
  extends Node
2
  # --fixed-fps 2000 --disable-render-loop
3
- @export var action_repeat := 8
4
- @export var speed_up = 1
5
  @export var onnx_model_path := ""
6
 
7
  @onready var start_time = Time.get_ticks_msec()
@@ -10,7 +10,6 @@ const MAJOR_VERSION := "0"
10
  const MINOR_VERSION := "3"
11
  const DEFAULT_PORT := "11008"
12
  const DEFAULT_SEED := "1"
13
- const DEFAULT_ACTION_REPEAT := "8"
14
  var stream : StreamPeerTCP = null
15
  var connected = false
16
  var message_center
@@ -44,17 +43,20 @@ func _initialize():
44
  Engine.time_scale = _get_speedup() * 1.0
45
  prints("physics ticks", Engine.physics_ticks_per_second, Engine.time_scale, _get_speedup(), speed_up)
46
 
47
-
48
- connected = connect_to_server()
49
- if connected:
50
- _set_heuristic("model")
51
- _handshake()
52
- _send_env_info()
53
- elif onnx_model_path != "":
54
  onnx_model = ONNXModel.new(onnx_model_path, 1)
55
  _set_heuristic("model")
56
- else:
57
- _set_heuristic("human")
 
 
 
 
 
 
58
 
59
  _set_seed()
60
  _set_action_repeat()
@@ -232,7 +234,7 @@ func _set_seed():
232
  seed(_seed)
233
 
234
  func _set_action_repeat():
235
- action_repeat = args.get("action_repeat", DEFAULT_ACTION_REPEAT).to_int()
236
 
237
  func disconnect_from_server():
238
  stream.disconnect_from_host()
 
1
  extends Node
2
  # --fixed-fps 2000 --disable-render-loop
3
+ @export_range(1, 10, 1, "or_greater") var action_repeat := 8
4
+ @export_range(1, 10, 1, "or_greater") var speed_up = 1
5
  @export var onnx_model_path := ""
6
 
7
  @onready var start_time = Time.get_ticks_msec()
 
10
  const MINOR_VERSION := "3"
11
  const DEFAULT_PORT := "11008"
12
  const DEFAULT_SEED := "1"
 
13
  var stream : StreamPeerTCP = null
14
  var connected = false
15
  var message_center
 
43
  Engine.time_scale = _get_speedup() * 1.0
44
  prints("physics ticks", Engine.physics_ticks_per_second, Engine.time_scale, _get_speedup(), speed_up)
45
 
46
+ # Run inference if onnx model path is set, otherwise wait for server connection
47
+ var run_onnx_model_inference : bool = onnx_model_path != ""
48
+ if run_onnx_model_inference:
49
+ assert(FileAccess.file_exists(onnx_model_path), "Onnx Model Path set on Sync node does not exist: " + onnx_model_path)
 
 
 
50
  onnx_model = ONNXModel.new(onnx_model_path, 1)
51
  _set_heuristic("model")
52
+ else:
53
+ connected = connect_to_server()
54
+ if connected:
55
+ _set_heuristic("model")
56
+ _handshake()
57
+ _send_env_info()
58
+ else:
59
+ _set_heuristic("human")
60
 
61
  _set_seed()
62
  _set_action_repeat()
 
234
  seed(_seed)
235
 
236
  func _set_action_repeat():
237
+ action_repeat = args.get("action_repeat", str(action_repeat)).to_int()
238
 
239
  func disconnect_from_server():
240
  stream.disconnect_from_host()
bin/VirtualCamera.console.exe CHANGED
Binary files a/bin/VirtualCamera.console.exe and b/bin/VirtualCamera.console.exe differ
 
bin/VirtualCamera.exe CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:2de407f42a3db0dece9ff075b69219a30493b90c67f0a14bc450b414fba39125
3
- size 71425536
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:910d1661cf406eb788be65369b4784b8bde02dd16e327fac690b501d08e9e886
3
+ size 69051392
bin/VirtualCamera.pck CHANGED
Binary files a/bin/VirtualCamera.pck and b/bin/VirtualCamera.pck differ
 
bin/VirtualCamera.x86_64 CHANGED
@@ -1,3 +1,3 @@
1
  version https://git-lfs.github.com/spec/v1
2
- oid sha256:843a1e5f8df1e6b2fe57ff86ca637436c7e1216f8409f2b079090c373548e86f
3
- size 72784744
 
1
  version https://git-lfs.github.com/spec/v1
2
+ oid sha256:fb6fa01639dd98489aac3e0dd5f9ecf0691b25b37a717f7380b1350d91ed23a4
3
+ size 61637496
project.godot CHANGED
@@ -8,45 +8,11 @@
8
 
9
  config_version=5
10
 
11
- _global_script_classes=[{
12
- "base": "Node2D",
13
- "class": &"ISensor2D",
14
- "language": &"GDScript",
15
- "path": "res://addons/godot_rl_agents/sensors/sensors_2d/ISensor2D.gd"
16
- }, {
17
- "base": "Node3D",
18
- "class": &"ISensor3D",
19
- "language": &"GDScript",
20
- "path": "res://addons/godot_rl_agents/sensors/sensors_3d/ISensor3D.gd"
21
- }, {
22
- "base": "Node3D",
23
- "class": &"RGBCameraSensor3D",
24
- "language": &"GDScript",
25
- "path": "res://addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd"
26
- }, {
27
- "base": "ISensor3D",
28
- "class": &"RayCastSensor3D",
29
- "language": &"GDScript",
30
- "path": "res://addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd"
31
- }, {
32
- "base": "ISensor2D",
33
- "class": &"RaycastSensor2D",
34
- "language": &"GDScript",
35
- "path": "res://addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd"
36
- }]
37
- _global_script_class_icons={
38
- "ISensor2D": "",
39
- "ISensor3D": "",
40
- "RGBCameraSensor3D": "",
41
- "RayCastSensor3D": "",
42
- "RaycastSensor2D": ""
43
- }
44
-
45
  [application]
46
 
47
  config/name="VirtualCamera"
48
  run/main_scene="res://Env.tscn"
49
- config/features=PackedStringArray("4.0")
50
  config/icon="res://icon.png"
51
 
52
  [display]
@@ -54,35 +20,39 @@ config/icon="res://icon.png"
54
  window/size/width=1280
55
  window/size/height=720
56
 
 
 
 
 
57
  [editor_plugins]
58
 
59
- enabled=PackedStringArray()
60
 
61
  [input]
62
 
63
  turn_left={
64
  "deadzone": 0.5,
65
- "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
66
  ]
67
  }
68
  turn_right={
69
  "deadzone": 0.5,
70
- "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
71
  ]
72
  }
73
  move_forwards={
74
  "deadzone": 0.5,
75
- "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
76
  ]
77
  }
78
  move_backwards={
79
  "deadzone": 0.5,
80
- "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
81
  ]
82
  }
83
  r_key={
84
  "deadzone": 0.5,
85
- "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":0,"unicode":0,"echo":false,"script":null)
86
  ]
87
  }
88
 
 
8
 
9
  config_version=5
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
  [application]
12
 
13
  config/name="VirtualCamera"
14
  run/main_scene="res://Env.tscn"
15
+ config/features=PackedStringArray("4.2")
16
  config/icon="res://icon.png"
17
 
18
  [display]
 
20
  window/size/width=1280
21
  window/size/height=720
22
 
23
+ [dotnet]
24
+
25
+ project/assembly_name="VirtualCamera"
26
+
27
  [editor_plugins]
28
 
29
+ enabled=PackedStringArray("res://addons/godot_rl_agents/plugin.cfg")
30
 
31
  [input]
32
 
33
  turn_left={
34
  "deadzone": 0.5,
35
+ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":65,"physical_keycode":0,"key_label":0,"unicode":97,"echo":false,"script":null)
36
  ]
37
  }
38
  turn_right={
39
  "deadzone": 0.5,
40
+ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":68,"physical_keycode":0,"key_label":0,"unicode":100,"echo":false,"script":null)
41
  ]
42
  }
43
  move_forwards={
44
  "deadzone": 0.5,
45
+ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":87,"physical_keycode":0,"key_label":0,"unicode":119,"echo":false,"script":null)
46
  ]
47
  }
48
  move_backwards={
49
  "deadzone": 0.5,
50
+ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":83,"physical_keycode":0,"key_label":0,"unicode":115,"echo":false,"script":null)
51
  ]
52
  }
53
  r_key={
54
  "deadzone": 0.5,
55
+ "events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":82,"physical_keycode":0,"key_label":0,"unicode":114,"echo":false,"script":null)
56
  ]
57
  }
58