import { insert_after } from "../../../../../../core/primitives/strings/insert_after.js";
import PREAMBLE_COMMON from "../common.js";

//language=GLSL
const PREAMBLE = `
#ifndef LPV_SHADER_CHUNK_PREAMBLE
#define LPV_SHADER_CHUNK_PREAMBLE

precision highp usampler2D;
precision highp usampler3D;

varying vec3 vWorldPosition;

uniform float lpv_f_intensity;


#endif

`;

const ACCUMULATION = `
    {
    
        vec3 world_normal = normalize(inverseTransformDirection( geometry.normal, viewMatrix ));
        
        vec3 probe_irradiance = lpv_sample_irradiance( vWorldPosition, world_normal, geometry.viewDir );
        
        irradiance  += probe_irradiance * lpv_f_intensity;
        
    }


`;


/**
 *
 * @param {string} source
 */
export function build_fragment_shader(source) {

    let result = PREAMBLE + PREAMBLE_COMMON + source;


    result = insert_after(
        result,
        '#include <lights_fragment_begin>',
        ACCUMULATION
    );

    return result;
}