Skip to content

Commit

Permalink
Add a command-line argument to toggle visuals in the physics and navi…
Browse files Browse the repository at this point in the history
…gation agents benchmarks (#85)

* Add an option to toggle visuals in the moving navigation agents benchmarks
* Toggle visuals off by default
* Create a new visualize command line argument
  • Loading branch information
OverloadedOrama authored Jul 15, 2024
1 parent 6606590 commit ed0d1f6
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 48 deletions.
9 changes: 6 additions & 3 deletions benchmarks/navigation/moving_agents_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ class TestScene:
add_child(nav_region)

for i in n_of_agents:
var agent_parent := Sprite2D.new()
agent_parent.scale = Vector2(0.1, 0.1)
agent_parent.texture = ICON
var agent_parent := Node2D.new()
agent_parent.position = _rand_pos()
var agent := NavigationAgent2D.new()
agent.avoidance_enabled = true
agent.target_position = _rand_pos()
agent_parent.add_child(agent)
add_child(agent_parent)
agents.append(agent_parent)
if Manager.visualize:
var sprite := Sprite2D.new()
sprite.scale = Vector2(0.1, 0.1)
sprite.texture = ICON
agent_parent.add_child(sprite)

func _physics_process(delta: float) -> void:
if NavigationServer2D.map_get_iteration_id(get_world_2d().navigation_map) == 0:
Expand Down
13 changes: 9 additions & 4 deletions benchmarks/navigation/moving_agents_3d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class TestScene:
func _ready() -> void:
sponza = SPONZA_SCENE.instantiate()
add_child(sponza)
sponza.get_node("DirectionalLight3D").queue_free()
sponza.get_node("OmniLights").queue_free()
sponza.get_node("Camera3D").current = Manager.visualize
var nav_region := NavigationRegion3D.new()
nav_region.navigation_mesh = NAVMESH
sponza.add_child(nav_region)
Expand All @@ -34,17 +37,19 @@ class TestScene:
mesh.material = material

for i in n_of_agents:
var agent_parent := MeshInstance3D.new()
agent_parent.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
agent_parent.mesh = mesh
var agent_parent := Node3D.new()
agent_parent.position = _rand_pos()

var agent := NavigationAgent3D.new()
agent.avoidance_enabled = true
agent.target_position = _rand_pos()
agent_parent.add_child(agent)
sponza.add_child(agent_parent)
agents.append(agent_parent)
if Manager.visualize:
var mesh_instance := MeshInstance3D.new()
mesh_instance.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
mesh_instance.mesh = mesh
agent_parent.add_child(mesh_instance)

func _physics_process(delta: float) -> void:
if NavigationServer3D.map_get_iteration_id(get_world_3d().navigation_map) == 0:
Expand Down
8 changes: 3 additions & 5 deletions benchmarks/physics/area_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ class TestScene:

var num_character_bodies: int
var num_area_2d: int
var visualize := true
var area_2d_nodes: Array[Area2D]
var window_size: Vector2i
var time_accum := 0.0
Expand All @@ -21,10 +20,9 @@ class TestScene:
SphereMesh.new(),
]

func _init(_num_character_bodies: int, _num_area_2d: int, _visualize: bool) -> void:
func _init(_num_character_bodies: int, _num_area_2d: int) -> void:
num_character_bodies = _num_character_bodies
num_area_2d = _num_area_2d
visualize = _visualize

meshes[0].size = Vector2(20.0, 20.0)
meshes[1].radius = 10.0
Expand Down Expand Up @@ -60,7 +58,7 @@ class TestScene:
var r := randi() % shapes.size()
var collision_shape := CollisionShape2D.new()
collision_shape.shape = shapes[r]
if visualize:
if Manager.visualize:
var mesh_instance := MeshInstance2D.new()
mesh_instance.mesh = meshes[r]
parent.add_child(mesh_instance)
Expand All @@ -74,4 +72,4 @@ func _init() -> void:


func benchmark_1000_area_2d() -> TestScene:
return TestScene.new(2000, 1000, true)
return TestScene.new(2000, 1000)
10 changes: 4 additions & 6 deletions benchmarks/physics/area_3d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class TestScene:

var num_character_bodies: int
var num_area_3d: int
var visualize := true
var area_3d_nodes: Array[Area3D]
var time_accum := 0.0
var shapes: Array[Shape3D] = [
Expand All @@ -23,13 +22,12 @@ class TestScene:
SphereMesh.new(),
]

func _init(_num_character_bodies: int, _num_area_3d: int, _visualize: bool) -> void:
func _init(_num_character_bodies: int, _num_area_3d: int) -> void:
num_character_bodies = _num_character_bodies
num_area_3d = _num_area_3d
visualize = _visualize

func _ready() -> void:
if visualize:
if Manager.visualize:
var camera := Camera3D.new()
camera.position = Vector3(0.0, 20.0, 20.0)
camera.rotate_x(-0.8)
Expand Down Expand Up @@ -64,7 +62,7 @@ class TestScene:
var r := randi() % shapes.size()
var collision_shape := CollisionShape3D.new()
collision_shape.shape = shapes[r]
if visualize:
if Manager.visualize:
var mesh_instance := MeshInstance3D.new()
mesh_instance.mesh = meshes[r]
parent.add_child(mesh_instance)
Expand All @@ -78,4 +76,4 @@ func _init() -> void:


