// NOTE: Shader automatically converted from Godot Engine 4.3.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/Colors.gdshaderinc"

uniform vec4 albedo : source_color;
uniform vec4 hslVariation;
uniform float hslVariationUVScale;
uniform vec2 hslVariationUVOffset;
uniform sampler2D texture_albedo : source_color, filter_linear_mipmap, repeat_enable;
varying vec3 albedoColor;
uniform float albedoToBacklight: hint_range(0.0, 1.0);
uniform vec3 backlight: source_color;

uniform float roughness : hint_range(0.0, 1.0);
uniform sampler2D texture_metallic : hint_default_white, filter_linear_mipmap, repeat_enable;
uniform vec4 metallic_texture_channel;
uniform sampler2D texture_roughness : hint_roughness_r, filter_linear_mipmap, repeat_enable;

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

uniform sampler2D texture_normal : hint_roughness_normal, filter_linear_mipmap, repeat_enable;
uniform float normal_scale : hint_range(-16.0, 16.0);

uniform sampler2D texture_ambient_occlusion : hint_default_white, filter_linear_mipmap, repeat_enable;
uniform vec4 ao_texture_channel;
uniform float ao_light_affect : hint_range(0.0, 1.0, 0.01);

uniform vec3 uv1_scale;
uniform vec3 uv1_offset;

uniform bool windEnabled = false;
uniform float windStrength = 0;
uniform vec2 windSpeed = vec2(1,1);
uniform float windScale = 1;
uniform sampler2D windNoise;
uniform vec2 windNoiseAngleOffset;
uniform vec2 windNoiseStrengthOffset;
uniform float windStart = 0;
uniform float windEnd = 1;
uniform float windWeightCurve:hint_range(0,1) = 0.5f;
uniform float windHeightCompensation :hint_range(0,1) = 0.5f;

uniform bool obstaclesEnabeld = false;
uniform vec4 obstacle1 = vec4( 0, 0, 0, 0 );
uniform vec4 obstacle2 = vec4( 0, 0, 0, 0 );
uniform vec4 obstacle3 = vec4( 0, 0, 0, 0 );
uniform vec4 obstacle4 = vec4( 0, 0, 0, 0 );
uniform float obstacleDeformationPower = 1.0;
uniform float obstacleDeformation = 1.0;
uniform float obstacleScale = 1.0;
uniform float maxDeformation = 0.3;
uniform float maxYOffset = 0.1;
uniform float obstacleYScale = 1.0;

vec3 deform( vec3 worldPosition, vec4 obstacle )
{
  vec3 direction = ( worldPosition - obstacle.xyz ) * vec3( 1.0, obstacleYScale, 1.0 );

  float d = length( direction );
  float size = obstacle.w * obstacleScale;

  if ( d < size && d >= 0.0 )
  {
    float amount = max( 0, ( size - d ) );
    amount = pow( amount, obstacleDeformationPower ) * obstacleDeformation;
    return worldPosition + normalize( direction ) * amount;
  }

  return worldPosition;
}

vec3 limitDeform( vec3 originalWorldPosition, vec3 deformedWorldPosition )
{
  vec3 direction =originalWorldPosition - deformedWorldPosition;

  float d = length( direction );

  vec3 outputPosition = deformedWorldPosition;

  if ( d > maxDeformation )
  {
    outputPosition = originalWorldPosition + -normalize( direction ) * maxDeformation;
  }

  outputPosition.y = clamp( outputPosition.y, originalWorldPosition.y - maxYOffset, originalWorldPosition.y + maxYOffset );

  return outputPosition;
}

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

  vec3 worldVertex = localToWorld( VERTEX, MODEL_MATRIX ).xyz;
  vec2 worldUV = worldVertex.xz * hslVariationUVScale;

  float hslAmountValue = ( 2.0 * texture( windNoise, mod( worldUV, vec2( 1,1 ) ) ).r - 1.0 );

  vec3 albedoHSL = RGBtoHSL( albedo.rgb ) + hslAmountValue * hslVariation.xyz;
  //albedoHSL = RGBtoHSL( albedo.rgb );
  //albedoHSL.y *= 1.0;
  albedoHSL.x = mod( albedoHSL.x, 1 );
  albedoHSL = clamp( albedoHSL, vec3( 0,0,0 ), vec3( 1,1,1 ) );
  albedoColor = mix( albedo.rgb, HSLtoRGB( albedoHSL ), hslVariation.w );


  if ( obstaclesEnabeld )
  {
    vec3 originalWorldVertex = worldVertex;

    worldVertex = deform( worldVertex, obstacle1 );
    worldVertex = deform( worldVertex, obstacle2 );
    worldVertex = deform( worldVertex, obstacle3 );
    worldVertex = deform( worldVertex, obstacle4 );

    worldVertex = limitDeform( originalWorldVertex, worldVertex );
  }



  if ( windEnabled )
  {

    float windAmount = normalizeToRange01( VERTEX.y, windStart, windEnd );
    float rawWindAmount = windAmount;
    windAmount = mix( windAmount, windAmount * windAmount, windWeightCurve );
    vec2 windUV = TIME * windSpeed + worldVertex.xz * windScale;
    float angle = texture( windNoise, windUV + windNoiseAngleOffset).r * PI * 2.0;
    float strength = texture( windNoise, windUV + windNoiseStrengthOffset ).r * windStrength;
    vec2 circle = onCircle( angle ) * strength;



    worldVertex += vec3( circle.x, 0, circle.y ) * windAmount;
    // VERTEX = worldToLocal( worldVertex + vec3( circle.x, 0, circle.y ) * windAmount, MODEL_MATRIX );


    VERTEX = worldToLocal( worldVertex, MODEL_MATRIX );
    float minY = min( VERTEX.y, 0 );    // VERTEX.y = mix( VERTEX.y, max( 0, VERTEX.y - strength * windAmount), windHeightCompensation * 2.0f );
    VERTEX.y = mix( VERTEX.y, max( minY, VERTEX.y - strength * windAmount), windHeightCompensation * 4.0f );

  }

}

void fragment()
{
	vec2 base_uv = UV;

	vec4 albedo_tex = texture(texture_albedo, base_uv);
	ALBEDO = albedoColor * albedo_tex.rgb;
  BACKLIGHT = ALBEDO * albedoToBacklight + backlight;


	float metallic_tex = dot(texture(texture_metallic, base_uv), metallic_texture_channel);
	METALLIC = metallic_tex * metallic;
	SPECULAR = specular;

	vec4 roughness_texture_channel = vec4(1.0, 0.0, 0.0, 0.0);
	float roughness_tex = dot(texture(texture_roughness, base_uv), roughness_texture_channel);
	ROUGHNESS = roughness_tex * roughness;

	NORMAL_MAP = texture(texture_normal, base_uv).rgb;
	NORMAL_MAP_DEPTH = normal_scale;


	AO = dot(texture(texture_ambient_occlusion, base_uv), ao_texture_channel);
	AO_LIGHT_AFFECT = ao_light_affect;
}
