# hashb/mujoco_web architecture

> Architecture diagram of the GitHub repository hashb/mujoco_web, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hashb/mujoco_web
- Repository: https://github.com/hashb/mujoco_web

## Overview

##### Component Details

Frontend Layer:
- Three.js visualization component
- UpdateProps and utility components (mujocoUtils, threeUtils)

Bridge Layer:
- Pthread support indication
- TypeScript type definitions interface (mujoco_wasm.d.ts)

Core Simulation Layer:
- MuJoCo physics engine core
- Model management (model.h)
- Simulation handler (simulation.h)
- State management (state.h)

##### Additional Elements

- Build system (CMake)
- Development tools (Vite, TypeScript)
- Asset pipeline
- Scene management

##### The state management system

Keep the abstraction level high enough to show the overall architecture but detailed enough to understand the key technical decisions, especially around the WebAssembly integration and physics simulation aspects.

- How the React frontend interfaces with the physics engine
- The role of WebAssembly in bridging JavaScript and C++
- The flow of simulation data
- How scenes are loaded and rendered
- The overall system architecture and its main components

Remember to include version numbers for key components (MuJoCo 3.2.5) and any critical dependencies to provide context for the implementation details.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Styles
    classDef frontend fill:#3498db,stroke:#2980b9,color:white
    classDef wasm fill:#2ecc71,stroke:#27ae60,color:white
    classDef physics fill:#e67e22,stroke:#d35400,color:white
    classDef utils fill:#9b59b6,stroke:#8e44ad,color:white
    classDef system fill:#95a5a6,stroke:#7f8c8d,color:white
    classDef docs fill:#34495e,stroke:#2c3e50,color:white

    %% Frontend Layer
    subgraph Frontend
        MujocoContainer["MuJoCo Container"]
        Mujoco["Mujoco Component"]
        UpdateProps["Update Props"]
        ThreeVis["Three.js Visualization"]
        MujocoUtils["Mujoco Utils"]
        ThreeUtils["Three Utils"]
    end

    %% Bridge Layer
    subgraph Bridge
        WASM{{"WebAssembly Module"}}
        TypeDefs["TypeScript Definitions"]
        ModelEnums["Model Enums"]
        Threading["pthread Support"]
    end

    %% Core Layer
    subgraph Core
        MujocoEngine["MuJoCo Physics Engine 3.2.5"]
        Model["Model Management"]
        Simulation["Simulation Handler"]
        State["State Management"]
    end

    %% Support Systems
    subgraph Build
        CMake["CMake Build System"]
        WasmFix["WASM Fixes"]
        Patches["MuJoCo Patches"]
    end

    subgraph Assets
        Scenes["Scene XML Files"]
        Models["3D Models"]
    end

    subgraph Documentation
        MujocoDocs["MuJoCo Docs"]
        ReactDocs["React Docs"]
    end

    %% Relationships
    MujocoContainer --> Mujoco
    Mujoco --> UpdateProps
    Mujoco --> ThreeVis
    Mujoco -.-> MujocoUtils
    ThreeVis -.-> ThreeUtils
    Mujoco --> WASM
    WASM --> TypeDefs
    WASM --> ModelEnums
    WASM <--> Threading
    WASM --> MujocoEngine
    MujocoEngine --> Model
    MujocoEngine --> Simulation
    MujocoEngine --> State
    Scenes --> Model
    Models --> ThreeVis
    CMake --> WasmFix
    Patches --> MujocoEngine

    %% Click Events
    click MujocoContainer "https://github.com/hashb/mujoco_web/blob/main/src/components/MujocoContainer.ts"
    click Mujoco "https://github.com/hashb/mujoco_web/blob/main/src/components/Mujoco.tsx"
    click UpdateProps "https://github.com/hashb/mujoco_web/blob/main/src/components/UpdateProps.ts"
    click ThreeUtils "https://github.com/hashb/mujoco_web/blob/main/src/components/threeUtils.ts"
    click TypeDefs "https://github.com/hashb/mujoco_web/blob/main/src/wasm/mujoco_wasm.d.ts"
    click ModelEnums "https://github.com/hashb/mujoco_web/blob/main/src/wasm/mujoco_model_enums.ts"
    click MujocoEngine "https://github.com/hashb/mujoco_web/blob/main/mujoco/src/main.cpp"
    click Model "https://github.com/hashb/mujoco_web/blob/main/mujoco/src/model.h"
    click Simulation "https://github.com/hashb/mujoco_web/blob/main/mujoco/src/simulation.h"
    click State "https://github.com/hashb/mujoco_web/blob/main/mujoco/src/state.h"
    click Scenes "https://github.com/hashb/mujoco_web/tree/main/public/examples/scenes/"
    click MujocoUtils "https://github.com/hashb/mujoco_web/blob/main/src/components/mujocoUtils.ts"
    click CMake "https://github.com/hashb/mujoco_web/blob/main/mujoco/CMakeLists.txt"
    click WasmFix "https://github.com/hashb/mujoco_web/blob/main/mujoco/cmake/fixWasm.cmake"
    click Patches "https://github.com/hashb/mujoco_web/tree/main/mujoco/patches/mujoco-3.2.5/"
    click MujocoDocs "https://github.com/hashb/mujoco_web/blob/main/docs/mujoco.md"
    click ReactDocs "https://github.com/hashb/mujoco_web/blob/main/docs/react.md"

    %% Legend
    subgraph Legend
        Frontend_L["Frontend Component"]:::frontend
        WASM_L["WebAssembly Component"]:::wasm
        Physics_L["Physics Component"]:::physics
        Utils_L["Utility Component"]:::utils
        System_L["System Component"]:::system
        Docs_L["Documentation"]:::docs
        Flow1["→ Direct Call"]
        Flow2["--> Event Based"]
    end

    %% Style Applications
    class MujocoContainer,Mujoco,UpdateProps,ThreeVis frontend
    class WASM,TypeDefs,ModelEnums,Threading wasm
    class MujocoEngine,Model,Simulation,State physics
    class MujocoUtils,ThreeUtils utils
    class CMake,WasmFix,Patches system
    class MujocoDocs,ReactDocs docs
```
