# ray-cast/ray-mmd architecture

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

- Interactive diagram: https://gitdiagram.com/ray-cast/ray-mmd
- Repository: https://github.com/ray-cast/ray-mmd

## Overview

##### Project’s Purpose and Components
- Ray-MMD is aimed at integrating a physically based rendering pipeline and shader effects into MikuMikuDance.
- Its purpose is to enhance rendering using realistic material properties, varied lighting, fog effects, post-process operations, and sky/atmosphere simulation.
- The key external requirements include MikuMikuDance (the host application), MikuMikuEffect (for shader effects management), and a Direct3D 9 environment with Shader Model 3.0 support.

##### Main High-Level Components
a. Rendering Core and Entry Point
– The “Main” directory holds core shader files (e.g., main.fx, variants) that kick off the rendering pipeline.
b. Shader Utility and Shared Resources
– The “Shader” directory contains shared code (e.g., BRDF, tone mapping, post-process effects) and textures used by many other modules.
c. Materials System
– The “Materials” folder is organized into several material types (ClearCoat, Cloth, Editor, Emissive, Hair, Metallic, Programmable, Skin, Subsurface, Transparent, Video) that define how surfaces interact with light.
d. Lighting Module
– The “Lighting” folder is a robust sub-system with definitions for multiple light sources (Directional, Disk, Point, Spot, Sphere, and more, including IES profiles), handling both basic illumination and shadow techniques.
e. Atmosphere and Fog Effects
– The “Fog” directory covers different fog implementations (atmospheric, ground, volumetric) to simulate scattering and distance effects.
f. Post-Processing and Outline Effects
– The “Outline” directory holds shaders specific to edge outlining, while various shader files in the “Shader” folder (and parts of “Lighting” and “Shadow”) implement post-process features such as FXAA, SMAA, bloom, and HDR.
g. Skybox and Environment
– The “Skybox” folder contains shader and texture files for various sky rendering techniques (static, time-of-day transitions, night effects).

h. Extensions
– The “Extension” folder provides plug-in effects such as ColorGrading, FilmGrain, FXAA, LightBloom, SMAA, Spectrum, and StereoImage. These extend or modify the rendering pipeline.

##### Relationships and Interactions
- The “Main” component (core renderer) drives the shader pipeline and delegates various tasks to sub-systems.
- The “Shader” and “Materials” modules are closely integrated; materials use shader routines to compute physical light interactions.
- The “Lighting” module delivers light data and shadow maps to the main shader pipeline, interfacing with both post-process modules and the materials system.
- “Fog,” “Skybox,” and “Shadow” components provide environmental effects that feed into the rendering pipeline.
- “Extensions” act as optional plug-ins that can further modify the image, applied as additional post-process or pre-render effects.

##### Architectural Patterns and Design Principles
- Modularity – The project is organized in a directory structure that mirrors functional modules (Materials, Lighting, Fog, etc.), encouraging independent development and plug-in based extensions.
- Data Flow Separation – The rendering pipeline is depicted as a sequence of processing stages: core shader entry (Main), material evaluation, lighting/shadow computation, environmental effects, and finally post-processing.
- Reusability – Shared shader functions, common effects, and texture resources are centralized in the “Shader” folder, reducing duplication.

##### Final Elements
– Clear labels for each component and sub-module.
- Core Rendering Components
- Modular Shader/Material Components
- Environmental/Post-Processing Effects
- Plug-In Extensions
- External Dependencies

