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

export function makeDilationShader() {
    return new ShaderMaterial({
        uniforms: {
            tInput: {
                type: 't',
                value: null
            },
            resolution: {
                type: 'v2',
                value: new Vector2(1, 1)
            }
        },
        blending: NoBlending,
        lights: false,
        fog: false,
        depthTest: false,
        depthWrite: false,
        transparent: true,
        vertexColors: false,
        vertexShader: ScreenSpaceQuadShader.vertexShader(),
        fragmentShader: `
        
                uniform sampler2D tInput;
                uniform vec2 resolution;
                
                varying vec2 vUv;
        
                void main(){
                
                    vec4 pixel_this = texture2D( tInput, vUv );
                    
                    if( pixel_this.x < 1.0){
                        // pixel is already filled, keep the value
                        
                        gl_FragColor = pixel_this;
                        
                        return;
                    }
                
                    vec2 pixel_size = vec2(1.0) / resolution.xy;
                
                    vec4 pixel_top = texture2D( tInput, vUv + vec2(0.0, -pixel_size.y) ); 
                
                    if(pixel_top.x < 1.0){
                        
                        gl_FragColor = pixel_top;
                        
                        return;
                        
                    }
                    
                    vec4 pixel_left = texture2D( tInput, vUv + vec2(-pixel_size.x, 0) ); 
                    
                    if(pixel_left.x < 1.0){
                        
                        gl_FragColor = pixel_left;
                        
                        return;
                        
                    }
                    
                    vec4 pixel_right = texture2D( tInput, vUv + vec2(pixel_size.x, 0.0) ); 
                                        
                    if(pixel_right.x < 1.0){
                        
                        gl_FragColor = pixel_right;
                        
                        return;
                        
                    }       
                    
                    vec4 pixel_down = texture2D( tInput, vUv + vec2(0.0, pixel_size.y) ); 
                    
                    if(pixel_down.x < 1.0){
                        
                        gl_FragColor = pixel_down;
                        
                        return;
                        
                    }else{
                        
                        gl_FragColor = vec4(1.0);
                        
                    }
                    
                }
        `
    });
}
