Browse Source

Update 3D navigation demo

Jonathan Nicholl 3 years ago
parent
commit
3d7c24ef1b

+ 4 - 4
3d/navmesh/README.md → 3d/navigation/README.md

@@ -1,13 +1,13 @@
-# 3D Navigation Mesh
+# 3D Navigation
 
 
-Navigation mesh demo for 3D scenes, with a character
-able to pathfind around a complex 3D environment.
+Navigation demo for 3D scenes, with a character
+able to pathfind around a static 3D environment.
 The navigation path is drawn using a line.
 The navigation path is drawn using a line.
 Code is provided for polyline following in 3D.
 Code is provided for polyline following in 3D.
 
 
 Language: GDScript
 Language: GDScript
 
 
-Renderer: GLES 3
+Renderer: Vulkan Clustered
 
 
 Check out this demo on the asset library: https://godotengine.org/asset-library/asset/124
 Check out this demo on the asset library: https://godotengine.org/asset-library/asset/124
 
 

+ 11 - 0
3d/navigation/default_env.tres

@@ -0,0 +1,11 @@
+[gd_resource type="Environment" load_steps=3 format=3 uid="uid://c115tt1j1r0g0"]
+
+[sub_resource type="ProceduralSkyMaterial" id="ProceduralSkyMaterial_3brqh"]
+
+[sub_resource type="Sky" id="1"]
+sky_material = SubResource("ProceduralSkyMaterial_3brqh")
+
+[resource]
+background_mode = 2
+sky = SubResource("1")
+tonemap_mode = 2

+ 0 - 0
3d/navmesh/icon.png → 3d/navigation/icon.png


+ 14 - 15
3d/navmesh/icon.png.import → 3d/navigation/icon.png.import

@@ -1,8 +1,9 @@
 [remap]
 [remap]
 
 
 importer="texture"
 importer="texture"
-type="StreamTexture2D"
-path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
+type="CompressedTexture2D"
+uid="uid://cpa2d0lxfn2mj"
+path="res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"
 metadata={
 metadata={
 "vram_texture": false
 "vram_texture": false
 }
 }
@@ -10,26 +11,24 @@ metadata={
 [deps]
 [deps]
 
 
 source_file="res://icon.png"
 source_file="res://icon.png"
-dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"]
+dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]
 
 
 [params]
 [params]
 
 
 compress/mode=0
 compress/mode=0
 compress/lossy_quality=0.7
 compress/lossy_quality=0.7
-compress/hdr_mode=0
+compress/hdr_compression=1
 compress/bptc_ldr=0
 compress/bptc_ldr=0
 compress/normal_map=0
 compress/normal_map=0
-flags/repeat=0
-flags/filter=true
-flags/mipmaps=false
-flags/anisotropic=false
-flags/srgb=2
+compress/channel_pack=0
+mipmaps/generate=false
+mipmaps/limit=-1
+roughness/mode=0
+roughness/src_normal=""
 process/fix_alpha_border=true
 process/fix_alpha_border=true
 process/premult_alpha=false
 process/premult_alpha=false
-process/HDR_as_SRGB=false
-process/invert_color=false
 process/normal_map_invert_y=false
 process/normal_map_invert_y=false
-stream=false
-size_limit=0
-detect_3d=true
-svg/scale=1.0
+process/hdr_as_srgb=false
+process/hdr_clamp_exposure=false
+process/size_limit=0
+detect_3d/compress_to=1

BIN
3d/navigation/level_mesh.res


+ 36 - 41
3d/navmesh/navmesh.gd → 3d/navigation/navmesh.gd

@@ -1,34 +1,30 @@
 extends Node3D
 extends Node3D
 
 
-const SPEED = 10.0
+const SPEED := 10.0
 
 
-var camrot = 0.0
-var m = StandardMaterial3D.new()
+@export var show_path := true
 
 
-var path = []
-var show_path = true
+var cam_rotation := 0.0
+var path: PackedVector3Array
 
 
-@onready var robot  = get_node(^"RobotBase")
-@onready var camera = get_node(^"CameraBase/Camera3D")
+@onready var robot: Position3D = $RobotBase
+@onready var camera: Camera3D = $CameraBase/Camera3D
 
 
 func _ready():
 func _ready():
 	set_process_input(true)
 	set_process_input(true)
