shader_type spatial;

render_mode blend_mix, depth_draw_opaque, cull_back, diffuse_burley, specular_schlick_ggx, alpha_to_coverage_and_one;

#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 float size = 1;
uniform float sizeMin = 1;
uniform float sizeMax = 1;
uniform float randomPosition;
uniform vec4 albedo : source_color;
uniform float rotation : hint_range(0,6.28) = 0;
uniform float rotationNoiseAmount = 0;
uniform float rotationNoiseScale = 0;
uniform sampler2D texture_albedo : source_color, filter_linear_mipmap, repeat_disable;
uniform sampler2D texture_normals : hint_roughness_normal, filter_linear_mipmap, repeat_disable;
uniform float normalBlendAmount: hint_range(0,1) = 0.5;
uniform vec3 normalBlendDirection = vec3( 0, 1, 0 );
uniform float roughness: hint_range(0,1) = 1;
uniform float metalness: hint_range(0,1) = 1;
uniform float specular: hint_range(0,1) = 1;


uniform float alpha_scissor_threshold : hint_range(0.0, 1.0, 0.001);
uniform float alpha_antialiasing_edge : hint_range(0.0, 1.0, 0.01);
uniform ivec2 albedo_texture_size;

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 float yMapStart;
uniform float yMapEnd;
uniform float yMapNoiseAmount;
uniform float yMapNoiseScale;
varying float yMapValue;
uniform sampler2D texture_yMapGradient : source_color, filter_linear_mipmap, repeat_disable;
varying float uvRotation;
uniform float discardNoiseScale = 1;
uniform float discardThreshold : hint_range(0,1) = 1;
varying float discardValue;
uniform float fresnelNormalSource  : hint_range(0,1) = 0.5;
uniform vec4 fresnelColor : source_color;
uniform float fresnelSharpness : hint_range(0.001,10) = 1;
uniform float fresnelScale = 1;
varying float fresnelAmount;

varying vec3 originalNormal;
uniform float windAmount = 1;
uniform vec2 windSpeed = vec2( 0.1, 0.1 );
uniform float windNoiseScale = 1;
uniform float windStart = 0;
uniform float windEnd = 1;

void vertex()
{
  vec4 worldVertex = MODEL_MATRIX *vec4( VERTEX, 1.0 );
  float windAngle = perlinPolar( TIME * windSpeed + windNoiseScale * vec2( worldVertex.x, worldVertex.z ) );
  float windStrength = perlinPolar( TIME * windSpeed + windNoiseScale * vec2( worldVertex.x, worldVertex.z ) + vec2( 1023.9, 334.90 ) );

  vec2 windDirection = onCircle( windAngle * 6.28 ) * windStrength;
  float yWindAmount = normalizeToRange01( worldVertex.y, windStart, windEnd ) * windAmount;

  float nrx  = perlinPolar( NORMAL.yz + worldVertex.yz );
  float nry  = perlinPolar( NORMAL.xz + worldVertex.xz );
  float nrz  = perlinPolar( NORMAL.xy + worldVertex.xy );
  float px   = perlinPolar( NORMAL.yz );
  float pz   = perlinPolar( NORMAL.xy );
  float filter = perlin( vec2( px, pz ) * discardNoiseScale );
    
  float scaleMix = perlin3D( vec3( nrx, nry, nrz ) ) ;
  float randomScale = mix( sizeMin, sizeMax, scaleMix );
  discardValue = filter;

  vec3 worldOffset = billboardWorldOffset( UV, INV_VIEW_MATRIX, MODEL_MATRIX );

  vec3 originalVertex = VERTEX;

  VERTEX = VERTEX + worldOffset * size * randomScale + vec3( nrx, nry, nrz ) * randomPosition;
  VERTEX = VERTEX + yWindAmount * vec3( windDirection.x, 0, windDirection.y );

  vec3 vertexNormal = originalVertex; vertexNormal.y = 0.0; vertexNormal = normalize( vertexNormal );
  vec3 normal = mix( NORMAL, vertexNormal, fresnelNormalSource );
  vec3 worldNormal = normalize( MODEL_NORMAL_MATRIX * normal );
  vec3 camDir = normalize( CAMERA_DIRECTION_WORLD );
  fresnelAmount =  fresnel( camDir, worldNormal, fresnelSharpness ) * fresnelScale;

  originalNormal = normalize( (vec4( localToWorldDirection( NORMAL, MODEL_MATRIX ), 1.0 ) * INV_VIEW_MATRIX ).xyz);
  NORMAL = normalize( mix( NORMAL, normalBlendDirection, normalBlendAmount ) );


  float random = perlinPolar( vec2( worldVertex.x, worldVertex.z ) * yMapNoiseScale );
  uvRotation = rotation + rotationNoiseAmount * perlinPolar( vec2( worldVertex.x + worldVertex.y, worldVertex.z - worldVertex.y) * rotationNoiseScale );
  uvRotation = mod( uvRotation, 6.28);
  float y = worldVertex.y + yMapNoiseAmount * random;
  yMapValue = normalizeToRange01( y, yMapEnd, yMapStart );


  // rotatedUV = rotateAround_v2( UV, mod( rotation + random2 * rotationNoiseAmount, PI * 2.0 ), vec2( 0.5, 0.5 ) );

}


void fragment()
{
  if ( discardValue > discardThreshold )
  {
    discard;
  }


  vec2 uv = rotateAround_v2( UV, uvRotation, vec2( 0.5, 0.5 ) );
  vec4 albedo_tex = texture( texture_albedo, uv );
  vec4 normalColor = texture( texture_normals, uv );
  vec4 yGradient = texture( texture_yMapGradient, vec2( 0, yMapValue ) );

	ALBEDO = albedo.rgb * albedo_tex.rgb * yGradient.rgb + fresnelColor.rgb * fresnelAmount;

  NORMAL_MAP = normalColor.rgb;

	ALPHA *= albedo.a * albedo_tex.a;
	ALPHA_SCISSOR_THRESHOLD = alpha_scissor_threshold;
	ALPHA_ANTIALIASING_EDGE = alpha_antialiasing_edge;
	ALPHA_TEXTURE_COORDINATE = UV * vec2( albedo_texture_size );

  METALLIC = metalness;
  ROUGHNESS = roughness;
  SPECULAR = specular;

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