mirror of
https://github.com/godotengine/godot-demo-projects.git
synced 2026-01-05 10:09:47 +03:00
Merge pull request #748 from jtnicholl/3d_navigation
Update 3D navigation demo for 4.0
This commit is contained in:
@@ -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
|
||||
|
||||
11
3d/navigation/default_env.tres
Normal file
11
3d/navigation/default_env.tres
Normal file
@@ -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
|
||||
|
Before Width: | Height: | Size: 2.6 KiB After Width: | Height: | Size: 2.6 KiB |
@@ -1,8 +1,9 @@
|
||||
[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
|
||||
}
|
||||
@@ -10,26 +11,24 @@ metadata={
|
||||
[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
|
||||
BIN
3d/navigation/level_mesh.res
Normal file
BIN
3d/navigation/level_mesh.res
Normal file
Binary file not shown.
@@ -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
|
||||
@@ -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.
|
||||
@@ -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)
|
||||
cam_rotation += event.relative.x * 0.005
|
||||
$CameraBase.set_rotation(Vector3(0, cam_rotation, 0))
|
||||
|
||||
|
||||
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()
|
||||
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
Normal file
BIN
3d/navigation/navmesh.res
Normal file
Binary file not shown.
51
3d/navigation/navmesh.tscn
Normal file
51
3d/navigation/navmesh.tscn
Normal file
@@ -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")
|
||||
|
Before Width: | Height: | Size: 129 B After Width: | Height: | Size: 129 B |
36
3d/navigation/particle.png.import
Normal file
36
3d/navigation/particle.png.import
Normal file
@@ -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
3d/navigation/project.godot
Normal file
20
3d/navigation/project.godot
Normal file
@@ -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
Normal file
BIN
3d/navigation/robot.res
Normal file
Binary file not shown.
|
Before Width: | Height: | Size: 21 KiB After Width: | Height: | Size: 21 KiB |
@@ -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 one or more lines are too long
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user