export const FP_SHADER_CHUNK_APPLY_DECALS = `
#ifndef FP_SHADER_CHUNK_APPLY_DECALS
    #define FP_SHADER_CHUNK_APPLY_DECALS
    
    // process decals
    for(uint i=0u; i < fp_cluster_metadata.w; i++){
        uint lookup_index = fp_cluster_metadata.z + i;

        uint light_descriptor = texelFetch(fp_t_light_lookup, address_to_data_texture_coordinates(lookup_index), 0 ).r;

        uint type = (light_descriptor) & 0x3u;
        uint light_address = light_descriptor >> 2;
        
        // decal
        vec4 light_data_0 = texelFetch(fp_t_light_data, address_to_data_texture_coordinates(light_address), 0);
        vec4 light_data_1 = texelFetch(fp_t_light_data, address_to_data_texture_coordinates(light_address+1u), 0);
        vec4 light_data_2 = texelFetch(fp_t_light_data, address_to_data_texture_coordinates(light_address+2u), 0);
        vec4 light_data_3 = texelFetch(fp_t_light_data, address_to_data_texture_coordinates(light_address+3u), 0);
        
        mat4 decal_transform_matrix = mat4(
              light_data_0,
              light_data_1,
              light_data_2,
              light_data_3
        );
        
        vec4 local_position = decal_transform_matrix*vec4(v_world_position, 1.0);
        
        if(max3(abs(local_position.xyz)) < 0.5){
            
            // we're inside decal volume
            vec4 light_data_4 = texelFetch(fp_t_light_data, address_to_data_texture_coordinates(light_address+4u), 0);
            
            // compute normal of the decal, we get this by extracting rotation matrix and transforming (0,1,0) vector using that 
            vec3 decal_normal = normalize(vec3(
                decal_transform_matrix[0][2], decal_transform_matrix[1][2], decal_transform_matrix[2][2]
            )); 
            
            // Get geometry normal in world-space
            vec3 g_normal = normalize( ( transpose(viewMatrix) * vec4( normal, 0.0 ) ).xyz );
                            
            float decal_surface_dot = dot(decal_normal, g_normal);
                            
            // we fade out decals when the projection angle to the surface gets too steep
            // 0.7 is cos(45deg) and 0.5 is cos(60deg), dot returns cos of angle between two normals
            float decal_surface_angle_fade = smoothstep(-0.5,-0.7,decal_surface_dot);
            
            if(decal_surface_angle_fade <= 0.0){
                continue;
            }
            
            vec2 decal_local_uv = (local_position.xy + 0.5);
            vec2 decal_uv = decal_local_uv*light_data_4.zw + light_data_4.xy;
                            
            vec4 decal_color = texture2D(fp_t_decal_atlas,decal_uv, -0.2);
            
            vec4 decal_color_tint = texelFetch(fp_t_light_data, address_to_data_texture_coordinates(light_address+5u), 0);
            
            decal_color*= decal_color_tint;
            
            // compute decal alpha
            float decal_alpha = decal_color.a * decal_surface_angle_fade;
            
            if(decal_alpha < 0.003){
                continue;
            }
            
            material.diffuseColor = material.diffuseColor*(1.0-decal_alpha) + decal_color.xyz*decal_alpha;
        } 
                      
    }
    
#endif
`;
