import { NoBlending, ShaderMaterial, Vector2 } from "three";
import { ScreenSpaceQuadShader } from "../../../shaders/ScreenSpaceQuadShader.js";

const fragmentShader = `

                uniform sampler2D tObjects;
                uniform sampler2D tParameters;
                uniform vec2 resolution;
                
                varying vec2 vUv;
                
                const float OBJECT_COUNT = 255.0;
                
                vec4 readColor( const in vec2 uv){
                    
                    float id = texture2D( tObjects, uv ).x;
                    
                        
                    if(id == 1.0){
                        //no object
                        return vec4(0.0);
                    }
                   
                    float param_v = id+ 0.5/ OBJECT_COUNT;
                        
                    //read parameters
                    vec4 color = texture2D( tParameters, vec2(0.5 , param_v));
                        
                    return color;
                }
                
                void main(){
                    gl_FragColor = readColor(  vUv );
                }
`;

export function makeHighlightDecodeShader() {
    const uniforms = {
        tObjects: {
            type: 't',
            value: null
        },
        resolution: {
            type: 'v2',
            value: new Vector2(1, 1)
        },
        tParameters: {
            type: 't',
            value: null
        }
    };

    const material = new ShaderMaterial({
        uniforms,
        vertexShader: ScreenSpaceQuadShader.vertexShader(),
        fragmentShader: fragmentShader,
        blending: NoBlending,
        lights: false,
        fog: false,
        depthTest: false,
        depthWrite: false,
        transparent: true,
        vertexColors: false
    });

    return material;
}
