# gkjohnson/batched-material-properties-demo architecture

> Architecture diagram of the GitHub repository gkjohnson/batched-material-properties-demo, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-31). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/gkjohnson/batched-material-properties-demo
- Repository: https://github.com/gkjohnson/batched-material-properties-demo

## Overview

##### Project type and purpose
- This is a single‐page front-end demo (no backend services) built with three.js.

##### File structure
- Top level:
- index.html – the HTML container
- index.js – demo entry point, scene setup, animation loop
- three.module.js – three.js core library
- src/ directory: three custom modules
- BatchedPropertiesTexture.js – builds a RGBA texture atlas encoding material properties
- BatchedStandardMaterial.js – extends three.js’ StandardMaterial shader to sample that texture
- RenderTarget2DArray.js – helper to allocate and manage a 2D array render target on the GPU
- No build system: assets are loaded directly by the browser.

##### Extracted insights from README
- The demo runs entirely in the browser, no server logic.
- It exercises three.js’ BatchedMesh API by providing per‐instance material data via textures.

##### Main components and their relationships
a. Browser (client)
b. Static host (GitHub Pages or any static web server)
c. Assets loaded by Browser:
- index.html → includes three.module.js & index.js
- JavaScript modules in src/
d. three.js rendering engine
e. Custom modules:
- BatchedPropertiesTexture → constructs data texture
- BatchedStandardMaterial → custom shader logic to read from that texture
- RenderTarget2DArray → manages offscreen GPU buffers if needed

##### Architectural patterns and design principles
- Modular JS: one file per concern (texture creation, material extension, render target management)
- Data‐driven design: material properties are not hard-coded but loaded from a texture

## Diagram (Mermaid)

```mermaid
graph TB
    %% External Service
    StaticHost(("Static Host")):::external

    %% Browser Layer
    subgraph "Browser"
        HTMLLoader["HTML Loader\n(index.html)"]:::runtime
        threeModule["three.module.js"]:::core
        indexJS["index.js"]:::runtime
    end

    %% Core Library
    ThreeEngine["three.js Engine"]:::core

    %% Custom Modules
    subgraph "Custom Modules"
        BPT["BatchedPropertiesTexture"]:::custom
        BSM["BatchedStandardMaterial"]:::custom
        RTA["RenderTarget2DArray"]:::custom
    end

    %% Runtime Context
    WebGL["WebGL Context / GPU"]:::runtime

    %% Connections
    StaticHost -->|"serves"| HTMLLoader
    HTMLLoader -->|"loads"| threeModule
    HTMLLoader -->|"loads"| indexJS
    threeModule -->|"provides core"| ThreeEngine
    indexJS -->|"initializes"| ThreeEngine
    indexJS -->|"calls createTexture()"| BPT
    indexJS -->|"calls new BatchedStandardMaterial()"| BSM
    indexJS -->|"calls new RenderTarget2DArray()"| RTA
    ThreeEngine -->|"initializes"| WebGL
    BPT -->|"configure shaders/buffers"| WebGL
    BSM -->|"configure shaders/buffers"| WebGL
    RTA -->|"manage offscreen buffers"| WebGL
    WebGL -->|"executes single draw call"| WebGL

    %% Click Events
    click HTMLLoader "https://github.com/gkjohnson/batched-material-properties-demo/blob/main/index.html"
    click indexJS "https://github.com/gkjohnson/batched-material-properties-demo/blob/main/index.js"
    click threeModule "https://github.com/gkjohnson/batched-material-properties-demo/blob/main/three.module.js"
    click BPT "https://github.com/gkjohnson/batched-material-properties-demo/blob/main/src/BatchedPropertiesTexture.js"
    click BSM "https://github.com/gkjohnson/batched-material-properties-demo/blob/main/src/BatchedStandardMaterial.js"
    click RTA "https://github.com/gkjohnson/batched-material-properties-demo/blob/main/src/RenderTarget2DArray.js"

    %% Styles
    classDef external fill:#f96,stroke:#333,stroke-width:2px;
    classDef runtime fill:#ccc,stroke:#333,stroke-width:1px;
    classDef core fill:#bbf,stroke:#333,stroke-width:1px;
    classDef custom fill:#9f9,stroke:#333,stroke-width:1px;
```
