[gd_resource type="ShaderMaterial" load_steps=7 format=2]

[ext_resource path="res://tex/ground/rock/displacement.jpg" type="Texture" id=1]
[ext_resource path="res://tex/ground/rock/ao.jpg" type="Texture" id=2]
[ext_resource path="res://tex/ground/rock/roughness.jpg" type="Texture" id=4]
[ext_resource path="res://tex/ground/rock/normal.jpg" type="Texture" id=5]
[ext_resource path="res://tex/ground/rock/albedo.jpg" type="Texture" id=6]

[sub_resource type="Shader" id=1]
code = "// NOTE: Shader automatically converted from Godot Engine 3.4.4.stable's SpatialMaterial.

shader_type spatial;
render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlick_ggx;
uniform vec4 albedo : hint_color;
uniform sampler2D texture_albedo_big;
uniform sampler2D texture_albedo : hint_albedo;
uniform float specular;
uniform float roughness : hint_range(0,1);
uniform float point_size : hint_range(0,128);
uniform sampler2D texture_roughness : hint_white;
uniform vec4 roughness_texture_channel;
uniform sampler2D texture_normal : hint_normal;
uniform float normal_scale : hint_range(-16,16);
uniform sampler2D texture_ambient_occlusion : hint_white;
uniform vec4 ao_texture_channel;
uniform float ao_light_affect;
uniform sampler2D texture_depth : hint_black;
uniform float depth_scale;
uniform int depth_min_layers;
uniform int depth_max_layers;
uniform vec2 depth_flip;
uniform vec3 uv1_scale;
uniform vec3 uv1_offset;
uniform vec2 uv2_scale;
uniform vec3 uv2_offset;

vec4 soft_light(vec4 base, vec4 blend){
	vec4 limit = step(0.5, blend);
	return mix(2.0 * base * blend + base * base * (1.0 - 2.0 * blend), sqrt(base) * (2.0 * blend - 1.0) + (2.0 * base) * (1.0 - blend), limit);
}

vec4 overlay(vec4 base, vec4 blend){
	vec4 limit = step(0.5, base);
	return mix(2.0 * base * blend, 1.0 - 2.0 * (1.0 - base) * (1.0 - blend), limit);
}

vec4 color_burn(vec4 base, vec4 blend){
	return 1.0 - (1.0 - base) / blend;
}

vec4 screen(vec4 base, vec4 blend){
	return 1.0 - (1.0 - base) * (1.0 - blend);
}

void vertex() {
	UV=UV*uv1_scale.xy+uv1_offset.xy;
	float depth = texture(texture_depth, UV).a;
	VERTEX.y += depth * depth_scale;
}

void fragment() {
	vec2 base_uv = UV;
	/*
	{
		vec3 view_dir = normalize(normalize(-VERTEX)*mat3(TANGENT*depth_flip.x,-BINORMAL*depth_flip.y,NORMAL));
		float num_layers = mix(float(depth_max_layers),float(depth_min_layers), abs(dot(vec3(0.0, 0.0, 1.0), view_dir)));
		float layer_depth = 1.0 / num_layers;
		float current_layer_depth = 0.0;
		vec2 P = view_dir.xy * depth_scale;
		vec2 delta = P / num_layers;
		vec2 ofs = base_uv;
		float depth = textureLod(texture_depth, ofs, 0.0).r;
		float current_depth = 0.0;
		while(current_depth < depth) {
			ofs -= delta;
			depth = textureLod(texture_depth, ofs, 0.0).r;
			current_depth += layer_depth;
		}
		vec2 prev_ofs = ofs + delta;
		float after_depth  = depth - current_depth;
		float before_depth = textureLod(texture_depth, prev_ofs, 0.0).r - current_depth + layer_depth;
		float weight = after_depth / (after_depth - before_depth);
		ofs = mix(ofs,prev_ofs,weight);
		//base_uv=ofs;
	}*/
	
	vec2 scaled_uv = base_uv*uv2_scale;
	vec4 albedo_tex = texture(texture_albedo,scaled_uv);
	vec4 albedo_tex_big = texture(texture_albedo_big,base_uv);
	//ALBEDO = albedo.rgb * (albedo_tex.rgb + albedo_tex_big.rgb);
	ALBEDO = screen(albedo_tex, albedo_tex_big).rgb;
	float roughness_tex = dot(texture(texture_roughness,scaled_uv),roughness_texture_channel);
	ROUGHNESS = roughness_tex * roughness;
	SPECULAR = specular;
	NORMALMAP = texture(texture_normal,scaled_uv).rgb;
	NORMALMAP_DEPTH = normal_scale;
	AO = dot(texture(texture_ambient_occlusion,scaled_uv),ao_texture_channel);
	AO_LIGHT_AFFECT = ao_light_affect;
}
"

[resource]
shader = SubResource( 1 )
shader_param/albedo = Color( 1, 1, 1, 1 )
shader_param/specular = 0.5
shader_param/roughness = 1.0
shader_param/point_size = 1.0
shader_param/roughness_texture_channel = Plane( 1, 0, 0, 0 )
shader_param/normal_scale = -1.0
shader_param/ao_texture_channel = Plane( 1, 0, 0, 0 )
shader_param/ao_light_affect = 0.0
shader_param/depth_scale = 0.05
shader_param/depth_min_layers = 8
shader_param/depth_max_layers = 32
shader_param/depth_flip = Vector2( 1, 1 )
shader_param/uv1_scale = Vector3( 1, 1, 1 )
shader_param/uv1_offset = Vector3( 0, 0, 0 )
shader_param/uv2_scale = Vector2( 512, 512 )
shader_param/uv2_offset = Vector3( 0, 0, 0 )
shader_param/texture_albedo = ExtResource( 6 )
shader_param/texture_roughness = ExtResource( 4 )
shader_param/texture_normal = ExtResource( 5 )
shader_param/texture_ambient_occlusion = ExtResource( 2 )
shader_param/texture_depth = ExtResource( 1 )
