143 lines
4.9 KiB
GDScript
143 lines
4.9 KiB
GDScript
extends SceneTree
|
|
## Renders a top-down orthographic picture of the farm (needs a window, not --headless):
|
|
## godot --path . --script res://game/tools/render_overview.gd -- --out=/tmp/farm.png
|
|
## Options: --scene=res://... (default the farm), --center=x,z (default 0,0), --size=meters
|
|
## (default 70), --res=pixels (default 1400), --grid=meters (0 = off, default 10),
|
|
## --zones (draw the Zones/ areas and markers on top), --pitch=deg / --yaw=deg (angled
|
|
## orthographic view instead of straight down; e.g. --pitch=50 --yaw=45 is the game camera).
|
|
|
|
var _out := "/tmp/farm_overview.png"
|
|
var _scene := "res://game/scenes/farm.tscn"
|
|
var _center := Vector2.ZERO
|
|
var _size := 70.0
|
|
var _res := 1400
|
|
var _grid := 10.0
|
|
var _zones := false
|
|
var _pitch := 90.0
|
|
var _yaw := 0.0
|
|
|
|
|
|
func _initialize() -> void:
|
|
for a in OS.get_cmdline_user_args():
|
|
var arg := String(a)
|
|
if arg.begins_with("--out="):
|
|
_out = arg.trim_prefix("--out=")
|
|
elif arg.begins_with("--scene="):
|
|
_scene = arg.trim_prefix("--scene=")
|
|
elif arg.begins_with("--center="):
|
|
var p := arg.trim_prefix("--center=").split(",")
|
|
_center = Vector2(float(p[0]), float(p[1]))
|
|
elif arg.begins_with("--size="):
|
|
_size = float(arg.trim_prefix("--size="))
|
|
elif arg.begins_with("--res="):
|
|
_res = int(arg.trim_prefix("--res="))
|
|
elif arg.begins_with("--grid="):
|
|
_grid = float(arg.trim_prefix("--grid="))
|
|
elif arg == "--zones":
|
|
_zones = true
|
|
elif arg.begins_with("--pitch="):
|
|
_pitch = float(arg.trim_prefix("--pitch="))
|
|
elif arg.begins_with("--yaw="):
|
|
_yaw = float(arg.trim_prefix("--yaw="))
|
|
_render.call_deferred()
|
|
|
|
|
|
func _render() -> void:
|
|
root.size = Vector2i(_res, _res)
|
|
var farm := (load(_scene) as PackedScene).instantiate()
|
|
root.add_child(farm)
|
|
for node in farm.find_children("*", "CanvasLayer", true, false):
|
|
(node as CanvasLayer).visible = false
|
|
var cam := Camera3D.new()
|
|
cam.projection = Camera3D.PROJECTION_ORTHOGONAL
|
|
cam.size = _size
|
|
cam.far = 400.0
|
|
root.add_child(cam)
|
|
var basis := Basis.from_euler(Vector3(deg_to_rad(-_pitch), deg_to_rad(_yaw), 0.0))
|
|
cam.global_transform = Transform3D(basis, Vector3(_center.x, 0.0, _center.y) + basis.z * 150.0)
|
|
cam.make_current()
|
|
if _grid > 0.0:
|
|
_draw_grid()
|
|
if _zones:
|
|
_draw_zones(farm)
|
|
for i in 8:
|
|
await process_frame
|
|
await RenderingServer.frame_post_draw
|
|
var err := root.get_texture().get_image().save_png(_out)
|
|
print("overview %s: %s" % [_out, error_string(err)])
|
|
quit()
|
|
|
|
|
|
func _draw_grid() -> void:
|
|
var mesh := ImmediateMesh.new()
|
|
mesh.surface_begin(Mesh.PRIMITIVE_LINES)
|
|
var half := _size * 0.5
|
|
var lo := Vector2(_center.x - half, _center.y - half)
|
|
var hi := Vector2(_center.x + half, _center.y + half)
|
|
var x := ceilf(lo.x / _grid) * _grid
|
|
while x <= hi.x:
|
|
mesh.surface_add_vertex(Vector3(x, 8.0, lo.y))
|
|
mesh.surface_add_vertex(Vector3(x, 8.0, hi.y))
|
|
_label(Vector3(x + 0.4, 8.1, lo.y + 1.2), "x%d" % int(x))
|
|
x += _grid
|
|
var z := ceilf(lo.y / _grid) * _grid
|
|
while z <= hi.y:
|
|
mesh.surface_add_vertex(Vector3(lo.x, 8.0, z))
|
|
mesh.surface_add_vertex(Vector3(hi.x, 8.0, z))
|
|
_label(Vector3(lo.x + 0.6, 8.1, z - 0.4), "z%d" % int(z))
|
|
z += _grid
|
|
mesh.surface_end()
|
|
_add_lines(mesh, Color(1, 1, 1, 0.6))
|
|
|
|
|
|
func _draw_zones(farm: Node) -> void:
|
|
var zones := farm.get_node_or_null("Zones")
|
|
if zones == null:
|
|
return
|
|
for node in zones.find_children("*", "Node3D", true, false):
|
|
var n := node as Node3D
|
|
if n is FarmZone:
|
|
var zone := n as FarmZone
|
|
var rect := zone.rect()
|
|
var mesh := ImmediateMesh.new()
|
|
mesh.surface_begin(Mesh.PRIMITIVE_LINES)
|
|
var c := [Vector2(rect.position.x, rect.position.y), Vector2(rect.end.x, rect.position.y),
|
|
Vector2(rect.end.x, rect.end.y), Vector2(rect.position.x, rect.end.y)]
|
|
for i in 4:
|
|
var a: Vector2 = c[i]
|
|
var b: Vector2 = c[(i + 1) % 4]
|
|
mesh.surface_add_vertex(Vector3(a.x, 9.0, a.y))
|
|
mesh.surface_add_vertex(Vector3(b.x, 9.0, b.y))
|
|
mesh.surface_end()
|
|
_add_lines(mesh, zone.tint)
|
|
_label(Vector3(rect.position.x + 0.5, 9.1, rect.position.y + 1.5), zone.display_name, zone.tint)
|
|
elif n is Marker3D:
|
|
_label(n.global_position + Vector3(0, 9.0, 0), "◆ " + n.name, Color(1, 0.3, 0.2))
|
|
|
|
|
|
func _add_lines(mesh: ImmediateMesh, color: Color) -> void:
|
|
var mat := StandardMaterial3D.new()
|
|
mat.shading_mode = BaseMaterial3D.SHADING_MODE_UNSHADED
|
|
mat.albedo_color = color
|
|
mat.transparency = BaseMaterial3D.TRANSPARENCY_ALPHA
|
|
var mi := MeshInstance3D.new()
|
|
mi.mesh = mesh
|
|
mi.material_override = mat
|
|
mi.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
|
root.add_child(mi)
|
|
|
|
|
|
func _label(pos: Vector3, text: String, color: Color = Color.WHITE) -> void:
|
|
var l := Label3D.new()
|
|
l.text = text
|
|
l.modulate = color
|
|
l.outline_modulate = Color.BLACK
|
|
l.outline_size = 8
|
|
l.font_size = 48
|
|
l.pixel_size = _size / float(_res) * 0.5
|
|
l.horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT
|
|
l.rotation = Vector3(-PI / 2.0, 0.0, 0.0)
|
|
l.no_depth_test = true
|
|
l.shaded = false
|
|
root.add_child(l)
|
|
l.global_position = pos
|