Skip to content

jinda-li/UnityECSGalaxy

Repository files navigation

Unity ECS Galaxy Demo - Seamless Universe

alt text alt text

A seamless infinite universe simulation built with Unity ECS.

  • Every galaxy you see can be reached
  • No boundaries, infinite exploration
  • Average 50K+ particles in the universe

System Architecture

Built with Unity ECS for seamless performance. Entities are just IDs that hold Components (data). Systems process matching entities in parallel - they read components, do the work, and write results back.

Entities & Components

flowchart TB
    E1["GalaxyCore Entity"]
    E2["GalaxyGas Entity"]
    E3["StarParticle Entity"]
    E4["CameraTracking Entity<br/>(Singleton)"]
    E5["UniverseSettings Entity<br/>(Singleton)"]
    
    subgraph A1["GalaxyCore Archetype"]
        AC1[GalaxyCoreComponent]
        AC2[GalaxyRenderComponent]
        AC3[GalaxyLODComponent]
        AC4[LocalTransform]
    end
    
    subgraph A2["GalaxyGas Archetype"]
        AG1[GalaxyGasComponent]
        AG2[LocalTransform]
    end
    
    subgraph A3["StarParticle Archetype"]
        AP1[StarParticleComponent]
        AP2[StarParticleRenderComponent]
        AP3[StarParticleColorComponent]
        AP4[LocalTransform]
    end
    
    subgraph A4["CameraTracking Archetype"]
        AS1[CameraTrackingComponent]
    end
    
    subgraph A5["UniverseSettings Archetype"]
        AU1[UniverseSettingsComponent]
    end
    
    E1 --- A1
    E2 --- A2
    E3 --- A3
    E4 --- A4
    E5 --- A5
    
    style E1 fill:#0d1117,stroke:#58a6ff,stroke-width:2px,color:#c9d1d9
    style E2 fill:#0d1117,stroke:#58a6ff,stroke-width:2px,color:#c9d1d9
    style E3 fill:#0d1117,stroke:#58a6ff,stroke-width:2px,color:#c9d1d9
    style E4 fill:#1c2128,stroke:#3fb950,stroke-width:2px,color:#c9d1d9
    style E5 fill:#1c2128,stroke:#3fb950,stroke-width:2px,color:#c9d1d9
    style A1 fill:#161b22,stroke:#58a6ff,stroke-width:2px,color:#c9d1d9
    style A2 fill:#161b22,stroke:#58a6ff,stroke-width:2px,color:#c9d1d9
    style A3 fill:#161b22,stroke:#58a6ff,stroke-width:2px,color:#c9d1d9
    style A4 fill:#161b22,stroke:#3fb950,stroke-width:2px,color:#c9d1d9
    style A5 fill:#161b22,stroke:#3fb950,stroke-width:2px,color:#c9d1d9
Loading

Systems

flowchart TB
    subgraph Systems["⚙️ SYSTEMS"]
        direction TB
        S1[CameraTrackingSystem<br/>Synchronize camera position to CameraTrackingComponent]
        S2[GalaxySpawnSystem<br/>Dynamically spawn/despawn galaxies based on distance]
        S3[GalaxyLODSystem<br/>Calculate LOD levels based on distance to camera]
        S4[StarParticleManagementSystem<br/>Spawn/despawn star particles based on LOD]
        S5[GalaxyCoreMovementSystem<br/>Update galaxy core position using velocity]
        S6[GalaxyGasMovementSystem<br/>Update gas position following galaxy core]
        S7[StarParticleMovementSystem<br/>Update star particle orbital positions]
        S8[StarParticleRenderSystem<br/>Update render properties: twinkle, fade-in, scale]
    end
    
    S1 --> S2
    S1 --> S3
    S3 --> S4
    S5 --> S6
    S5 --> S7
    S7 --> S8
    
    style Systems fill:#161b22,stroke:#a371f7,stroke-width:2px,color:#c9d1d9
Loading

Optimization Strategies

alt text

1. LOD System

  • 5-level LOD based on distance from camera
  • Particles dynamically spawn/despawn as LOD changes

2. Frame Budget Control

  • Limits operations per frame, spreads work across multiple frames
  • Prioritizes closer galaxies for smoother transitions

3. Spatial Partitioning

  • Only galaxies within spawn radius are active
  • Uses 3D grid cells for efficient spatial management

4. Data Caching

  • Cache frequently queried data to avoid redundant lookups

5. Parallel Job Processing

  • Movement and rendering updates run as parallel jobs

6. Archetype Optimization

  • Minimize runtime component addition/removal
  • Prefer SetComponent over AddComponent

7. Entity Graphics Instancing

  • Custom ECS-compatible shader
  • Renders thousands of particles in a single draw call

About

A seamless infinite universe simulation built with Unity ECS.

Resources

Stars

Watchers

Forks

Contributors