harvest/game/tests/test_look.gd
Forrest 4b86f655c4 #30: forced lighting, intro sounds, scarecrow chest glow
- DayNight.forced ("" / "morning" / "night") overrides target(); snap()
  still works. DayNight.morning() is the MORNING phase's look.
  lanterns_dark keeps every lantern at 0.
- synth_audio.gd: ufo_hum (4 s loop), ufo_whine, part_fall, life_flash,
  ufo_zoom, car_door, each on its own seed (existing wavs unchanged).
  FarmAudio cues for those plus door_creak (kenney creak1/2), all in
  LOCAL_CUES.
- PlayerCharacter: a blue core glow on the chest bone (BoneAttachment3D):
  an unshaded gem, an additive halo pulled toward the camera, and a weak
  pulsing OmniLight3D. On for every scarecrow; set_core_glow(on) hides
  it. set_tint skips it; the hit flash overlay and ToonLook leave it alone.
- test_look: forced/lanterns_dark, the intro cues, the glow on both
  scarecrows, hit flash intact, set_core_glow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-07 21:51:14 -04:00

255 lines
12 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.
## The opening intro's hooks into the look and sound: DayNight.forced holds the morning or the
## night look whatever the phase (and lets go again), lanterns_dark keeps the lanterns off, the
## intro's sound cues have their sounds, and every scarecrow has its blue core glow in its chest
## (on the chest bone, toon look, tint, and hit flash leaving it alone) until set_core_glow(false).
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")
_intro_hooks(r, sim, a)
look.queue_free()
farm.queue_free()
await r.process_frame
func _intro_hooks(r: GameTestRunner, sim: FarmSim, partner: Avatar) -> void:
var dn := sim.day_night
var c := sim.campaign
c.begin_day()
dn.forced = "night"
dn.snap()
r.check(dn.target() == DayNight.NIGHT and is_equal_approx(sim.sun.light_energy, float(DayNight.NIGHT["energy"]))
and is_equal_approx(dn.lantern.light_energy, float(DayNight.NIGHT["lantern"])),
"DayNight.forced = \"night\" holds the night look by day, the lantern lit")
dn.lanterns_dark = true
dn.snap()
var lit := dn.lanterns.filter(func(l: OmniLight3D) -> bool: return l.light_energy != 0.0)
r.check(dn.lanterns.size() == 2 and lit.is_empty(), "lanterns_dark keeps the lanterns off at night")
dn.lanterns_dark = false
dn.forced = "morning"
dn.snap()
r.check(dn.target() == DayNight.morning() and dn.target() != DayNight.DAY
and is_equal_approx(sim.sun.light_energy, float(DayNight.morning()["energy"])),
"forced = \"morning\" holds the morning look by day")
dn.forced = ""
dn.snap()
r.check(dn.target() == DayNight.DAY and is_equal_approx(sim.sun.light_energy, float(DayNight.DAY["energy"]))
and is_equal_approx(dn.lantern.light_energy, 0.0), "cleared, the phase's look comes back")
var audio := sim.audio
var missing: Array[String] = []
for cue in ["door_creak", "car_door", "ufo_hum", "ufo_whine", "part_fall", "life_flash", "ufo_zoom"]:
if audio.stream_of(cue) == null or not FarmAudio.LOCAL_CUES.has(cue):
missing.append(cue)
var hum := audio.stream_of("ufo_hum") as AudioStreamWAV
r.check(missing.is_empty() and hum != null and hum.loop_mode == AudioStreamWAV.LOOP_FORWARD,
"the intro's sounds resolve and stay local, and the UFO's hum loops (missing: %s)" % [missing])
for body: PlayerCharacter in [sim.player, partner.body]:
var who := "player 1's" if body == sim.player else "the partner's"
var glow := body.core_glow
var attach := glow.get_parent() as BoneAttachment3D if glow != null else null
var gem := glow.get_node("Gem") as MeshInstance3D if glow != null else null
var gem_mat := gem.get_active_material(0) as StandardMaterial3D if gem != null else null
r.check(glow != null and glow.is_visible_in_tree() and attach != null and attach.bone_name == PlayerCharacter.CORE_BONE
and gem_mat != null and gem_mat.albedo_color == PlayerCharacter.CORE_GEM_COLOR and gem.material_overlay == null,
"%s scarecrow has its core glow on the chest bone, untinted, untouched by the toon look and the hit flash" % who)
var rel := glow.global_position - body.global_position if glow != null else Vector3.ZERO
var ahead := rel.dot(-body.model.global_basis.z)
r.check(rel.y > 0.7 and rel.y < 1.2 and ahead > 0.15, "at chest height on its front (%.2f m up, %.2f m ahead)" % [rel.y, ahead])
var mannequin := sim.player.model.find_children("Mannequin_Medium_*", "MeshInstance3D", true, false)
r.check(mannequin.size() == 6 and mannequin.all(func(m: Node) -> bool: return (m as MeshInstance3D).material_overlay != null),
"the hit flash still covers the scarecrow's body (%d parts)" % mannequin.size())
var light := sim.player.core_glow.get_node("CoreLight") as OmniLight3D
sim.player.set_core_glow(false)
r.check(not sim.player.core_glow.visible and not light.is_visible_in_tree() and partner.body.core_glow.visible,
"set_core_glow(false) hides it, light and all")
sim.player.set_core_glow(true)
r.check(light.is_visible_in_tree(), "and set_core_glow(true) shows it again")
## 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