func benchmark_1000_area_3d() -> TestScene:
return TestScene.new(2000, 1000, true)
return TestScene.new(2000, 1000)
8 changes: 3 additions & 5 deletions benchmarks/physics/character_body_2d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ class TestScene:
var tile_map_scene := preload("res://supplemental/tilemap_scene.tscn")
var tile_map_node: Node2D
var n_of_character_bodies: int
var visualize := true
var character_bodies: Array[CharacterBody2D] = []
var capsule_mesh := CapsuleMesh.new()
var window_size: Vector2i

func _init(_n_of_character_bodies: int, _visualize: bool) -> void:
func _init(_n_of_character_bodies: int) -> void:
n_of_character_bodies = _n_of_character_bodies
visualize = _visualize
capsule_mesh.radius = 10.0
capsule_mesh.height = 28.0

Expand All @@ -33,7 +31,7 @@ class TestScene:
var collision_shape := CollisionShape2D.new()
collision_shape.shape = CapsuleShape2D.new()
body.add_child(collision_shape)
if visualize:
if Manager.visualize:
var mesh_instance := MeshInstance2D.new()
mesh_instance.mesh = capsule_mesh
body.add_child(mesh_instance)
Expand Down Expand Up @@ -64,4 +62,4 @@ func _init() -> void:


func benchmark_1000_character_bodies_2d() -> TestScene:
return TestScene.new(1000, true)
return TestScene.new(1000)
8 changes: 3 additions & 5 deletions benchmarks/physics/character_body_3d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,11 @@ class TestScene:
var grid_map_scene := preload("res://supplemental/gridmap_scene.tscn")
var grid_map_node: Node3D
var n_of_character_bodies: int
var visualize := true
var character_bodies: Array[CharacterBody3D] = []
var capsule_mesh := CapsuleMesh.new()

func _init(_n_of_character_bodies: int, _visualize: bool) -> void:
func _init(_n_of_character_bodies: int) -> void:
n_of_character_bodies = _n_of_character_bodies
visualize = _visualize

func _ready() -> void:
grid_map_node = grid_map_scene.instantiate()
Expand All @@ -32,7 +30,7 @@ class TestScene:
var collision_shape := CollisionShape3D.new()
collision_shape.shape = CapsuleShape3D.new()
body.add_child(collision_shape)
if visualize:
if Manager.visualize:
var mesh_instance := MeshInstance3D.new()
mesh_instance.mesh = capsule_mesh
body.add_child(mesh_instance)
Expand Down Expand Up @@ -63,4 +61,4 @@ func _init() -> void:


func benchmark_1000_character_bodies_3d() -> TestScene:
return TestScene.new(1000, true)
return TestScene.new(1000)
7 changes: 3 additions & 4 deletions benchmarks/physics/rigid_body_2d.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
extends Benchmark

const VISUALIZE := true
const SPREAD_H := 1600.0;
const SPREAD_V := 800.0;

Expand All @@ -22,7 +21,7 @@ func _init() -> void:
func setup_scene(create_body_func: Callable, unique_shape: bool, ccd_mode: RigidBody2D.CCDMode, boundary: bool, num_shapes: int) -> Node2D:
var scene_root := Node2D.new()

if VISUALIZE:
if Manager.visualize:
var camera := Camera2D.new()
camera.position = Vector2(0.0, -100.0)
camera.zoom = Vector2(0.5, 0.5)
Expand Down Expand Up @@ -61,7 +60,7 @@ func create_square(unique_shape: bool, ccd_mode: RigidBody2D.CCDMode) -> RigidBo
var collision_shape := CollisionShape2D.new()
rigid_body.continuous_cd = ccd_mode

if VISUALIZE:
if Manager.visualize:
var mesh_instance := MeshInstance2D.new()
mesh_instance.mesh = square_mesh
rigid_body.add_child(mesh_instance)
Expand All @@ -81,7 +80,7 @@ func create_circle(unique_shape: bool, ccd_mode: RigidBody2D.CCDMode) -> RigidBo
var collision_shape := CollisionShape2D.new()
rigid_body.continuous_cd = ccd_mode

if VISUALIZE:
if Manager.visualize:
var mesh_instance := MeshInstance2D.new()
mesh_instance.mesh = circle_mesh
rigid_body.add_child(mesh_instance)
Expand Down
8 changes: 4 additions & 4 deletions benchmarks/physics/rigid_body_3d.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
extends Benchmark

const VISUALIZE := true

const SPREAD_H := 20.0;
const SPREAD_V := 10.0;

Expand All @@ -19,7 +19,7 @@ func _init() -> void:
func setup_scene(create_body_func: Callable, unique_shape: bool, ccd_mode: bool, boundary: bool, num_shapes: int) -> Node3D:
var scene_root := Node3D.new()

if VISUALIZE:
if Manager.visualize:
var camera := Camera3D.new()
camera.position = Vector3(0.0, 20.0, 20.0)
camera.rotate_x(-0.8)
Expand Down Expand Up @@ -60,7 +60,7 @@ func create_box(unique_shape: bool, ccd_mode: bool) -> RigidBody3D:
var collision_shape := CollisionShape3D.new()
rigid_body.continuous_cd = ccd_mode

if VISUALIZE:
if Manager.visualize:
var mesh_instance := MeshInstance3D.new()
mesh_instance.mesh = box_mesh
rigid_body.add_child(mesh_instance)
Expand All @@ -80,7 +80,7 @@ func create_sphere(unique_shape: bool, ccd_mode: bool) -> RigidBody3D:
var collision_shape := CollisionShape3D.new()
rigid_body.continuous_cd = ccd_mode

if VISUALIZE:
if Manager.visualize:
var mesh_instance := MeshInstance3D.new()
mesh_instance.mesh = sphere_mesh
rigid_body.add_child(mesh_instance)
Expand Down
10 changes: 4 additions & 6 deletions benchmarks/physics/softbody_3d.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ class TestScene:
extends Node3D

var n_of_rigidbodies: int
var visualize := true
var shapes: Array[Shape3D] = [
BoxShape3D.new(),
CapsuleShape3D.new(),
Expand All @@ -20,9 +19,8 @@ class TestScene:
SphereMesh.new(),
]

func _init(_n_of_rigidbodies: int, _visualize: bool) -> void:
func _init(_n_of_rigidbodies: int) -> void:
n_of_rigidbodies = _n_of_rigidbodies
visualize = _visualize

func _ready() -> void:
var softbody := SoftBody3D.new()
Expand All @@ -36,7 +34,7 @@ class TestScene:
softbody.set_point_pinned(275, true)
softbody.set_point_pinned(517, true)
add_child(softbody)
if visualize:
if Manager.visualize:
var camera := Camera3D.new()
camera.position = Vector3(0.0, 20.0, 20.0)
camera.rotate_x(-0.8)
Expand All @@ -56,7 +54,7 @@ class TestScene:
var r := randi() % shapes.size()
var collision_shape := CollisionShape3D.new()
collision_shape.shape = shapes[r]
if visualize:
if Manager.visualize:
var mesh_instance := MeshInstance3D.new()
mesh_instance.mesh = meshes[r]
parent.add_child(mesh_instance)
Expand All @@ -70,4 +68,4 @@ func _init() -> void:


func benchmark_softbody_3d_500_rigidbodies() -> TestScene:
return TestScene.new(500, true)
return TestScene.new(500)
10 changes: 4 additions & 6 deletions benchmarks/physics/triangle_mesh.gd
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ class TestScene:

var collision_scene := preload("res://supplemental/8ball.glb")
var n_of_rigidbodies: int
var visualize := true
var shapes: Array[Shape3D] = [
BoxShape3D.new(),
CapsuleShape3D.new(),
Expand All @@ -21,16 +20,15 @@ class TestScene:
SphereMesh.new(),
]

func _init(_n_of_rigidbodies: int, _visualize: bool) -> void:
func _init(_n_of_rigidbodies: int) -> void:
n_of_rigidbodies = _n_of_rigidbodies
visualize = _visualize

func _ready() -> void:
var collision_node := collision_scene.instantiate() as Node3D
collision_node.rotate_y(PI / 2.0)
collision_node.scale = Vector3(7, 7, 7)
add_child(collision_node)
if visualize:
if Manager.visualize:
var camera := Camera3D.new()
camera.position = Vector3(0.0, 20.0, 20.0)
camera.rotate_x(-0.8)
Expand All @@ -49,7 +47,7 @@ class TestScene:
var r := randi() % shapes.size()
var collision_shape := CollisionShape3D.new()
collision_shape.shape = shapes[r]
if visualize:
if Manager.visualize:
var mesh_instance := MeshInstance3D.new()
mesh_instance.mesh = meshes[r]
parent.add_child(mesh_instance)
Expand All @@ -63,4 +61,4 @@ func _init() -> void:


func benchmark_triangle_mesh_3d_1000_rigidbodies() -> TestScene:
return TestScene.new(1000, true)
return TestScene.new(1000)
2 changes: 2 additions & 0 deletions main.gd
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var arg_exclude_benchmarks := ""
var arg_save_json := ""
var arg_json_results_prefix := ""
var arg_run_benchmarks := false
var arg_visualize := false

@onready var tree := $Tree as Tree
var categories := {}
Expand All @@ -34,6 +35,7 @@ func _ready() -> void:

print("Variable %s set by command line to %s" % [var_name, self.get(var_name)])

Manager.visualize = arg_visualize
# No point in copying JSON without any results yet.
$CopyJSON.visible = false

Expand Down
1 change: 1 addition & 0 deletions manager.gd
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ var test_results := {}

var save_json_to_path := ""
var json_results_prefix := ""
var visualize := false


## Recursively walks the given directory and returns all files found
Expand Down

0 comments on commit ed0d1f6

Please sign in to comment.