harvest/game/tests/test_look.gd
2026-10-07 13:41:38 -04:00

194 lines
8.7 KiB
GDScript

extends RefCounted
## The toon look (ToonLook): lit, opaque StandardMaterial3Ds become toon ShaderMaterials with the
## same color, texture, and vertex-color use (double-sided ones get the double-sided shader), one
## per source material; unshaded, transparent, billboard, and no-depth-test materials stay. Nodes
## added later are swapped by the end of their frame, after their builder tinted them (the
## co-op partner's blue scarecrow). On the real farm no lit StandardMaterial3D is left showing,
## and the outline quad never casts a shadow and draws under the transparent effects. Label3Ds
## stop rendering in 3D (they'd be pixelated) and ScreenLabels draws them as flat text where they
## project, at least MIN_SIZE tall, only while shown and in front of the camera, and nudged apart
## where they'd overlap.
const TOON := "res://game/resources/toon.gdshader"
const TOON_DOUBLE_SIDED := "res://game/resources/toon_double_sided.gdshader"
func run(r: GameTestRunner) -> void:
_materials(r)
await _farm(r)
func _materials(r: GameTestRunner) -> void:
var look := ToonLook.new()
var lit := StandardMaterial3D.new()
lit.albedo_color = Color(0.8, 0.3, 0.1)
var tex := PlaceholderTexture2D.new()
lit.albedo_texture = tex
lit.vertex_color_use_as_albedo = true
var toon := look.toon_material(lit) as ShaderMaterial
r.check(toon != null and toon.shader.resource_path == TOON, "a lit, opaque material becomes a toon ShaderMaterial")
r.check(toon != null and toon.get_shader_parameter("albedo") == lit.albedo_color
and toon.get_shader_parameter("albedo_texture") == tex and toon.get_shader_parameter("use_vertex_color") == true,
"with the same color, texture, and vertex-color use")
r.check(look.toon_material(lit) == toon and look.toon_material(toon) == toon,
"one toon material per source (and swapping again changes nothing)")
var two_sided := StandardMaterial3D.new()
two_sided.cull_mode = BaseMaterial3D.CULL_DISABLED
r.check((look.toon_material(two_sided) as ShaderMaterial).shader.resource_path == TOON_DOUBLE_SIDED,
"a double-sided material gets the double-sided toon shader")
var unshaded := StandardMaterial3D.new()
unshaded.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
var glass := StandardMaterial3D.new()
glass.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
var facing := StandardMaterial3D.new()
facing.billboard_mode = BaseMaterial3D.BILLBOARD_ENABLED
var overlay := StandardMaterial3D.new()
overlay.no_depth_test = true
var kept := 0
for m: StandardMaterial3D in [unshaded, glass, facing, overlay]:
if look.toon_material(m) == m:
kept += 1
r.check(kept == 4, "unshaded, transparent, billboard, and no-depth-test materials stay (%d of 4)" % kept)
var mesh := BoxMesh.new()
mesh.material = lit
var mi := MeshInstance3D.new()
mi.mesh = mesh
look.swap_node(mi)
r.check(mi.get_surface_override_material(0) == toon and mesh.material == lit,
"a mesh's own material is swapped with a surface override (the mesh resource is untouched)")
var over := MeshInstance3D.new()
over.mesh = mesh
over.material_override = two_sided
look.swap_node(over)
r.check(over.material_override == look.toon_material(two_sided), "a material override is swapped in place")
var debris := CPUParticles3D.new()
debris.mesh = mesh
look.swap_node(debris)
r.check(debris.material_override == toon, "particles swap their mesh's material through an override")
mi.free()
over.free()
debris.free()
look.free()
func _farm(r: GameTestRunner) -> void:
var farm := (load("res://game/scenes/farm.tscn") as PackedScene).instantiate() as Node3D
r.root.add_child(farm)
await r.process_frame
var sim := farm.get_node("FarmSim") as FarmSim
sim.set_physics_process(false)
var look := ToonLook.new()
r.root.add_child(look)
var left := _lit_left(farm)
r.check(left.is_empty(), "no lit StandardMaterial3D is left showing on the farm (%d left: %s)" % [left.size(), ", ".join(left.slice(0, 5))])
r.check(_toon_count(farm) > 100, "the farm's props, characters, and weapons are toon-shaded (%d surfaces)" % _toon_count(farm))
var a := sim.add_partner(false, "Ben")
var body := a.body
r.check(not _lit_left(body).is_empty(), "the partner's scarecrow arrives with lit materials (tinted blue)")
await r.process_frame
await r.process_frame
r.check(_lit_left(body).is_empty(), "and is toon-shaded by the end of the frame it arrived in")
var blue := false
for mi in body.find_children("*", "MeshInstance3D", true, false):
var m := (mi as MeshInstance3D).material_override as ShaderMaterial
if m != null and m.get_shader_parameter("albedo") == Color(0.55, 0.75, 1.0):
blue = true
r.check(blue, "keeping its blue tint")
var label3ds := farm.find_children("*", "Label3D", true, false)
r.check(label3ds.size() >= 5 and label3ds.all(func(l: Node) -> bool: return (l as Label3D).layers == 0),
"the farm's %d Label3Ds don't render in 3D (ScreenLabels draws them)" % label3ds.size())
var cam := r.root.get_camera_3d()
var sign := Label3D.new()
sign.text = "Wood 4"
sign.font_size = 40
sign.pixel_size = 0.0075
sign.outline_size = 10
sign.modulate = Color(1.0, 0.8, 0.3)
farm.add_child(sign)
sign.global_position = cam.global_position - cam.global_basis.z * 12.0
sign.reset_physics_interpolation() # drawn where it's displayed, as in 3D
await r.process_frame
var drawn := look.labels.entries().filter(func(e: Dictionary) -> bool: return e["label"] == sign)
r.check(drawn.size() == 1 and drawn[0]["text"] == "Wood 4" and drawn[0]["color"] == sign.modulate
and drawn[0]["font_size"] >= ScreenLabels.MIN_SIZE and drawn[0]["outline_size"] > 0
and r.root.get_visible_rect().encloses(drawn[0]["rect"]),
"a label added later is drawn flat on screen with its text, color, and outline, readable size (%s)"
% [drawn[0]["font_size"] if drawn.size() == 1 else "not drawn"])
var twin := sign.duplicate() as Label3D
twin.text = "Alien Goop 0"
farm.add_child(twin)
twin.global_position = sign.global_position
twin.reset_physics_interpolation()
await r.process_frame
var pair := look.labels.entries().filter(func(e: Dictionary) -> bool: return e["label"] in [sign, twin])
r.check(pair.size() == 2 and not (pair[0]["rect"] as Rect2).intersects(pair[1]["rect"]),
"two labels at the same spot are nudged apart, so both can be read")
twin.queue_free()
sign.visible = false
r.check(not look.labels.entries().any(func(e: Dictionary) -> bool: return e["label"] == sign), "but not while hidden")
sign.visible = true
sign.global_position = cam.global_position + cam.global_basis.z * 12.0
sign.reset_physics_interpolation()
await r.process_frame # (the displayed position moves with the frame)
r.check(not look.labels.entries().any(func(e: Dictionary) -> bool: return e["label"] == sign), "or behind the camera")
var outline := look.outline
var outline_mat := outline.material_override as ShaderMaterial
r.check(outline.is_inside_tree() and outline_mat.shader.resource_path == "res://game/resources/pixel_outline.gdshader"
and outline.cast_shadow == GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
and outline_mat.render_priority == Material.RENDER_PRIORITY_MIN,
"the outline quad is in the world, casts no shadow, and draws under the transparent effects")
look.queue_free()
farm.queue_free()
await r.process_frame
## The nodes under root still showing a lit StandardMaterial3D, by name.
static func _lit_left(root: Node) -> Array[String]:
var out: Array[String] = []
for node in [root] + root.find_children("*", "GeometryInstance3D", true, false):
var g := node as GeometryInstance3D
if g == null:
continue
for m in _shown(g):
if m is BaseMaterial3D and ToonLook.swappable(m as BaseMaterial3D):
out.append(String(g.name))
return out
static func _toon_count(root: Node) -> int:
var n := 0
for node in root.find_children("*", "GeometryInstance3D", true, false):
for m in _shown(node as GeometryInstance3D):
var s := m as ShaderMaterial
if s != null and s.shader != null and s.shader.resource_path in [TOON, TOON_DOUBLE_SIDED]:
n += 1
return n
## The materials a node draws with.
static func _shown(g: GeometryInstance3D) -> Array[Material]:
var out: Array[Material] = []
if g.material_override != null:
out.append(g.material_override)
return out
var mesh: Mesh = null
if g is MeshInstance3D:
var mi := g as MeshInstance3D
if mi.mesh != null:
for i in mi.get_surface_override_material_count():
if mi.get_active_material(i) != null:
out.append(mi.get_active_material(i))
return out
if g is CPUParticles3D:
mesh = (g as CPUParticles3D).mesh
elif g is MultiMeshInstance3D and (g as MultiMeshInstance3D).multimesh != null:
mesh = (g as MultiMeshInstance3D).multimesh.mesh
if mesh != null:
for i in mesh.get_surface_count():
if mesh.surface_get_material(i) != null:
out.append(mesh.surface_get_material(i))
return out