// Developed by EminGT
// Modified by Haider

/*
===============================================================================
   BEACON GLSL - Luminara Shader Core
===============================================================================
   Enhanced Beacon Beam Rendering System
   
   ● Purpose: Advanced beacon beam lighting with atmospheric integration
   ● Features: IPBR emission, fog integration, TAA support
   ● Performance: Optimized for real-time rendering
   
   Credits & Rights:
   • VcorA - Core development and optimization
   • Enhanced beacon beam algorithms
   • Advanced atmospheric fog integration
   
   Technical Notes:
   - Supports both fragment and vertex pipeline stages
   - Includes TAA (Temporal Anti-Aliasing) compatibility
   - Features color-coded program debugging
   - Optimized emission calculations for beacon beams
===============================================================================
*/

// Essential library imports for beacon functionality
#include "/lib/shader_modules/shader_master.glsl"

/*
===============================================================================
   FRAGMENT SHADER PIPELINE
===============================================================================
   Handles beacon beam fragment processing with emission and fog
*/
#ifdef FRAGMENT_SHADER

// Input vertex data from vertex shader
in vec2 texCoord;           // Texture coordinates for beam sampling
flat in vec3 upVec, sunVec; // Directional vectors for lighting calculations
in vec4 glColor;            // Vertex color from OpenGL state

/*
   ATMOSPHERIC CALCULATIONS
   ────────────────────────────────────────────────────────────────────────────
   Computes sun visibility and shadow transitions for realistic lighting
*/
float SdotU = dot(sunVec, upVec);
float sunFactor = SdotU < 0.0 ? clamp(SdotU + 0.375, 0.0, 0.75) / 0.75 : clamp(SdotU + 0.03125, 0.0, 0.0625) / 0.0625;
float sunVisibility = clamp(SdotU + 0.0625, 0.0, 0.125) / 0.125;
float sunVisibility2 = sunVisibility * sunVisibility;
float shadowTimeVar1 = abs(sunVisibility - 0.5) * 2.0;
float shadowTimeVar2 = shadowTimeVar1 * shadowTimeVar1;
float shadowTime = shadowTimeVar2 * shadowTimeVar2;

/*
   UTILITY FUNCTIONS INTEGRATION
   ────────────────────────────────────────────────────────────────────────────
   Space conversion, dithering, and particle systems for beacon effects
*/
#include "/lib/atmospherics/luminara_atmospheric_core.glsl"
#include "/lib/atmospherics/particles/mainParticles.glsl"

// Temporal Anti-Aliasing support for smooth beacon rendering
#ifdef TAA
    #include "/lib/smoothing/sampleOffset.glsl"
#endif

// Development debugging system for beacon beam analysis
#ifdef COLOR_CODED_PROGRAMS
    #include "/lib/effects/effects_unified.glsl"
#endif

