победа над водопадами, шейдеры m2
This commit is contained in:
@@ -1,5 +1,9 @@
|
||||
extends CharacterBody3D
|
||||
|
||||
const GEOSET_CONTROLLER_SCRIPT := preload("res://src/scenes/character/character_geoset_controller.gd")
|
||||
const TEXTURE_COMPOSITOR_SCRIPT := preload("res://src/scenes/character/character_texture_compositor.gd")
|
||||
const OUTFIT_RESOLVER_SCRIPT := preload("res://src/scenes/character/wow_character_outfit_resolver.gd")
|
||||
|
||||
const TILE_SIZE := 533.33333
|
||||
const CHUNK_SIZE := TILE_SIZE / 16.0
|
||||
const UNIT_SIZE := CHUNK_SIZE / 8.0
|
||||
@@ -14,26 +18,39 @@ const UNIT_SIZE := CHUNK_SIZE / 8.0
|
||||
@export var backward_speed: float = 4.5
|
||||
@export var strafe_speed: float = 4.5
|
||||
@export var sprint_multiplier: float = 6.0
|
||||
@export var flight_vertical_speed: float = 7.0
|
||||
@export var acceleration: float = 28.0
|
||||
@export var mouse_sensitivity: float = 0.003
|
||||
@export var camera_pitch_min: float = deg_to_rad(-65.0)
|
||||
@export var camera_pitch_max: float = deg_to_rad(35.0)
|
||||
@export var camera_height: float = 1.7
|
||||
@export var camera_distance: float = 8.0
|
||||
@export var camera_min_distance: float = 2.0
|
||||
@export var camera_max_distance: float = 18.0
|
||||
@export var camera_zoom_step: float = 1.0
|
||||
@export var ground_offset: float = 0.05
|
||||
@export var ground_snap_speed: float = 24.0
|
||||
|
||||
@export var camera_pivot_path: NodePath = NodePath("CameraPivot")
|
||||
@export var camera_path: NodePath = NodePath("CameraPivot/Camera3D")
|
||||
@export var visual_path: NodePath = NodePath("Visual")
|
||||
@export var character_model_path: String = "res://src/resources/characters/HUMAN/MALE/HumanMale.glb"
|
||||
@export var character_class_id: int = 1
|
||||
@export var character_scale: float = 1.0
|
||||
@export var character_yaw_offset_degrees: float = 90.0
|
||||
@export var animation_blend_time: float = 0.15
|
||||
|
||||
var _camera_pivot: Node3D
|
||||
var _camera: Camera3D
|
||||
var _visual: Node3D
|
||||
var _character_root: Node3D
|
||||
var _animation_player: AnimationPlayer
|
||||
var _active_animation := ""
|
||||
var _captured := false
|
||||
var _yaw := 0.0
|
||||
var _pitch := deg_to_rad(-18.0)
|
||||
var _horizontal_velocity := Vector3.ZERO
|
||||
var _flight_enabled := false
|
||||
var _adt_cache: Dictionary = {}
|
||||
|
||||
|
||||
@@ -47,6 +64,7 @@ func _ready() -> void:
|
||||
if spawn_at_tile_center:
|
||||
global_position.x = (float(spawn_tile_x) + 0.5) * TILE_SIZE
|
||||
global_position.z = (float(spawn_tile_y) + 0.5) * TILE_SIZE
|
||||
_load_character_visual()
|
||||
var ground := _sample_ground_height(global_position)
|
||||
if is_finite(ground):
|
||||
global_position.y = ground + ground_offset
|
||||
@@ -58,9 +76,18 @@ func _unhandled_input(event: InputEvent) -> void:
|
||||
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_RIGHT:
|
||||
_captured = event.pressed
|
||||
Input.mouse_mode = Input.MOUSE_MODE_CAPTURED if _captured else Input.MOUSE_MODE_VISIBLE
|
||||
elif event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_WHEEL_UP:
|
||||
camera_distance = clampf(camera_distance - camera_zoom_step, camera_min_distance, camera_max_distance)
|
||||
_apply_camera_transform()
|
||||
elif event is InputEventMouseButton and event.pressed and event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
|
||||
camera_distance = clampf(camera_distance + camera_zoom_step, camera_min_distance, camera_max_distance)
|
||||
_apply_camera_transform()
|
||||
elif event is InputEventKey and event.pressed and event.keycode == KEY_ESCAPE:
|
||||
_captured = false
|
||||
Input.mouse_mode = Input.MOUSE_MODE_VISIBLE
|
||||
elif event is InputEventKey and event.pressed and not event.echo and event.keycode == KEY_SPACE:
|
||||
_flight_enabled = not _flight_enabled
|
||||
_horizontal_velocity = Vector3.ZERO
|
||||
|
||||
if _captured and event is InputEventMouseMotion:
|
||||
_yaw -= event.relative.x * mouse_sensitivity
|
||||
@@ -74,18 +101,24 @@ func _physics_process(delta: float) -> void:
|
||||
var target_velocity := Vector3.ZERO
|
||||
if input_dir.length_squared() > 0.0:
|
||||
target_velocity = _movement_vector(input_dir)
|
||||
if _flight_enabled:
|
||||
target_velocity.y += _flight_vertical_velocity()
|
||||
|
||||
_horizontal_velocity = _horizontal_velocity.move_toward(target_velocity, acceleration * delta)
|
||||
global_position += _horizontal_velocity * delta
|
||||
|
||||
var ground := _sample_ground_height(global_position)
|
||||
if is_finite(ground):
|
||||
var target_y := ground + ground_offset
|
||||
global_position.y = lerpf(global_position.y, target_y, clampf(ground_snap_speed * delta, 0.0, 1.0))
|
||||
if not _flight_enabled:
|
||||
var ground := _sample_ground_height(global_position)
|
||||
if is_finite(ground):
|
||||
var target_y := ground + ground_offset
|
||||
global_position.y = lerpf(global_position.y, target_y, clampf(ground_snap_speed * delta, 0.0, 1.0))
|
||||
|
||||
if _visual and _horizontal_velocity.length_squared() > 0.01:
|
||||
var horizontal_motion := Vector2(_horizontal_velocity.x, _horizontal_velocity.z)
|
||||
if _visual and horizontal_motion.length_squared() > 0.01:
|
||||
_visual.global_rotation.y = atan2(-_horizontal_velocity.x, -_horizontal_velocity.z)
|
||||
|
||||
_update_character_animation(horizontal_motion.length_squared() > 0.04)
|
||||
|
||||
_apply_camera_transform()
|
||||
|
||||
|
||||
@@ -105,8 +138,12 @@ func _get_input_dir() -> Vector2:
|
||||
func _movement_vector(input_dir: Vector2) -> Vector3:
|
||||
var forward := -global_basis.z
|
||||
var right := global_basis.x
|
||||
forward.y = 0.0
|
||||
right.y = 0.0
|
||||
if _flight_enabled and _camera_pivot:
|
||||
forward = -_camera_pivot.global_basis.z
|
||||
right = _camera_pivot.global_basis.x
|
||||
else:
|
||||
forward.y = 0.0
|
||||
right.y = 0.0
|
||||
forward = forward.normalized()
|
||||
right = right.normalized()
|
||||
|
||||
@@ -116,6 +153,167 @@ func _movement_vector(input_dir: Vector2) -> Vector3:
|
||||
return (forward * -input_dir.y * speed_z + right * input_dir.x * speed_x) * sprint
|
||||
|
||||
|
||||
func _flight_vertical_velocity() -> float:
|
||||
var dir := 0.0
|
||||
if Input.is_key_pressed(KEY_E):
|
||||
dir += 1.0
|
||||
if Input.is_key_pressed(KEY_Q):
|
||||
dir -= 1.0
|
||||
var sprint := sprint_multiplier if Input.is_key_pressed(KEY_SHIFT) else 1.0
|
||||
return dir * flight_vertical_speed * sprint
|
||||
|
||||
|
||||
func _load_character_visual() -> void:
|
||||
if character_model_path.is_empty() or _visual == null:
|
||||
return
|
||||
if _visual is MeshInstance3D:
|
||||
(_visual as MeshInstance3D).mesh = null
|
||||
_visual.position = Vector3.ZERO
|
||||
|
||||
if _character_root and is_instance_valid(_character_root):
|
||||
_character_root.queue_free()
|
||||
_character_root = null
|
||||
_animation_player = null
|
||||
_active_animation = ""
|
||||
|
||||
var scene: PackedScene = load(character_model_path)
|
||||
if scene == null:
|
||||
push_warning("ThirdPersonWowController: cannot load character model: %s" % character_model_path)
|
||||
return
|
||||
|
||||
_character_root = Node3D.new()
|
||||
_character_root.name = "CharacterModel"
|
||||
_character_root.set_script(GEOSET_CONTROLLER_SCRIPT)
|
||||
_character_root.scale = Vector3.ONE * maxf(character_scale, 0.001)
|
||||
_character_root.rotation_degrees.y = character_yaw_offset_degrees
|
||||
|
||||
var instance := scene.instantiate()
|
||||
instance.name = character_model_path.get_file().get_basename()
|
||||
_character_root.add_child(instance)
|
||||
|
||||
var compositor := Node.new()
|
||||
compositor.name = "CharacterTextureCompositor"
|
||||
compositor.set_script(TEXTURE_COMPOSITOR_SCRIPT)
|
||||
compositor.set("textures_dir", _character_textures_dir(character_model_path))
|
||||
compositor.set("extracted_dir", extracted_dir)
|
||||
_character_root.add_child(compositor)
|
||||
|
||||
_visual.add_child(_character_root)
|
||||
await get_tree().process_frame
|
||||
_fit_character_to_ground()
|
||||
_apply_character_starter_outfit(compositor)
|
||||
_animation_player = _find_animation_player(_character_root)
|
||||
if _animation_player:
|
||||
_prepare_animation_player(_animation_player)
|
||||
_update_character_animation(false)
|
||||
|
||||
|
||||
func _fit_character_to_ground() -> void:
|
||||
if _character_root == null:
|
||||
return
|
||||
var aabb := _calculate_aabb(_character_root)
|
||||
if aabb.size == Vector3.ZERO:
|
||||
return
|
||||
var local_min_y := aabb.position.y - _character_root.global_position.y
|
||||
_character_root.position.y -= local_min_y
|
||||
|
||||
|
||||
func _apply_character_starter_outfit(compositor: Node) -> void:
|
||||
if _character_root == null or compositor == null:
|
||||
return
|
||||
var resolver: RefCounted = OUTFIT_RESOLVER_SCRIPT.new()
|
||||
if not resolver.call("load_dbcs", extracted_dir):
|
||||
return
|
||||
var inferred: Dictionary = resolver.call("infer_race_gender_from_model_path", character_model_path)
|
||||
var race := String(inferred.get("race", "Human"))
|
||||
var gender := String(inferred.get("gender", "Male"))
|
||||
var outfit: Dictionary = resolver.call("resolve_start_outfit", race, gender, character_class_id)
|
||||
if outfit.is_empty():
|
||||
return
|
||||
if compositor.has_method("set_equipment_components"):
|
||||
compositor.call("set_equipment_components", outfit.get("textures", PackedStringArray()))
|
||||
if _character_root.has_method("apply_outfit_defaults"):
|
||||
_character_root.call("apply_outfit_defaults", outfit)
|
||||
|
||||
|
||||
func _update_character_animation(is_moving: bool) -> void:
|
||||
if _animation_player == null:
|
||||
return
|
||||
var wanted := _choose_animation(["Run", "Walk"]) if is_moving else _choose_animation(["Stand", "Idle"])
|
||||
if wanted.is_empty() or wanted == _active_animation:
|
||||
return
|
||||
_active_animation = wanted
|
||||
_animation_player.play(wanted, animation_blend_time)
|
||||
|
||||
|
||||
func _prepare_animation_player(player: AnimationPlayer) -> void:
|
||||
player.playback_default_blend_time = animation_blend_time
|
||||
for animation_name in player.get_animation_list():
|
||||
var lower := String(animation_name).to_lower()
|
||||
if lower == "stand" or lower == "idle" or lower == "run" or lower == "walk" or lower.contains("stand") or lower.contains("run") or lower.contains("walk"):
|
||||
var animation := player.get_animation(animation_name)
|
||||
if animation:
|
||||
animation.loop_mode = Animation.LOOP_LINEAR
|
||||
|
||||
|
||||
func _choose_animation(candidates: Array[String]) -> String:
|
||||
if _animation_player == null:
|
||||
return ""
|
||||
for candidate in candidates:
|
||||
if _animation_player.has_animation(candidate):
|
||||
return candidate
|
||||
var list := _animation_player.get_animation_list()
|
||||
for candidate in candidates:
|
||||
var lower := candidate.to_lower()
|
||||
for animation_name in list:
|
||||
if String(animation_name).to_lower().contains(lower):
|
||||
return String(animation_name)
|
||||
return ""
|
||||
|
||||
|
||||
func _find_animation_player(root: Node) -> AnimationPlayer:
|
||||
if root is AnimationPlayer:
|
||||
return root
|
||||
for child in root.get_children():
|
||||
var found := _find_animation_player(child)
|
||||
if found:
|
||||
return found
|
||||
return null
|
||||
|
||||
|
||||
func _calculate_aabb(root: Node) -> AABB:
|
||||
var result := AABB()
|
||||
var has_aabb := false
|
||||
for mesh_inst in _iter_mesh_instances(root):
|
||||
if mesh_inst.mesh == null:
|
||||
continue
|
||||
var local_aabb := mesh_inst.mesh.get_aabb()
|
||||
var global_aabb := mesh_inst.global_transform * local_aabb
|
||||
if not has_aabb:
|
||||
result = global_aabb
|
||||
has_aabb = true
|
||||
else:
|
||||
result = result.merge(global_aabb)
|
||||
return result if has_aabb else AABB()
|
||||
|
||||
|
||||
func _iter_mesh_instances(root: Node) -> Array[MeshInstance3D]:
|
||||
var result: Array[MeshInstance3D] = []
|
||||
_collect_mesh_instances(root, result)
|
||||
return result
|
||||
|
||||
|
||||
func _collect_mesh_instances(node: Node, result: Array[MeshInstance3D]) -> void:
|
||||
if node is MeshInstance3D:
|
||||
result.append(node)
|
||||
for child in node.get_children():
|
||||
_collect_mesh_instances(child, result)
|
||||
|
||||
|
||||
func _character_textures_dir(model_path: String) -> String:
|
||||
return model_path.get_base_dir().path_join(model_path.get_file().get_basename() + "_textures")
|
||||
|
||||
|
||||
func _apply_camera_transform() -> void:
|
||||
if _camera_pivot:
|
||||
_camera_pivot.position = Vector3(0.0, camera_height, 0.0)
|
||||
|
||||
Reference in New Issue
Block a user