# danielmartinezsebastian/r3f-offscreen-webworker architecture

> Architecture diagram of the GitHub repository danielmartinezsebastian/r3f-offscreen-webworker, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-22). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/danielmartinezsebastian/r3f-offscreen-webworker
- Repository: https://github.com/danielmartinezsebastian/r3f-offscreen-webworker

## Overview

##### Project type and purpose
- This is a modern front‐end web application built with Next.js 14 that demonstrates offscreen 3D rendering using React Three Fiber (R3F) in a WebWorker.
- Its goal is to offload complex 3D animations to a background thread so the main UI thread stays responsive.

##### File structure
- Top‐level config & build files (.eslintrc.json, next.config.mjs, tailwind.config.ts, tsconfig.json, package.json).
- public/ holds static assets (pmndrs.glb 3D model, textures folder).
- src/app/ contains the Next.js “app router” pages and global CSS.
- src/components/OffScreenCanvas.tsx implements the offscreen canvas in the browser.
- src/scenes/ contains scene definitions (Scene.tsx, AlternativeScene.tsx).
- src/workers/sceneWorker.tsx is the WebWorker entry point that instantiates Three.js offscreen.

##### README for additional insights
- Confirms use of Next.js 14, R3F, and WebWorker to render 3D scenes off the main thread.
- No backend API—this is a static/SSR front-end demo.

##### Detail without overcomplication
- Both React component tree (app → OffScreenCanvas → scenes) and Worker internals (Three.js Renderer → Scene Graph → GPU).
- Essential pieces: Next.js routing, React hydration, OffscreenCanvas transfer, Worker rendering, asset loading.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% Next.js Server
    subgraph "Next.js Server (SSR/Static Host)" 
        direction TB
        NextServer["Next.js Server"]:::external
    end

    %% Client Browser Main UI Thread
    subgraph "Client Browser (Main UI Thread)" 
        direction TB
        ReactApp["React App\n(page.tsx, layout.tsx)"]:::main
        OffScreenCanvas["OffScreenCanvas Component"]:::main
    end

    %% Worker Thread
    subgraph "WebWorker Thread (3D Rendering)" 
        direction TB
        Worker["sceneWorker.tsx\n(WebWorker)"]:::worker
        R3FRenderer["Three.js Renderer\n+ R3F"]:::worker
        subgraph "Scene Definitions" 
            direction TB
            SceneDef["Scene.tsx"]:::worker
            AltSceneDef["AlternativeScene.tsx"]:::worker
        end
        GPU["GPU / Canvas Context"]:::worker
    end

    %% Static Assets
    subgraph "Static Asset Server" 
        direction TB
        AssetsModel["pmndrs.glb"]:::external
        AssetsTextures["textures/"]:::external
    end

    %% Config & Tools
    subgraph "Project Configurations" 
        direction TB
        Tailwind["tailwind.config.ts"]:::external
        PostCSS["postcss.config.mjs"]:::external
        TSConfig["tsconfig.json"]:::external
        ESLint[".eslintrc.json"]:::external
        PackageJSON["package.json"]:::external
        PackageLock["package-lock.json"]:::external
    end

    %% Connections
    NextServer -->|"serves HTML/CSS/JS"| ReactApp
    ReactApp -->|"hydration"| OffScreenCanvas
    OffScreenCanvas -->|"postMessage(canvas)"| Worker
    Worker -->|"instantiates"| R3FRenderer
    R3FRenderer -->|"uses scenes"| SceneDef
    R3FRenderer -->|"uses scenes"| AltSceneDef
    Worker -->|"fetch model.glb/textures"| AssetsModel
    Worker -->|"fetch model.glb/textures"| AssetsTextures
    Worker -->|"renders frames"| GPU
    GPU -->|"draws into"| OffScreenCanvas

    %% Click Events
    click NextServer "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/next.config.mjs"
    click ReactApp "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/src/app/page.tsx"
    click ReactApp "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/src/app/layout.tsx"
    click ReactApp "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/src/app/globals.css"
    click OffScreenCanvas "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/src/components/OffScreenCanvas.tsx"
    click Worker "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/src/workers/sceneWorker.tsx"
    click SceneDef "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/src/scenes/Scene.tsx"
    click AltSceneDef "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/src/scenes/AlternativeScene.tsx"
    click AssetsModel "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/public/pmndrs.glb"
    click AssetsTextures "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/tree/master/public/textures/"
    click Tailwind "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/tailwind.config.ts"
    click PostCSS "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/postcss.config.mjs"
    click TSConfig "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/tsconfig.json"
    click ESLint "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/.eslintrc.json"
    click PackageJSON "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/package.json"
    click PackageLock "https://github.com/danielmartinezsebastian/r3f-offscreen-webworker/blob/master/package-lock.json"

    %% Styles
    classDef main fill:#cde,stroke:#036,stroke-width:1px
    classDef worker fill:#cfc,stroke:#090,stroke-width:1px
    classDef external fill:#fec,stroke:#fa0,stroke-width:1px
    class NextServer external
    class ReactApp main
    class OffScreenCanvas main
    class Worker worker
    class R3FRenderer worker
    class SceneDef worker
    class AltSceneDef worker
    class GPU worker
    class AssetsModel external
    class AssetsTextures external
    class Tailwind external
    class PostCSS external
    class TSConfig external
    class ESLint external
    class PackageJSON external
    class PackageLock external
```