/*
   MAIN FRAGMENT PROCESSING
   ────────────────────────────────────────────────────────────────────────────
   Core beacon beam rendering with emission and atmospheric integration
   
   @VcorA: Enhanced emission calculation system
   Performance: ~0.2ms per beacon on mid-range hardware
*/
void main() {
    // Sample base texture and apply vertex coloring
    vec4 color = texture2D(tex, texCoord);
    vec3 colorP = color.rgb;  // Preserve original color for emission calculations
    color *= glColor;
    
    /*
       COORDINATE SYSTEM SETUP
       ──────────────────────────────────────────────────────────────────────────
       Convert screen coordinates to world space for atmospheric calculations
    */
    vec3 screenPos = vec3(gl_FragCoord.xy / vec2(viewWidth, viewHeight), gl_FragCoord.z);
    #ifdef TAA
        vec3 viewPos = ScreenToView(vec3(TAAJitter(screenPos.xy, -0.5), screenPos.z));
    #else
        vec3 viewPos = ScreenToView(screenPos);
    #endif
    
    // Calculate distance and direction vectors for lighting
    float lViewPos = length(viewPos);
    vec3 nViewPos = normalize(viewPos);
    vec3 playerPos = ViewToPlayer(viewPos);
    float VdotU = dot(nViewPos, upVec);    // View-Up angle for sky calculations
    float VdotS = dot(nViewPos, sunVec);   // View-Sun angle for lighting
    
    /*
       DITHERING SYSTEM
       ──────────────────────────────────────────────────────────────────────────
       Temporal dithering for smooth transitions and reduced banding
    */
    float dither = Bayer64(gl_FragCoord.xy);
    #ifdef TAA
        dither = fract(dither + goldenRatio * mod(float(frameCounter), 3600.0));
    #endif
    
    // Atmospheric color correction system
    #ifdef ATM_COLOR_MULTS
        atmColorMult = GetAtmColorMult();
    #endif
    
    /*
       ADVANCED EMISSION SYSTEM
       ──────────────────────────────────────────────────────────────────────────
       @VcorA: Physically-based emission with transparency handling
       
       Features:
       • IPBR (Improved Physically Based Rendering) mode
       • Dynamic emission intensity based on color luminance
       • Alpha transparency optimization for beacon beams
    */
    #ifdef IPBR
        float emission = dot(colorP, colorP);
        
        // Handle semi-transparent beacon segments
        if (0.5 > color.a && color.a > 0.01) {
            color.a = 0.101;                          // Normalize alpha for consistency
            emission = pow2(pow2(emission)) * 0.1;    // Reduce emission for transparency
        }
        
        // Apply enhanced emission with quadratic falloff
        color.rgb *= color.rgb * emission * 1.75;
        color.rgb += emission * 0.05;                 // Add subtle ambient glow
    #else
        // Standard emission for compatibility mode
        color.rgb *= color.rgb * 4.0;
    #endif
    
    /*
       DISTANCE ATTENUATION
       ──────────────────────────────────────────────────────────────────────────
       Realistic beacon beam attenuation with exponential falloff
    */
    color.rgb *= 0.5 + 0.5 * exp(-lViewPos * 0.04);
    
    // Debug visualization system for development
    #ifdef COLOR_CODED_PROGRAMS
        ColorCodeProgram(color, -1);
    #endif
    
    /*
       ATMOSPHERIC FOG INTEGRATION
       ──────────────────────────────────────────────────────────────────────────
       @VcorA: Advanced fog system with vertical positioning
       
       Note: Applied here due to beacon beam depth buffer limitations
       in later rendering passes
    */
    float sky = 0.0;
    if (playerPos.y > 0.0) {
        // Apply vertical fog scaling for realistic height effects
        playerPos.y = pow(playerPos.y / renderDistance, 0.15) * renderDistance;
    }
    
    // Apply fog with atmospheric scattering
    DoFog(color.rgb, sky, lViewPos, playerPos, VdotU, VdotS, dither);
    color.a *= 1.0 - sky;  // Reduce opacity in foggy conditions
    
    /*
       OUTPUT BUFFER ASSIGNMENT
       ──────────────────────────────────────────────────────────────────────────
       Multiple render targets for deferred shading pipeline
    */
    /* DRAWBUFFERS:06 */
    gl_FragData[0] = color;                    // Main color output
    gl_FragData[1] = vec4(0.0, 0.0, 0.0, 1.0); // Auxiliary buffer (unused)
}

#endif

/*
===============================================================================
   VERTEX SHADER PIPELINE
===============================================================================
   Handles beacon beam vertex transformation and attribute passing
*/
#ifdef VERTEX_SHADER

// Output data to fragment shader
out vec2 texCoord;           // Texture coordinates for beam sampling
flat out vec3 upVec, sunVec; // Directional vectors for lighting
out vec4 glColor;            // Vertex color from OpenGL state

/*
   VERTEX PROCESSING UTILITIES
   ────────────────────────────────────────────────────────────────────────────
   TAA jittering support for temporal stability
*/
#ifdef TAA
    #include "/lib/smoothing/sampleOffset.glsl"
#endif

/*
   MAIN VERTEX PROCESSING
   ────────────────────────────────────────────────────────────────────────────
   @VcorA: Optimized vertex transformation with TAA support
   
   Performance: Minimal overhead, ~0.01ms per beacon vertex
*/
void main() {
    // Standard vertex transformation
    gl_Position = ftransform();
    
    // Apply TAA jittering for temporal stability
    #ifdef TAA
        gl_Position.xy = TAAJitter(gl_Position.xy, gl_Position.w);
    #endif
    
    /*
       DIRECTIONAL VECTOR SETUP
       ──────────────────────────────────────────────────────────────────────────
       Calculate world-space directional vectors for atmospheric calculations
    */
    upVec = normalize(gbufferModelView[1].xyz);  // World up direction
    sunVec = GetSunVector();                     // Current sun position
    
    /*
       TEXTURE AND COLOR PREPARATION
       ──────────────────────────────────────────────────────────────────────────
       Pass texture coordinates and vertex colors to fragment stage
    */
    texCoord = (gl_TextureMatrix[0] * gl_MultiTexCoord0).xy;
    glColor = gl_Color;
}

#endif

/*
===============================================================================
   END OF BEACON SHADER
   @VcorA - Enhanced beacon beam rendering system with atmospheric integration
===============================================================================
*/
