shader_type spatial;

render_mode blend_add, specular_disabled;

uniform sampler2D particle_texture;
uniform vec4 particle_color : source_color;
uniform sampler2D DEPTH_TEXTURE: hint_depth_texture;

void vertex() {
    mat4 mat_world = mat4(normalize(INV_VIEW_MATRIX[0]), normalize(INV_VIEW_MATRIX[1]) ,normalize(INV_VIEW_MATRIX[2]), MODEL_MATRIX[3]);
	mat_world = mat_world * mat4(vec4(cos(INSTANCE_CUSTOM.x), -sin(INSTANCE_CUSTOM.x), 0.0, 0.0), vec4(sin(INSTANCE_CUSTOM.x), cos(INSTANCE_CUSTOM.x), 0.0, 0.0), vec4(0.0, 0.0, 1.0, 0.0), vec4(0.0, 0.0, 0.0, 1.0));
	MODELVIEW_MATRIX = VIEW_MATRIX * mat_world;
	MODELVIEW_NORMAL_MATRIX = mat3(MODELVIEW_MATRIX);
    UV += vec2(float(INSTANCE_ID%2), float(INSTANCE_ID/2%2));
}

void fragment() {
    float depth = texture(DEPTH_TEXTURE, SCREEN_UV).r;
    depth = PROJECTION_MATRIX[3][2] / (depth + PROJECTION_MATRIX[2][2]);
    depth += VERTEX.z;
    depth = max(0.0,min(1.0, (depth*10.0 - 0.1)));
    ALBEDO = texture(particle_texture,UV/2.0).xyz * COLOR.xyz * depth;
    EMISSION = ALBEDO/8.0;
    BACKLIGHT = ALBEDO;
    SPECULAR = 0.0;
}

