
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Colors.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Noise.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Transform.gdshaderinc"
#include "res://addons/rokojori_action_library/Runtime/Shading/Library/Normals.gdshaderinc"

uniform vec4 albedo : source_color;
uniform sampler2D texture_albedo : source_color, filter_linear_mipmap, repeat_enable;
uniform sampler2D texture_albedoNoise : source_color, filter_linear_mipmap, repeat_enable;
uniform float albedoNoise : hint_range(0.0, 1.0) = 0;
uniform float albedoNoiseOffset : hint_range(-1.0, 1.0) = 0.1 ;
uniform float albedoNoiseUVScale : hint_range(0.0, 5.0) = 1.0;
uniform float albedoNoiseToRoughness: hint_range(-5.0, 5.0) = 0.1;
uniform float roughness : hint_range(0.0, 1.0) = 1.0;
uniform float roughnessOffset : hint_range(-1.0, 1.0) = 0.1;

uniform float roughnessOverwriteAmount : hint_range(0.0, 1.0) = 0;
uniform float roughnessOverwriteValue : hint_range(0.0, 1.0) = 0;

uniform sampler2D texture_roughness : hint_roughness_r, filter_linear_mipmap, repeat_enable;

uniform float specular : hint_range(0.0, 1.0, 0.01);
uniform sampler2D texture_specular : hint_default_white, filter_linear_mipmap, repeat_enable;


uniform sampler2D texture_normal : hint_roughness_normal, filter_linear_mipmap, repeat_enable;
uniform float normal_scale : hint_range(0, 16.0) = 1.0;

uniform float rim : hint_range(0.0, 1.0, 0.01) = 0.5; 
uniform float rim_tint : hint_range(0.0, 1.0, 0.01) = 0.85;

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

uniform sampler2D microNormalTexture : hint_normal, filter_linear_mipmap, repeat_enable;
uniform sampler2D microNormalMaskTexture : hint_default_white, filter_linear_mipmap, repeat_enable;
uniform float microNormalScale = 1;

uniform float subsurface_scattering_strength : hint_range(0.0, 1.0, 0.01) = 1.0;
uniform sampler2D texture_subsurface_scattering : hint_default_white, filter_linear_mipmap, repeat_enable;

#ifdef USE_TRANSMISSION

uniform bool transmissionEnabled = false;
uniform vec4 transmittance_color : source_color = vec4( 1,1,1,1);
uniform float transmittance_depth : hint_range(0.001, 8.0, 0.001) = 8.0;
uniform sampler2D texture_subsurface_transmittance : hint_default_white, filter_linear_mipmap, repeat_enable;
uniform float transmittance_boost : hint_range(0.0, 1.0, 0.01) = 0.5;

#endif

uniform vec3 uv1_scale;
uniform vec3 uv1_offset;

uniform vec3 uv2_scale;
uniform vec3 uv2_offset;

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


void fragment() 
{
	vec2 base_uv = UV;
	vec2 base_uv2 = UV2;
  

	vec4 albedo_tex = texture(texture_albedo, base_uv);
  vec4 noise_tex = ( texture(texture_albedoNoise, base_uv * albedoNoiseUVScale) + albedoNoiseOffset ) * albedoNoise;
  float noiseLuma = ( noise_tex.r + noise_tex.g + noise_tex.b ) / 3.0;
	ALBEDO = albedo.rgb * albedo_tex.rgb + noise_tex.rgb;

  
  float specular_tex = 1.0 - pow( texture(texture_specular, base_uv).r, 2.2 );
	SPECULAR = specular * specular_tex;

	float roughness_tex = texture(texture_roughness, base_uv).r;
  float roughnessCombined = clamp( roughness_tex * roughness + roughnessOffset + noiseLuma * albedoNoiseToRoughness, 0, 1);
  roughnessCombined = mix( roughnessCombined, roughnessOverwriteValue, roughnessOverwriteAmount );
	ROUGHNESS = roughnessCombined;

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

	RIM = rim;
	RIM_TINT = rim_tint;

	AO = texture(texture_ambient_occlusion, base_uv).r;
	AO_LIGHT_AFFECT = ao_light_affect;

	float sss_tex = texture(texture_subsurface_scattering, base_uv).r;
	SSS_STRENGTH = subsurface_scattering_strength * sss_tex;

  #ifdef USE_TRANSMISSION
  
  float transmittance_tex = texture(texture_subsurface_transmittance, base_uv).r;
  SSS_TRANSMITTANCE_COLOR = transmittance_color;
  SSS_TRANSMITTANCE_DEPTH = transmittance_depth;
  SSS_TRANSMITTANCE_BOOST = transmittance_boost * transmittance_tex;
  
  #endif

  BACKLIGHT = albedo.rgb * albedo_tex.rgb * 0.2;

	vec4 microNormalSampled = texture( microNormalTexture, base_uv2 );
  vec4 microNormalMaskSampled = texture( microNormalMaskTexture, base_uv );
  vec3 microNormal = scaleNormalMap( microNormalSampled.rgb, microNormalScale * 1.5 );

	NORMAL_MAP = mix( NORMAL_MAP, addNormalMaps( NORMAL_MAP, microNormal ), microNormalScale );
}
