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

shader_type spatial;
render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_burley, specular_schlick_ggx;

#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Math.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Transform.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Noise.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Light.gdshaderinc"


uniform vec4 albedo : source_color;
uniform sampler2D depthTexture:hint_depth_texture;
uniform sampler2D screenTexture:hint_screen_texture;

uniform float roughness : hint_range(0.0, 1.0);
uniform float specular : hint_range(0.0, 1.0, 0.01);
uniform float metallic : hint_range(0.0, 1.0, 0.01);

uniform float normal_scale : hint_range(-16.0, 16.0);

uniform sampler2D waterNoiseOffset : source_color, filter_linear_mipmap, repeat_enable;
uniform sampler2D waterNoiseNormal : hint_roughness_normal, filter_linear_mipmap, repeat_enable;
uniform float waterNoiseScale;
uniform float waterNoiseWaveHeight;
uniform vec2 waterNoiseScroll;
varying vec2 waterUV;
uniform float sinkStart;
uniform float sinkEnd;
uniform float sinkOffset;
uniform float sinkBend: hint_range(0.0, 1.0) = 0.5;
uniform vec4 deepWater:source_color; 
varying float deepWaterAmount;
uniform vec4 centerWater:source_color; 
varying float centerWaterAmount;
uniform vec4 fromTopColor:source_color;
varying vec3 world_position;
uniform float alphaFadeDistance = 10;
uniform float albedoFadeDistance = 10;

void vertex()
{

  vec3 worldPosition = ( vec4( VERTEX, 1 ) * MODEL_MATRIX ).xyz;
  UV = worldPosition.xz * waterNoiseScale + waterNoiseScroll * TIME;
  float heightAmount = texture( waterNoiseOffset, UV ).r;
  VERTEX.y += heightAmount * waterNoiseWaveHeight;
  
  deepWaterAmount = 1.0 - heightAmount;
  
  
    
  float d = length( worldPosition );
  float m = mapClamped( d, sinkStart, sinkEnd, 0, 1 );
  centerWaterAmount = 1.0 - m;
  m = mix( m, m*m*m, sinkBend );
  float offset = mix( 0, sinkOffset, m );
  VERTEX.y += offset;
  
   world_position = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;

}

void fragment()
{
  float depth = texture(depthTexture, SCREEN_UV).x;
  vec3 ndc = vec3(SCREEN_UV * 2.0 - 1.0, depth);
  vec4 view = INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
  view.xyz /= view.w;
  float linear_depth = -view.z;

  vec4 world = INV_VIEW_MATRIX * INV_PROJECTION_MATRIX * vec4(ndc, 1.0);
  vec3 worldPosition = world.xyz / world.w;
    
  float depthDistance = length( worldPosition - world_position );
  float alphaFade = 1.0 - clamp( depthDistance / alphaFadeDistance, 0, 1 );
  float albedoFade = 1.0 - clamp( depthDistance * 5.0 / albedoFadeDistance , 0, 1 );
	vec2 base_uv = UV;
  float fresnelAmount = 1.0 - fresnel( NORMAL, VIEW, 3 );
	ALBEDO = mix( albedo.rgb, deepWater.rgb, deepWaterAmount * deepWater.a );
  ALBEDO = mix( ALBEDO, centerWater.rgb, centerWaterAmount * centerWater.a );
  //ALBEDO = mix( ALBEDO, texture( screenTexture, SCREEN_U
  vec3 screenRGB = mix( texture( screenTexture, SCREEN_UV ).rgb*2.0, vec3( 1,1,1 ), 0.5) ;
  ALBEDO = mix( ALBEDO, screenRGB, albedoFade );
  //ALBEDO = vec3( 1, 1, 1 ) * worldDistance;
  
	METALLIC = mix( metallic, 0, albedoFade );;
	SPECULAR = mix( specular, 0, albedoFade );

	ROUGHNESS = mix( roughness, 1, albedoFade );
  
  
  vec3 waterNormal = texture( waterNoiseNormal, UV ).rgb;
  
	NORMAL_MAP =  waterNormal;
	NORMAL_MAP_DEPTH = normal_scale;
  
  ALPHA = 1.0 - alphaFade;

}