feat(M01): add explicit streaming focus

Work-Package: M01-RND-STREAMING-FOCUS-001

Agent: sindo-main-codex
This commit is contained in:
2026-07-13 15:41:16 +04:00
parent fbef131bcb
commit 7815385b3b
12 changed files with 253 additions and 61 deletions
+66 -38
View File
@@ -1,5 +1,5 @@
@tool
## Streams Azeroth terrain around the active camera with chunk-based LODs.
## Streams Azeroth terrain around an explicit [StreamingFocus] with chunk-based LODs.
extends Node3D
const ADT_BUILDER_SCRIPT := preload("res://addons/mpq_extractor/loaders/adt_builder.gd")
@@ -12,6 +12,8 @@ const WMO_BUILDER_SCRIPT := preload("res://addons/mpq_extractor/loaders/wmo_buil
const M2_BUILDER_SCRIPT := preload("res://addons/mpq_extractor/loaders/m2_builder.gd")
const M2_NATIVE_ANIMATED_BUILDER_SCRIPT := preload("res://addons/mpq_extractor/loaders/m2_native_animated_builder.gd")
const M2_NATIVE_ANIMATOR_SCRIPT := preload("res://src/scenes/streaming/m2_native_animator.gd")
const STREAMING_FOCUS_SCRIPT := preload("res://src/domain/streaming/streaming_focus.gd")
const GODOT_WORLD_POSITION_SCRIPT := preload("res://src/domain/coordinates/godot_world_position.gd")
const REQUIRED_BAKED_TILE_FORMAT_VERSION := 5
const REQUIRED_SPLAT_TILE_FORMAT_VERSION := 1
const REQUIRED_CONTROL_SPLAT_TILE_FORMAT_VERSION := 3
@@ -29,6 +31,9 @@ const QUALITY_HIGH := "High"
@export var extracted_dir: String = "res://data/extracted"
@export var map_name: String = "Azeroth"
## Optional scene adapter source. Any Node3D may provide the runtime focus.
@export var streaming_focus_source_path: NodePath
## Camera used only by the optional automatic overview positioning feature.
@export var camera_path: NodePath
@export_enum("Custom", "Performance", "Balanced", "High") var quality_preset: String = QUALITY_CUSTOM
@@ -213,6 +218,8 @@ var _tile_min := Vector2i(63, 63)
var _tile_max := Vector2i(0, 0)
var _camera_initialized := false
var _editor_signature := ""
var _streaming_focus: StreamingFocus
var _missing_focus_source_reported := false
var _last_focus_pos := Vector3.ZERO
var _dbg_chunks_created := 0
var _dbg_chunks_null := 0
@@ -293,6 +300,8 @@ func _ready() -> void:
_tick_editor_streaming()
elif auto_position_camera:
_position_camera_over_world()
if not Engine.is_editor_hint():
_capture_streaming_focus_from_source()
set_process(true)
call_deferred("_refresh_streaming_targets_after_ready")
@@ -364,7 +373,7 @@ func _process(delta: float) -> void:
if Engine.is_editor_hint():
_tick_editor_streaming()
else:
_refresh_streaming_targets(false)
refresh_streaming_focus(false)
did_refresh = true
_profile_section(timings, "refresh", section_start, profile_enabled)
_log_hitch_profile(profile_start, timings, did_refresh, profile_enabled)
@@ -590,10 +599,11 @@ func _tick_editor_streaming() -> void:
var focus_world := _tile_center_to_world(editor_preview_center_x, editor_preview_center_y)
if editor_follow_view_camera:
var editor_camera := _get_stream_camera()
var editor_camera := _get_editor_view_camera()
if editor_camera:
focus_world += editor_camera.global_position
_refresh_editor_streaming_targets_at(focus_world, false)
_set_streaming_focus_from_vector3(focus_world)
_refresh_editor_streaming_targets_at(_streaming_focus_to_vector3(), false)
func _scan_available_tiles() -> void:
@@ -681,24 +691,28 @@ func _load_tiles_from_directory() -> void:
_tile_max.y = max(_tile_max.y, ty)
func _refresh_streaming_targets(force: bool) -> void:
var camera := _get_stream_camera()
if camera == null:
return
## Replaces the current streaming point without requiring a Node or Camera3D.
func set_streaming_focus(streaming_focus: StreamingFocus) -> void:
_streaming_focus = streaming_focus
## Samples the configured scene source, then refreshes streaming from the typed focus.
## Returns false when no valid focus is available; existing streamed content is retained.
func refresh_streaming_focus(force: bool = false) -> bool:
_capture_streaming_focus_from_source()
if _streaming_focus == null or _streaming_focus.world_position == null:
return false
_terrain_root.position = Vector3.ZERO
_refresh_streaming_targets_at(camera.global_position, force)
_refresh_streaming_targets_at(_streaming_focus_to_vector3(), force)
return true
func _refresh_streaming_targets_after_ready() -> void:
var camera := _get_stream_camera()
if camera == null:
return
_terrain_root.position = Vector3.ZERO
_has_refresh_focus = false
_refresh_streaming_targets_at(camera.global_position, false)
refresh_streaming_focus(false)
## Core streaming update — works for both game (camera pos) and editor (preview center pos).
## Core streaming update for runtime and editor focus positions.
func _refresh_editor_streaming_targets_at(focus_pos: Vector3, force: bool) -> void:
if not force and not _should_refresh_focus(focus_pos):
return
@@ -3048,32 +3062,46 @@ func _is_tile_queued(key: String) -> bool:
return false
func _get_stream_camera() -> Camera3D:
if Engine.is_editor_hint():
# The 3D editor viewport camera lives in EditorInterface, not get_viewport().
# get_viewport().get_camera_3d() from a @tool script returns whichever Camera3D
# belongs to the edited scene (often null), so editor-view movement is invisible
# unless we reach into EditorInterface.
var vp := EditorInterface.get_editor_viewport_3d(0)
if vp != null:
var cam := vp.get_camera_3d()
if cam:
return cam
func _capture_streaming_focus_from_source() -> void:
if streaming_focus_source_path == NodePath():
return
var focus_source := get_node_or_null(streaming_focus_source_path) as Node3D
if focus_source == null:
if not _missing_focus_source_reported:
push_warning("Streaming focus source is missing or is not Node3D: %s" % streaming_focus_source_path)
_missing_focus_source_reported = true
return
_missing_focus_source_reported = false
_set_streaming_focus_from_vector3(focus_source.global_position)
if camera_path != NodePath():
var from_path := get_node_or_null(camera_path)
if from_path is Camera3D:
return from_path
var viewport_camera := get_viewport().get_camera_3d()
if viewport_camera:
return viewport_camera
func _set_streaming_focus_from_vector3(world_position: Vector3) -> void:
var typed_world_position = GODOT_WORLD_POSITION_SCRIPT.new(
world_position.x,
world_position.y,
world_position.z
)
set_streaming_focus(STREAMING_FOCUS_SCRIPT.new(typed_world_position))
for child in get_children():
if child is Camera3D and child.current:
return child
return null
func _streaming_focus_to_vector3() -> Vector3:
var world_position: GodotWorldPosition = _streaming_focus.world_position
return Vector3(world_position.x_units, world_position.y_units, world_position.z_units)
func _get_editor_view_camera() -> Camera3D:
# EditorInterface owns this camera. This method is the explicit editor adapter;
# runtime streaming never discovers a viewport camera.
var editor_viewport := EditorInterface.get_editor_viewport_3d(0)
if editor_viewport == null:
return null
return editor_viewport.get_camera_3d()
func _get_auto_position_camera() -> Camera3D:
if camera_path == NodePath():
return null
return get_node_or_null(camera_path) as Camera3D
func _can_use_baked_tile_cache() -> bool:
@@ -5365,7 +5393,7 @@ func _position_camera_over_world() -> void:
if _camera_initialized:
return
var camera := _get_stream_camera()
var camera := _get_auto_position_camera()
if camera == null:
return