// NOTE: Shader automatically converted from Godot Engine 4.4.stable.mono's StandardMaterial3D.

shader_type spatial;
render_mode blend_mix, depth_draw_never, cull_back, diffuse_burley, specular_schlick_ggx, depth_test_disabled;

#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Depth.gdshaderinc"

uniform vec4 albedo : source_color;
uniform sampler2D albedoTexture : source_color, filter_linear_mipmap, repeat_enable;
uniform sampler2D opacityTexture : source_color, filter_linear_mipmap, repeat_enable;
uniform sampler2D depthTexture: hint_depth_texture;

uniform vec3 uv1_scale;
uniform vec3 uv1_offset;

uniform bool checkVisible = false;
uniform float visibilityDistance = 1;
uniform float visibilityDistanceScale = 0.01;

void vertex() 
{
	UV = UV * uv1_scale.xy + uv1_offset.xy;
}

void fragment() 
{
  vec3 viewPosition = VERTEX;
  
  bool visible = isVisibleAt( depthTexture, viewPosition, PROJECTION_MATRIX, INV_PROJECTION_MATRIX );
  
  if ( checkVisible && ! visible )
  {
    discard;
  }
  
  vec3 depthViewPosition = getDepthViewPositionAtViewPosition( depthTexture, viewPosition, PROJECTION_MATRIX, INV_PROJECTION_MATRIX );
  float dist = length( depthViewPosition - viewPosition );
  
  float dVis = 1.0 - clamp( dist / ( visibilityDistance * visibilityDistanceScale ), 0, 1 );
  
   
	vec4 sampledAlbedo = texture( albedoTexture, UV );
  vec4 sampledOpacity = texture( opacityTexture, UV );
  
	ALBEDO = albedo.rgb * sampledAlbedo.rgb;
	ALPHA *= albedo.a * sampledAlbedo.a * sampledOpacity.r * dVis;
}
