# tscircuit/3d-viewer architecture

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

- Interactive diagram: https://gitdiagram.com/tscircuit/3d-viewer
- Repository: https://github.com/tscircuit/3d-viewer

## Overview

##### Main System Components

a) Core Rendering Layer
- Center the `CadViewer` component as the main entry point
- Three.js integration layer
- GeomContext provider system

b) Data Processing Pipeline
- Conversion flow from React components/JSON soup to 3D models
- Geometry generation system

##### Component Organization

Create distinct sections for:

a) Input Handlers
- React Component API (`<board>`, `<resistor>`, etc.)
- Soup JSON input processor
- JSCAD model processor

b) Rendering Pipeline
- Geometry generation system
- Three.js component system
- Model loading and caching system

##### Specific Implementation Details

Key technical elements:

a) Core Components:
**CadViewer -> CadViewerContainer -> ThreeErrorBoundary -> Actual 3D Rendering**

b) Model Processing Pipeline:
**Input -> GeomContext -> Geometry Generation -> Three.js Models**

##### Key Flows to Highlight

Important data flows:

a) Component Processing:
**React Components -> Soup Conversion -> Geometry Generation -> 3D Models**

b) Asset Loading:
**External Models (STL/OBJ) -> Global Object Loader -> Three.js Scene**

##### Integration Points

Crucial integration areas:

- Story book integration for component testing
- GLTF export functionality
- Error boundary system
- Theme system integration

##### Specific Technical Details

Implementation-specific elements:

- Hooks system for model management
- Geometry context provider pattern
- Three.js component hierarchy
- Silkscreen text rendering system

##### Error handling pathways

Remember to:
- Keep the main component relationships clear
- Both synchronous and asynchronous flows
- Where error boundaries exist
- Public API entry points
- Important configuration points

This will result in a comprehensive system design that accurately represents the architecture of this 3D PCB viewer library while maintaining clarity and usefulness for development purposes.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Input
        RC[React Components]
        SJ[Soup JSON]
        JM[JSCAD Models]
    end

    subgraph Processing
        CC["Children to Soup Converter"]:::hook
        SP["Soup Processor"]:::processor
        GG["Geometry Generation"]:::processor
        GOL["Global Object Loader"]:::hook
    end

    subgraph Core
        CV["CadViewer"]:::core
        CVC["CadViewerContainer"]:::core
        TEB["ThreeErrorBoundary"]:::error
        GC["GeomContext"]:::context
    end

    subgraph "Three.js Layer"
        GM["GeomModel"]:::three
        SM["STLModel"]:::three
        JCM["JscadModel"]:::three
    end

    subgraph "Geometry System"
        BCO["Board Creation"]:::geom
        SST["Silkscreen Text"]:::geom
        PH["Plated Holes"]:::geom
    end

    subgraph Features
        GLTF["GLTF Export"]:::feature
        TH["Theme System"]:::feature
    end

    RC --> CC
    SJ --> SP
    JM --> GG
    CC --> SP
    SP --> GG
    GG --> |Geometry Data| GC
    GOL --> |3D Assets| GM

    CV --> CVC
    CVC --> TEB
    TEB --> GM
    GC --> GM
    GM --> SM
    GM --> JCM

    GG --> BCO
    GG --> SST
    GG --> PH

    GM --> GLTF
    TH --> CVC

    classDef core fill:#3498db
    classDef processor fill:#2ecc71
    classDef three fill:#e67e22
    classDef hook fill:#9b59b6
    classDef context fill:#f1c40f
    classDef error fill:#e74c3c
    classDef geom fill:#1abc9c
    classDef feature fill:#34495e

    click CV "https://github.com/tscircuit/3d-viewer/blob/main/src/CadViewer.tsx"
    click CVC "https://github.com/tscircuit/3d-viewer/blob/main/src/CadViewerContainer.tsx"
    click TEB "https://github.com/tscircuit/3d-viewer/blob/main/src/three-components/ThreeErrorBoundary.tsx"
    click GC "https://github.com/tscircuit/3d-viewer/blob/main/src/GeomContext.ts"
    click GM "https://github.com/tscircuit/3d-viewer/blob/main/src/three-components/GeomModel.tsx"
    click SM "https://github.com/tscircuit/3d-viewer/blob/main/src/three-components/STLModel.tsx"
    click JCM "https://github.com/tscircuit/3d-viewer/blob/main/src/three-components/JscadModel.tsx"
    click SP "https://github.com/tscircuit/3d-viewer/blob/main/src/soup-to-3d/index.ts"
    click GG "https://github.com/tscircuit/3d-viewer/tree/main/src/geoms/"
    click CC "https://github.com/tscircuit/3d-viewer/blob/main/src/hooks/use-convert-children-to-soup.ts"
    click GOL "https://github.com/tscircuit/3d-viewer/blob/main/src/hooks/use-global-obj-loader.ts"
    click BCO "https://github.com/tscircuit/3d-viewer/blob/main/src/geoms/create-board-with-outline.ts"
    click SST "https://github.com/tscircuit/3d-viewer/blob/main/src/geoms/create-geoms-for-silkscreen-text.ts"
    click PH "https://github.com/tscircuit/3d-viewer/blob/main/src/geoms/plated-hole.ts"
    click GLTF "https://github.com/tscircuit/3d-viewer/blob/main/src/hooks/exporter/gltf.ts"
    click TH "https://github.com/tscircuit/3d-viewer/blob/main/src/themes.ts"
```
