Skip to content

Commit

Permalink
Merge pull request #748 from jtnicholl/3d_navigation
Browse files Browse the repository at this point in the history
Update 3D navigation demo for 4.0
  • Loading branch information
aaronfranke authored Jul 11, 2022
2 parents de27fa1 + 3d7c24e commit 5efee67
Show file tree
Hide file tree
Showing 18 changed files with 172 additions and 278 deletions.
8 changes: 4 additions & 4 deletions 3d/navmesh/README.md → 3d/navigation/README.md
Original file line number Diff line number Diff line change
@@ -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.
Code is provided for polyline following in 3D.

Language: GDScript

Renderer: GLES 3
Renderer: Vulkan Clustered

Check out this demo on the asset library: https://godotengine.org/asset-library/asset/124

Expand Down
11 changes: 11 additions & 0 deletions 3d/navigation/default_env.tres
Original file line number Diff line number Diff line change
@@ -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
File renamed without changes
29 changes: 14 additions & 15 deletions 3d/navmesh/icon.png.import → 3d/navigation/icon.png.import
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
[remap]

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={
"vram_texture": false
}

[deps]

source_file="res://icon.png"
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"]
dest_files=["res://.godot/imported/icon.png-487276ed1e3a0c39cad0279d744ee560.ctex"]

[params]

compress/mode=0
compress/lossy_quality=0.7
compress/hdr_mode=0
compress/hdr_compression=1
compress/bptc_ldr=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/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=true
svg/scale=1.0
process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
Binary file added 3d/navigation/level_mesh.res
Binary file not shown.
77 changes: 36 additions & 41 deletions 3d/navmesh/navmesh.gd → 3d/navigation/navmesh.gd
Original file line number Diff line number Diff line change
@@ -1,34 +1,30 @@
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():
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,
# 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
# and where we want to go.
var destination = path[0]
var destination := path[0]
direction = destination - robot.position

# If the next node is closer than we intend to 'step', then
Expand All @@ -37,7 +33,7 @@ func _physics_process(delta):
if step_size > direction.length():
step_size = direction.length()
# 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.
# TODO: This information should be set to the CharacterBody properties instead of arguments.
Expand All @@ -51,39 +47,38 @@ func _physics_process(delta):
if direction:
# Direction is relative, so apply it to the robot's location to
# 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.
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:
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:
draw_path(path)

if event is InputEventMouseMotion:
elif event is InputEventMouseMotion:
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()
Binary file added 3d/navigation/navmesh.res
Binary file not shown.
51 changes: 51 additions & 0 deletions 3d/navigation/navmesh.tscn
Original file line number Diff line number Diff line change
@@ -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")
File renamed without changes
36 changes: 36 additions & 0 deletions 3d/navigation/particle.png.import
Original file line number Diff line number Diff line change
@@ -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 changes: 20 additions & 0 deletions 3d/navigation/project.godot
Original file line number Diff line number Diff line change
@@ -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"
Binary file added 3d/navigation/robot.res
Binary file not shown.
File renamed without changes.
File renamed without changes
9 changes: 0 additions & 9 deletions 3d/navmesh/default_env.tres

This file was deleted.

140 changes: 0 additions & 140 deletions 3d/navmesh/navmesh.tscn

This file was deleted.

37 changes: 0 additions & 37 deletions 3d/navmesh/particle.png.import

This file was deleted.

32 changes: 0 additions & 32 deletions 3d/navmesh/project.godot

This file was deleted.

0 comments on commit 5efee67

Please sign in to comment.