- Android back: application/config/quit_on_go_back=false; Main.go_back() (from NOTIFICATION_WM_GO_BACK_REQUEST, phones only) closes the shop or pouch panel or leaves build mode, else pauses; paused it closes the question, then settings, then resumes (shared with the pause action); at the main menu it closes the question or settings, else quits. - Phones hide the main menu's Quit and the pause menu's Quit the game. - ScreenLabels.ensure() makes the one ScreenLabels (ToonLook's on desktop; Main's on phones, which watch()es the tree for Label3Ds). Label sizes grow with the window's content_scale_factor (1 on desktop). - FollowCamera starts at PHONE_DISTANCE (12 m) on phones. - MobilePort.forced for tests; game/tests/mobile/platform_test.gd. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
142 lines
5.8 KiB
GDScript
142 lines
5.8 KiB
GDScript
class_name ToonLook
|
|
extends Node
|
|
## The game's look: toon shading and pixel outlines on everything 3D. Main adds one at start.
|
|
## * Every lit, opaque StandardMaterial3D in the scene tree is swapped for a toon ShaderMaterial
|
|
## (toon.gdshader; toon_double_sided.gdshader for double-sided ones) with the same color,
|
|
## texture, and vertex-color use: the farm's imported props, the KayKit characters, the
|
|
## weapons, and the primitives built in code. Unshaded or transparent materials (effects and
|
|
## indicators: beams, flames, rings, zone tints, ghosts) are left alone, as are ShaderMaterials
|
|
## (the crows' and UFOs' shaders include the toon lighting themselves).
|
|
## * The swap goes on the nodes (material_override, or a surface override per surface), so the
|
|
## asset files never change. Nodes added later (enemy pools, the partner's scarecrow, debris,
|
|
## the farmers' return) are swapped at the end of the frame they arrive in, after the code that
|
|
## built them has set and tinted their materials.
|
|
## * A full-screen quad (pixel_outline.gdshader) darkens depth edges and lightens creases for
|
|
## whatever camera is current. It needs the Forward+ renderer.
|
|
## * Label3Ds aren't rendered in 3D (they'd be pixelated): ScreenLabels draws them as sharp text
|
|
## over the 3D view, under the HUD. (Phones have no ToonLook; Main adds the ScreenLabels.)
|
|
## The pixel size (rendering the 3D view small and scaling it up with sharp pixels) is a display
|
|
## setting: GameSettings.pixel_size.
|
|
|
|
const TOON := preload("res://game/resources/toon.gdshader")
|
|
const TOON_DOUBLE_SIDED := preload("res://game/resources/toon_double_sided.gdshader")
|
|
const OUTLINE := preload("res://game/resources/pixel_outline.gdshader")
|
|
|
|
var outline: MeshInstance3D
|
|
var labels: ScreenLabels
|
|
## Source material -> its toon material (shared materials, like the farm pack's, convert once).
|
|
var _swapped := {}
|
|
var _pending: Array = [] # untyped: a node may be freed before the flush
|
|
|
|
|
|
func _ready() -> void:
|
|
outline = make_outline()
|
|
add_child(outline)
|
|
labels = ScreenLabels.ensure(self) # its own Labels layer, under the HUD (or the one already there)
|
|
get_tree().node_added.connect(_on_node_added)
|
|
apply_to(get_tree().root)
|
|
|
|
|
|
## Swaps the materials of root and everything under it (and hands its Label3Ds to ScreenLabels).
|
|
func apply_to(root: Node) -> void:
|
|
if root is GeometryInstance3D:
|
|
_take(root as GeometryInstance3D)
|
|
for node in root.find_children("*", "GeometryInstance3D", true, false):
|
|
_take(node as GeometryInstance3D)
|
|
|
|
|
|
## Swaps one node's materials: its material override, else each surface of its mesh (a
|
|
## MeshInstance3D), else the single surface of a particle or MultiMesh mesh (as an override).
|
|
func swap_node(g: GeometryInstance3D) -> void:
|
|
if g.material_override != null:
|
|
g.material_override = toon_material(g.material_override)
|
|
return
|
|
var mi := g as MeshInstance3D
|
|
if mi != null:
|
|
if mi.mesh == null:
|
|
return
|
|
for i in mi.get_surface_override_material_count():
|
|
var src := mi.get_active_material(i)
|
|
if src == null:
|
|
continue
|
|
var toon := toon_material(src)
|
|
if toon != src:
|
|
mi.set_surface_override_material(i, toon)
|
|
return
|
|
var mesh: Mesh = null
|
|
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 and mesh.get_surface_count() == 1 and mesh.surface_get_material(0) != null:
|
|
var src := mesh.surface_get_material(0)
|
|
var toon := toon_material(src)
|
|
if toon != src:
|
|
g.material_override = toon
|
|
|
|
|
|
## The toon material that replaces src (one per source material), or src itself when it stays.
|
|
func toon_material(src: Material) -> Material:
|
|
if _swapped.has(src):
|
|
return _swapped[src]
|
|
var m := src as BaseMaterial3D
|
|
if m == null or not swappable(m):
|
|
return src
|
|
var toon := ShaderMaterial.new()
|
|
toon.resource_name = m.resource_name
|
|
toon.shader = TOON_DOUBLE_SIDED if m.cull_mode == BaseMaterial3D.CULL_DISABLED else TOON
|
|
toon.render_priority = m.render_priority
|
|
toon.next_pass = m.next_pass
|
|
toon.set_shader_parameter("albedo", m.albedo_color)
|
|
toon.set_shader_parameter("albedo_texture", m.albedo_texture)
|
|
toon.set_shader_parameter("use_vertex_color", m.vertex_color_use_as_albedo)
|
|
toon.set_shader_parameter("uv1_scale", m.uv1_scale)
|
|
toon.set_shader_parameter("uv1_offset", m.uv1_offset)
|
|
_swapped[src] = toon
|
|
return toon
|
|
|
|
|
|
## Whether a material takes the toon look: lit, opaque, depth-tested, not a billboard.
|
|
static func swappable(m: BaseMaterial3D) -> bool:
|
|
return m.shading_mode != BaseMaterial3D.SHADING_MODE_UNSHADED \
|
|
and m.transparency == BaseMaterial3D.TRANSPARENCY_DISABLED \
|
|
and m.billboard_mode == BaseMaterial3D.BILLBOARD_DISABLED \
|
|
and not m.no_depth_test
|
|
|
|
|
|
## The outline pass: a quad that pixel_outline.gdshader pins over the whole screen.
|
|
static func make_outline() -> MeshInstance3D:
|
|
var quad := QuadMesh.new()
|
|
quad.size = Vector2(2.0, 2.0)
|
|
var mat := ShaderMaterial.new()
|
|
mat.shader = OUTLINE
|
|
mat.render_priority = Material.RENDER_PRIORITY_MIN # under the transparent effects
|
|
var mi := MeshInstance3D.new()
|
|
mi.name = "Outline"
|
|
mi.mesh = quad
|
|
mi.material_override = mat
|
|
mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
mi.gi_mode = GeometryInstance3D.GI_MODE_DISABLED
|
|
mi.extra_cull_margin = 16384.0 # the screen pins it; never let the camera's frustum cull it
|
|
return mi
|
|
|
|
|
|
func _on_node_added(node: Node) -> void:
|
|
if node is GeometryInstance3D:
|
|
if _pending.is_empty():
|
|
_flush.call_deferred()
|
|
_pending.append(node)
|
|
|
|
|
|
func _flush() -> void:
|
|
for g: Variant in _pending:
|
|
if is_instance_valid(g) and (g as Node).is_inside_tree():
|
|
_take(g as GeometryInstance3D)
|
|
_pending.clear()
|
|
|
|
|
|
func _take(g: GeometryInstance3D) -> void:
|
|
if g is Label3D:
|
|
labels.track(g as Label3D)
|
|
else:
|
|
swap_node(g)
|