Godot Road Generator

repository·main·Indexed 22 days ago

https://github.com/theduckcow/godot-road-generator

A Godot plugin for creating flexible 3D highways, streets, and intersections. Features include cross-section based geometry, procedural intersections, AI traffic path generation via RoadLaneAgent, Terrain3D integration, and AStar navigation support. Compatible with Godot 4.4+ (v0.9.1), Godot 4.3+ (v0.9.0), and Godot 3.5-3.6 (v0.6.0).

Tokens
4K
Snippets
9
Records
25
Agent score
78%

What's inside godot-road-generator

  1. Understand the difference between `_src` and `_exp` resource collections

    main

    When working with custom road meshes, each prefab type contains two distinct collections of resources:

    1. _src (Source): These are the original exports directly from Godot. They represent the raw assets before processing.
    2. _exp (Export): These are the processed assets that the generator actually uses. These meshes have been flattened into a single mesh to ensure compatibility and performance during the road generation process.
  2. Install and use Godot Road Generator

    main

    To begin using the Godot Road Generator, follow the official Getting Started tutorial.

    For immediate visual reference and to see how the tools are implemented in practice, open the road_demos/demo_menu.tscn scene within the project. From there, you can navigate to various connected demo scenes. The "Museum" scene is recommended as a primary starting point for exploring the addon's capabilities.

  3. Use demo resources in your own projects

    main

    The road_demos/demo_resources/ folder contains sample assets used in the project's demo scenes. While you are free to use these resources in your own games, they are provided as examples rather than canonical implementations.

    Important: Because these assets may change as the demo scenes evolve, it is recommended to copy any resources you wish to use into your own project folder outside of the addon directory to ensure they remain stable.

  4. Delete RoadPoints and clean up resources

    main

    To remove a road segment, you should queue_free() the RoadPoint.

    Because a RoadPoint acts as the parent to the RoadSegment (which contains the physical mesh, RoadLanes, and collision mesh), removing the RoadPoint automatically cleans up these associated resources.

    Important: If you have vehicles (actors) using these lanes, you must implement a routine to free or unregister any cars across the RoadLanes that are children of the RoadPoint being removed to avoid orphaned references or errors.

  5. Explore road generator features and demos

    main

    You can view the plugin's capabilities by opening the road_demos/demo_menu.tscn scene in Godot. This menu allows you to navigate to various connected demo scenes. A good starting point for understanding the tool is the "Museum" scene.

    Key features demonstrated in the demos include:

    • Cross-section based geometry: Smoothly interpolating lane settings between RoadPoint nodes.
    • RoadContainer organization: Grouping RoadPoint nodes and snapping containers together.
    • Procedural intersections: Dynamically connecting points to create non-planar intersections.
    • AI path generation: Automatic RoadLane placement for traffic agents.
    • Terrain3D integration: Automatically flattening terrain to match road levels.
  6. Ensure smooth normals when exporting custom road meshes to gLTF

    main

    To prevent visual artifacts like split gutter edges where procedurally generated segments meet, you must ensure normals are correctly exported to the gLTF format.

    Requirement: You must use the Blender modifier for normal weights during your mesh preparation. Without this modifier, the gutter edges of the road will not line up smoothly between segments.

  7. Implement procedural road generation and culling

    main

    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