Feat: load 3D models from card definition assets on game board
Client: - Added ModelCache autoload (autoloads/ModelCache.gd) - Downloads .glb files from CDN via HTTPRequest - Disk cache: user://remote_model_cache/<sha256>.glb - Memory cache: PackedScene per URL for instant re-instantiation - Concurrent requests for same URL queue and share one download - Loads via GLTFDocument.append_from_file + generate_scene - game_screen.gd: _add_unit_mesh now looks up cardDefinitionId → ContentCache.get_definition() → finds Model3D asset URL → calls ModelCache.load_model() to async replace placeholder geometry - Placeholder (sphere/cylinder/box) shown immediately - Real model swaps in on download completion - Guards against body being freed before model arrives - Added _unit_bodies dict for O(1) body lookup - project.godot: registered ModelCache autoload Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
This commit is contained in:
@@ -143,7 +143,8 @@ var _friendly_unit_ids: Dictionary = {} # instanceId → true
|
||||
|
||||
# ── Scene refs ────────────────────────────────────────────────────────────────
|
||||
var _tile_bodies : Dictionary = {} # "x,y" → StaticBody3D
|
||||
var _unit_meshes : Dictionary = {} # instanceId → MeshInstance3D
|
||||
var _unit_meshes : Dictionary = {} # instanceId → MeshInstance3D (placeholder)
|
||||
var _unit_bodies : Dictionary = {} # instanceId → StaticBody3D
|
||||
|
||||
# ── Lifecycle ─────────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -387,6 +388,7 @@ func _build_board(board: Dictionary) -> void:
|
||||
for c in _hi_root.get_children(): c.queue_free()
|
||||
_tile_bodies.clear()
|
||||
_unit_meshes.clear()
|
||||
_unit_bodies.clear()
|
||||
for tile: Dictionary in board.get("tiles", []):
|
||||
var coord : Dictionary = tile.get("coord", {})
|
||||
_add_tile(coord.get("x", 0), coord.get("y", 0),
|
||||
@@ -433,6 +435,7 @@ func _update_tile_affinity(x: int, y: int, affinity: String) -> void:
|
||||
func _rebuild_units(state: Dictionary) -> void:
|
||||
for c in _units_root.get_children(): c.queue_free()
|
||||
_unit_meshes.clear()
|
||||
_unit_bodies.clear()
|
||||
var my_id := Auth.user_id
|
||||
for player: Dictionary in state.get("players", []):
|
||||
var is_mine : bool = str(player.get("accountId", "")).to_lower() == my_id.to_lower()
|
||||
@@ -448,25 +451,24 @@ func _rebuild_units(state: Dictionary) -> void:
|
||||
func _add_unit_mesh(unit: Dictionary, team_color: Color) -> void:
|
||||
var pd : Dictionary = unit.get("position", {})
|
||||
if pd.is_empty(): return
|
||||
var wp := _coord_to_world(pd.get("x", 0), pd.get("y", 0))
|
||||
var wp := _coord_to_world(pd.get("x", 0), pd.get("y", 0))
|
||||
var card_type : String = unit.get("cardType", "Spirit")
|
||||
var iid : String = unit.get("instanceId", "")
|
||||
var exhausted : bool = unit.get("isExhausted", false)
|
||||
|
||||
var body := StaticBody3D.new()
|
||||
var body := StaticBody3D.new()
|
||||
body.set_meta("unit_id", iid)
|
||||
|
||||
var shape := CollisionShape3D.new()
|
||||
var mi := MeshInstance3D.new()
|
||||
var mat := StandardMaterial3D.new()
|
||||
var shape := CollisionShape3D.new()
|
||||
var mi := MeshInstance3D.new()
|
||||
var mat := StandardMaterial3D.new()
|
||||
mat.roughness = 0.65
|
||||
|
||||
var y_offset : float
|
||||
match card_type:
|
||||
"ArchSpirit":
|
||||
var cyl := CylinderMesh.new()
|
||||
cyl.top_radius = 0.22
|
||||
cyl.bottom_radius = 0.22
|
||||
cyl.height = 0.8
|
||||
cyl.top_radius = 0.22; cyl.bottom_radius = 0.22; cyl.height = 0.8
|
||||
mi.mesh = cyl
|
||||
mat.albedo_color = team_color.lightened(0.25)
|
||||
mat.emission_enabled = true
|
||||
@@ -475,7 +477,6 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color) -> void:
|
||||
cs.radius = 0.22; cs.height = 0.8
|
||||
shape.shape = cs
|
||||
y_offset = 0.5 + 0.4
|
||||
|
||||
"Ruin":
|
||||
var bm := BoxMesh.new()
|
||||
bm.size = Vector3(0.25, 1.0, 0.25)
|
||||
@@ -485,11 +486,9 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color) -> void:
|
||||
bs.size = Vector3(0.25, 1.0, 0.25)
|
||||
shape.shape = bs
|
||||
y_offset = 0.5 + 0.5
|
||||
|
||||
_:
|
||||
var sm := SphereMesh.new()
|
||||
sm.radius = 0.28
|
||||
sm.height = 0.28
|
||||
sm.radius = 0.28; sm.height = 0.28
|
||||
mi.mesh = sm
|
||||
mat.albedo_color = team_color
|
||||
var ss := SphereShape3D.new()
|
||||
@@ -498,7 +497,7 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color) -> void:
|
||||
y_offset = 0.5 + 0.28
|
||||
|
||||
mi.material_override = mat
|
||||
if unit.get("isExhausted", false):
|
||||
if exhausted:
|
||||
mat.albedo_color = mat.albedo_color.darkened(0.45)
|
||||
|
||||
body.add_child(shape)
|
||||
@@ -506,6 +505,47 @@ func _add_unit_mesh(unit: Dictionary, team_color: Color) -> void:
|
||||
body.position = wp + Vector3(0, y_offset, 0)
|
||||
_units_root.add_child(body)
|
||||
_unit_meshes[iid] = mi
|
||||
_unit_bodies[iid] = body
|
||||
|
||||
# ── Try to load the real 3D model from the card definition ──────────────
|
||||
var def_id : String = str(unit.get("cardDefinitionId", ""))
|
||||
if def_id.is_empty():
|
||||
return
|
||||
var def := ContentCache.get_definition(def_id)
|
||||
var model_url := _find_model3d_url(def)
|
||||
if model_url.is_empty():
|
||||
return
|
||||
|
||||
ModelCache.load_model(model_url, func(model_node: Node3D) -> void:
|
||||
# Guard: body may have been freed by a state rebuild before load finished.
|
||||
if not is_instance_valid(body):
|
||||
if model_node != null:
|
||||
model_node.queue_free()
|
||||
return
|
||||
if model_node == null:
|
||||
return
|
||||
|
||||
# Remove the placeholder MeshInstance3D.
|
||||
for child in body.get_children():
|
||||
if child is MeshInstance3D:
|
||||
child.queue_free()
|
||||
|
||||
# Position model so its local origin sits at the body's position.
|
||||
# Models should be authored with their base (foot) at y = 0.
|
||||
# Adjust the body back down so the model base lands on the tile surface.
|
||||
body.position = wp + Vector3(0, 0.5, 0)
|
||||
body.add_child(model_node)
|
||||
_unit_meshes[iid] = model_node
|
||||
)
|
||||
|
||||
|
||||
func _find_model3d_url(def: Dictionary) -> String:
|
||||
for asset: Variant in def.get("assets", []):
|
||||
var d := asset as Dictionary
|
||||
if d == null: continue
|
||||
if str(d.get("assetType", "")) == "Model3D":
|
||||
return str(d.get("cdnUrl", ""))
|
||||
return ""
|
||||
|
||||
# ── Highlights ────────────────────────────────────────────────────────────────
|
||||
|
||||
|
||||
Reference in New Issue
Block a user