-	m.flags_unshaded = true
-	m.flags_use_point_size = true
-	m.albedo_color = Color.WHITE
 
 
 
 
-func _physics_process(delta):
-	var direction = Vector3()
+func _physics_process(delta: float):
+	var direction := Vector3()
 
 
 	# We need to scale the movement speed by how much delta has passed,
 	# We need to scale the movement speed by how much delta has passed,
 	# otherwise the motion won't be smooth.
 	# otherwise the motion won't be smooth.
-	var step_size = delta * SPEED
+	var step_size := delta * SPEED
 
 
-	if path.size() > 0:
+	if not path.is_empty():
 		# Direction is the difference between where we are now
 		# Direction is the difference between where we are now
 		# and where we want to go.
 		# and where we want to go.
-		var destination = path[0]
+		var destination := path[0]
 		direction = destination - robot.position
 		direction = destination - robot.position
 
 
 		# If the next node is closer than we intend to 'step', then
 		# If the next node is closer than we intend to 'step', then
@@ -37,7 +33,7 @@ func _physics_process(delta):
 		if step_size > direction.length():
 		if step_size > direction.length():
 			step_size = direction.length()
 			step_size = direction.length()
 			# We should also remove this node since we're about to reach it.
 			# We should also remove this node since we're about to reach it.
-			path.remove(0)
+			path.remove_at(0)
 
 
 		# Move the robot towards the path node, by how far we want to travel.
 		# Move the robot towards the path node, by how far we want to travel.
 		# TODO: This information should be set to the CharacterBody properties instead of arguments.
 		# TODO: This information should be set to the CharacterBody properties instead of arguments.
@@ -51,39 +47,38 @@ func _physics_process(delta):
 		if direction:
 		if direction:
 			# Direction is relative, so apply it to the robot's location to
 			# Direction is relative, so apply it to the robot's location to
 			# get a point we can actually look at.
 			# get a point we can actually look at.
-			var look_at_point = robot.position + direction.normalized()
+			var look_at_point := robot.position + direction.normalized()
 			# Make the robot look at the point.
 			# Make the robot look at the point.
 			robot.look_at(look_at_point, Vector3.UP)
 			robot.look_at(look_at_point, Vector3.UP)
 
 
 
 
-func _unhandled_input(event):
+func _unhandled_input(event: InputEvent):
 	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
 	if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
-		var from = camera.project_ray_origin(event.position)
-		var to = from + camera.project_ray_normal(event.position) * 1000
-		var target_point = get_closest_point_to_segment(from, to)
+		var map := get_world_3d().navigation_map
+		var from := camera.project_ray_origin(event.position)
+		var to := from + camera.project_ray_normal(event.position) * 1000
+		var target_point := NavigationServer3D.map_get_closest_point_to_segment(map, from, to)
 
 
-		# Set the path between the robots current location and our target.
-		path = get_simple_path(robot.position, target_point, true)
+		# Set the path between the robot's current location and our target.
+		path = NavigationServer3D.map_get_path(map, robot.position, target_point, true)
 
 
 		if show_path:
 		if show_path:
 			draw_path(path)
 			draw_path(path)
 
 
-	if event is InputEventMouseMotion:
+	elif event is InputEventMouseMotion:
 		if event.button_mask & (MOUSE_BUTTON_MASK_MIDDLE + MOUSE_BUTTON_MASK_RIGHT):
 		if event.button_mask & (MOUSE_BUTTON_MASK_MIDDLE + MOUSE_BUTTON_MASK_RIGHT):
-			camrot += event.relative.x * 0.005
-			get_node(^"CameraBase").set_rotation(Vector3(0, camrot, 0))
-			print("Camera3D Rotation: ", camrot)
-
-
-func draw_path(path_array):
-	var im = get_node(^"Draw")
-	im.set_material_override(m)
-	im.clear()
-	im.begin(Mesh.PRIMITIVE_POINTS, null)
-	im.add_vertex(path_array[0])
-	im.add_vertex(path_array[path_array.size() - 1])
-	im.end()
-	im.begin(Mesh.PRIMITIVE_LINE_STRIP, null)
-	for x in path:
-		im.add_vertex(x)
-	im.end()
+			cam_rotation += event.relative.x * 0.005
+			$CameraBase.set_rotation(Vector3(0, cam_rotation, 0))
+
+
+func draw_path(path_array: PackedVector3Array) -> void:
+	var im: ImmediateMesh = $DrawPath.mesh
+	im.clear_surfaces()
+	im.surface_begin(Mesh.PRIMITIVE_POINTS, null)
+	im.surface_add_vertex(path_array[0])
+	im.surface_add_vertex(path_array[path_array.size() - 1])
+	im.surface_end()
+	im.surface_begin(Mesh.PRIMITIVE_LINE_STRIP, null)
+	for current_vector in path:
+		im.surface_add_vertex(current_vector)
+	im.surface_end()

