A4: Item models and icons
tools/models/items.py builds every item in headless Blender from primitives: the boathook, the eight weapons and powder keg, four consumables and five kinds of ammo, each with its own outline. It writes an OBJ per item (the Debian Blender's glTF exporter needs numpy), a JSON with the grip and the left hand's support point, and a 128 px Cycles icon rendered on the CPU. Materials are named after Palette colours, and TRIM2_/TRIM3_ surfaces are the tier treatment: rope whipping from tier 2, brass bands from tier 3; tier 0 is faded. ItemModels paints the models from Palette by tier. EntityView puts the held item's grip in each figure's right hand, turned with the player's pitch and put away aboard, falling, gliding or swimming, and draws items lying in the world with their models, one MultiMesh per model and tier. The hotbar shows each slot's icon. make models rebuilds them; make item-captures renders the items held, over the shoulder, on the ground and by tier. Closes #44 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
## Draws what moves in a match: players, the sky barge while it crosses, items lying
|
||||
## in the world and sea chests. `sync()` matches everything to the simulation as it is
|
||||
## Draws what moves in a match: players with what they hold, the sky barge while it
|
||||
## crosses, items lying in the world and sea chests. `sync()` matches everything to the simulation as it is
|
||||
## now. These are stand-in shapes built in code, in the weathered seafaring palette of
|
||||
## docs/STYLE.md: an oilskin coat, a sou'wester and a canvas wing-sail. The art
|
||||
## pass replaces them. Nothing here is ever used for collision.
|
||||
## pass replaces them. Held and lying items are the models of A4 (ItemModels): a held
|
||||
## one sits with its grip in the figure's right hand and points where its player looks.
|
||||
## Nothing here is ever used for collision.
|
||||
class_name EntityView
|
||||
extends Node3D
|
||||
|
||||
@@ -22,6 +24,10 @@ const ITEM: Color = Palette.GILT
|
||||
## carries them all at one point; this only spreads them out to be seen.
|
||||
const DECK_SPACING: float = 1.3
|
||||
const DECK_ROWS: int = 4
|
||||
## Where a figure's right hand holds an item, from its feet, facing +Z; its right is -X.
|
||||
const HAND: Vector3 = Vector3(-0.24, 1.2, 0.32)
|
||||
## Items lying in the world are drawn this much larger than life, to be seen.
|
||||
const PICKUP_SCALE: float = 1.4
|
||||
|
||||
## The entity drawn in the brightest oilskin, or 0.
|
||||
var local_id: int = 0
|
||||
@@ -33,7 +39,9 @@ var _sim: Simulation
|
||||
## Entity ID -> the figure's root node.
|
||||
var _figures: Dictionary = {}
|
||||
var _barge: Node3D
|
||||
var _pickups: MultiMeshInstance3D
|
||||
## Items lying in the world: one MultiMeshInstance3D per item model and tier.
|
||||
var _pickups: Node3D
|
||||
var _pickup_fields: Dictionary = {}
|
||||
var _chests: MultiMeshInstance3D
|
||||
|
||||
|
||||
@@ -42,7 +50,8 @@ func setup(sim: Simulation) -> void:
|
||||
_barge = _build_barge()
|
||||
_barge.visible = false
|
||||
add_child(_barge)
|
||||
_pickups = _box_field("Pickups", Vector3(0.34, 0.34, 0.34), true)
|
||||
_pickups = Node3D.new()
|
||||
_pickups.name = "Pickups"
|
||||
add_child(_pickups)
|
||||
_chests = _box_field("Chests", Vector3(0.9, 0.55, 0.6), false)
|
||||
add_child(_chests)
|
||||
@@ -62,6 +71,31 @@ func barge() -> Node3D:
|
||||
return _barge
|
||||
|
||||
|
||||
## Items drawn lying in the world.
|
||||
func pickup_count() -> int:
|
||||
var total: int = 0
|
||||
for field: MultiMeshInstance3D in _pickup_fields.values():
|
||||
total += field.multimesh.instance_count
|
||||
return total
|
||||
|
||||
|
||||
## The catalog code of the item a player holds: -1 for the boathook. A full copy of
|
||||
## the player (the server's, or the client's own) knows its slots; others carry the
|
||||
## held code the snapshot gave them.
|
||||
static func held_code(entity: SimEntity) -> int:
|
||||
if entity.slot_items.size() > 0:
|
||||
var slot: int = entity.selected - 1
|
||||
return entity.slot_items[slot] if slot >= 0 and slot < entity.slot_items.size() else -1
|
||||
return entity.item
|
||||
|
||||
|
||||
static func held_tier(entity: SimEntity) -> int:
|
||||
var slot: int = entity.selected - 1
|
||||
if slot >= 0 and slot < entity.slot_tiers.size():
|
||||
return entity.slot_tiers[slot]
|
||||
return 1
|
||||
|
||||
|
||||
func sync() -> void:
|
||||
if _sim == null:
|
||||
return
|
||||
@@ -99,18 +133,13 @@ func sync() -> void:
|
||||
aboard += 1
|
||||
figure.scale.y = Tuning.CROUCH_HEIGHT / Tuning.PLAYER_HEIGHT if entity.crouching else 1.0
|
||||
(figure.get_node("Sail") as Node3D).visible = entity.drop_state == Drop.GLIDING
|
||||
_hold(figure, entity)
|
||||
for entity_id: int in _figures.keys():
|
||||
if not seen.has(entity_id):
|
||||
_figures[entity_id].queue_free()
|
||||
_figures.erase(entity_id)
|
||||
|
||||
var items: MultiMesh = _pickups.multimesh
|
||||
items.instance_count = _sim.pickups.size()
|
||||
for index: int in _sim.pickups.size():
|
||||
var pickup: SimEntity = _sim.entities.find(_sim.pickups[index])
|
||||
var at: Vector3 = pickup.position + Vector3(0.0, 0.3, 0.0) if pickup != null else Vector3(0.0, -1000.0, 0.0)
|
||||
items.set_instance_transform(index, Transform3D(Basis(Vector3.UP, 0.6 * index), at))
|
||||
items.set_instance_color(index, ITEM)
|
||||
_sync_pickups()
|
||||
var chests: MultiMesh = _chests.multimesh
|
||||
chests.instance_count = _sim.loot.chest_positions.size()
|
||||
for index: int in _sim.loot.chest_positions.size():
|
||||
@@ -122,6 +151,76 @@ func _process(_delta: float) -> void:
|
||||
sync()
|
||||
|
||||
|
||||
## Puts the held item's model in the figure's hand, pointing where the player looks.
|
||||
## Nothing is held aboard the barge, falling, gliding or swimming.
|
||||
func _hold(figure: Node3D, entity: SimEntity) -> void:
|
||||
var hand: Node3D = figure.get_node("Hand")
|
||||
var held: MeshInstance3D = hand.get_node("Held")
|
||||
var busy: bool = entity.drop_state != Drop.NONE or entity.swimming or entity.eliminated
|
||||
hand.visible = not busy
|
||||
if busy:
|
||||
return
|
||||
var id: String = ItemModels.id_of(held_code(entity))
|
||||
var mesh: ArrayMesh = ItemModels.model(id, held_tier(entity))
|
||||
if held.mesh != mesh:
|
||||
held.mesh = mesh
|
||||
var pitch: float = float(entity.pitch) / PlayerCommand.PITCH_MAX * (PI * 0.5)
|
||||
hand.rotation = Vector3(-pitch, 0.0, 0.0)
|
||||
|
||||
|
||||
func _sync_pickups() -> void:
|
||||
var groups: Dictionary = {}
|
||||
for index: int in _sim.pickups.size():
|
||||
var pickup: SimEntity = _sim.entities.find(_sim.pickups[index])
|
||||
if pickup == null:
|
||||
continue
|
||||
var id: String = ItemModels.id_of(pickup.item)
|
||||
var key: String = "%s@%d" % [id, ItemModels.shown_tier(id, pickup.item_tier)]
|
||||
if not groups.has(key):
|
||||
groups[key] = []
|
||||
groups[key].append(pickup)
|
||||
for key: String in _pickup_fields.keys():
|
||||
if not groups.has(key):
|
||||
_pickup_fields[key].multimesh.instance_count = 0
|
||||
for key: String in groups:
|
||||
var field: MultiMeshInstance3D = _pickup_fields.get(key)
|
||||
if field == null:
|
||||
field = _item_field(key)
|
||||
_pickup_fields[key] = field
|
||||
_pickups.add_child(field)
|
||||
var lying: Array = groups[key]
|
||||
var lift: float = pickup_lift(field.multimesh.mesh)
|
||||
field.multimesh.instance_count = lying.size()
|
||||
for index: int in lying.size():
|
||||
var pickup: SimEntity = lying[index]
|
||||
# Laid on their side, turned by ID so a pile doesn't line up.
|
||||
var basis: Basis = Basis(Vector3.UP, 0.9 * pickup.id).scaled(Vector3.ONE * PICKUP_SCALE)
|
||||
field.multimesh.set_instance_transform(index, Transform3D(basis, pickup.position + Vector3(0.0, lift, 0.0)))
|
||||
|
||||
|
||||
## How far above the spot an item lying there is placed, so the model's lowest point,
|
||||
## not its grip, rests on the ground.
|
||||
static func pickup_lift(mesh: Mesh) -> float:
|
||||
return 0.02 - mesh.get_aabb().position.y * PICKUP_SCALE
|
||||
|
||||
|
||||
func _item_field(key: String) -> MultiMeshInstance3D:
|
||||
var multimesh: MultiMesh = MultiMesh.new()
|
||||
multimesh.transform_format = MultiMesh.TRANSFORM_3D
|
||||
var mesh: ArrayMesh = ItemModels.model(key.get_slice("@", 0), key.get_slice("@", 1).to_int())
|
||||
if mesh == null:
|
||||
var stand_in: BoxMesh = BoxMesh.new()
|
||||
stand_in.size = Vector3(0.25, 0.25, 0.25)
|
||||
multimesh.mesh = stand_in
|
||||
else:
|
||||
multimesh.mesh = mesh
|
||||
var field: MultiMeshInstance3D = MultiMeshInstance3D.new()
|
||||
field.name = key.replace("@", "_")
|
||||
field.multimesh = multimesh
|
||||
field.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
||||
return field
|
||||
|
||||
|
||||
func _build_figure(entity_id: int) -> Node3D:
|
||||
var coat: Color = OILSKINS[0] if entity_id == local_id else OILSKINS[1 + entity_id % (OILSKINS.size() - 1)]
|
||||
var figure: Node3D = Node3D.new()
|
||||
@@ -150,6 +249,13 @@ func _build_figure(entity_id: int) -> Node3D:
|
||||
sail.rotation.x = deg_to_rad(-8.0)
|
||||
sail.visible = false
|
||||
figure.add_child(sail)
|
||||
var hand: Node3D = Node3D.new()
|
||||
hand.name = "Hand"
|
||||
hand.position = HAND
|
||||
figure.add_child(hand)
|
||||
var held: MeshInstance3D = MeshInstance3D.new()
|
||||
held.name = "Held"
|
||||
hand.add_child(held)
|
||||
return figure
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
## The models and icons of every item (A4), as tools/models/items.py builds them in
|
||||
## Blender: assets/models/items/<id>.obj with <id>.json beside it, and
|
||||
## assets/icons/items/<id>.png.
|
||||
##
|
||||
## Each OBJ surface's material is named after the Palette colour it takes, and parts
|
||||
## of the tier treatment are named TRIM2_<colour> and TRIM3_<colour>. model() keeps the
|
||||
## surfaces a tier shows and paints them from Palette, so colours still live in one
|
||||
## place; tier 0 is drawn weathered, its paint and metal faded toward grey wood.
|
||||
##
|
||||
## Every model's origin is the grip, where the right hand closes, and its working end
|
||||
## points along +Z, the way a figure faces. info() gives the support point for the left
|
||||
## hand and the bounds.
|
||||
class_name ItemModels
|
||||
extends RefCounted
|
||||
|
||||
const MODEL_DIR: String = "res://assets/models/items"
|
||||
const ICON_DIR: String = "res://assets/icons/items"
|
||||
## Material names in the OBJ files -> Palette colours.
|
||||
const COLOURS: Dictionary = {
|
||||
"TAR": Palette.TAR, "TAR_WEATHERED": Palette.TAR_WEATHERED, "PLANK": Palette.PLANK,
|
||||
"PLANK_BLEACHED": Palette.PLANK_BLEACHED, "DRIFTWOOD": Palette.DRIFTWOOD,
|
||||
"CRATE_WOOD": Palette.CRATE_WOOD, "BARK": Palette.BARK, "CANVAS": Palette.CANVAS,
|
||||
"TANNED_SAIL": Palette.TANNED_SAIL, "SALVAGE_IRON": Palette.SALVAGE_IRON,
|
||||
"OCHRE_RED": Palette.OCHRE_RED, "SEA_PAINT": Palette.SEA_PAINT, "GILT": Palette.GILT,
|
||||
"PAPER": Palette.PAPER,
|
||||
}
|
||||
## How far tier 0's colours fade toward bleached plank.
|
||||
const WEATHERING: float = 0.35
|
||||
|
||||
static var _meshes: Dictionary = {}
|
||||
static var _info: Dictionary = {}
|
||||
static var _icons: Dictionary = {}
|
||||
|
||||
|
||||
## Every item ID the game has: the boathook, then the catalog's weapons, consumables
|
||||
## and ammo.
|
||||
static func ids() -> Array[String]:
|
||||
var out: Array[String] = [ItemCatalog.BOATHOOK]
|
||||
for code: int in ItemCatalog.count():
|
||||
out.append(ItemCatalog.get_def(code).id)
|
||||
return out
|
||||
|
||||
|
||||
## The item ID for a catalog code, with -1 (or any code with no item) the boathook.
|
||||
static func id_of(code: int) -> String:
|
||||
var def: ItemDef = ItemCatalog.get_def(code)
|
||||
return def.id if def != null else ItemCatalog.BOATHOOK
|
||||
|
||||
|
||||
static func has_model(id: String) -> bool:
|
||||
return ResourceLoader.exists("%s/%s.obj" % [MODEL_DIR, id])
|
||||
|
||||
|
||||
## The model for an item at a tier (0 to 3), painted; null if there's no model. Shared
|
||||
## between callers: don't change it.
|
||||
static func model(id: String, tier: int = 1) -> ArrayMesh:
|
||||
tier = shown_tier(id, tier)
|
||||
var key: String = "%s@%d" % [id, tier]
|
||||
if _meshes.has(key):
|
||||
return _meshes[key]
|
||||
var made: ArrayMesh = null
|
||||
if has_model(id):
|
||||
var source: Mesh = load("%s/%s.obj" % [MODEL_DIR, id]) as Mesh
|
||||
if source != null:
|
||||
made = _paint(source, tier)
|
||||
made.resource_name = key
|
||||
_meshes[key] = made
|
||||
return made
|
||||
|
||||
|
||||
## The tier an item is drawn at: weapons and the boathook by their tier, 0 to 3;
|
||||
## consumables and ammo have no tiers and are always drawn as tier 1.
|
||||
static func shown_tier(id: String, tier: int) -> int:
|
||||
var def: ItemDef = ItemCatalog.get_def(ItemCatalog.code_of(id))
|
||||
if id != ItemCatalog.BOATHOOK and (def == null or not def.is_weapon()):
|
||||
return 1
|
||||
return clampi(tier, 0, 3)
|
||||
|
||||
|
||||
## The grip, support point (or null) and bounds of a model, in its own space.
|
||||
static func info(id: String) -> Dictionary:
|
||||
if _info.has(id):
|
||||
return _info[id]
|
||||
var parsed: Variant = JSON.parse_string(FileAccess.get_file_as_string("%s/%s.json" % [MODEL_DIR, id]))
|
||||
var out: Dictionary = parsed if parsed is Dictionary else {}
|
||||
_info[id] = out
|
||||
return out
|
||||
|
||||
|
||||
static func icon(id: String) -> Texture2D:
|
||||
if _icons.has(id):
|
||||
return _icons[id]
|
||||
var path: String = "%s/%s.png" % [ICON_DIR, id]
|
||||
var found: Texture2D = load(path) as Texture2D if ResourceLoader.exists(path) else null
|
||||
_icons[id] = found
|
||||
return found
|
||||
|
||||
|
||||
## The tier a material first shows at: 0 for the item itself, 2 or 3 for its trim.
|
||||
static func trim_level(material_name: String) -> int:
|
||||
if material_name.begins_with("TRIM") and material_name.length() > 5:
|
||||
return material_name.substr(4, 1).to_int()
|
||||
return 0
|
||||
|
||||
|
||||
## The Palette colour a material takes at a tier, or null if the name isn't one.
|
||||
static func colour_of(material_name: String, tier: int) -> Variant:
|
||||
# "TRIM2_GILT" -> "GILT".
|
||||
var base: String = material_name.substr(6) if trim_level(material_name) > 0 else material_name
|
||||
if not COLOURS.has(base):
|
||||
return null
|
||||
var colour: Color = COLOURS[base]
|
||||
if tier == 0:
|
||||
colour = colour.lerp(Palette.PLANK_BLEACHED, WEATHERING)
|
||||
return colour
|
||||
|
||||
|
||||
## Lets go of every loaded model and icon.
|
||||
static func clear() -> void:
|
||||
_meshes.clear()
|
||||
_info.clear()
|
||||
_icons.clear()
|
||||
|
||||
|
||||
static func _paint(source: Mesh, tier: int) -> ArrayMesh:
|
||||
var out: ArrayMesh = ArrayMesh.new()
|
||||
for surface: int in source.get_surface_count():
|
||||
var material: Material = source.surface_get_material(surface)
|
||||
var name: String = material.resource_name if material != null else ""
|
||||
if trim_level(name) > tier:
|
||||
continue
|
||||
var arrays: Array = source.surface_get_arrays(surface)
|
||||
out.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
|
||||
var paint: StandardMaterial3D = StandardMaterial3D.new()
|
||||
var colour: Variant = colour_of(name, tier)
|
||||
paint.albedo_color = colour if colour != null else Palette.HOLD_SHADOW
|
||||
paint.roughness = 0.85
|
||||
if name.contains("SALVAGE_IRON") or name.contains("GILT"):
|
||||
paint.metallic = 0.35
|
||||
out.surface_set_material(out.get_surface_count() - 1, paint)
|
||||
out.surface_set_name(out.get_surface_count() - 1, name)
|
||||
return out
|
||||
@@ -0,0 +1 @@
|
||||
uid://csga052nxxm7d
|
||||
Reference in New Issue
Block a user