W5: Vegetation and harvestable props
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>
This commit is contained in:
@@ -0,0 +1,350 @@
|
||||
## Draws the island's props: pines, scrub, boulders, wrecks and crates.
|
||||
##
|
||||
## The island is cut into CHUNK-metre squares, and each square gets one MultiMesh per
|
||||
## kind, so the GPU draws thousands of props in a few hundred instanced calls, culls
|
||||
## whole squares outside the view, and drops each kind beyond its own visibility range
|
||||
## (small things vanish first). Meshes are built here from code, low-poly, flat-shaded,
|
||||
## and coloured per vertex in the weathered palette from docs/ORIGINALITY.md. Each
|
||||
## instance gets a slight colour shift so a stand of pines isn't one flat green.
|
||||
##
|
||||
## Destruction comes from the simulation: `sync()` reads new entries in
|
||||
## PropSet.destroyed and collapses those instances to nothing. Collision never comes from
|
||||
## here; SimGround holds the props' boxes on the server and in client prediction alike.
|
||||
class_name PropView
|
||||
extends Node3D
|
||||
|
||||
## Side of the culling squares, in metres.
|
||||
const CHUNK: float = 128.0
|
||||
## Beyond this distance from the camera a kind isn't drawn, in metres. Pines and wrecks
|
||||
## carry the skyline; scrub and crates are only worth drawing close up.
|
||||
const RANGES: Dictionary = {"pine": 520.0, "scrub": 170.0, "boulder": 380.0, "wreck": 420.0, "crate": 140.0}
|
||||
## Fade band at the end of each range, in metres.
|
||||
const FADE: float = 30.0
|
||||
## How far an instance's colour may stray from white, per channel.
|
||||
const TINT: float = 0.12
|
||||
|
||||
const BARK: Color = Color(0.40, 0.30, 0.22)
|
||||
const NEEDLES: Array[Color] = [Color(0.22, 0.36, 0.28), Color(0.26, 0.40, 0.30), Color(0.31, 0.44, 0.32)]
|
||||
const SCRUB: Array[Color] = [Color(0.45, 0.48, 0.30), Color(0.53, 0.53, 0.34)]
|
||||
const STONE: Color = Color(0.56, 0.55, 0.51)
|
||||
const LICHEN: Color = Color(0.62, 0.61, 0.45)
|
||||
const TAR: Color = Color(0.31, 0.26, 0.22)
|
||||
const PLANK: Color = Color(0.64, 0.60, 0.52)
|
||||
const CRATE: Color = Color(0.68, 0.55, 0.38)
|
||||
|
||||
## Props drawn and props hidden since setup.
|
||||
var drawn: int = 0
|
||||
var hidden_count: int = 0
|
||||
## Main-thread microseconds the most recent `sync()` took.
|
||||
var last_sync_usec: int = 0
|
||||
|
||||
var _props: PropSet
|
||||
## Per prop: the instance node index and the instance index within it.
|
||||
var _node_of: PackedInt32Array = PackedInt32Array()
|
||||
var _index_of: PackedInt32Array = PackedInt32Array()
|
||||
var _nodes: Array[MultiMeshInstance3D] = []
|
||||
var _hidden: PackedByteArray = PackedByteArray()
|
||||
## How many entries of PropSet.destroyed have been applied.
|
||||
var _cursor: int = 0
|
||||
|
||||
|
||||
## Builds every instance for `props`. Call once, before or after adding to the tree.
|
||||
func setup(props: PropSet) -> void:
|
||||
_props = props
|
||||
var count: int = props.count()
|
||||
_node_of.resize(count)
|
||||
_index_of.resize(count)
|
||||
_hidden.resize(count)
|
||||
_hidden.fill(0)
|
||||
var meshes: Array[ArrayMesh] = []
|
||||
for kind: String in PropSet.KINDS:
|
||||
meshes.append(build_mesh(kind))
|
||||
# (chunk key, kind code) -> prop IDs, in prop order so the layout is deterministic.
|
||||
var groups: Dictionary = {}
|
||||
var keys: Array[Vector3i] = []
|
||||
for prop: int in count:
|
||||
var centre: Vector3 = props.boxes[prop].get_center()
|
||||
var key: Vector3i = Vector3i(floori(centre.x / CHUNK), floori(centre.z / CHUNK), props.kinds[prop])
|
||||
if not groups.has(key):
|
||||
groups[key] = PackedInt32Array()
|
||||
keys.append(key)
|
||||
var members: PackedInt32Array = groups[key]
|
||||
members.append(prop)
|
||||
groups[key] = members
|
||||
for key: Vector3i in keys:
|
||||
var members: PackedInt32Array = groups[key]
|
||||
var kind: String = PropSet.KINDS[key.z]
|
||||
var multimesh: MultiMesh = MultiMesh.new()
|
||||
multimesh.transform_format = MultiMesh.TRANSFORM_3D
|
||||
multimesh.use_colors = true
|
||||
multimesh.mesh = meshes[key.z]
|
||||
multimesh.instance_count = members.size()
|
||||
for index: int in members.size():
|
||||
var prop: int = members[index]
|
||||
multimesh.set_instance_transform(index, instance_transform(props, prop))
|
||||
multimesh.set_instance_color(index, _tint(prop))
|
||||
_node_of[prop] = _nodes.size()
|
||||
_index_of[prop] = index
|
||||
var instance: MultiMeshInstance3D = MultiMeshInstance3D.new()
|
||||
instance.name = "%s_%d_%d" % [kind, key.x, key.y]
|
||||
instance.multimesh = multimesh
|
||||
instance.visibility_range_end = RANGES[kind]
|
||||
instance.visibility_range_end_margin = FADE
|
||||
instance.visibility_range_fade_mode = GeometryInstance3D.VISIBILITY_RANGE_FADE_SELF
|
||||
# Scrub and crates are too small to cast shadows worth their cost.
|
||||
if kind == "scrub" or kind == "crate":
|
||||
instance.cast_shadow = GeometryInstance3D.SHADOW_CASTING_SETTING_OFF
|
||||
_nodes.append(instance)
|
||||
add_child(instance)
|
||||
drawn = count
|
||||
_cursor = 0
|
||||
sync()
|
||||
|
||||
|
||||
## Applies destructions the simulation recorded since the last call. Cheap: it touches
|
||||
## only the new entries. Returns how many props it hid.
|
||||
func sync() -> int:
|
||||
var started: int = Time.get_ticks_usec()
|
||||
var applied: int = 0
|
||||
while _cursor < _props.destroyed.size():
|
||||
var prop: int = _props.destroyed[_cursor]
|
||||
_cursor += 1
|
||||
if _hidden[prop] != 0:
|
||||
continue
|
||||
_hidden[prop] = 1
|
||||
_nodes[_node_of[prop]].multimesh.set_instance_transform(_index_of[prop], Transform3D(Basis.from_scale(Vector3.ZERO), Vector3.ZERO))
|
||||
drawn -= 1
|
||||
hidden_count += 1
|
||||
applied += 1
|
||||
last_sync_usec = Time.get_ticks_usec() - started
|
||||
return applied
|
||||
|
||||
|
||||
func _process(_delta: float) -> void:
|
||||
if _props != null:
|
||||
sync()
|
||||
|
||||
|
||||
func is_drawn(prop: int) -> bool:
|
||||
return _hidden[prop] == 0
|
||||
|
||||
|
||||
func node_count() -> int:
|
||||
return _nodes.size()
|
||||
|
||||
|
||||
## The MultiMeshInstance3D drawing `prop`, and its index there.
|
||||
func node_for(prop: int) -> MultiMeshInstance3D:
|
||||
return _nodes[_node_of[prop]]
|
||||
|
||||
|
||||
func instance_index(prop: int) -> int:
|
||||
return _index_of[prop]
|
||||
|
||||
|
||||
## The transform that fits a kind's unit mesh to the prop's collision box. Meshes span
|
||||
## -0.5 .. 0.5 across and 0 .. 1 up, so the visible prop matches what blocks players.
|
||||
## Wrecks lie along the long side of their box; crates turn only a little, so their
|
||||
## corners stay close to the box.
|
||||
static func instance_transform(props: PropSet, prop: int) -> Transform3D:
|
||||
var box: AABB = props.boxes[prop]
|
||||
var kind: String = props.kind_name(prop)
|
||||
var yaw: float = props.yaws[prop]
|
||||
var scale: Vector3 = box.size
|
||||
if kind == "wreck":
|
||||
var along_x: bool = box.size.x >= box.size.z
|
||||
scale = Vector3(maxf(box.size.x, box.size.z), box.size.y, minf(box.size.x, box.size.z))
|
||||
yaw = (0.0 if along_x else PI * 0.5) + (PI if yaw > PI else 0.0)
|
||||
elif kind == "crate":
|
||||
yaw = (yaw / TAU - 0.5) * deg_to_rad(16.0)
|
||||
scale = Vector3(box.size.x * 0.92, box.size.y, box.size.z * 0.92)
|
||||
var basis: Basis = Basis(Vector3.UP, yaw).scaled_local(scale)
|
||||
var base: Vector3 = Vector3(box.get_center().x, box.position.y, box.get_center().z)
|
||||
return Transform3D(basis, base)
|
||||
|
||||
|
||||
func _tint(prop: int) -> Color:
|
||||
# A hash of the ID, not a random stream, so the view never touches simulation RNG.
|
||||
var h: int = hash(prop * 2654435761)
|
||||
var shift: float = (float(h & 0xff) / 255.0 - 0.5) * 2.0 * TINT
|
||||
var warm: float = (float((h >> 8) & 0xff) / 255.0 - 0.5) * TINT
|
||||
return Color(1.0 + shift + warm, 1.0 + shift, 1.0 + shift - warm)
|
||||
|
||||
|
||||
## The unit mesh for a kind: footprint -0.5 .. 0.5, height 0 .. 1. Pines' canopies
|
||||
## spread well past their trunk footprint, since only the trunk blocks players.
|
||||
static func build_mesh(kind: String) -> ArrayMesh:
|
||||
var tool: SurfaceTool = SurfaceTool.new()
|
||||
tool.begin(Mesh.PRIMITIVE_TRIANGLES)
|
||||
match kind:
|
||||
"pine":
|
||||
_prism(tool, 6, 0.5, 0.35, 0.0, 0.42, BARK)
|
||||
_cone(tool, 9, 3.9, 0.20, 0.42, NEEDLES[0], 0.0)
|
||||
_cone(tool, 9, 3.0, 0.42, 0.36, NEEDLES[1], 0.35)
|
||||
_cone(tool, 8, 2.0, 0.63, 0.37, NEEDLES[2], 0.7)
|
||||
"scrub":
|
||||
_lump(tool, Vector3(0.0, 0.0, 0.0), Vector3(0.5, 1.0, 0.5), 7, 3, SCRUB[0], 11)
|
||||
_lump(tool, Vector3(0.18, 0.0, -0.12), Vector3(0.28, 0.7, 0.28), 6, 3, SCRUB[1], 23)
|
||||
_lump(tool, Vector3(-0.2, 0.0, 0.18), Vector3(0.28, 0.6, 0.28), 6, 3, SCRUB[1], 37)
|
||||
"boulder":
|
||||
_lump(tool, Vector3(0.0, 0.0, 0.0), Vector3(0.5, 1.0, 0.5), 8, 5, STONE, 5, 0.18, LICHEN)
|
||||
"wreck":
|
||||
_hull(tool)
|
||||
"crate":
|
||||
_crate(tool)
|
||||
tool.generate_normals()
|
||||
var material: StandardMaterial3D = StandardMaterial3D.new()
|
||||
material.vertex_color_use_as_albedo = true
|
||||
# The palette is written in sRGB, like every other colour in the project.
|
||||
material.vertex_color_is_srgb = true
|
||||
material.roughness = 1.0
|
||||
# Matte: a shiny sky highlight on needle tips reads as snow.
|
||||
material.metallic_specular = 0.15
|
||||
if kind == "wreck":
|
||||
material.cull_mode = BaseMaterial3D.CULL_DISABLED
|
||||
tool.set_material(material)
|
||||
return tool.commit()
|
||||
|
||||
|
||||
## A vertical prism of `sides`, radius `radius` at the bottom and `top_radius` at the
|
||||
## top, from y `from` to `to`. No smoothing: every face its own vertices.
|
||||
static func _prism(tool: SurfaceTool, sides: int, radius: float, top_radius: float, from: float, to: float, colour: Color) -> void:
|
||||
for side: int in sides:
|
||||
var a: float = TAU * side / sides
|
||||
var b: float = TAU * (side + 1) / sides
|
||||
var p0: Vector3 = Vector3(cos(a) * radius, from, sin(a) * radius)
|
||||
var p1: Vector3 = Vector3(cos(b) * radius, from, sin(b) * radius)
|
||||
var p2: Vector3 = Vector3(cos(b) * top_radius, to, sin(b) * top_radius)
|
||||
var p3: Vector3 = Vector3(cos(a) * top_radius, to, sin(a) * top_radius)
|
||||
_quad(tool, p0, p1, p2, p3, colour)
|
||||
|
||||
|
||||
## A cone tier for a pine: a skirt from `radius` at `base` to a point `height` above,
|
||||
## with a flat underside. `twist` turns the tier so tiers don't line up.
|
||||
static func _cone(tool: SurfaceTool, sides: int, radius: float, base: float, height: float, colour: Color, twist: float) -> void:
|
||||
var tip: Vector3 = Vector3(0.0, base + height, 0.0)
|
||||
var under: Vector3 = Vector3(0.0, base + height * 0.12, 0.0)
|
||||
for side: int in sides:
|
||||
var a: float = TAU * side / sides + twist
|
||||
var b: float = TAU * (side + 1) / sides + twist
|
||||
# Alternate rim heights give the tier a ragged, wind-cut edge.
|
||||
var drop_a: float = 0.03 if side % 2 == 0 else -0.02
|
||||
var drop_b: float = 0.03 if (side + 1) % 2 == 0 else -0.02
|
||||
if side + 1 == sides:
|
||||
drop_b = 0.03
|
||||
var p0: Vector3 = Vector3(cos(a) * radius, base - drop_a, sin(a) * radius)
|
||||
var p1: Vector3 = Vector3(cos(b) * radius, base - drop_b, sin(b) * radius)
|
||||
_tri(tool, p1, p0, tip, colour)
|
||||
_tri(tool, p0, p1, under, colour.darkened(0.35))
|
||||
|
||||
|
||||
## A lumpy dome or rock: latitude rings over a half-ellipsoid of `extent` standing on
|
||||
## y = 0 at `centre`, with radii jittered by a fixed hash of `salt`. With `speckle`,
|
||||
## upward faces take `top_colour` the way lichen does.
|
||||
static func _lump(tool: SurfaceTool, centre: Vector3, extent: Vector3, sides: int, rings: int, colour: Color, salt: int,
|
||||
jitter: float = 0.14, top_colour: Color = Color.TRANSPARENT) -> void:
|
||||
var grid: Array[PackedVector3Array] = []
|
||||
for ring: int in rings + 1:
|
||||
var row: PackedVector3Array = PackedVector3Array()
|
||||
var lat: float = PI * 0.5 * ring / rings
|
||||
for side: int in sides:
|
||||
var lon: float = TAU * side / sides
|
||||
# Never wider than 1, so the lump stays inside its footprint.
|
||||
var wobble: float = 1.0 - jitter * 2.0 * _hash01(salt, ring * 31 + side)
|
||||
if ring == rings:
|
||||
wobble = 1.0
|
||||
var radial: float = cos(lat) * wobble
|
||||
var up: float = sin(lat) * (0.85 + 0.15 * wobble)
|
||||
row.append(centre + Vector3(cos(lon) * radial * extent.x, up * extent.y, sin(lon) * radial * extent.z))
|
||||
grid.append(row)
|
||||
for ring: int in rings:
|
||||
for side: int in sides:
|
||||
var next: int = (side + 1) % sides
|
||||
var a: Vector3 = grid[ring][side]
|
||||
var b: Vector3 = grid[ring][next]
|
||||
var c: Vector3 = grid[ring + 1][next]
|
||||
var d: Vector3 = grid[ring + 1][side]
|
||||
var shade: Color = colour
|
||||
if top_colour.a > 0.0 and ring >= rings - 2 and _hash01(salt + 7, ring * 31 + side) > 0.45:
|
||||
shade = top_colour
|
||||
# Lower rings sit in shadow and grime.
|
||||
shade = shade.darkened(0.18 * (1.0 - float(ring) / rings))
|
||||
_quad(tool, a, d, c, b, shade)
|
||||
|
||||
|
||||
## A beached hull, long along x: tarred planks tapering to a stem at +x and a transom
|
||||
## at -x, open on top, with grey ribs standing out of it.
|
||||
static func _hull(tool: SurfaceTool) -> void:
|
||||
# Cross-sections along x: (x, half beam scale, depth scale).
|
||||
var stations: Array[Vector3] = [
|
||||
Vector3(-0.5, 0.78, 0.82), Vector3(-0.25, 1.0, 1.0), Vector3(0.1, 0.96, 1.0),
|
||||
Vector3(0.32, 0.7, 0.92), Vector3(0.5, 0.0, 0.8),
|
||||
]
|
||||
# Section profile from port gunwale through the keel to starboard: (z, y).
|
||||
var profile: Array[Vector2] = [
|
||||
Vector2(-0.5, 0.62), Vector2(-0.44, 0.3), Vector2(-0.24, 0.08), Vector2(0.0, 0.0),
|
||||
Vector2(0.24, 0.08), Vector2(0.44, 0.3), Vector2(0.5, 0.62),
|
||||
]
|
||||
var sections: Array[PackedVector3Array] = []
|
||||
for station: Vector3 in stations:
|
||||
var row: PackedVector3Array = PackedVector3Array()
|
||||
for point: Vector2 in profile:
|
||||
var y: float = 0.62 - (0.62 - point.y) * station.z
|
||||
row.append(Vector3(station.x, y, point.x * station.y))
|
||||
sections.append(row)
|
||||
for s: int in stations.size() - 1:
|
||||
for p: int in profile.size() - 1:
|
||||
var shade: Color = TAR if p % 2 == 0 else TAR.lightened(0.12)
|
||||
_quad(tool, sections[s][p], sections[s + 1][p], sections[s + 1][p + 1], sections[s][p + 1], shade)
|
||||
# Transom.
|
||||
var stern: PackedVector3Array = sections[0]
|
||||
for p: int in range(1, profile.size() - 1):
|
||||
_tri(tool, stern[0], stern[p + 1], stern[p], TAR.lightened(0.05))
|
||||
# Ribs: thin frames rising above the gunwale, bleached grey.
|
||||
for x: float in [-0.3, -0.08, 0.14]:
|
||||
for z: float in [-0.46, 0.46]:
|
||||
_box(tool, Vector3(x, 0.4, z * 0.98), Vector3(0.025, 0.6, 0.06), PLANK)
|
||||
# A plank thwart across the middle.
|
||||
_box(tool, Vector3(-0.15, 0.5, 0.0), Vector3(0.05, 0.03, 0.92), PLANK)
|
||||
|
||||
|
||||
## A plank crate with darker corner battens.
|
||||
static func _crate(tool: SurfaceTool) -> void:
|
||||
_box(tool, Vector3(0.0, 0.5, 0.0), Vector3(0.96, 0.96, 0.96), CRATE)
|
||||
var batten: Color = CRATE.darkened(0.45)
|
||||
for x: float in [-0.46, 0.46]:
|
||||
for z: float in [-0.46, 0.46]:
|
||||
_box(tool, Vector3(x, 0.5, z), Vector3(0.08, 1.0, 0.08), batten)
|
||||
for y: float in [0.06, 0.94]:
|
||||
_box(tool, Vector3(0.0, y, 0.0), Vector3(1.0, 0.08, 1.0), batten)
|
||||
|
||||
|
||||
static func _box(tool: SurfaceTool, centre: Vector3, size: Vector3, colour: Color) -> void:
|
||||
var h: Vector3 = size * 0.5
|
||||
var c: Array[Vector3] = []
|
||||
for i: int in 8:
|
||||
c.append(centre + Vector3(h.x if i & 1 else -h.x, h.y if i & 2 else -h.y, h.z if i & 4 else -h.z))
|
||||
_quad(tool, c[2], c[3], c[1], c[0], colour) # -z
|
||||
_quad(tool, c[7], c[6], c[4], c[5], colour) # +z
|
||||
_quad(tool, c[6], c[2], c[0], c[4], colour) # -x
|
||||
_quad(tool, c[3], c[7], c[5], c[1], colour) # +x
|
||||
_quad(tool, c[6], c[7], c[3], c[2], colour) # +y
|
||||
_quad(tool, c[0], c[1], c[5], c[4], colour) # -y
|
||||
|
||||
|
||||
## Two triangles a-b-c and a-c-d, counter-clockwise seen from outside.
|
||||
static func _quad(tool: SurfaceTool, a: Vector3, b: Vector3, c: Vector3, d: Vector3, colour: Color) -> void:
|
||||
_tri(tool, a, b, c, colour)
|
||||
_tri(tool, a, c, d, colour)
|
||||
|
||||
|
||||
static func _tri(tool: SurfaceTool, a: Vector3, b: Vector3, c: Vector3, colour: Color) -> void:
|
||||
tool.set_color(colour)
|
||||
# Godot's front faces wind clockwise, so emit in reverse.
|
||||
tool.add_vertex(a)
|
||||
tool.add_vertex(c)
|
||||
tool.add_vertex(b)
|
||||
|
||||
|
||||
static func _hash01(salt: int, index: int) -> float:
|
||||
return float(hash(salt * 7919 + index * 104729) & 0xffff) / 65535.0
|
||||
@@ -0,0 +1 @@
|
||||
uid://c4qtatighmhh7
|
||||
Reference in New Issue
Block a user