BIN
3d/navigation/navmesh.res


+ 51 - 0
3d/navigation/navmesh.tscn

@@ -0,0 +1,51 @@
+[gd_scene load_steps=8 format=3 uid="uid://dkhg8e00d02f2"]
+
+[ext_resource type="Script" path="res://navmesh.gd" id="1"]
+[ext_resource type="NavigationMesh" uid="uid://xl4o2ckjxava" path="res://navmesh.res" id="2_lcfvj"]
+[ext_resource type="Environment" uid="uid://c115tt1j1r0g0" path="res://default_env.tres" id="3"]
+[ext_resource type="ArrayMesh" uid="uid://gmytdjp2bcsq" path="res://level_mesh.res" id="3_1cas0"]
+[ext_resource type="ArrayMesh" uid="uid://prwe6io8p1iv" path="res://robot.res" id="5_ple0n"]
+
+[sub_resource type="StandardMaterial3D" id="StandardMaterial3D_noou6"]
+shading_mode = 0
+use_point_size = true
+
+[sub_resource type="ImmediateMesh" id="ImmediateMesh_dvj5w"]
+
+[node name="Node3D" type="Node3D"]
+_import_path = NodePath(".")
+script = ExtResource("1")
+
+[node name="NavigationRegion3D" type="NavigationRegion3D" parent="."]
+navmesh = ExtResource("2_lcfvj")
+
+[node name="LevelMesh" type="MeshInstance3D" parent="NavigationRegion3D"]
+transform = Transform3D(2, 0, 0, 0, 2, 0, 0, 0, 2, 0, -0.0452547, 0)
+mesh = ExtResource("3_1cas0")
+
+[node name="DirectionalLight3D" type="DirectionalLight3D" parent="."]
+transform = Transform3D(0.623013, -0.733525, 0.271654, 0.321394, 0.55667, 0.766044, -0.713134, -0.389948, 0.582563, 10.0773, 5.02381, 0)
+light_energy = 5.0
+shadow_enabled = true
+
+[node name="WorldEnvironment" type="WorldEnvironment" parent="."]
+environment = ExtResource("3")
+
+[node name="DrawPath" type="MeshInstance3D" parent="."]
+material_override = SubResource("StandardMaterial3D_noou6")
+mesh = SubResource("ImmediateMesh_dvj5w")
+
+[node name="CameraBase" type="Node3D" parent="."]
+transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 2.07475, 0, 1.96678)
+
+[node name="Camera3D" type="Camera3D" parent="CameraBase"]
+transform = Transform3D(-0.560554, -0.429252, 0.708182, 0.106298, 0.8108, 0.575591, -0.821267, 0.397928, -0.408869, 18.091, 14.744, -7.017)
+fov = 50.0
+near = 0.1
+
+[node name="RobotBase" type="Position3D" parent="."]
+
+[node name="Robot" type="MeshInstance3D" parent="RobotBase"]
+transform = Transform3D(-0.5, 0, -7.54979e-08, 0, 1, 0, 7.54979e-08, 0, -0.5, 0, 1, 0)
+gi_mode = 2
+mesh = ExtResource("5_ple0n")

+ 0 - 0
3d/navmesh/particle.png → 3d/navigation/particle.png


+ 36 - 0
3d/navigation/particle.png.import

@@ -0,0 +1,36 @@
+[remap]
+
+importer="texture"
+type="CompressedTexture2D"
+uid="uid://c27ndr5sgvyr8"
+path.s3tc="res://.godot/imported/particle.png-c2ba3d91e96c62035d672392a1197218.s3tc.ctex"
+path.etc2="res://.godot/imported/particle.png-c2ba3d91e96c62035d672392a1197218.etc2.ctex"
+metadata={
+"imported_formats": ["s3tc", "etc2"],
+"vram_texture": true
+}
+
+[deps]
+
+source_file="res://particle.png"
+dest_files=["res://.godot/imported/particle.png-c2ba3d91e96c62035d672392a1197218.s3tc.ctex", "res://.godot/imported/particle.png-c2ba3d91e96c62035d672392a1197218.etc2.ctex"]
+
+[params]
+
+compress/mode=2
+compress/lossy_quality=0.7
+compress/hdr_compression=1
+compress/bptc_ldr=0
+compress/normal_map=0
+compress/channel_pack=0
+mipmaps/generate=false
+mipmaps/limit=-1
+roughness/mode=0
+roughness/src_normal=""
+process/fix_alpha_border=true
+process/premult_alpha=false
+process/normal_map_invert_y=false
+process/hdr_as_srgb=false
+process/hdr_clamp_exposure=false
+process/size_limit=0
+detect_3d/compress_to=1

+ 20 - 0
3d/navigation/project.godot

@@ -0,0 +1,20 @@
+; Engine configuration file.
+; It's best edited using the editor UI and not directly,
+; since the parameters that go here are not all obvious.
+;
+; Format:
+;   [section] ; section goes between []
+;   param=value ; assign values to parameters
+
+config_version=5
+
+[application]
+
+config/name="3D Navigation"
+config/description="Navigation demo for 3D scenes, with a character
+able to pathfind around a static 3D environment.
+The navigation path is drawn using a line.
+Code is provided for polyline following in 3D."
+run/main_scene="res://navmesh.tscn"
+config/features=PackedStringArray("4.0")
+config/icon="res://icon.png"

BIN
3d/navigation/robot.res


+ 0 - 0
3d/navmesh/screenshots/.gdignore → 3d/navigation/screenshots/.gdignore


+ 0 - 0
3d/navmesh/screenshots/nav.png → 3d/navigation/screenshots/nav.png


+ 0 - 9
3d/navmesh/default_env.tres

@@ -1,9 +0,0 @@
-[gd_resource type="Environment" load_steps=2 format=2]
-
-[sub_resource type="Sky" id=1]
-ground_horizon_color = Color( 0.156863, 0.184314, 0.211765, 1 )
-
-[resource]
-background_mode = 2
-background_sky = SubResource( 1 )
-ssao_blur = 1

File diff suppressed because it is too large
+ 0 - 6
3d/navmesh/navmesh.tscn


+ 0 - 37
3d/navmesh/particle.png.import

@@ -1,37 +0,0 @@
-[remap]
-
-importer="texture"
-type="StreamTexture2D"
-path.s3tc="res://.godot/imported/particle.png-c2ba3d91e96c62035d672392a1197218.s3tc.stex"
-path.etc="res://.godot/imported/particle.png-c2ba3d91e96c62035d672392a1197218.etc.stex"
-metadata={
-"imported_formats": ["s3tc", "etc"],
-"vram_texture": true
-}
-
-[deps]
-
-source_file="res://particle.png"
-dest_files=["res://.godot/imported/particle.png-c2ba3d91e96c62035d672392a1197218.s3tc.stex", "res://.godot/imported/particle.png-c2ba3d91e96c62035d672392a1197218.etc.stex"]
-
-[params]
-
-compress/mode=2
-compress/lossy_quality=0.7
-compress/hdr_mode=0
-compress/bptc_ldr=0
-compress/normal_map=0
-flags/repeat=true
-flags/filter=true
-flags/mipmaps=true
-flags/anisotropic=false
-flags/srgb=1
-process/fix_alpha_border=true
-process/premult_alpha=false
-process/HDR_as_SRGB=false
-process/invert_color=false
-process/normal_map_invert_y=false
-stream=false
-size_limit=0
-detect_3d=false
-svg/scale=1.0

+ 0 - 32
3d/navmesh/project.godot

@@ -1,32 +0,0 @@
-; Engine configuration file.
-; It's best edited using the editor UI and not directly,
-; since the parameters that go here are not all obvious.
-;
-; Format:
-;   [section] ; section goes between []
-;   param=value ; assign values to parameters
-
-config_version=4
-
-[application]
-
-config/name="3D Navigation Mesh"
-config/description="Navigation mesh demo for 3D scenes, with a character
-able to pathfind around a complex 3D environment.
-The navigation path is drawn using a line.
-Code is provided for polyline following in 3D."
-run/main_scene="res://navmesh.tscn"
-config/icon="res://icon.png"
-
-[gdnative]
-
-singletons=[]
-
-[rendering]
-
-quality/driver/driver_name="GLES2"
-quality/intended_usage/framebuffer_allocation=3
-vram_compression/import_etc=true
-vram_compression/import_etc2=false
-quality/shadows/filter_mode=2
-quality/filters/msaa=2

Some files were not shown because too many files changed in this diff