# pmndrs/react-three-next architecture

> Architecture diagram of the GitHub repository pmndrs/react-three-next, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/pmndrs/react-three-next
- Repository: https://github.com/pmndrs/react-three-next

## Overview

##### Key Components

a. Client Layer:
- Browser/PWA Container
- Static Assets (/public folder)
- Client-side Runtime

b. Next.js Application Layer:
- App Router (from app/ directory)
- Components System
- DOM Components (/src/components/dom)
- Canvas Components (/src/components/canvas)
- Layout System (app/layout.jsx)

c. 3D Rendering Layer:
- Three.js Runtime
- React Three Fiber (@react-three/fiber)
- GLSL Shaders (from /src/templates/Shader)
- Scene Management
- Performance Monitoring

##### Connection Flows

- Data flow between Next.js pages and 3D components
- Portal system using tunnel-rat for component communication
- Viewport management with gl.scissor
- Static asset loading paths

##### Technical Details

Annotations for:
- Bundle size (~79kb JS)
- PWA support
- GLSL import system
- Viewport scissoring
- Performance metrics

##### Component Relationships

Relationship between:
- View.jsx and Scene.jsx
- Layout.jsx and page components
- Canvas components and DOM components
- Shader templates and 3D rendering

- The non-unmounting canvas architecture
- The ability to embed 3D components anywhere in the DOM
- The performance optimization system
- The modular component structure

- Component types
- Data flow directions
- Integration points
- Performance optimization points

Keep the abstraction level focused on the architectural uniqueness of this project, particularly the integration between Next.js and Three.js, while maintaining clarity about the performance optimizations and component portal system.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph CL["Client Layer"]
        Browser["Browser/PWA Container"]
        StaticAssets["Static Assets"]:::static
        ClientRuntime["Client-side Runtime"]
    end

    subgraph NAL["Next.js Application Layer"]
        AppRouter["App Router"]:::nextjs
        Layout["Layout System"]:::nextjs
        
        subgraph Components["Components System"]
            direction TB
            DOMComps["DOM Components"]:::dom
            CanvasComps["Canvas Components"]:::canvas
        end
    end

    subgraph TRL["3D Rendering Layer"]
        direction TB
        ThreeRuntime["Three.js Runtime"]:::threejs
        R3F["React Three Fiber"]:::threejs
        
        subgraph Rendering["Rendering System"]
            direction TB
            SceneMgmt["Scene Management"]:::threejs
            ViewArch["View Component"]:::threejs
            Shaders["GLSL Shaders"]:::shader
        end
        
        PerfMon["Performance Monitoring"]:::perf
    end

    subgraph Special["Technical Features"]
        direction TB
        Portal["Portal System\n(tunnel-rat)"]:::tech
        Viewport["Viewport Management\n(gl.scissor)"]:::tech
        Bundle["Bundle Size\n(~79kb JS)"]:::tech
        PWA["PWA Support"]:::tech
    end

    Browser --> AppRouter
    StaticAssets --> ClientRuntime
    AppRouter --> Layout
    Layout --> Components
    DOMComps <--> CanvasComps
    CanvasComps --> R3F
    R3F --> ThreeRuntime
    ThreeRuntime --> Rendering
    Rendering --> PerfMon
    ViewArch --> Portal
    SceneMgmt --> Viewport

    %% Component Mappings
    click StaticAssets "https://github.com/pmndrs/react-three-next/tree/main/public"
    click AppRouter "https://github.com/pmndrs/react-three-next/tree/main/app"
    click DOMComps "https://github.com/pmndrs/react-three-next/tree/main/src/components/dom"
    click CanvasComps "https://github.com/pmndrs/react-three-next/tree/main/src/components/canvas"
    click Layout "https://github.com/pmndrs/react-three-next/blob/main/src/components/dom/Layout.jsx"
    click SceneMgmt "https://github.com/pmndrs/react-three-next/blob/main/src/components/canvas/Scene.jsx"
    click ViewArch "https://github.com/pmndrs/react-three-next/blob/main/src/components/canvas/View.jsx"
    click Shaders "https://github.com/pmndrs/react-three-next/tree/main/src/templates/Shader"
    click PerfMon "https://github.com/pmndrs/react-three-next/tree/main/src/helpers"

    %% Styling
    classDef nextjs fill:#1E40AF,color:white
    classDef threejs fill:#22C55E,color:white
    classDef dom fill:#7C3AED,color:white
    classDef canvas fill:#059669,color:white
    classDef shader fill:#B91C1C,color:white
    classDef static fill:#6B7280,color:white
    classDef perf fill:#C026D3,color:white
    classDef tech fill:#0369A1,color:white

    %% Legend
    subgraph Legend
        direction TB
        L1["Next.js Components"]:::nextjs
        L2["Three.js Components"]:::threejs
        L3["DOM Components"]:::dom
        L4["Canvas Components"]:::canvas
        L5["Shader System"]:::shader
        L6["Static Assets"]:::static
        L7["Performance"]:::perf
        L8["Technical Features"]:::tech
    end
```
