To generate roads procedurally, you can monitor the edges of a RoadContainer. An edge is defined as a RoadPoint within a container that is not connected in both directions (leaving an opening).
Culling Logic using Buffers
Use a "near buffer" and a "far buffer" to manage road segments based on the player's distance to prevent flickering:
- Add RoadPoint: If the edge is closer to the player than the near buffer.
- Do Nothing: If the edge is between the two buffers.
- Delete RoadPoint: If the edge is further out than the far buffer.
Note: The distance between buffers must be at least as wide as the largest distance between two roads being placed to avoid flickering. Larger distances between RoadPoints require larger buffers, which can cause performance lag spikes during generation.
Adding RoadPoints
A simple method for placement is to take the current edge's position, take its z-basis direction, rotate it by a set number of degrees, and extend the new RoadPoint in that direction. For organic designs, you should implement a higher-level "plan" for sequential placement.
# Conceptual logic for adding a road point
var new_pos = current_edge_pos
var direction = current_edge.z_basis
direction = direction.rotated(Vector3.UP, rotation_amount)
new_pos += direction * extension_distance
# Create new RoadPoint at new_pos