Commit
•
e22408d
1
Parent(s):
2b780bf
Upload folder using huggingface_hub
Browse files- .gitignore +2 -0
- AIController3D.gd +86 -0
- Camera3D.gd +1 -1
- FlyCam.gd +1 -1
- Meshes.gd +3 -6
- addons/godot_rl_agents/controller/ai_controller_2d.gd +76 -2
- addons/godot_rl_agents/controller/ai_controller_3d.gd +74 -2
- addons/godot_rl_agents/onnx/csharp/ONNXInference.cs +85 -75
- addons/godot_rl_agents/onnx/csharp/SessionConfigurator.cs +121 -96
- addons/godot_rl_agents/onnx/wrapper/ONNX_wrapper.gd +6 -1
- addons/godot_rl_agents/sensors/sensors_2d/ExampleRaycastSensor2D.tscn +1 -1
- addons/godot_rl_agents/sensors/sensors_2d/GridSensor2D.gd +216 -0
- addons/godot_rl_agents/sensors/sensors_3d/GridSensor3D.gd +233 -0
- addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.tscn +14 -15
- addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd +7 -3
- addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.tscn +6 -12
- addons/godot_rl_agents/sync.gd +15 -13
- bin/Racer.console.exe +0 -0
- bin/Racer.exe +2 -2
- bin/Racer.pck +2 -2
- bin/Racer.x86_64 +2 -2
- bonus_reward.gd +0 -10
- game.gd +3 -8
- game.tscn +4 -2
- grass.tres +2 -2
- managers/game_manager.gd +2 -11
- project.godot +20 -63
- resources/green_jeep.tres +2 -2
- resources/red_convertible.tres +2 -2
- resources/taxi.tres +2 -2
- rock.tscn +1 -1
- tests/TestUI.gd +0 -5
- tree.tscn +2 -2
- vehicle.gd +68 -169
- vehicle.tscn +7 -6
.gitignore
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
# png-files extracted by Godot import
|
2 |
+
assets/glb_files/**/*.png
|
AIController3D.gd
ADDED
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
extends AIController3D
|
2 |
+
|
3 |
+
var n_steps_without_positive_reward = 0
|
4 |
+
|
5 |
+
|
6 |
+
func reset():
|
7 |
+
done = true
|
8 |
+
n_steps = 0
|
9 |
+
n_steps_without_positive_reward = 0
|
10 |
+
reward = 0.0
|
11 |
+
needs_reset = false
|
12 |
+
|
13 |
+
|
14 |
+
func get_obs():
|
15 |
+
var next_waypoint_position = GameManager.get_next_waypoint(_player).position
|
16 |
+
|
17 |
+
var goal_distance = position.distance_to(next_waypoint_position)
|
18 |
+
goal_distance = clamp(goal_distance, 0.0, 40.0)
|
19 |
+
var goal_vector = (next_waypoint_position - position).normalized()
|
20 |
+
goal_vector = goal_vector.rotated(Vector3.UP, -rotation.y)
|
21 |
+
var obs = []
|
22 |
+
obs.append(goal_distance / 40.0)
|
23 |
+
obs.append_array([goal_vector.x, goal_vector.y, goal_vector.z])
|
24 |
+
|
25 |
+
var next_next_waypoint_position = GameManager.get_next_next_waypoint(_player).position
|
26 |
+
|
27 |
+
var next_next_goal_distance = position.distance_to(next_next_waypoint_position)
|
28 |
+
next_next_goal_distance = clamp(next_next_goal_distance, 0.0, 80.0)
|
29 |
+
var next_next_goal_vector = (next_next_waypoint_position - position).normalized()
|
30 |
+
next_next_goal_vector = next_next_goal_vector.rotated(Vector3.UP, -rotation.y)
|
31 |
+
obs.append(next_next_goal_distance / 80.0)
|
32 |
+
obs.append_array([next_next_goal_vector.x, next_next_goal_vector.y, next_next_goal_vector.z])
|
33 |
+
|
34 |
+
obs.append(clamp(_player.brake / 40.0, -1.0, 1.0))
|
35 |
+
obs.append(clamp(_player.engine_force / 40.0, -1.0, 1.0))
|
36 |
+
obs.append(clamp(_player.steering, -1.0, 1.0))
|
37 |
+
obs.append_array(
|
38 |
+
[
|
39 |
+
clamp(_player.linear_velocity.x / 40.0, -1.0, 1.0),
|
40 |
+
clamp(_player.linear_velocity.y / 40.0, -1.0, 1.0),
|
41 |
+
clamp(_player.linear_velocity.z / 40.0, -1.0, 1.0)
|
42 |
+
]
|
43 |
+
)
|
44 |
+
obs.append_array(_player.sensor.get_observation())
|
45 |
+
return {"obs": obs}
|
46 |
+
|
47 |
+
|
48 |
+
func get_action_space():
|
49 |
+
return {
|
50 |
+
"turn": {"size": 1, "action_type": "continuous"},
|
51 |
+
"accelerate": {"size": 2, "action_type": "discrete"},
|
52 |
+
"brake": {"size": 2, "action_type": "discrete"},
|
53 |
+
}
|
54 |
+
|
55 |
+
|
56 |
+
func set_action(action):
|
57 |
+
_player.turn_action = action["turn"][0]
|
58 |
+
_player.acc_action = action["accelerate"] == 1
|
59 |
+
_player.brake_action = action["brake"] == 1
|
60 |
+
|
61 |
+
|
62 |
+
func get_reward():
|
63 |
+
var total_reward = reward + shaping_reward()
|
64 |
+
if total_reward <= 0.0:
|
65 |
+
n_steps_without_positive_reward += 1
|
66 |
+
else:
|
67 |
+
n_steps_without_positive_reward -= 1
|
68 |
+
n_steps_without_positive_reward = max(0, n_steps_without_positive_reward)
|
69 |
+
return total_reward
|
70 |
+
|
71 |
+
|
72 |
+
func shaping_reward():
|
73 |
+
var s_reward = 0.0
|
74 |
+
var goal_distance = _player.position.distance_to(
|
75 |
+
GameManager.get_next_waypoint(_player).position
|
76 |
+
)
|
77 |
+
#prints(goal_distance, best_goal_distance, best_goal_distance - goal_distance)
|
78 |
+
if goal_distance < _player.best_goal_distance:
|
79 |
+
s_reward += _player.best_goal_distance - goal_distance
|
80 |
+
_player.best_goal_distance = goal_distance
|
81 |
+
|
82 |
+
# A speed based reward
|
83 |
+
var speed_reward = _player.linear_velocity.length() / 100
|
84 |
+
speed_reward = clamp(speed_reward, 0.0, 0.1)
|
85 |
+
|
86 |
+
return s_reward + speed_reward
|
Camera3D.gd
CHANGED
@@ -3,7 +3,7 @@ extends Camera3D
|
|
3 |
|
4 |
|
5 |
@export var lerp_speed = 3.0
|
6 |
-
@export_node_path(VehicleBody3D) var target_path
|
7 |
@export var offset := Vector3.ZERO
|
8 |
|
9 |
var target = null
|
|
|
3 |
|
4 |
|
5 |
@export var lerp_speed = 3.0
|
6 |
+
@export_node_path("VehicleBody3D") var target_path
|
7 |
@export var offset := Vector3.ZERO
|
8 |
|
9 |
var target = null
|
FlyCam.gd
CHANGED
@@ -12,7 +12,7 @@ func set_control(value):
|
|
12 |
$Camera3D.current = value
|
13 |
|
14 |
|
15 |
-
func _process(
|
16 |
var input_dir = Input.get_vector("turn_left", "turn_right", "accelerate", "reverse")
|
17 |
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
18 |
direction.y = Input.get_axis("cam_down", "cam_up")
|
|
|
12 |
$Camera3D.current = value
|
13 |
|
14 |
|
15 |
+
func _process(_delta):
|
16 |
var input_dir = Input.get_vector("turn_left", "turn_right", "accelerate", "reverse")
|
17 |
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
|
18 |
direction.y = Input.get_axis("cam_down", "cam_up")
|
Meshes.gd
CHANGED
@@ -1,15 +1,12 @@
|
|
1 |
extends Node3D
|
2 |
|
3 |
|
4 |
-
|
5 |
-
|
6 |
-
var node = get_node(name)
|
7 |
if node == null:
|
8 |
print("mesh not found")
|
9 |
return
|
10 |
for child in get_children():
|
11 |
child.set_visible(false)
|
12 |
-
|
13 |
node.set_visible(true)
|
14 |
-
|
15 |
-
|
|
|
1 |
extends Node3D
|
2 |
|
3 |
|
4 |
+
func set_mesh(mesh_name):
|
5 |
+
var node = get_node(mesh_name)
|
|
|
6 |
if node == null:
|
7 |
print("mesh not found")
|
8 |
return
|
9 |
for child in get_children():
|
10 |
child.set_visible(false)
|
11 |
+
|
12 |
node.set_visible(true)
|
|
|
|
addons/godot_rl_agents/controller/ai_controller_2d.gd
CHANGED
@@ -1,8 +1,82 @@
|
|
1 |
-
extends
|
2 |
class_name AIController2D
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
2 |
class_name AIController3D
|
3 |
|
4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
13 |
-
|
14 |
-
/// Path to the ONNX model. Use Initialize to change it.
|
15 |
-
/// </summary>
|
16 |
-
private string modelPath;
|
17 |
-
private int batchSize;
|
18 |
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
20 |
|
21 |
-
|
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"]/
|
33 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
44 |
}
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
-
|
52 |
|
53 |
-
IDisposableReadOnlyCollection<DisposableNamedOnnxValue> results;
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
|
|
|
|
69 |
|
70 |
-
|
71 |
-
|
72 |
-
|
|
|
73 |
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
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 |
-
|
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
|
22 |
{
|
23 |
-
|
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 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
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 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
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.tscn
CHANGED
@@ -1,42 +1,41 @@
|
|
1 |
-
[gd_scene load_steps=3 format=
|
2 |
|
3 |
-
[ext_resource path="res://addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd"
|
4 |
|
5 |
-
[sub_resource type="ViewportTexture" id=1]
|
6 |
viewport_path = NodePath("SubViewport")
|
7 |
|
8 |
[node name="RGBCameraSensor3D" type="Node3D"]
|
9 |
-
script = ExtResource(
|
10 |
|
11 |
[node name="RemoteTransform3D" type="RemoteTransform3D" parent="."]
|
12 |
remote_path = NodePath("../SubViewport/Camera3D")
|
13 |
|
14 |
[node name="SubViewport" type="SubViewport" parent="."]
|
15 |
-
size =
|
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 |
-
|
25 |
-
|
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(
|
34 |
-
color = Color(
|
35 |
-
__meta__ = {
|
36 |
-
"_edit_use_anchors_": false
|
37 |
-
}
|
38 |
|
39 |
[node name="CameraTexture" type="Sprite2D" parent="Control/TextureRect"]
|
40 |
-
texture = SubResource(
|
41 |
-
offset = Vector2(
|
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.gd
CHANGED
@@ -61,11 +61,15 @@ var geo = null
|
|
61 |
|
62 |
func _update():
|
63 |
if Engine.is_editor_hint():
|
64 |
-
|
65 |
-
|
66 |
|
67 |
func _ready() -> void:
|
68 |
-
|
|
|
|
|
|
|
|
|
69 |
|
70 |
func _spawn_nodes():
|
71 |
print("spawning nodes")
|
|
|
61 |
|
62 |
func _update():
|
63 |
if Engine.is_editor_hint():
|
64 |
+
if is_node_ready():
|
65 |
+
_spawn_nodes()
|
66 |
|
67 |
func _ready() -> void:
|
68 |
+
if Engine.is_editor_hint():
|
69 |
+
if get_child_count() == 0:
|
70 |
+
_spawn_nodes()
|
71 |
+
else:
|
72 |
+
_spawn_nodes()
|
73 |
|
74 |
func _spawn_nodes():
|
75 |
print("spawning nodes")
|
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="
|
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="
|
21 |
target_position = Vector3(-1.38686, 2.84701, 10.5343)
|
22 |
|
23 |
-
[node name="
|
24 |
target_position = Vector3(1.38686, -2.84701, 10.5343)
|
25 |
|
26 |
-
[node name="
|
27 |
target_position = Vector3(1.38686, 2.84701, 10.5343)
|
28 |
|
29 |
-
[node name="
|
30 |
target_position = Vector3(4.06608, -2.84701, 9.81639)
|
31 |
|
32 |
-
[node name="
|
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 |
-
@
|
4 |
-
@
|
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 |
-
|
49 |
-
if
|
50 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
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",
|
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/Racer.console.exe
CHANGED
Binary files a/bin/Racer.console.exe and b/bin/Racer.console.exe differ
|
|
bin/Racer.exe
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:910d1661cf406eb788be65369b4784b8bde02dd16e327fac690b501d08e9e886
|
3 |
+
size 69051392
|
bin/Racer.pck
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:f47ee489e09ef1b86b9751f5c453ccf6aad34c32c866885f63fbcb2682424e10
|
3 |
+
size 59479568
|
bin/Racer.x86_64
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
-
size
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fb6fa01639dd98489aac3e0dd5f9ecf0691b25b37a717f7380b1350d91ed23a4
|
3 |
+
size 61637496
|
bonus_reward.gd
CHANGED
@@ -1,16 +1,6 @@
|
|
1 |
extends Area3D
|
2 |
|
3 |
|
4 |
-
# Called when the node enters the scene tree for the first time.
|
5 |
-
func _ready():
|
6 |
-
pass # Replace with function body.
|
7 |
-
|
8 |
-
|
9 |
-
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
10 |
-
func _process(delta):
|
11 |
-
pass
|
12 |
-
|
13 |
-
|
14 |
func _on_body_entered(body):
|
15 |
body = body as VehicleBody3D
|
16 |
if body:
|
|
|
1 |
extends Area3D
|
2 |
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
func _on_body_entered(body):
|
5 |
body = body as VehicleBody3D
|
6 |
if body:
|
game.gd
CHANGED
@@ -1,14 +1,9 @@
|
|
1 |
extends Node3D
|
2 |
|
3 |
-
|
4 |
-
# Called when the node enters the scene tree for the first time.
|
5 |
@export var speed_up = 1
|
|
|
|
|
6 |
# Called when the node enters the scene tree for the first time.
|
7 |
func _ready():
|
8 |
-
Engine.physics_ticks_per_second = speed_up*60
|
9 |
Engine.time_scale = speed_up * 1.0
|
10 |
-
|
11 |
-
|
12 |
-
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
13 |
-
func _process(delta):
|
14 |
-
pass
|
|
|
1 |
extends Node3D
|
2 |
|
|
|
|
|
3 |
@export var speed_up = 1
|
4 |
+
|
5 |
+
|
6 |
# Called when the node enters the scene tree for the first time.
|
7 |
func _ready():
|
8 |
+
Engine.physics_ticks_per_second = speed_up * 60
|
9 |
Engine.time_scale = speed_up * 1.0
|
|
|
|
|
|
|
|
|
|
game.tscn
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[gd_scene load_steps=18 format=3 uid="uid://4cc2pbxkyxe4"]
|
2 |
|
3 |
-
[ext_resource type="Texture2D" uid="uid://
|
4 |
[ext_resource type="Script" path="res://game.gd" id="1_6oqji"]
|
5 |
[ext_resource type="PackedScene" uid="uid://b1bwg2tirgxwk" path="res://vehicle.tscn" id="6_lk73p"]
|
6 |
[ext_resource type="Script" path="res://Waypoints.gd" id="7_60see"]
|
@@ -269,7 +269,9 @@ transform = Transform3D(0.637612, 0, 0.770357, 0, 0.875053, 0, -0.770357, 0, 0.6
|
|
269 |
[node name="kenny_track2" parent="." instance=ExtResource("13_hx40f")]
|
270 |
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 36.2995, 0, -20.6384)
|
271 |
|
272 |
-
[node name="
|
|
|
|
|
273 |
transform = Transform3D(0.965926, -0.258819, 0, 0.258819, 0.965926, 0, 0, 0, 1, -16, 2.63354, 21.0934)
|
274 |
shape = SubResource("BoxShape3D_coivy")
|
275 |
|
|
|
1 |
[gd_scene load_steps=18 format=3 uid="uid://4cc2pbxkyxe4"]
|
2 |
|
3 |
+
[ext_resource type="Texture2D" uid="uid://d0mpabcwlpoah" path="res://assets/lilienstein_1k.hdr" id="1_005ev"]
|
4 |
[ext_resource type="Script" path="res://game.gd" id="1_6oqji"]
|
5 |
[ext_resource type="PackedScene" uid="uid://b1bwg2tirgxwk" path="res://vehicle.tscn" id="6_lk73p"]
|
6 |
[ext_resource type="Script" path="res://Waypoints.gd" id="7_60see"]
|
|
|
269 |
[node name="kenny_track2" parent="." instance=ExtResource("13_hx40f")]
|
270 |
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 36.2995, 0, -20.6384)
|
271 |
|
272 |
+
[node name="StaticBody3D" type="StaticBody3D" parent="kenny_track2"]
|
273 |
+
|
274 |
+
[node name="CollisionShape3D" type="CollisionShape3D" parent="kenny_track2/StaticBody3D"]
|
275 |
transform = Transform3D(0.965926, -0.258819, 0, 0.258819, 0.965926, 0, 0, 0, 1, -16, 2.63354, 21.0934)
|
276 |
shape = SubResource("BoxShape3D_coivy")
|
277 |
|
grass.tres
CHANGED
@@ -1,7 +1,7 @@
|
|
1 |
[gd_resource type="StandardMaterial3D" load_steps=3 format=3 uid="uid://bn5xuf3in8pv"]
|
2 |
|
3 |
-
[ext_resource type="Texture2D" uid="uid://
|
4 |
-
[ext_resource type="Texture2D" uid="uid://
|
5 |
|
6 |
[resource]
|
7 |
albedo_texture = ExtResource("1_t8e2h")
|
|
|
1 |
[gd_resource type="StandardMaterial3D" load_steps=3 format=3 uid="uid://bn5xuf3in8pv"]
|
2 |
|
3 |
+
[ext_resource type="Texture2D" uid="uid://cgeeok0gto7sj" path="res://assets/stable/grass6.png" id="1_t8e2h"]
|
4 |
+
[ext_resource type="Texture2D" uid="uid://ctg6hrx4h3eqe" path="res://assets/stable/NormalMap.png" id="2_slclo"]
|
5 |
|
6 |
[resource]
|
7 |
albedo_texture = ExtResource("1_t8e2h")
|
managers/game_manager.gd
CHANGED
@@ -13,17 +13,8 @@ var player_list = []
|
|
13 |
var countdown_scene = preload("res://UICountDown.tscn")
|
14 |
var fly_cam : Node3D
|
15 |
|
16 |
-
|
17 |
-
|
18 |
-
await get_tree().create_timer(1.0).timeout
|
19 |
-
var callable = Callable(self, "unpause_players")
|
20 |
-
callable.call()
|
21 |
-
return
|
22 |
-
var countdown = countdown_scene.instantiate()
|
23 |
-
add_child(countdown)
|
24 |
-
countdown.run_countdown(callable)
|
25 |
-
|
26 |
-
func _process(delta):
|
27 |
if Input.is_action_just_pressed("ui_cancel"):
|
28 |
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if Input.mouse_mode == Input.MOUSE_MODE_VISIBLE else Input.MOUSE_MODE_VISIBLE
|
29 |
|
|
|
13 |
var countdown_scene = preload("res://UICountDown.tscn")
|
14 |
var fly_cam : Node3D
|
15 |
|
16 |
+
|
17 |
+
func _process(_delta):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
if Input.is_action_just_pressed("ui_cancel"):
|
19 |
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if Input.mouse_mode == Input.MOUSE_MODE_VISIBLE else Input.MOUSE_MODE_VISIBLE
|
20 |
|
project.godot
CHANGED
@@ -8,63 +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": "VehicleBody3D",
|
23 |
-
"class": &"Player",
|
24 |
-
"language": &"GDScript",
|
25 |
-
"path": "res://vehicle.gd"
|
26 |
-
}, {
|
27 |
-
"base": "Node3D",
|
28 |
-
"class": &"RGBCameraSensor3D",
|
29 |
-
"language": &"GDScript",
|
30 |
-
"path": "res://addons/godot_rl_agents/sensors/sensors_3d/RGBCameraSensor3D.gd"
|
31 |
-
}, {
|
32 |
-
"base": "ISensor3D",
|
33 |
-
"class": &"RayCastSensor3D",
|
34 |
-
"language": &"GDScript",
|
35 |
-
"path": "res://addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd"
|
36 |
-
}, {
|
37 |
-
"base": "ISensor2D",
|
38 |
-
"class": &"RaycastSensor2D",
|
39 |
-
"language": &"GDScript",
|
40 |
-
"path": "res://addons/godot_rl_agents/sensors/sensors_2d/RaycastSensor2D.gd"
|
41 |
-
}, {
|
42 |
-
"base": "Control",
|
43 |
-
"class": &"UICountDown",
|
44 |
-
"language": &"GDScript",
|
45 |
-
"path": "res://UICountDown.gd"
|
46 |
-
}, {
|
47 |
-
"base": "Node",
|
48 |
-
"class": &"Utils",
|
49 |
-
"language": &"GDScript",
|
50 |
-
"path": "res://utils.gd"
|
51 |
-
}]
|
52 |
-
_global_script_class_icons={
|
53 |
-
"ISensor2D": "",
|
54 |
-
"ISensor3D": "",
|
55 |
-
"Player": "",
|
56 |
-
"RGBCameraSensor3D": "",
|
57 |
-
"RayCastSensor3D": "",
|
58 |
-
"RaycastSensor2D": "",
|
59 |
-
"UICountDown": "",
|
60 |
-
"Utils": ""
|
61 |
-
}
|
62 |
-
|
63 |
[application]
|
64 |
|
65 |
config/name="GodotRacer"
|
66 |
run/main_scene="res://train.tscn"
|
67 |
-
config/features=PackedStringArray("4.
|
68 |
config/icon="res://icon.png"
|
69 |
|
70 |
[autoload]
|
@@ -72,9 +20,13 @@ config/icon="res://icon.png"
|
|
72 |
GameManager="*res://managers/game_manager.gd"
|
73 |
EventManager="*res://managers/event_manager.gd"
|
74 |
|
|
|
|
|
|
|
|
|
75 |
[editor_plugins]
|
76 |
|
77 |
-
enabled=PackedStringArray()
|
78 |
|
79 |
[filesystem]
|
80 |
|
@@ -84,47 +36,52 @@ import/blender/enabled=false
|
|
84 |
|
85 |
turn_left={
|
86 |
"deadzone": 0.5,
|
87 |
-
"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":65,"unicode":0,"echo":false,"script":null)
|
88 |
]
|
89 |
}
|
90 |
turn_right={
|
91 |
"deadzone": 0.5,
|
92 |
-
"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":68,"unicode":0,"echo":false,"script":null)
|
93 |
]
|
94 |
}
|
95 |
accelerate={
|
96 |
"deadzone": 0.5,
|
97 |
-
"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":87,"unicode":0,"echo":false,"script":null)
|
98 |
]
|
99 |
}
|
100 |
reverse={
|
101 |
"deadzone": 0.5,
|
102 |
-
"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":83,"unicode":0,"echo":false,"script":null)
|
103 |
]
|
104 |
}
|
105 |
next_player={
|
106 |
"deadzone": 0.5,
|
107 |
-
"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":4194321,"unicode":0,"echo":false,"script":null)
|
108 |
]
|
109 |
}
|
110 |
previous_player={
|
111 |
"deadzone": 0.5,
|
112 |
-
"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":4194319,"unicode":0,"echo":false,"script":null)
|
113 |
]
|
114 |
}
|
115 |
toggle_flycam={
|
116 |
"deadzone": 0.5,
|
117 |
-
"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":67,"unicode":0,"echo":false,"script":null)
|
118 |
]
|
119 |
}
|
120 |
cam_up={
|
121 |
"deadzone": 0.5,
|
122 |
-
"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":32,"unicode":0,"echo":false,"script":null)
|
123 |
]
|
124 |
}
|
125 |
cam_down={
|
126 |
"deadzone": 0.5,
|
127 |
-
"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":4194326,"unicode":0,"echo":false,"script":null)
|
|
|
|
|
|
|
|
|
|
|
128 |
]
|
129 |
}
|
130 |
|
|
|
8 |
|
9 |
config_version=5
|
10 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
[application]
|
12 |
|
13 |
config/name="GodotRacer"
|
14 |
run/main_scene="res://train.tscn"
|
15 |
+
config/features=PackedStringArray("4.1")
|
16 |
config/icon="res://icon.png"
|
17 |
|
18 |
[autoload]
|
|
|
20 |
GameManager="*res://managers/game_manager.gd"
|
21 |
EventManager="*res://managers/event_manager.gd"
|
22 |
|
23 |
+
[dotnet]
|
24 |
+
|
25 |
+
project/assembly_name="GodotRacer"
|
26 |
+
|
27 |
[editor_plugins]
|
28 |
|
29 |
+
enabled=PackedStringArray("res://addons/godot_rl_agents/plugin.cfg")
|
30 |
|
31 |
[filesystem]
|
32 |
|
|
|
36 |
|
37 |
turn_left={
|
38 |
"deadzone": 0.5,
|
39 |
+
"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":65,"key_label":0,"unicode":0,"echo":false,"script":null)
|
40 |
]
|
41 |
}
|
42 |
turn_right={
|
43 |
"deadzone": 0.5,
|
44 |
+
"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":68,"key_label":0,"unicode":0,"echo":false,"script":null)
|
45 |
]
|
46 |
}
|
47 |
accelerate={
|
48 |
"deadzone": 0.5,
|
49 |
+
"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":87,"key_label":0,"unicode":0,"echo":false,"script":null)
|
50 |
]
|
51 |
}
|
52 |
reverse={
|
53 |
"deadzone": 0.5,
|
54 |
+
"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":83,"key_label":0,"unicode":0,"echo":false,"script":null)
|
55 |
]
|
56 |
}
|
57 |
next_player={
|
58 |
"deadzone": 0.5,
|
59 |
+
"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":4194321,"key_label":0,"unicode":0,"echo":false,"script":null)
|
60 |
]
|
61 |
}
|
62 |
previous_player={
|
63 |
"deadzone": 0.5,
|
64 |
+
"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":4194319,"key_label":0,"unicode":0,"echo":false,"script":null)
|
65 |
]
|
66 |
}
|
67 |
toggle_flycam={
|
68 |
"deadzone": 0.5,
|
69 |
+
"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":67,"key_label":0,"unicode":0,"echo":false,"script":null)
|
70 |
]
|
71 |
}
|
72 |
cam_up={
|
73 |
"deadzone": 0.5,
|
74 |
+
"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":32,"key_label":0,"unicode":0,"echo":false,"script":null)
|
75 |
]
|
76 |
}
|
77 |
cam_down={
|
78 |
"deadzone": 0.5,
|
79 |
+
"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":4194326,"key_label":0,"unicode":0,"echo":false,"script":null)
|
80 |
+
]
|
81 |
+
}
|
82 |
+
r_key={
|
83 |
+
"deadzone": 0.5,
|
84 |
+
"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":0,"physical_keycode":82,"key_label":0,"unicode":114,"echo":false,"script":null)
|
85 |
]
|
86 |
}
|
87 |
|
resources/green_jeep.tres
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[gd_resource type="ArrayMesh" load_steps=7 format=3 uid="uid://bmafldwqk71je"]
|
2 |
|
3 |
-
[sub_resource type="Image" id="
|
4 |
data = {
|
5 |
"data": PackedByteArray(245, 245, 245, 228, 228, 228, 206, 206, 206, 177, 177, 177, 142, 142, 142, 102, 102, 102, 62, 62, 62, 31, 31, 31, 193, 234, 255, 142, 198, 226, 75, 147, 184, 24, 86, 118, 211, 233, 166, 156, 188, 98, 102, 145, 50, 54, 91, 19, 251, 180, 161, 220, 90, 58, 171, 58, 29, 126, 26, 9, 249, 243, 166, 248, 227, 37, 223, 183, 10, 175, 144, 0, 255, 198, 76, 253, 160, 0, 240, 121, 0, 198, 86, 0, 217, 185, 157, 189, 151, 117, 146, 104, 66, 101, 69, 29, 248, 213, 201, 241, 188, 169, 217, 161, 123, 190, 145, 108, 123, 91, 65, 98, 62, 43, 67, 40, 26, 37, 22, 10, 246, 218, 248, 239, 168, 246, 229, 84, 243, 173, 7, 189, 211, 175, 247, 164, 101, 226, 125, 58, 191, 72, 11, 131, 120, 180, 254, 44, 138, 251, 10, 97, 203, 3, 50, 106, 120, 254, 122, 53, 231, 29, 26, 151, 9, 11, 76, 2, 196, 217, 225, 151, 176, 186, 101, 120, 127, 55, 66, 71, 255, 217, 193, 247, 191, 177, 214, 162, 163, 140, 102, 125),
|
6 |
"format": "RGB8",
|
@@ -10,7 +10,7 @@ data = {
|
|
10 |
}
|
11 |
|
12 |
[sub_resource type="ImageTexture" id="ImageTexture_16068"]
|
13 |
-
image = SubResource("
|
14 |
|
15 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_sxmic"]
|
16 |
resource_name = "Material"
|
|
|
1 |
[gd_resource type="ArrayMesh" load_steps=7 format=3 uid="uid://bmafldwqk71je"]
|
2 |
|
3 |
+
[sub_resource type="Image" id="Image_eiuj6"]
|
4 |
data = {
|
5 |
"data": PackedByteArray(245, 245, 245, 228, 228, 228, 206, 206, 206, 177, 177, 177, 142, 142, 142, 102, 102, 102, 62, 62, 62, 31, 31, 31, 193, 234, 255, 142, 198, 226, 75, 147, 184, 24, 86, 118, 211, 233, 166, 156, 188, 98, 102, 145, 50, 54, 91, 19, 251, 180, 161, 220, 90, 58, 171, 58, 29, 126, 26, 9, 249, 243, 166, 248, 227, 37, 223, 183, 10, 175, 144, 0, 255, 198, 76, 253, 160, 0, 240, 121, 0, 198, 86, 0, 217, 185, 157, 189, 151, 117, 146, 104, 66, 101, 69, 29, 248, 213, 201, 241, 188, 169, 217, 161, 123, 190, 145, 108, 123, 91, 65, 98, 62, 43, 67, 40, 26, 37, 22, 10, 246, 218, 248, 239, 168, 246, 229, 84, 243, 173, 7, 189, 211, 175, 247, 164, 101, 226, 125, 58, 191, 72, 11, 131, 120, 180, 254, 44, 138, 251, 10, 97, 203, 3, 50, 106, 120, 254, 122, 53, 231, 29, 26, 151, 9, 11, 76, 2, 196, 217, 225, 151, 176, 186, 101, 120, 127, 55, 66, 71, 255, 217, 193, 247, 191, 177, 214, 162, 163, 140, 102, 125),
|
6 |
"format": "RGB8",
|
|
|
10 |
}
|
11 |
|
12 |
[sub_resource type="ImageTexture" id="ImageTexture_16068"]
|
13 |
+
image = SubResource("Image_eiuj6")
|
14 |
|
15 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_sxmic"]
|
16 |
resource_name = "Material"
|
resources/red_convertible.tres
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[gd_resource type="ArrayMesh" load_steps=6 format=3 uid="uid://45kjpktk2ec4"]
|
2 |
|
3 |
-
[sub_resource type="Image" id="
|
4 |
data = {
|
5 |
"data": PackedByteArray(245, 245, 245, 228, 228, 228, 206, 206, 206, 177, 177, 177, 142, 142, 142, 102, 102, 102, 62, 62, 62, 31, 31, 31, 193, 234, 255, 142, 198, 226, 75, 147, 184, 24, 86, 118, 211, 233, 166, 156, 188, 98, 102, 145, 50, 54, 91, 19, 251, 180, 161, 220, 90, 58, 171, 58, 29, 126, 26, 9, 249, 243, 166, 248, 227, 37, 223, 183, 10, 175, 144, 0, 255, 198, 76, 253, 160, 0, 240, 121, 0, 198, 86, 0, 217, 185, 157, 189, 151, 117, 146, 104, 66, 101, 69, 29, 248, 213, 201, 241, 188, 169, 217, 161, 123, 190, 145, 108, 123, 91, 65, 98, 62, 43, 67, 40, 26, 37, 22, 10, 246, 218, 248, 239, 168, 246, 229, 84, 243, 173, 7, 189, 211, 175, 247, 164, 101, 226, 125, 58, 191, 72, 11, 131, 120, 180, 254, 44, 138, 251, 10, 97, 203, 3, 50, 106, 120, 254, 122, 53, 231, 29, 26, 151, 9, 11, 76, 2, 196, 217, 225, 151, 176, 186, 101, 120, 127, 55, 66, 71, 255, 217, 193, 247, 191, 177, 214, 162, 163, 140, 102, 125),
|
6 |
"format": "RGB8",
|
@@ -10,7 +10,7 @@ data = {
|
|
10 |
}
|
11 |
|
12 |
[sub_resource type="ImageTexture" id="ImageTexture_8w6u5"]
|
13 |
-
image = SubResource("
|
14 |
|
15 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_6vwct"]
|
16 |
resource_name = "Material.006"
|
|
|
1 |
[gd_resource type="ArrayMesh" load_steps=6 format=3 uid="uid://45kjpktk2ec4"]
|
2 |
|
3 |
+
[sub_resource type="Image" id="Image_eqtqe"]
|
4 |
data = {
|
5 |
"data": PackedByteArray(245, 245, 245, 228, 228, 228, 206, 206, 206, 177, 177, 177, 142, 142, 142, 102, 102, 102, 62, 62, 62, 31, 31, 31, 193, 234, 255, 142, 198, 226, 75, 147, 184, 24, 86, 118, 211, 233, 166, 156, 188, 98, 102, 145, 50, 54, 91, 19, 251, 180, 161, 220, 90, 58, 171, 58, 29, 126, 26, 9, 249, 243, 166, 248, 227, 37, 223, 183, 10, 175, 144, 0, 255, 198, 76, 253, 160, 0, 240, 121, 0, 198, 86, 0, 217, 185, 157, 189, 151, 117, 146, 104, 66, 101, 69, 29, 248, 213, 201, 241, 188, 169, 217, 161, 123, 190, 145, 108, 123, 91, 65, 98, 62, 43, 67, 40, 26, 37, 22, 10, 246, 218, 248, 239, 168, 246, 229, 84, 243, 173, 7, 189, 211, 175, 247, 164, 101, 226, 125, 58, 191, 72, 11, 131, 120, 180, 254, 44, 138, 251, 10, 97, 203, 3, 50, 106, 120, 254, 122, 53, 231, 29, 26, 151, 9, 11, 76, 2, 196, 217, 225, 151, 176, 186, 101, 120, 127, 55, 66, 71, 255, 217, 193, 247, 191, 177, 214, 162, 163, 140, 102, 125),
|
6 |
"format": "RGB8",
|
|
|
10 |
}
|
11 |
|
12 |
[sub_resource type="ImageTexture" id="ImageTexture_8w6u5"]
|
13 |
+
image = SubResource("Image_eqtqe")
|
14 |
|
15 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_6vwct"]
|
16 |
resource_name = "Material.006"
|
resources/taxi.tres
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[gd_resource type="ArrayMesh" load_steps=6 format=3 uid="uid://b7menhm5bg0rq"]
|
2 |
|
3 |
-
[sub_resource type="Image" id="
|
4 |
data = {
|
5 |
"data": PackedByteArray(245, 245, 245, 228, 228, 228, 206, 206, 206, 177, 177, 177, 142, 142, 142, 102, 102, 102, 62, 62, 62, 31, 31, 31, 193, 234, 255, 142, 198, 226, 75, 147, 184, 24, 86, 118, 211, 233, 166, 156, 188, 98, 102, 145, 50, 54, 91, 19, 251, 180, 161, 220, 90, 58, 171, 58, 29, 126, 26, 9, 249, 243, 166, 248, 227, 37, 223, 183, 10, 175, 144, 0, 255, 198, 76, 253, 160, 0, 240, 121, 0, 198, 86, 0, 217, 185, 157, 189, 151, 117, 146, 104, 66, 101, 69, 29, 248, 213, 201, 241, 188, 169, 217, 161, 123, 190, 145, 108, 123, 91, 65, 98, 62, 43, 67, 40, 26, 37, 22, 10, 246, 218, 248, 239, 168, 246, 229, 84, 243, 173, 7, 189, 211, 175, 247, 164, 101, 226, 125, 58, 191, 72, 11, 131, 120, 180, 254, 44, 138, 251, 10, 97, 203, 3, 50, 106, 120, 254, 122, 53, 231, 29, 26, 151, 9, 11, 76, 2, 196, 217, 225, 151, 176, 186, 101, 120, 127, 55, 66, 71, 255, 217, 193, 247, 191, 177, 214, 162, 163, 140, 102, 125),
|
6 |
"format": "RGB8",
|
@@ -10,7 +10,7 @@ data = {
|
|
10 |
}
|
11 |
|
12 |
[sub_resource type="ImageTexture" id="ImageTexture_ci2m6"]
|
13 |
-
image = SubResource("
|
14 |
|
15 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_vh00m"]
|
16 |
resource_name = "Material"
|
|
|
1 |
[gd_resource type="ArrayMesh" load_steps=6 format=3 uid="uid://b7menhm5bg0rq"]
|
2 |
|
3 |
+
[sub_resource type="Image" id="Image_8b4xe"]
|
4 |
data = {
|
5 |
"data": PackedByteArray(245, 245, 245, 228, 228, 228, 206, 206, 206, 177, 177, 177, 142, 142, 142, 102, 102, 102, 62, 62, 62, 31, 31, 31, 193, 234, 255, 142, 198, 226, 75, 147, 184, 24, 86, 118, 211, 233, 166, 156, 188, 98, 102, 145, 50, 54, 91, 19, 251, 180, 161, 220, 90, 58, 171, 58, 29, 126, 26, 9, 249, 243, 166, 248, 227, 37, 223, 183, 10, 175, 144, 0, 255, 198, 76, 253, 160, 0, 240, 121, 0, 198, 86, 0, 217, 185, 157, 189, 151, 117, 146, 104, 66, 101, 69, 29, 248, 213, 201, 241, 188, 169, 217, 161, 123, 190, 145, 108, 123, 91, 65, 98, 62, 43, 67, 40, 26, 37, 22, 10, 246, 218, 248, 239, 168, 246, 229, 84, 243, 173, 7, 189, 211, 175, 247, 164, 101, 226, 125, 58, 191, 72, 11, 131, 120, 180, 254, 44, 138, 251, 10, 97, 203, 3, 50, 106, 120, 254, 122, 53, 231, 29, 26, 151, 9, 11, 76, 2, 196, 217, 225, 151, 176, 186, 101, 120, 127, 55, 66, 71, 255, 217, 193, 247, 191, 177, 214, 162, 163, 140, 102, 125),
|
6 |
"format": "RGB8",
|
|
|
10 |
}
|
11 |
|
12 |
[sub_resource type="ImageTexture" id="ImageTexture_ci2m6"]
|
13 |
+
image = SubResource("Image_8b4xe")
|
14 |
|
15 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_vh00m"]
|
16 |
resource_name = "Material"
|
rock.tscn
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[gd_scene load_steps=6 format=3 uid="uid://boym8e5gw11rn"]
|
2 |
|
3 |
-
[ext_resource type="Texture2D" uid="uid://
|
4 |
|
5 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_wx26a"]
|
6 |
albedo_color = Color(0.682353, 0.682353, 0.682353, 1)
|
|
|
1 |
[gd_scene load_steps=6 format=3 uid="uid://boym8e5gw11rn"]
|
2 |
|
3 |
+
[ext_resource type="Texture2D" uid="uid://peuef6xem1ud" path="res://assets/stable/rock1.png" id="1_bjstv"]
|
4 |
|
5 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_wx26a"]
|
6 |
albedo_color = Color(0.682353, 0.682353, 0.682353, 1)
|
tests/TestUI.gd
CHANGED
@@ -1,11 +1,6 @@
|
|
1 |
extends Node3D
|
2 |
|
3 |
|
4 |
-
# Called when the node enters the scene tree for the first time.
|
5 |
-
func _ready():
|
6 |
-
pass # Replace with function body.
|
7 |
-
|
8 |
-
|
9 |
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
10 |
func _process(delta):
|
11 |
if Input.is_action_just_pressed("ui_accept"):
|
|
|
1 |
extends Node3D
|
2 |
|
3 |
|
|
|
|
|
|
|
|
|
|
|
4 |
# Called every frame. 'delta' is the elapsed time since the previous frame.
|
5 |
func _process(delta):
|
6 |
if Input.is_action_just_pressed("ui_accept"):
|
tree.tscn
CHANGED
@@ -1,8 +1,8 @@
|
|
1 |
[gd_scene load_steps=8 format=3 uid="uid://bf3l8h10mqiae"]
|
2 |
|
3 |
[ext_resource type="ArrayMesh" uid="uid://djjljl05k75qe" path="res://resources/tree.tres" id="1_cwaw2"]
|
4 |
-
[ext_resource type="Texture2D" uid="uid://
|
5 |
-
[ext_resource type="Texture2D" uid="uid://
|
6 |
[ext_resource type="ArrayMesh" uid="uid://bf53y70b82mye" path="res://resources/tree2.tres" id="3_vg6ye"]
|
7 |
|
8 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2x7jd"]
|
|
|
1 |
[gd_scene load_steps=8 format=3 uid="uid://bf3l8h10mqiae"]
|
2 |
|
3 |
[ext_resource type="ArrayMesh" uid="uid://djjljl05k75qe" path="res://resources/tree.tres" id="1_cwaw2"]
|
4 |
+
[ext_resource type="Texture2D" uid="uid://mifm3sxpvpb" path="res://assets/stable/tree_bark1.png" id="1_ph1l0"]
|
5 |
+
[ext_resource type="Texture2D" uid="uid://baago1331i3ih" path="res://assets/stable/tree_leaves5.png" id="2_bwuwj"]
|
6 |
[ext_resource type="ArrayMesh" uid="uid://bf53y70b82mye" path="res://resources/tree2.tres" id="3_vg6ye"]
|
7 |
|
8 |
[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_2x7jd"]
|
vehicle.gd
CHANGED
@@ -9,240 +9,142 @@ var steer_target = 0
|
|
9 |
@export var engine_force_value = 40
|
10 |
@export var brake_force_value = 60
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# ------------------ Godot RL Agents Logic ------------------------------------#
|
18 |
-
var _heuristic := "human"
|
19 |
-
var done := false
|
20 |
-
# example actions
|
21 |
var turn_action := 0.0
|
22 |
var acc_action := false
|
23 |
var brake_action := false
|
24 |
-
var
|
25 |
-
var
|
26 |
-
var
|
27 |
-
var starting_rotation : Vector3
|
28 |
-
var best_goal_distance
|
29 |
-
var n_steps_without_positive_reward = 0
|
30 |
-
var n_steps = 0
|
31 |
@onready var sensor = $RayCastSensor3D
|
|
|
32 |
|
33 |
|
34 |
-
|
35 |
-
|
36 |
func reset():
|
37 |
position = starting_position
|
38 |
rotation = starting_rotation
|
39 |
-
|
40 |
-
|
41 |
-
reward = 0.0
|
42 |
GameManager.reset_waypoints(self)
|
43 |
best_goal_distance = position.distance_to(GameManager.get_next_waypoint(self).position)
|
44 |
-
|
45 |
-
pass
|
46 |
|
47 |
|
48 |
-
func reset_if_done():
|
49 |
-
if done:
|
50 |
-
reset()
|
51 |
-
|
52 |
-
func get_obs():
|
53 |
-
var next_waypoint_position = GameManager.get_next_waypoint(self).position
|
54 |
-
|
55 |
-
var goal_distance = position.distance_to(next_waypoint_position)
|
56 |
-
goal_distance = clamp(goal_distance, 0.0, 40.0)
|
57 |
-
var goal_vector = (next_waypoint_position - position).normalized()
|
58 |
-
goal_vector = goal_vector.rotated(Vector3.UP, -rotation.y)
|
59 |
-
var obs = []
|
60 |
-
obs.append(goal_distance/40.0)
|
61 |
-
obs.append_array([goal_vector.x,
|
62 |
-
goal_vector.y,
|
63 |
-
goal_vector.z])
|
64 |
-
|
65 |
-
var next_next_waypoint_position = GameManager.get_next_next_waypoint(self).position
|
66 |
-
|
67 |
-
var next_next_goal_distance = position.distance_to(next_next_waypoint_position)
|
68 |
-
next_next_goal_distance = clamp(next_next_goal_distance, 0.0, 80.0)
|
69 |
-
var next_next_goal_vector = (next_next_waypoint_position - position).normalized()
|
70 |
-
next_next_goal_vector = next_next_goal_vector.rotated(Vector3.UP, -rotation.y)
|
71 |
-
obs.append(next_next_goal_distance/80.0)
|
72 |
-
obs.append_array([next_next_goal_vector.x,
|
73 |
-
next_next_goal_vector.y,
|
74 |
-
next_next_goal_vector.z])
|
75 |
-
|
76 |
-
obs.append(clamp(brake/40.0,-1.0,1.0))
|
77 |
-
obs.append(clamp(engine_force/40.0,-1.0,1.0))
|
78 |
-
obs.append(clamp(steering,-1.0, 1.0))
|
79 |
-
obs.append_array([clamp(linear_velocity.x/40.0,-1.0,1.0),
|
80 |
-
clamp(linear_velocity.y/40.0,-1.0,1.0),
|
81 |
-
clamp(linear_velocity.z/40.0,-1.0,1.0)])
|
82 |
-
obs.append_array(sensor.get_observation())
|
83 |
-
return {
|
84 |
-
"obs":obs
|
85 |
-
}
|
86 |
-
|
87 |
-
func get_reward():
|
88 |
-
var total_reward = reward + shaping_reward()
|
89 |
-
if total_reward <= 0.0:
|
90 |
-
n_steps_without_positive_reward += 1
|
91 |
-
else:
|
92 |
-
n_steps_without_positive_reward -= 1
|
93 |
-
n_steps_without_positive_reward = max(0, n_steps_without_positive_reward)
|
94 |
-
return total_reward
|
95 |
-
|
96 |
-
func zero_reward():
|
97 |
-
reward = 0.0
|
98 |
-
|
99 |
-
func shaping_reward():
|
100 |
-
var s_reward = 0.0
|
101 |
-
var goal_distance = position.distance_to(GameManager.get_next_waypoint(self).position)
|
102 |
-
#prints(goal_distance, best_goal_distance, best_goal_distance - goal_distance)
|
103 |
-
if goal_distance < best_goal_distance:
|
104 |
-
|
105 |
-
s_reward += best_goal_distance - goal_distance
|
106 |
-
best_goal_distance = goal_distance
|
107 |
-
|
108 |
-
# A speed based reward
|
109 |
-
var speed_reward = linear_velocity.length() / 100
|
110 |
-
speed_reward = clamp(speed_reward, 0.0, 0.1)
|
111 |
-
|
112 |
-
return s_reward + speed_reward
|
113 |
-
|
114 |
-
|
115 |
-
func set_heuristic(heuristic):
|
116 |
-
# sets the heuristic from "human" or "model" nothing to change here
|
117 |
-
self._heuristic = heuristic
|
118 |
-
|
119 |
-
func get_obs_space():
|
120 |
-
var obs = get_obs()
|
121 |
-
return {
|
122 |
-
"obs": {
|
123 |
-
"size": [len(obs["obs"])],
|
124 |
-
"space": "box"
|
125 |
-
},
|
126 |
-
}
|
127 |
-
|
128 |
-
func get_action_space():
|
129 |
-
return {
|
130 |
-
"turn" : {
|
131 |
-
"size": 1,
|
132 |
-
"action_type": "continuous"
|
133 |
-
},
|
134 |
-
"accelerate" : {
|
135 |
-
"size": 2,
|
136 |
-
"action_type": "discrete"
|
137 |
-
},
|
138 |
-
"brake" : {
|
139 |
-
"size": 2,
|
140 |
-
"action_type": "discrete"
|
141 |
-
},
|
142 |
-
}
|
143 |
-
|
144 |
-
func get_done():
|
145 |
-
return done
|
146 |
-
|
147 |
-
|
148 |
-
func set_action(action):
|
149 |
-
turn_action = action["turn"][0]
|
150 |
-
acc_action = action["accelerate"] == 1
|
151 |
-
brake_action = action["brake"] == 1
|
152 |
# ----------------------------------------------------------------------------#
|
153 |
|
|
|
154 |
func get_steer_target():
|
155 |
-
if
|
156 |
return Input.get_axis("turn_right", "turn_left")
|
157 |
else:
|
158 |
return clamp(turn_action, -1.0, 1.0)
|
159 |
|
|
|
160 |
func get_accelerate_value():
|
161 |
-
if
|
162 |
return Input.is_action_pressed("accelerate")
|
163 |
else:
|
164 |
return acc_action
|
165 |
|
|
|
166 |
func get_brake_value():
|
167 |
-
if
|
168 |
return Input.is_action_pressed("reverse")
|
169 |
else:
|
170 |
return brake_action
|
171 |
|
|
|
172 |
func _ready():
|
|
|
173 |
GameManager.register_player(self)
|
174 |
best_goal_distance = position.distance_to(GameManager.get_next_waypoint(self).position)
|
175 |
$Node/Camera3D.current = controlled
|
176 |
starting_position = position
|
177 |
starting_rotation = rotation
|
178 |
-
|
179 |
$Meshes.set_mesh(VEHICLE_TYPES_STRINGS[vehicle_type])
|
180 |
|
181 |
-
|
|
|
182 |
controlled = value
|
183 |
$Node/Camera3D.current = value
|
184 |
|
|
|
185 |
func crossed_waypoint():
|
186 |
-
reward += 100.0
|
187 |
|
188 |
best_goal_distance = position.distance_to(GameManager.get_next_waypoint(self).position)
|
189 |
|
|
|
190 |
func bonus_reward():
|
191 |
print("bonus reward")
|
192 |
-
reward += 50.0
|
193 |
|
194 |
-
func set_done_false():
|
195 |
-
done = false
|
196 |
|
197 |
func check_reset_conditions():
|
198 |
-
if done:
|
199 |
return
|
200 |
|
201 |
-
|
202 |
-
|
203 |
-
|
204 |
-
|
205 |
-
|
|
|
|
|
|
|
|
|
206 |
reset()
|
207 |
-
return
|
208 |
-
|
209 |
-
# #var up_axis = transform
|
210 |
-
if n_steps_without_positive_reward > 1000:
|
211 |
-
print("resetting due to n_steps_without_positive_reward >1000")
|
212 |
-
reward -= 10.0
|
213 |
-
done = true
|
214 |
-
reset()
|
215 |
-
return
|
216 |
-
|
217 |
if transform.basis.y.dot(Vector3.UP) < 0.0:
|
218 |
-
print(
|
219 |
-
|
220 |
-
|
|
|
|
|
|
|
|
|
221 |
reset()
|
222 |
return
|
223 |
-
|
224 |
if position.y < -5.0:
|
225 |
-
print("resetting due to position.y < -5.0")
|
226 |
-
reward -= 10.0
|
227 |
-
done = true
|
228 |
reset()
|
229 |
return
|
230 |
-
|
231 |
|
232 |
func _print_goal_info():
|
233 |
var next_waypoint_position = GameManager.get_next_waypoint(self).position
|
234 |
-
|
235 |
var goal_distance = position.distance_to(next_waypoint_position)
|
236 |
goal_distance = clamp(goal_distance, 0.0, 20.0)
|
237 |
var goal_vector = (next_waypoint_position - position).normalized()
|
238 |
goal_vector = goal_vector.rotated(Vector3.UP, -rotation.y)
|
239 |
-
|
240 |
prints(goal_distance, goal_vector)
|
241 |
|
|
|
242 |
func _physics_process(delta):
|
243 |
-
if
|
244 |
-
|
245 |
-
|
246 |
reset()
|
247 |
return
|
248 |
check_reset_conditions()
|
@@ -262,9 +164,9 @@ func _physics_process(delta):
|
|
262 |
engine_force = engine_force_value
|
263 |
else:
|
264 |
engine_force = 0
|
265 |
-
|
266 |
$Arrow3D.look_at(GameManager.get_next_waypoint(self).position)
|
267 |
-
|
268 |
if not accelerating and get_brake_value():
|
269 |
# Increase engine force at low speeds to make the initial acceleration faster.
|
270 |
if fwd_mps >= -1:
|
@@ -274,11 +176,8 @@ func _physics_process(delta):
|
|
274 |
else:
|
275 |
engine_force = -engine_force_value
|
276 |
else:
|
277 |
-
brake = 1*brake_force_value
|
278 |
else:
|
279 |
brake = 0.0
|
280 |
|
281 |
steering = move_toward(steering, steer_target, STEER_SPEED * delta)
|
282 |
-
|
283 |
-
|
284 |
-
|
|
|
9 |
@export var engine_force_value = 40
|
10 |
@export var brake_force_value = 60
|
11 |
|
12 |
+
enum VEHICLE_TYPES {
|
13 |
+
RedConvertible, Car, GreenJeep, PoliceCar, FireTruck, Delivery, GarbageTruck, Hatchback
|
14 |
+
}
|
15 |
+
var VEHICLE_TYPES_STRINGS = [
|
16 |
+
"RedConvertible",
|
17 |
+
"Car",
|
18 |
+
"GreenJeep",
|
19 |
+
"PoliceCar",
|
20 |
+
"FireTruck",
|
21 |
+
"Delivery",
|
22 |
+
"GarbageTruck",
|
23 |
+
"Hatchback"
|
24 |
+
]
|
25 |
+
@export var vehicle_type: VEHICLE_TYPES = VEHICLE_TYPES.RedConvertible
|
26 |
|
27 |
# ------------------ Godot RL Agents Logic ------------------------------------#
|
|
|
|
|
|
|
28 |
var turn_action := 0.0
|
29 |
var acc_action := false
|
30 |
var brake_action := false
|
31 |
+
var starting_position: Vector3
|
32 |
+
var starting_rotation: Vector3
|
33 |
+
var best_goal_distance
|
|
|
|
|
|
|
|
|
34 |
@onready var sensor = $RayCastSensor3D
|
35 |
+
@onready var ai_controller = $AIController3D
|
36 |
|
37 |
|
|
|
|
|
38 |
func reset():
|
39 |
position = starting_position
|
40 |
rotation = starting_rotation
|
41 |
+
linear_velocity = Vector3.ZERO
|
42 |
+
angular_velocity = Vector3.ZERO
|
|
|
43 |
GameManager.reset_waypoints(self)
|
44 |
best_goal_distance = position.distance_to(GameManager.get_next_waypoint(self).position)
|
45 |
+
ai_controller.reset()
|
|
|
46 |
|
47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
# ----------------------------------------------------------------------------#
|
49 |
|
50 |
+
|
51 |
func get_steer_target():
|
52 |
+
if ai_controller.heuristic == "human":
|
53 |
return Input.get_axis("turn_right", "turn_left")
|
54 |
else:
|
55 |
return clamp(turn_action, -1.0, 1.0)
|
56 |
|
57 |
+
|
58 |
func get_accelerate_value():
|
59 |
+
if ai_controller.heuristic == "human":
|
60 |
return Input.is_action_pressed("accelerate")
|
61 |
else:
|
62 |
return acc_action
|
63 |
|
64 |
+
|
65 |
func get_brake_value():
|
66 |
+
if ai_controller.heuristic == "human":
|
67 |
return Input.is_action_pressed("reverse")
|
68 |
else:
|
69 |
return brake_action
|
70 |
|
71 |
+
|
72 |
func _ready():
|
73 |
+
ai_controller.init(self)
|
74 |
GameManager.register_player(self)
|
75 |
best_goal_distance = position.distance_to(GameManager.get_next_waypoint(self).position)
|
76 |
$Node/Camera3D.current = controlled
|
77 |
starting_position = position
|
78 |
starting_rotation = rotation
|
79 |
+
|
80 |
$Meshes.set_mesh(VEHICLE_TYPES_STRINGS[vehicle_type])
|
81 |
|
82 |
+
|
83 |
+
func set_control(value: bool):
|
84 |
controlled = value
|
85 |
$Node/Camera3D.current = value
|
86 |
|
87 |
+
|
88 |
func crossed_waypoint():
|
89 |
+
ai_controller.reward += 100.0
|
90 |
|
91 |
best_goal_distance = position.distance_to(GameManager.get_next_waypoint(self).position)
|
92 |
|
93 |
+
|
94 |
func bonus_reward():
|
95 |
print("bonus reward")
|
96 |
+
ai_controller.reward += 50.0
|
97 |
|
|
|
|
|
98 |
|
99 |
func check_reset_conditions():
|
100 |
+
if ai_controller.done:
|
101 |
return
|
102 |
|
103 |
+
if ai_controller.n_steps_without_positive_reward > 1000:
|
104 |
+
print(
|
105 |
+
(
|
106 |
+
"resetting "
|
107 |
+
+ VEHICLE_TYPES_STRINGS[vehicle_type]
|
108 |
+
+ " due to n_steps_without_positive_reward > 1000"
|
109 |
+
)
|
110 |
+
)
|
111 |
+
ai_controller.reward -= 10.0
|
112 |
reset()
|
113 |
+
return
|
114 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
if transform.basis.y.dot(Vector3.UP) < 0.0:
|
116 |
+
print(
|
117 |
+
(
|
118 |
+
"resetting "
|
119 |
+
+ VEHICLE_TYPES_STRINGS[vehicle_type]
|
120 |
+
+ " due to transform.basis.y.dot(Vector3.UP) < 0.0"
|
121 |
+
)
|
122 |
+
)
|
123 |
reset()
|
124 |
return
|
125 |
+
|
126 |
if position.y < -5.0:
|
127 |
+
print("resetting " + VEHICLE_TYPES_STRINGS[vehicle_type] + " due to position.y < -5.0")
|
128 |
+
ai_controller.reward -= 10.0
|
|
|
129 |
reset()
|
130 |
return
|
131 |
+
|
132 |
|
133 |
func _print_goal_info():
|
134 |
var next_waypoint_position = GameManager.get_next_waypoint(self).position
|
135 |
+
|
136 |
var goal_distance = position.distance_to(next_waypoint_position)
|
137 |
goal_distance = clamp(goal_distance, 0.0, 20.0)
|
138 |
var goal_vector = (next_waypoint_position - position).normalized()
|
139 |
goal_vector = goal_vector.rotated(Vector3.UP, -rotation.y)
|
140 |
+
|
141 |
prints(goal_distance, goal_vector)
|
142 |
|
143 |
+
|
144 |
func _physics_process(delta):
|
145 |
+
if ai_controller.heuristic == "human" and not controlled:
|
146 |
+
return
|
147 |
+
if ai_controller.needs_reset or Input.is_action_just_pressed("r_key"):
|
148 |
reset()
|
149 |
return
|
150 |
check_reset_conditions()
|
|
|
164 |
engine_force = engine_force_value
|
165 |
else:
|
166 |
engine_force = 0
|
167 |
+
|
168 |
$Arrow3D.look_at(GameManager.get_next_waypoint(self).position)
|
169 |
+
|
170 |
if not accelerating and get_brake_value():
|
171 |
# Increase engine force at low speeds to make the initial acceleration faster.
|
172 |
if fwd_mps >= -1:
|
|
|
176 |
else:
|
177 |
engine_force = -engine_force_value
|
178 |
else:
|
179 |
+
brake = 1 * brake_force_value
|
180 |
else:
|
181 |
brake = 0.0
|
182 |
|
183 |
steering = move_toward(steering, steer_target, STEER_SPEED * delta)
|
|
|
|
|
|
vehicle.tscn
CHANGED
@@ -1,4 +1,4 @@
|
|
1 |
-
[gd_scene load_steps=
|
2 |
|
3 |
[ext_resource type="Script" path="res://vehicle.gd" id="1"]
|
4 |
[ext_resource type="PackedScene" uid="uid://c7ev21tk2uwfl" path="res://assets/wheelDefault.tscn" id="2_d8d30"]
|
@@ -15,6 +15,7 @@
|
|
15 |
[ext_resource type="PackedScene" uid="uid://x4bpkxih2cfc" path="res://assets/hatchbackSports.tscn" id="12_keoic"]
|
16 |
[ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd" id="14_qlofu"]
|
17 |
[ext_resource type="PackedScene" uid="uid://bs0osbn6djn0i" path="res://arrow_3d.tscn" id="15_irtpm"]
|
|
|
18 |
|
19 |
[sub_resource type="PhysicsMaterial" id="1"]
|
20 |
friction = 0.5
|
@@ -22,7 +23,7 @@ friction = 0.5
|
|
22 |
[sub_resource type="BoxShape3D" id="7"]
|
23 |
size = Vector3(0.954078, 0.576755, 2.32662)
|
24 |
|
25 |
-
[node name="Vehicle" type="VehicleBody3D"
|
26 |
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.00169557, 0.222867, -0.0955184)
|
27 |
collision_layer = 2
|
28 |
center_of_mass_mode = 1
|
@@ -37,7 +38,6 @@ use_as_steering = true
|
|
37 |
wheel_roll_influence = 0.4
|
38 |
wheel_radius = 0.25
|
39 |
wheel_friction_slip = 1.0
|
40 |
-
suspension_travel = 0.2
|
41 |
suspension_stiffness = 40.0
|
42 |
damping_compression = 0.88
|
43 |
|
@@ -50,7 +50,6 @@ use_as_traction = true
|
|
50 |
wheel_roll_influence = 0.4
|
51 |
wheel_radius = 0.25
|
52 |
wheel_friction_slip = 1.0
|
53 |
-
suspension_travel = 0.2
|
54 |
suspension_stiffness = 40.0
|
55 |
damping_compression = 0.88
|
56 |
|
@@ -64,7 +63,6 @@ use_as_steering = true
|
|
64 |
wheel_roll_influence = 0.4
|
65 |
wheel_radius = 0.25
|
66 |
wheel_friction_slip = 1.0
|
67 |
-
suspension_travel = 0.2
|
68 |
suspension_stiffness = 40.0
|
69 |
damping_compression = 0.88
|
70 |
|
@@ -76,7 +74,6 @@ use_as_traction = true
|
|
76 |
wheel_roll_influence = 0.4
|
77 |
wheel_radius = 0.25
|
78 |
wheel_friction_slip = 1.0
|
79 |
-
suspension_travel = 0.2
|
80 |
suspension_stiffness = 40.0
|
81 |
damping_compression = 0.88
|
82 |
|
@@ -362,3 +359,7 @@ target_position = Vector3(52.2964, 10.4421, 59.6326)
|
|
362 |
|
363 |
[node name="node_11 5" type="RayCast3D" parent="RayCastSensor3D"]
|
364 |
target_position = Vector3(51.4973, 17.3152, 58.7215)
|
|
|
|
|
|
|
|
|
|
1 |
+
[gd_scene load_steps=19 format=3 uid="uid://b1bwg2tirgxwk"]
|
2 |
|
3 |
[ext_resource type="Script" path="res://vehicle.gd" id="1"]
|
4 |
[ext_resource type="PackedScene" uid="uid://c7ev21tk2uwfl" path="res://assets/wheelDefault.tscn" id="2_d8d30"]
|
|
|
15 |
[ext_resource type="PackedScene" uid="uid://x4bpkxih2cfc" path="res://assets/hatchbackSports.tscn" id="12_keoic"]
|
16 |
[ext_resource type="Script" path="res://addons/godot_rl_agents/sensors/sensors_3d/RaycastSensor3D.gd" id="14_qlofu"]
|
17 |
[ext_resource type="PackedScene" uid="uid://bs0osbn6djn0i" path="res://arrow_3d.tscn" id="15_irtpm"]
|
18 |
+
[ext_resource type="Script" path="res://AIController3D.gd" id="16_3hqsi"]
|
19 |
|
20 |
[sub_resource type="PhysicsMaterial" id="1"]
|
21 |
friction = 0.5
|
|
|
23 |
[sub_resource type="BoxShape3D" id="7"]
|
24 |
size = Vector3(0.954078, 0.576755, 2.32662)
|
25 |
|
26 |
+
[node name="Vehicle" type="VehicleBody3D"]
|
27 |
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.00169557, 0.222867, -0.0955184)
|
28 |
collision_layer = 2
|
29 |
center_of_mass_mode = 1
|
|
|
38 |
wheel_roll_influence = 0.4
|
39 |
wheel_radius = 0.25
|
40 |
wheel_friction_slip = 1.0
|
|
|
41 |
suspension_stiffness = 40.0
|
42 |
damping_compression = 0.88
|
43 |
|
|
|
50 |
wheel_roll_influence = 0.4
|
51 |
wheel_radius = 0.25
|
52 |
wheel_friction_slip = 1.0
|
|
|
53 |
suspension_stiffness = 40.0
|
54 |
damping_compression = 0.88
|
55 |
|
|
|
63 |
wheel_roll_influence = 0.4
|
64 |
wheel_radius = 0.25
|
65 |
wheel_friction_slip = 1.0
|
|
|
66 |
suspension_stiffness = 40.0
|
67 |
damping_compression = 0.88
|
68 |
|
|
|
74 |
wheel_roll_influence = 0.4
|
75 |
wheel_radius = 0.25
|
76 |
wheel_friction_slip = 1.0
|
|
|
77 |
suspension_stiffness = 40.0
|
78 |
damping_compression = 0.88
|
79 |
|
|
|
359 |
|
360 |
[node name="node_11 5" type="RayCast3D" parent="RayCastSensor3D"]
|
361 |
target_position = Vector3(51.4973, 17.3152, 58.7215)
|
362 |
+
|
363 |
+
[node name="AIController3D" type="Node3D" parent="."]
|
364 |
+
script = ExtResource("16_3hqsi")
|
365 |
+
reset_after = 10000
|