[gd_scene load_steps=12 format=2]

[ext_resource path="res://material_maker/panels/preview_3d/map_renderer.gd" type="Script" id=2]

[sub_resource type="World" id=1]

[sub_resource type="Shader" id=2]
code = "shader_type spatial;
render_mode depth_test_disable,depth_draw_always,unshaded,cull_disabled,world_vertex_coords;

varying vec3 p;

void vertex() {
	p = NORMAL;
	VERTEX=vec3(UV, 0.5);
}

void fragment() {
	ALBEDO = 0.5*normalize(p)+vec3(0.5);
}
"

[sub_resource type="ShaderMaterial" id=3]
shader = SubResource( 2 )

[sub_resource type="Shader" id=4]
code = "shader_type spatial;
render_mode depth_test_disable,depth_draw_always,unshaded,cull_disabled,world_vertex_coords;

uniform vec3 position = vec3(0.0);
uniform vec3 size = vec3(1.0);

varying vec3 p;

void vertex() {
	p = (VERTEX-position)/size;
	VERTEX=vec3(UV, 0.5);
}


void fragment() {
	ALBEDO = p;
}
"

[sub_resource type="ShaderMaterial" id=5]
shader = SubResource( 4 )
shader_param/position = Vector3( 0, 0, 0 )
shader_param/size = Vector3( 1, 1, 1 )

[sub_resource type="Shader" id=6]
code = "shader_type canvas_item;
render_mode blend_disabled;

uniform sampler2D tex;
uniform float size = 512.0;

const int STEPS = 64;

vec4 dilate_distance_h(vec2 uv) {
	vec2 e = vec2(1.0/size, 0.0);
	float d = float(STEPS)/size;
	float rv = 0.0;
	vec2 source_uv;
	vec4 source_color;
	for (int i = 0; i < STEPS; ++i) {
		source_uv = uv+float(i)*e;
		source_color = texture(tex, source_uv);
		if (source_color.a >= 1.0) {
			rv = 1.0-float(i)*e.x/d;
			break;
		}
		source_uv = uv-float(i)*e;
		source_color = texture(tex, source_uv);
		if (source_color.a >= 1.0) {
			rv = 1.0-float(i)*e.x/d;
			break;
		}
	}
	return vec4(source_color.rgb, rv);
}

void fragment() {
	COLOR = dilate_distance_h(UV);
}
"
custom_defines = ""

[sub_resource type="ShaderMaterial" id=7]
shader = SubResource( 6 )
shader_param/size = 512.0

[sub_resource type="Shader" id=8]
code = "shader_type canvas_item;
render_mode blend_disabled;

uniform sampler2D tex;
uniform float size = 512.0;

const int STEPS = 64;

vec4 dilate_distance_v(vec2 uv) {
	vec2 e = vec2(0.0, 1.0/size);
	float d = float(STEPS)/size;
	vec4 p = texture(tex, uv);
	for (int i = 0; i < STEPS; ++i) {
		vec2 dx = float(i)*e;
		vec4 p2 = texture(tex, uv+dx);
		if (p2.a > p.a) {
			p2.a = 1.0-sqrt((1.0-p2.a)*(1.0-p2.a)+dx.y*dx.y/d/d);
			p = mix(p, p2, step(p.a, p2.a));
		}
		p2 = texture(tex, uv-dx);
		if (p2.a > p.a) {
			p2.a = 1.0-sqrt((1.0-p2.a)*(1.0-p2.a)+dx.y*dx.y/d/d);
			p = mix(p, p2, step(p.a, p2.a));
		}
	}
	return p;
}

void fragment() {
	COLOR = dilate_distance_v(UV);
}
"
custom_defines = ""

[sub_resource type="ShaderMaterial" id=9]
shader = SubResource( 8 )
shader_param/size = 512.0

[sub_resource type="PrismMesh" id=10]

[node name="MapRenderer" type="Viewport"]
size = Vector2( 2048, 2048 )
own_world = true
world = SubResource( 1 )
transparent_bg = true
keep_3d_linear = true
render_target_update_mode = 0
script = ExtResource( 2 )
mesh_normal_material = SubResource( 3 )
inv_uv_material = SubResource( 5 )
dilate_pass1 = SubResource( 7 )
dilate_pass2 = SubResource( 9 )

[node name="MeshInstance" type="MeshInstance" parent="."]
mesh = SubResource( 10 )
material/0 = SubResource( 5 )
__meta__ = {
"_editor_description_": ""
}

[node name="Camera" type="Camera" parent="."]
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, 0.5, 6 )
projection = 1