Happy diagramming!

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Dependencies
    ext["External Dependencies"]:::external
    depA["MikuMikuDance"]:::external
    depB["MikuMikuEffect"]:::external
    depC["Direct3D9/ShaderModel3.0"]:::external

    %% Core Renderer / Main Shader
    coreRenderer["Core Renderer / Main Shader"]:::core

    %% Pipeline Integration Node (merging outputs)
    pipeline["Pipeline Integration"]:::core

    %% Final Render Output
    final["Final Rendered Output"]:::output

    %% Subgraph: Shader Utilities & Common Functions
    subgraph "Shader Utilities & Common Functions"
        shaderUtil["Shader Utilities"]:::shader
    end

    %% Subgraph: Materials System
    subgraph "Materials System"
        mClear["ClearCoat"]:::materials
        mCloth["Cloth"]:::materials
        mEditor["Editor"]:::materials
        mEmissive["Emissive"]:::materials
        mHair["Hair"]:::materials
        mMetallic["Metallic"]:::materials
        mProgrammable["Programmable"]:::materials
        mSkin["Skin"]:::materials
        mSubsurface["Subsurface"]:::materials
        mTransparent["Transparent"]:::materials
        mVideo["Video"]:::materials
    end

    %% Subgraph: Lighting Module
    subgraph "Lighting Module"
        lDirectional["DirectionalLight"]:::lighting
        lDisk["DiskLight"]:::lighting
        lPoint["PointLight"]:::lighting
        lPointIES["PointLightIES"]:::lighting
        lRectangle["RectangleLight"]:::lighting
        lSphere["SphereLight"]:::lighting
        lSpot["SpotLight"]:::lighting
        lSpotIES["SpotLightIES"]:::lighting
        lTube["TubeLight"]:::lighting
    end

    %% Subgraph: Fog & Atmospheric Effects
    subgraph "Fog & Atmospheric Effects"
        fAtmos["AtmosphericFog"]:::effects
        fGround["GroundFog"]:::effects
        fVolCube["VolumetricCube"]:::effects
        fVolSphere["VolumetricSphere"]:::effects
    end

    %% Subgraph: Outline & Post-Processing
    subgraph "Outline & Post-Processing"
        opOutline["Outline Effects"]:::effects
        opBloom["PostProcessBloom"]:::effects
        opHDR["PostProcessHDR"]:::effects
    end

    %% Subgraph: Skybox & Environment
    subgraph "Skybox & Environment"
        sbHelipad["Helipad GoldenHour"]:::effects
        sbHemisphere["Sky Hemisphere"]:::effects
        sbNight["Sky Night"]:::effects
        sbTimeDay["Time Of Day"]:::effects
        sbTimeNight["Time Of Night"]:::effects
    end

    %% Subgraph: Extension Pack
    subgraph "Extension Pack"
        eColor["ColorGrading"]:::extension
        eFXAA["FXAA"]:::extension
        eFilm["FilmGrain"]:::extension
        eLightBloom["LightBloom"]:::extension
        eOptical["OpticalFlares"]:::extension
        eSMAA["SMAA"]:::extension
        eSpectrum["Spectrum"]:::extension
        eStereo["StereoImage"]:::extension
    end

    %% Connections for External Dependencies
    ext --> coreRenderer
    depA --> coreRenderer
    depB --> coreRenderer
    depC --> coreRenderer

    %% Core Renderer distributes to submodules
    coreRenderer --> shaderUtil
    coreRenderer --> mClear
    coreRenderer --> lDirectional
    coreRenderer --> eColor

    %% Lighting Module representative interactions (using lDirectional as a proxy)
    lDirectional --> fAtmos
    lDirectional --> opOutline
    lDirectional --> sbHelipad

    %% Submodules feed into Pipeline Integration
    shaderUtil --> pipeline
    mClear --> pipeline
    lDirectional --> pipeline
    fAtmos --> pipeline
    opOutline --> pipeline
    sbHelipad --> pipeline
    eColor --> pipeline

    %% Pipeline Integration produces final output
    pipeline --> final

    %% Styles
    classDef external fill:#ffffcc,stroke:#000,stroke-width:2px;
    classDef core fill:#ffcccc,stroke:#000,stroke-width:2px;
    classDef shader fill:#ccffcc,stroke:#000,stroke-width:2px;
    classDef materials fill:#cce5ff,stroke:#000,stroke-width:2px;
    classDef lighting fill:#d1c4e9,stroke:#000,stroke-width:2px;
    classDef effects fill:#ffeedb,stroke:#000,stroke-width:2px;
    classDef extension fill:#ffe6ff,stroke:#000,stroke-width:2px;
    classDef output fill:#e0f7fa,stroke:#000,stroke-width:2px;

    %% Click Events for Core Renderer
    click coreRenderer "https://github.com/ray-cast/ray-mmd/blob/master/Main/main.fx"

    %% Click Events for Shader Utilities
    click shaderUtil "https://github.com/ray-cast/ray-mmd/blob/master/Shader/common.fxsub"

    %% Click Events for Materials System
    click mClear "https://github.com/ray-cast/ray-mmd/tree/master/Materials/ClearCoat"
    click mCloth "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Cloth"
    click mEditor "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Editor"
    click mEmissive "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Emissive"
    click mHair "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Hair"
    click mMetallic "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Metallic"
    click mProgrammable "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Programmable"
    click mSkin "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Skin"
    click mSubsurface "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Subsurface"
    click mTransparent "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Transparent"
    click mVideo "https://github.com/ray-cast/ray-mmd/tree/master/Materials/Video"

    %% Click Events for Lighting Module
    click lDirectional "https://github.com/ray-cast/ray-mmd/tree/master/Lighting/DirectionalLight"
    click lDisk "https://github.com/ray-cast/ray-mmd/tree/master/Lighting/DiskLight"
    click lPoint "https://github.com/ray-cast/ray-mmd/tree/master/Lighting/PointLight"
    click lPointIES "https://github.com/ray-cast/ray-mmd/tree/master/Lighting/PointLightIES"
    click lRectangle "https://github.com/ray-cast/ray-mmd/tree/master/Lighting/RectangleLight"
    click lSphere "https://github.com/ray-cast/ray-mmd/tree/master/Lighting/SphereLight"
    click lSpot "https://github.com/ray-cast/ray-mmd/tree/master/Lighting/SpotLight"
    click lSpotIES "https://github.com/ray-cast/ray-mmd/tree/master/Lighting/SpotLightIES"
    click lTube "https://github.com/ray-cast/ray-mmd/tree/master/Lighting/TubeLight"

    %% Click Events for Fog & Atmospheric Effects
    click fAtmos "https://github.com/ray-cast/ray-mmd/tree/master/Fog/AtmosphericFog"
    click fGround "https://github.com/ray-cast/ray-mmd/tree/master/Fog/GroundFog"
    click fVolCube "https://github.com/ray-cast/ray-mmd/tree/master/Fog/VolumetricCube"
    click fVolSphere "https://github.com/ray-cast/ray-mmd/tree/master/Fog/VolumetricSphere"

    %% Click Events for Outline & Post-Processing
    click opOutline "https://github.com/ray-cast/ray-mmd/tree/master/Outline"
    click opBloom "https://github.com/ray-cast/ray-mmd/blob/master/Shader/PostProcessBloom.fxsub"
    click opHDR "https://github.com/ray-cast/ray-mmd/blob/master/Shader/PostProcessHDR.fxsub"

    %% Click Events for Skybox & Environment
    click sbHelipad "https://github.com/ray-cast/ray-mmd/tree/master/Skybox/HelipadGoldenHour"
    click sbHemisphere "https://github.com/ray-cast/ray-mmd/tree/master/Skybox/SkyHemisphere"
    click sbNight "https://github.com/ray-cast/ray-mmd/tree/master/Skybox/SkyNight"
    click sbTimeDay "https://github.com/ray-cast/ray-mmd/tree/master/Skybox/TimeOfDay"
    click sbTimeNight "https://github.com/ray-cast/ray-mmd/tree/master/Skybox/TimeOfNight"

    %% Click Events for Extension Pack
    click eColor "https://github.com/ray-cast/ray-mmd/tree/master/Extension/ColorGrading"
    click eFXAA "https://github.com/ray-cast/ray-mmd/tree/master/Extension/FXAA"
    click eFilm "https://github.com/ray-cast/ray-mmd/tree/master/Extension/FilmGrain"
    click eLightBloom "https://github.com/ray-cast/ray-mmd/tree/master/Extension/LightBloom"
    click eOptical "https://github.com/ray-cast/ray-mmd/tree/master/Extension/OpticalFlares"
    click eSMAA "https://github.com/ray-cast/ray-mmd/tree/master/Extension/SMAA"
    click eSpectrum "https://github.com/ray-cast/ray-mmd/tree/master/Extension/Spectrum"
    click eStereo "https://github.com/ray-cast/ray-mmd/tree/master/Extension/StereoImage"
```
