# evanw/webgl-path-tracing architecture

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

- Interactive diagram: https://gitdiagram.com/evanw/webgl-path-tracing
- Repository: https://github.com/evanw/webgl-path-tracing

## Overview

##### Main Components Identification
- User Interface (index.html): This file acts as the entry point and container for the WebGL canvas and any UI elements. It essentially loads the JavaScript files and sets up the environment for rendering.
- WebGL Rendering Engine (webgl-path-tracing.js): This is the heart of the project. It implements the path tracing algorithm, handles the rendering loop, and compiles the scene into a GLSL shader dynamically.
- GL Utilities (glUtils.js): This helper file likely provides functions that encapsulate common WebGL tasks (such as shader compilation, buffer handling, etc.) that the rendering engine relies on.
- Mathematical Library (sylvester.src.js): A separate math library used for vector and matrix operations, which are crucial for 3D transformations and ray calculations in path tracing.

##### Relationships and Interactions
- index.html loads and initializes the JavaScript files. It creates the canvas element, gets the WebGL context, and starts the rendering loop.
- webgl-path-tracing.js acts as the driver by setting up the scene, invoking glUtils.js functions to compile shaders, and utilizing sylvester.src.js for complex mathematical operations.
- Data flows from user interactions (if any, such as camera or scene updates) captured via the UI in index.html to the rendering engine, which then recalculates and recompiles shaders as necessary when geometry or material changes occur.

##### Architectural Patterns and Design Principles
- Dynamic Shader Compilation: Highlight the process where scene descriptions are dynamically turned into GLSL shader code, emphasizing that changes in geometry or material trigger a recompilation.

##### Additional Details to Capture
- The shader compilation process: Create a node or sub-component showing that changes in geometry/material inputs trigger a procedure that compiles new GLSL code.
- Integration with WebGL: Make sure that the interaction between JavaScript components and the WebGL API is clearly depicted. This could include a node that represents the GPU shader pipeline.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "User Interface Layer"
        UI1["index.html (HTML UI)"]
    end

    subgraph "Rendering Engine Layer"
        Engine["webgl-path-tracing.js (WebGL Rendering Engine)"]
        ShaderCompiler["Dynamic GLSL Compiler"]
        WebGLAPI["WebGL API (GPU Interaction)"]
        subgraph "Helper Libraries"
            GLUtils["glUtils.js (GL Utility Functions)"]
            MathLib["sylvester.src.js (Math Helper Library)"]
        end
    end

    UI1 -->|"initializes"| Engine
    UI1 -->|"triggersUIEvents"| Engine
    Engine -->|"invokesUtility"| GLUtils
    Engine -->|"performsMath"| MathLib
    Engine -->|"compilesShader"| ShaderCompiler
    Engine -->|"rendersVia"| WebGLAPI
    Engine -->|"renderLoop"| Engine

    click UI1 "https://github.com/evanw/webgl-path-tracing/blob/master/index.html"
    click Engine "https://github.com/evanw/webgl-path-tracing/blob/master/webgl-path-tracing.js"
    click GLUtils "https://github.com/evanw/webgl-path-tracing/blob/master/glUtils.js"
    click MathLib "https://github.com/evanw/webgl-path-tracing/blob/master/sylvester.src.js"

    classDef ui fill:#f9c74f,stroke:#333,stroke-width:2px;
    classDef rendering fill:#90be6d,stroke:#333,stroke-width:2px;
    classDef utility fill:#f9844a,stroke:#333,stroke-width:2px;
    classDef math fill:#577590,stroke:#333,stroke-width:2px;
    classDef pipeline fill:#f94144,stroke:#333,stroke-width:2px;

    class UI1 ui;
    class Engine rendering;
    class GLUtils utility;
    class MathLib math;
    class ShaderCompiler pipeline;
    class WebGLAPI pipeline;
```
