PropSet places about 3,000 pines, scrub, boulders, wrecks and crates per island from the seed, by elevation band with grove noise, clear of points of interest. Each prop is one collision box in SimGround with health from Tuning.PROP_KINDS; destroying it removes the box at once (SimGround.remove_solid). Prop health is part of the state hash. PropView draws them as one MultiMesh per kind per 128 m square (about 150 instanced draws), with per-kind visibility ranges, procedural flat-shaded meshes fitted to the collision boxes, and cheap sync of felled props. Closes #12 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
126 lines
5.8 KiB
GDScript
126 lines
5.8 KiB
GDScript
extends TestCase
|
|
|
|
## The client side of W5: PropView draws every prop, matches collision, hides felled
|
|
## props cheaply, and keeps the draw-call count bounded.
|
|
|
|
const SEED: int = 2
|
|
|
|
var _island: Island
|
|
var _props: PropSet
|
|
|
|
|
|
func before_each() -> void:
|
|
if _island == null:
|
|
_island = Island.generate(SEED)
|
|
_props = PropSet.generate(_island, PoiSet.generate(_island))
|
|
|
|
|
|
func test_every_prop_has_exactly_one_instance() -> void:
|
|
var view: PropView = PropView.new()
|
|
view.setup(_props)
|
|
var seen: Dictionary = {}
|
|
var total: int = 0
|
|
for child: Node in view.get_children():
|
|
total += (child as MultiMeshInstance3D).multimesh.instance_count
|
|
assert_eq(total, _props.count())
|
|
assert_eq(view.drawn, _props.count())
|
|
for prop: int in _props.count():
|
|
var key: Vector2i = Vector2i(view.node_for(prop).get_index(), view.instance_index(prop))
|
|
assert_false(seen.has(key), "two props share an instance")
|
|
seen[key] = true
|
|
assert_true(view.node_for(prop).name.begins_with(_props.kind_name(prop)), "prop %d is drawn as the wrong kind" % prop)
|
|
view.free()
|
|
|
|
|
|
func test_instances_fit_their_collision_boxes() -> void:
|
|
# The unit meshes span -0.5..0.5 and 0..1, so the transformed footprint must match
|
|
# the box the simulation collides with (pines' canopies excepted: only the trunk).
|
|
for prop: int in _props.count():
|
|
var box: AABB = _props.boxes[prop]
|
|
var xf: Transform3D = PropView.instance_transform(_props, prop)
|
|
assert_near(xf.origin.y, box.position.y, 0.001)
|
|
assert_near(xf.origin.x, box.get_center().x, 0.001)
|
|
assert_near(xf.origin.z, box.get_center().z, 0.001)
|
|
var top: Vector3 = xf * Vector3(0.0, 1.0, 0.0)
|
|
assert_near(top.y, box.end.y, 0.001, "prop %d height" % prop)
|
|
# Round kinds are checked around their rim, boxy ones at their corners.
|
|
var outline: Array[Vector3] = [Vector3(0.5, 0.0, 0.5), Vector3(-0.5, 0.0, 0.5), Vector3(0.5, 0.0, -0.5), Vector3(-0.5, 0.0, -0.5)]
|
|
if _props.kind_name(prop) in ["pine", "scrub", "boulder"]:
|
|
outline.clear()
|
|
for step: int in 12:
|
|
outline.append(Vector3(cos(TAU * step / 12.0), 0.0, sin(TAU * step / 12.0)) * 0.5)
|
|
for corner: Vector3 in outline:
|
|
var at: Vector3 = xf * corner
|
|
var spill: float = maxf(maxf(box.position.x - at.x, at.x - box.end.x), maxf(box.position.z - at.z, at.z - box.end.z))
|
|
# Crates may turn up to 8 degrees, which pushes a corner out by about 10%.
|
|
var allowed: float = 0.12 * maxf(box.size.x, box.size.z) if _props.kind_name(prop) == "crate" else 0.01
|
|
assert_true(spill <= allowed, "%s %d footprint spills %.2f m past its box" % [_props.kind_name(prop), prop, spill])
|
|
|
|
|
|
func test_meshes_face_outwards_and_stay_in_the_unit_footprint() -> void:
|
|
for kind: String in PropSet.KINDS:
|
|
var mesh: ArrayMesh = PropView.build_mesh(kind)
|
|
var arrays: Array = mesh.surface_get_arrays(0)
|
|
var vertices: PackedVector3Array = arrays[Mesh.ARRAY_VERTEX]
|
|
var normals: PackedVector3Array = arrays[Mesh.ARRAY_NORMAL]
|
|
assert_true(vertices.size() >= 36, kind + " has too few vertices")
|
|
var bounds: AABB = AABB(vertices[0], Vector3.ZERO)
|
|
var centre: Vector3 = Vector3.ZERO
|
|
for vertex: Vector3 in vertices:
|
|
bounds = bounds.expand(vertex)
|
|
centre += vertex
|
|
centre /= vertices.size()
|
|
assert_true(bounds.position.y >= -0.001 and bounds.end.y <= 1.001, "%s spans y %s" % [kind, bounds])
|
|
if kind != "pine":
|
|
assert_true(bounds.position.x >= -0.501 and bounds.end.x <= 0.501, "%s spans x %s" % [kind, bounds])
|
|
assert_true(bounds.position.z >= -0.501 and bounds.end.z <= 0.501, "%s spans z %s" % [kind, bounds])
|
|
# Most faces point away from the middle; inside-out winding would flip this.
|
|
var outward: int = 0
|
|
for index: int in vertices.size():
|
|
if normals[index].dot(vertices[index] - centre) > 0.0:
|
|
outward += 1
|
|
assert_true(outward > vertices.size() * 0.6, "%s: only %d of %d normals face out" % [kind, outward, vertices.size()])
|
|
|
|
|
|
func test_felled_props_vanish_and_the_rest_stay() -> void:
|
|
var view: PropView = PropView.new()
|
|
view.setup(_props)
|
|
var victims: Array[int] = [0, 17, _props.count() - 1]
|
|
for prop: int in victims:
|
|
while _props.is_standing(prop):
|
|
_props.damage(prop, 1000)
|
|
assert_eq(view.sync(), victims.size())
|
|
assert_eq(view.sync(), 0, "a second sync has nothing new")
|
|
assert_eq(view.drawn, _props.count() - victims.size())
|
|
assert_eq(view.hidden_count, victims.size())
|
|
for prop: int in [0, 1, 17, 18, _props.count() - 1]:
|
|
assert_eq(view.is_drawn(prop), not victims.has(prop))
|
|
view.free()
|
|
|
|
|
|
func test_draw_calls_and_main_thread_cost_stay_bounded() -> void:
|
|
# Software rendering can't time the GPU. What can be checked here: the number of
|
|
# instanced draws is bounded by culling squares times kinds, every draw has a
|
|
# visibility range, building takes a loading-screen moment, and applying a burst of
|
|
# destruction costs well under a millisecond of the frame.
|
|
var started: int = Time.get_ticks_usec()
|
|
var view: PropView = PropView.new()
|
|
view.setup(_props)
|
|
var build_ms: int = (Time.get_ticks_usec() - started) / 1000
|
|
var squares: int = ceili(Tuning.WORLD_SIZE_METRES / PropView.CHUNK)
|
|
assert_true(view.node_count() <= squares * squares * PropSet.KINDS.size())
|
|
assert_true(view.node_count() < _props.count() / 10, "%d draws for %d props" % [view.node_count(), _props.count()])
|
|
for child: Node in view.get_children():
|
|
var instance: MultiMeshInstance3D = child
|
|
assert_true(instance.visibility_range_end > 0.0 and instance.visibility_range_end <= 600.0, instance.name)
|
|
var burst: int = 0
|
|
for prop: int in range(0, _props.count(), 60):
|
|
_props.damage(prop, 100000)
|
|
burst += 1
|
|
view.sync()
|
|
print(" prop view: %d props in %d draws, built in %d ms, %d-prop destruction sync %d us"
|
|
% [_props.count(), view.node_count(), build_ms, burst, view.last_sync_usec])
|
|
assert_true(build_ms < 4000, "setup took %d ms" % build_ms)
|
|
assert_true(view.last_sync_usec < 1000, "sync of %d felled props took %d us" % [burst, view.last_sync_usec])
|
|
view.free()
|