render: extract animated M2 materializer

This commit is contained in:
2026-07-18 01:48:55 +04:00
parent 9ab4c0762d
commit 456f3e2334
16 changed files with 725 additions and 78 deletions
@@ -0,0 +1,120 @@
class_name M2AnimatedInstanceMaterializer
extends RefCounted
## Duplicates one ordered animated M2 batch, applies instance render settings,
## starts playback and attaches a non-empty batch to the supplied parent.
## All calls mutate Godot scene objects and therefore belong on the main thread.
const M2_ANIMATED_SCENE_FINALIZER_SCRIPT := preload(
"res://src/render/m2/m2_animated_scene_finalizer.gd"
)
const M2_ANIMATION_PLAYBACK_CONTROLLER_SCRIPT := preload(
"res://src/render/m2/m2_animation_playback_controller.gd"
)
var _animated_scene_finalizer := M2_ANIMATED_SCENE_FINALIZER_SCRIPT.new()
var _animation_playback_controller := M2_ANIMATION_PLAYBACK_CONTROLLER_SCRIPT.new()
## Returns the attached batch root and detached diagnostic entries. Invalid or
## empty input, and a batch whose duplicates all fail, returns an empty result.
## The caller owns the returned/attached batch through `m2_parent_root`.
func materialize_batch(
m2_parent_root: Node3D,
relative_path: String,
prototype: Node3D,
transforms: Array,
start_index: int,
instance_count: int,
batch_serial: int,
visibility_range_end: float,
visibility_range_margin: float,
cast_shadows: bool,
native_animator_script: Script,
collect_native_diagnostics: bool
) -> Dictionary:
if (
m2_parent_root == null
or transforms.is_empty()
or instance_count <= 0
or prototype == null
):
return {}
var model_name := relative_path.get_file().get_basename()
var batch_root := Node3D.new()
batch_root.name = "%s_anim_%d" % [model_name, batch_serial]
var diagnostic_entries: Array[Dictionary] = []
for batch_offset in instance_count:
var instance_index := start_index + batch_offset
var instance := prototype.duplicate(
Node.DUPLICATE_SIGNALS | Node.DUPLICATE_GROUPS | Node.DUPLICATE_SCRIPTS
) as Node3D
if instance == null:
continue
instance.name = "%s_%d" % [model_name, instance_index]
instance.transform = transforms[instance_index]
_apply_visibility_range_recursive(
instance,
visibility_range_end,
visibility_range_margin
)
_apply_shadow_cast_recursive(instance, cast_shadows)
_animation_playback_controller.copy_native_animator_data(
prototype,
instance,
native_animator_script
)
batch_root.add_child(instance)
var animation_players := _animated_scene_finalizer.animation_players_in_subtree(
instance
)
var native_diagnostics := _animation_playback_controller.start_instance_playback(
instance,
relative_path,
instance_index,
native_animator_script,
animation_players,
collect_native_diagnostics
)
for native_diagnostic in native_diagnostics:
diagnostic_entries.append({
"instance_index": instance_index,
"state": native_diagnostic,
})
if batch_root.get_child_count() <= 0:
batch_root.queue_free()
return {}
m2_parent_root.add_child(batch_root)
return {
"batch_root": batch_root,
"native_diagnostics": diagnostic_entries,
}
func _apply_visibility_range_recursive(
node: Node,
range_end: float,
range_end_margin: float
) -> void:
if range_end <= 0.0:
return
if node is GeometryInstance3D:
var geometry := node as GeometryInstance3D
geometry.visibility_range_end = range_end
geometry.visibility_range_end_margin = range_end_margin
for child in node.get_children():
_apply_visibility_range_recursive(child, range_end, range_end_margin)
func _apply_shadow_cast_recursive(node: Node, cast_shadows: bool) -> void:
if node is GeometryInstance3D:
var geometry := node as GeometryInstance3D
geometry.cast_shadow = (
GeometryInstance3D.SHADOW_CASTING_SETTING_ON
if cast_shadows
else GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
)
for child in node.get_children():
_apply_shadow_cast_recursive(child, cast_shadows)
@@ -0,0 +1 @@
uid://dkgy3rvsmlkvm
@@ -2,8 +2,8 @@ class_name M2AnimationPlaybackController
extends RefCounted
## Applies deterministic playback to one animated M2 instance and copies the
## runtime fields of exact-script native animators. The caller owns instance
## duplication, SceneTree attachment and diagnostic log formatting.
## runtime fields of exact-script native animators. The materializer owns
## instance duplication/attachment; its caller owns diagnostic log formatting.
## Copies the five historical runtime fields between matching native animators
+32 -49
View File
@@ -78,8 +78,8 @@ const M2_ANIMATION_LOAD_PIPELINE_STATE_SCRIPT := preload(
const M2_ANIMATED_SCENE_FINALIZER_SCRIPT := preload(
"res://src/render/m2/m2_animated_scene_finalizer.gd"
)
const M2_ANIMATION_PLAYBACK_CONTROLLER_SCRIPT := preload(
"res://src/render/m2/m2_animation_playback_controller.gd"
const M2_ANIMATED_INSTANCE_MATERIALIZER_SCRIPT := preload(
"res://src/render/m2/m2_animated_instance_materializer.gd"
)
const M2_MESH_RESOURCE_CACHE_STATE_SCRIPT := preload(
"res://src/render/m2/m2_mesh_resource_cache_state.gd"
@@ -281,7 +281,7 @@ var _m2_mesh_resource_extractor := M2_MESH_RESOURCE_EXTRACTOR_SCRIPT.new()
var _m2_mesh_load_pipeline_state := M2_MESH_LOAD_PIPELINE_STATE_SCRIPT.new()
var _m2_animation_load_pipeline_state := M2_ANIMATION_LOAD_PIPELINE_STATE_SCRIPT.new()
var _m2_animated_scene_finalizer := M2_ANIMATED_SCENE_FINALIZER_SCRIPT.new()
var _m2_animation_playback_controller := M2_ANIMATION_PLAYBACK_CONTROLLER_SCRIPT.new()
var _m2_animated_instance_materializer := M2_ANIMATED_INSTANCE_MATERIALIZER_SCRIPT.new()
var _wmo_build_jobs: Dictionary = {}
var _wmo_build_queue: Array = []
var _tile_loading_tasks: Dictionary = {}
@@ -4429,53 +4429,36 @@ func _materialize_m2_animated_batch(
start: int,
count: int,
serial: int) -> void:
if transforms.is_empty() or count <= 0 or prototype == null:
var materialization := _m2_animated_instance_materializer.materialize_batch(
m2_root,
rel_path,
prototype,
transforms,
start,
count,
serial,
m2_visibility_range,
CHUNK_SIZE,
m2_cast_shadows,
M2_NATIVE_ANIMATOR_SCRIPT,
debug_streaming
)
if materialization.is_empty():
return
var batch_root := Node3D.new()
batch_root.name = "%s_anim_%d" % [rel_path.get_file().get_basename(), serial]
for i in count:
var instance := prototype.duplicate(Node.DUPLICATE_SIGNALS | Node.DUPLICATE_GROUPS | Node.DUPLICATE_SCRIPTS) as Node3D
if instance == null:
continue
instance.name = "%s_%d" % [rel_path.get_file().get_basename(), start + i]
instance.transform = transforms[start + i]
_apply_visibility_range_recursive(instance, m2_visibility_range)
_apply_shadow_cast_recursive(instance, m2_cast_shadows)
_m2_animation_playback_controller.copy_native_animator_data(
prototype,
instance,
M2_NATIVE_ANIMATOR_SCRIPT
)
batch_root.add_child(instance)
var animation_players := _m2_animated_scene_finalizer.animation_players_in_subtree(
instance
)
var native_diagnostics := _m2_animation_playback_controller.start_instance_playback(
instance,
rel_path,
start + i,
M2_NATIVE_ANIMATOR_SCRIPT,
animation_players,
debug_streaming
)
if debug_streaming:
for state in native_diagnostics:
print("M2_NATIVE_ANIMATOR path=%s instance=%d prepared=%s processing=%s mesh=%s bones=%d surfaces=%d length=%.2f" % [
_normalize_m2_rel_path(rel_path),
start + i,
str(state.get("prepared", false)),
str(state.get("processing", false)),
str(state.get("has_mesh", false)),
int(state.get("bones", 0)),
int(state.get("surfaces", 0)),
float(state.get("length", 0.0)),
])
if batch_root.get_child_count() <= 0:
batch_root.queue_free()
return
m2_root.add_child(batch_root)
if debug_streaming:
for diagnostic_entry in materialization.get("native_diagnostics", []):
var state: Dictionary = diagnostic_entry.get("state", {})
print("M2_NATIVE_ANIMATOR path=%s instance=%d prepared=%s processing=%s mesh=%s bones=%d surfaces=%d length=%.2f" % [
_normalize_m2_rel_path(rel_path),
int(diagnostic_entry.get("instance_index", -1)),
str(state.get("prepared", false)),
str(state.get("processing", false)),
str(state.get("has_mesh", false)),
int(state.get("bones", 0)),
int(state.get("surfaces", 0)),
float(state.get("length", 0.0)),
])
var batch_root: Node3D = materialization.get("batch_root", null)
_set_editor_owner_recursive(batch_root)
@@ -0,0 +1,277 @@
extends SceneTree
## Synthetic batch duplication, render-property, playback, ownership-boundary
## and bounded-timing regression for animated M2 instance materialization.
const MATERIALIZER_SCRIPT := preload(
"res://src/render/m2/m2_animated_instance_materializer.gd"
)
const NATIVE_ANIMATOR_SCRIPT := preload("res://src/scenes/streaming/m2_native_animator.gd")
const MATERIALIZER_PATH := "res://src/render/m2/m2_animated_instance_materializer.gd"
const LOADER_PATH := "res://src/scenes/streaming/streaming_world_loader.gd"
func _initialize() -> void:
var failures: Array[String] = []
_verify_batch_contract(failures)
_verify_invalid_inputs(failures)
_verify_ownership_boundaries(failures)
var elapsed_milliseconds := _verify_bounded_timing(failures)
if not failures.is_empty():
for failure in failures:
push_error("M2_ANIMATED_INSTANCE_MATERIALIZER: %s" % failure)
quit(1)
return
print(
"M2_ANIMATED_INSTANCE_MATERIALIZER PASS cases=12 instances=1000 elapsed_ms=%.3f"
% elapsed_milliseconds
)
quit(0)
func _verify_batch_contract(failures: Array[String]) -> void:
var materializer = MATERIALIZER_SCRIPT.new()
var parent_root := Node3D.new()
get_root().add_child(parent_root)
var prototype := _make_animated_prototype()
var transforms: Array = [
Transform3D(Basis.IDENTITY, Vector3.ZERO),
Transform3D(Basis.IDENTITY, Vector3(1.0, 2.0, 3.0)),
Transform3D(Basis.IDENTITY, Vector3(4.0, 5.0, 6.0)),
]
var result: Dictionary = materializer.materialize_batch(
parent_root,
"World\\Doodads\\AnimatedTree.m2",
prototype,
transforms,
1,
2,
7,
345.0,
16.0,
false,
NATIVE_ANIMATOR_SCRIPT,
true
)
_expect_false(result.is_empty(), "valid batch returned", failures)
var batch_root: Node3D = result.get("batch_root", null)
_expect_same(batch_root.get_parent(), parent_root, "batch attached to supplied parent", failures)
_expect_string_equal(batch_root.name, "AnimatedTree_anim_7", "batch name", failures)
_expect_equal(batch_root.get_child_count(), 2, "ordered duplicate count", failures)
for batch_offset in 2:
var instance := batch_root.get_child(batch_offset) as Node3D
var instance_index := batch_offset + 1
_expect_string_equal(instance.name, "AnimatedTree_%d" % instance_index, "instance name", failures)
_expect_true(instance.transform == transforms[instance_index], "instance transform", failures)
var geometry := instance.get_node("Branch/Geometry") as GeometryInstance3D
_expect_float_equal(geometry.visibility_range_end, 345.0, "recursive visibility end", failures)
_expect_float_equal(geometry.visibility_range_end_margin, 16.0, "recursive visibility margin", failures)
_expect_equal(
geometry.cast_shadow,
GeometryInstance3D.SHADOW_CASTING_SETTING_OFF,
"recursive shadow mode",
failures
)
var player := instance.get_node("AnimationPlayer") as AnimationPlayer
_expect_string_equal(player.current_animation, "Stand", "playback started", failures)
var animator: Node = instance.get_node("Branch/NativeAnimator")
_expect_same(
animator.bones,
prototype.get_node("Branch/NativeAnimator").bones,
"native fields copied by reference",
failures
)
var diagnostic_entries: Array = result.get("native_diagnostics", [])
_expect_equal(diagnostic_entries.size(), 2, "one native diagnostic per instance", failures)
for diagnostic_offset in diagnostic_entries.size():
var entry: Dictionary = diagnostic_entries[diagnostic_offset]
_expect_equal(
int(entry.get("instance_index", -1)),
diagnostic_offset + 1,
"diagnostic index",
failures
)
_expect_equal(
int((entry.get("state", {}) as Dictionary).get("bones", 0)),
1,
"diagnostic state",
failures
)
var source_geometry := prototype.get_node("Branch/Geometry") as GeometryInstance3D
_expect_float_equal(source_geometry.visibility_range_end, 0.0, "prototype render state unchanged", failures)
prototype.free()
parent_root.free()
func _verify_invalid_inputs(failures: Array[String]) -> void:
var materializer = MATERIALIZER_SCRIPT.new()
var prototype := Node3D.new()
var parent_root := Node3D.new()
_expect_true(
materializer.materialize_batch(
null, "a.m2", prototype, [Transform3D.IDENTITY], 0, 1, 0,
0.0, 0.0, true, NATIVE_ANIMATOR_SCRIPT, false
).is_empty(),
"null parent rejected",
failures
)
_expect_true(
materializer.materialize_batch(
parent_root, "a.m2", prototype, [], 0, 1, 0,
0.0, 0.0, true, NATIVE_ANIMATOR_SCRIPT, false
).is_empty(),
"empty transforms rejected",
failures
)
_expect_true(
materializer.materialize_batch(
parent_root, "a.m2", prototype, [Transform3D.IDENTITY], 0, 0, 0,
0.0, 0.0, true, NATIVE_ANIMATOR_SCRIPT, false
).is_empty(),
"zero count rejected",
failures
)
_expect_equal(parent_root.get_child_count(), 0, "invalid input has no attachment", failures)
prototype.free()
parent_root.free()
func _verify_ownership_boundaries(failures: Array[String]) -> void:
var materializer_source := FileAccess.get_file_as_string(MATERIALIZER_PATH)
var loader_source := FileAccess.get_file_as_string(LOADER_PATH)
_expect_true(
loader_source.contains("M2_ANIMATED_INSTANCE_MATERIALIZER_SCRIPT.new()"),
"loader composes materializer",
failures
)
_expect_equal(
loader_source.count("_m2_animated_instance_materializer.materialize_batch("),
1,
"loader delegates once",
failures
)
for retained_loader_rule in [
"_set_editor_owner_recursive(batch_root)",
"M2_NATIVE_ANIMATOR path=%s instance=%d",
"_normalize_m2_rel_path(rel_path)",
]:
_expect_true(
loader_source.contains(retained_loader_rule),
"loader retains %s" % retained_loader_rule,
failures
)
for removed_loader_rule in [
"prototype.duplicate(Node.DUPLICATE_SIGNALS | Node.DUPLICATE_GROUPS | Node.DUPLICATE_SCRIPTS)",
"_m2_animation_playback_controller",
]:
_expect_false(
loader_source.contains(removed_loader_rule),
"loader releases %s" % removed_loader_rule,
failures
)
for required_materializer_rule in [
"Node.DUPLICATE_SIGNALS | Node.DUPLICATE_GROUPS | Node.DUPLICATE_SCRIPTS",
"_animation_playback_controller.copy_native_animator_data(",
"_animation_playback_controller.start_instance_playback(",
"m2_parent_root.add_child(batch_root)",
]:
_expect_true(
materializer_source.contains(required_materializer_rule),
"materializer owns %s" % required_materializer_rule,
failures
)
for forbidden_dependency in [
"ResourceLoader.",
"FileAccess.",
"WorkerThreadPool.",
"MultiMesh",
".owner =",
"_m2_build_jobs",
"_render_budget_scheduler",
]:
_expect_false(
materializer_source.contains(forbidden_dependency),
"materializer omits %s ownership" % forbidden_dependency,
failures
)
func _verify_bounded_timing(failures: Array[String]) -> float:
var materializer = MATERIALIZER_SCRIPT.new()
var parent_root := Node3D.new()
get_root().add_child(parent_root)
var prototype := Node3D.new()
var transforms: Array = []
for instance_index in 1000:
transforms.append(
Transform3D(Basis.IDENTITY, Vector3(float(instance_index), 0.0, 0.0))
)
var started_microseconds := Time.get_ticks_usec()
var result: Dictionary = materializer.materialize_batch(
parent_root, "world/timing.m2", prototype, transforms, 0, 1000, 1,
0.0, 0.0, true, NATIVE_ANIMATOR_SCRIPT, false
)
var elapsed_milliseconds := float(Time.get_ticks_usec() - started_microseconds) / 1000.0
var batch_root := result.get("batch_root", null) as Node3D
_expect_equal(batch_root.get_child_count(), 1000, "timing batch complete", failures)
_expect_true(elapsed_milliseconds < 1000.0, "1000 instances under 1 second", failures)
prototype.free()
parent_root.free()
return elapsed_milliseconds
func _make_animated_prototype() -> Node3D:
var prototype := Node3D.new()
var branch := Node3D.new()
branch.name = "Branch"
prototype.add_child(branch)
var geometry := MeshInstance3D.new()
geometry.name = "Geometry"
branch.add_child(geometry)
var native_animator: Node = NATIVE_ANIMATOR_SCRIPT.new()
native_animator.name = "NativeAnimator"
native_animator.mesh_instance_path = NodePath("../Geometry")
native_animator.bones = [{"parent": -1}]
native_animator.surfaces = [{"vertices": PackedVector3Array()}]
native_animator.animation_length = 8.0
branch.add_child(native_animator)
var player := AnimationPlayer.new()
player.name = "AnimationPlayer"
var library := AnimationLibrary.new()
var stand := Animation.new()
stand.length = 2.0
library.add_animation("Stand", stand)
player.add_animation_library("", library)
prototype.add_child(player)
return prototype
func _expect_true(condition: bool, label: String, failures: Array[String]) -> void:
if not condition:
failures.append(label)
func _expect_false(condition: bool, label: String, failures: Array[String]) -> void:
_expect_true(not condition, label, failures)
func _expect_equal(actual: int, expected: int, label: String, failures: Array[String]) -> void:
if actual != expected:
failures.append("%s expected=%d actual=%d" % [label, expected, actual])
func _expect_float_equal(actual: float, expected: float, label: String, failures: Array[String]) -> void:
if not is_equal_approx(actual, expected):
failures.append("%s expected=%.6f actual=%.6f" % [label, expected, actual])
func _expect_string_equal(actual: String, expected: String, label: String, failures: Array[String]) -> void:
if actual != expected:
failures.append("%s expected=%s actual=%s" % [label, expected, actual])
func _expect_same(actual: Variant, expected: Variant, label: String, failures: Array[String]) -> void:
if not is_same(actual, expected):
failures.append(label)
@@ -0,0 +1 @@
uid://kxr85qkrfnhb
@@ -5,6 +5,7 @@ extends SceneTree
const FINALIZER_SCRIPT := preload("res://src/render/m2/m2_animated_scene_finalizer.gd")
const FINALIZER_PATH := "res://src/render/m2/m2_animated_scene_finalizer.gd"
const MATERIALIZER_PATH := "res://src/render/m2/m2_animated_instance_materializer.gd"
const LOADER_PATH := "res://src/scenes/streaming/streaming_world_loader.gd"
@@ -115,6 +116,7 @@ func _verify_material_mapping(failures: Array[String]) -> void:
func _verify_ownership_boundaries(failures: Array[String]) -> void:
var finalizer_source := FileAccess.get_file_as_string(FINALIZER_PATH)
var materializer_source := FileAccess.get_file_as_string(MATERIALIZER_PATH)
var loader_source := FileAccess.get_file_as_string(LOADER_PATH)
_expect_true(loader_source.contains("M2_ANIMATED_SCENE_FINALIZER_SCRIPT.new()"), "loader composes finalizer", failures)
for removed_loader_function in [
@@ -128,9 +130,14 @@ func _verify_ownership_boundaries(failures: Array[String]) -> void:
"_m2_animated_scene_finalizer.repair_materials(candidate, material_source)",
"_m2_animated_scene_finalizer.finalize_candidate(candidate)",
"_m2_animated_scene_finalizer.mesh_instances_in_subtree(root)",
"_m2_animated_scene_finalizer.animation_players_in_subtree(",
]:
_expect_equal(loader_source.count(delegated_call), 1, "single loader delegation: %s" % delegated_call, failures)
_expect_equal(
materializer_source.count("_animated_scene_finalizer.animation_players_in_subtree("),
1,
"single materializer player-inventory delegation",
failures
)
for retained_loader_rule in [
"ResourceLoader.load_threaded_get(path)",
"RENDER_BUDGET_SCHEDULER_SCRIPT.M2_ANIMATION_FINALIZE",
@@ -6,6 +6,7 @@ extends SceneTree
const CONTROLLER_SCRIPT := preload("res://src/render/m2/m2_animation_playback_controller.gd")
const NATIVE_ANIMATOR_SCRIPT := preload("res://src/scenes/streaming/m2_native_animator.gd")
const CONTROLLER_PATH := "res://src/render/m2/m2_animation_playback_controller.gd"
const MATERIALIZER_PATH := "res://src/render/m2/m2_animated_instance_materializer.gd"
const LOADER_PATH := "res://src/scenes/streaming/streaming_world_loader.gd"
@@ -143,8 +144,9 @@ func _verify_native_copy_start_and_diagnostics(failures: Array[String]) -> void:
func _verify_ownership_boundaries(failures: Array[String]) -> void:
var controller_source := FileAccess.get_file_as_string(CONTROLLER_PATH)
var materializer_source := FileAccess.get_file_as_string(MATERIALIZER_PATH)
var loader_source := FileAccess.get_file_as_string(LOADER_PATH)
_expect_true(loader_source.contains("M2_ANIMATION_PLAYBACK_CONTROLLER_SCRIPT.new()"), "loader composes playback controller", failures)
_expect_true(materializer_source.contains("M2_ANIMATION_PLAYBACK_CONTROLLER_SCRIPT.new()"), "materializer composes playback controller", failures)
for removed_loader_function in [
"func _start_m2_animations(",
"func _copy_m2_native_animator_data(",
@@ -152,13 +154,15 @@ func _verify_ownership_boundaries(failures: Array[String]) -> void:
"func _choose_default_m2_animation(",
]:
_expect_false(loader_source.contains(removed_loader_function), "legacy helper removed: %s" % removed_loader_function, failures)
_expect_equal(loader_source.count("_m2_animation_playback_controller.copy_native_animator_data("), 1, "native copy delegates once", failures)
_expect_equal(loader_source.count("_m2_animation_playback_controller.start_instance_playback("), 1, "playback delegates once", failures)
for retained_loader_rule in [
"prototype.duplicate(Node.DUPLICATE_SIGNALS | Node.DUPLICATE_GROUPS | Node.DUPLICATE_SCRIPTS)",
_expect_equal(materializer_source.count("_animation_playback_controller.copy_native_animator_data("), 1, "native copy delegates once", failures)
_expect_equal(materializer_source.count("_animation_playback_controller.start_instance_playback("), 1, "playback delegates once", failures)
for retained_materializer_rule in [
"Node.DUPLICATE_SIGNALS | Node.DUPLICATE_GROUPS | Node.DUPLICATE_SCRIPTS",
"batch_root.add_child(instance)",
"_apply_visibility_range_recursive(instance, m2_visibility_range)",
"_apply_shadow_cast_recursive(instance, m2_cast_shadows)",
"_apply_shadow_cast_recursive(instance, cast_shadows)",
]:
_expect_true(materializer_source.contains(retained_materializer_rule), "materializer retains %s" % retained_materializer_rule, failures)
for retained_loader_rule in [
"_set_editor_owner_recursive(batch_root)",
"M2_NATIVE_ANIMATOR path=%s instance=%d",
]: